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.



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

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

Instance Attribute Details

#file_handleObject

Returns the value of attribute file_handle.



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

def file_handle
  @file_handle
end

#versionObject (readonly)

Returns the value of attribute version.



34
35
36
# File 'lib/fsdb/database.rb', line 34

def version
  @version
end

Instance Method Details

#just_gimme_the_damn_object!Object



68
69
70
# File 'lib/fsdb/database.rb', line 68

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.



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

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



72
73
74
75
76
77
# File 'lib/fsdb/database.rb', line 72

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

#start_usingObject



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

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

#stop_usingObject



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

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

#sync_object_exclusiveObject



101
102
103
# File 'lib/fsdb/database.rb', line 101

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.



97
98
99
# File 'lib/fsdb/database.rb', line 97

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)


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

def unused?;    @users == 0; end

#update(new_mtime, version, object) ⇒ Object



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

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