Class: PEROBS::PersistentObjectCacheLine

Inherits:
Object
  • Object
show all
Defined in:
lib/perobs/PersistentObjectCacheLine.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

WATERMARK =

This defines the minimum size of the cache line. If it is too large, the time to find an entry will grow too much. If it is too small the number of cache lines will be too large and create more store overhead. By running benchmarks it turned out that 8 is a pretty good compromise.

8

Instance Method Summary collapse

Constructor Details

#initializePersistentObjectCacheLine

Returns a new instance of PersistentObjectCacheLine.



43
44
45
# File 'lib/perobs/PersistentObjectCacheLine.rb', line 43

def initialize
  @entries = []
end

Instance Method Details

#delete(uid) ⇒ Object

Delete the entry that matches the given UID

Parameters:

  • uid (Integer)


77
78
79
# File 'lib/perobs/PersistentObjectCacheLine.rb', line 77

def delete(uid)
  @entries.delete_if { |e| e.obj.uid == uid }
end

#flush(now) ⇒ Object

Save all modified entries and delete all but the most recently added.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/perobs/PersistentObjectCacheLine.rb', line 82

def flush(now)
  if now || @entries.length > WATERMARK
    @entries.each do |e|
      if e.modified
        e.obj.save
        e.modified = false
      end
    end

    # Delete all but the first WATERMARK entry.
    @entries = @entries[0..WATERMARK - 1] if @entries.length > WATERMARK
  end
end

#get(uid) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/perobs/PersistentObjectCacheLine.rb', line 63

def get(uid)
  if (index = @entries.find_index{ |e| e.obj.uid == uid })
    if index > 0
      # Move the entry to the front.
      @entries.unshift(@entries.delete_at(index))
    end
    @entries.first
  else
    nil
  end
end

#insert(object, modified) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/perobs/PersistentObjectCacheLine.rb', line 47

def insert(object, modified)
  if (index = @entries.find_index{ |e| e.obj.uid == object.uid })
    # We have found and removed an existing entry for this particular
    # object. If the modified flag is set, ensure that the entry has it
    # set as well.
    entry = @entries.delete_at(index)
    entry.modified = true if modified && !entry.modified
  else
    # There is no existing entry for this object. Create a new one.
    entry = Entry.new(object, modified)
  end

  # Insert the entry at the beginning of the line.
  @entries.unshift(entry)
end