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.



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

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

Instance Attribute Details

#file_handleObject

Returns the value of attribute file_handle.



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

def file_handle
  @file_handle
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#just_gimme_the_damn_object!Object



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

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.



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

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



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

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

#start_usingObject

called in context of lock on db’s cache_mutex.



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

def start_using;  @users += 1; end

#stop_usingObject

called in context of lock on db’s cache_mutex.



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

def stop_using;   @users -= 1; end

#sync_object_exclusiveObject



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

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.



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

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.

Returns:

  • (Boolean)


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

def unused?;      @users == 0; end

#update(new_mtime, version, object) ⇒ Object



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

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