Class: FSDB::Database::CacheEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/fsdb/database.rb

Overview

:nodoc:

Constant Summary collapse

TIME_DELTA =
Database::MTIME_RESOLUTION + Database::CLOCK_SKEW

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCacheEntry

Returns a new instance of CacheEntry.



41
42
43
44
45
46
47
# File 'lib/fsdb/database.rb', line 41

def initialize
  @mutex      = Mutex.new
  @modex      = Modex.new
  @users      = 0
  
  stale!
end

Instance Attribute Details

#file_handleObject

Returns the value of attribute file_handle.



37
38
39
# File 'lib/fsdb/database.rb', line 37

def file_handle
  @file_handle
end

#versionObject (readonly)

Returns the value of attribute version.



36
37
38
# File 'lib/fsdb/database.rb', line 36

def version
  @version
end

Instance Method Details

#just_gimme_the_damn_object!Object



70
71
72
# File 'lib/fsdb/database.rb', line 70

def just_gimme_the_damn_object!
  @object
end

#object(mtime) ⇒ Object

Yields to block that loads the object, if needed. Called between #start_using and #stop_using.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fsdb/database.rb', line 51

def object(mtime)
  @mutex.synchronize do
    if @object and mtime == @mtime
      if @check_time - mtime < TIME_DELTA
        # If we last checked the file during the same second as mtime, the
        # file might have been touched after we checked it, so we may have
        # to load it again. (Assume resolution of file mtime <= 1.)
        @check_time = Time.now
        yield @version
      end
    else
      @check_time = Time.now
      yield nil
    end

    @object
  end
end

#stale!Object



74
75
76
77
78
79
# File 'lib/fsdb/database.rb', line 74

def stale!
  @check_time = nil
  @mtime      = nil
  @version    = nil
  @object     = nil
end

#start_usingObject



89
# File 'lib/fsdb/database.rb', line 89

def start_using; Thread.exclusive {@users += 1}; end

#stop_usingObject



90
# File 'lib/fsdb/database.rb', line 90

def stop_using;  Thread.exclusive {@users -= 1}; end

#sync_object_exclusiveObject



103
104
105
# File 'lib/fsdb/database.rb', line 103

def sync_object_exclusive
  @modex.synchronize(Modex::EX) { yield }
end

#sync_object_shared(do_when_first, do_when_last) ⇒ Object

Protects object during #browse, #edit, and so on. Should be locked as long as the object is being used. It’s ok to lock the @mutex within the context of a lock on @modex.



99
100
101
# File 'lib/fsdb/database.rb', line 99

def sync_object_shared do_when_first, do_when_last
  @modex.synchronize(Modex::SH, do_when_first, do_when_last, self) { yield }
end

#unused?Boolean

called in context of lock on db’s cache_mutex, which is also required for start_using.

Returns:

  • (Boolean)


94
# File 'lib/fsdb/database.rb', line 94

def unused?;    @users == 0; end

#update(new_mtime, version, object) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/fsdb/database.rb', line 81

def update(new_mtime, version, object)
  # only called in @mutex or object_exclusive context, so no need to lock
  @check_time = new_mtime
  @mtime      = new_mtime
  @version    = version
  @object     = object
end