Class: Xampl::InMemoryPersister

Inherits:
Persister show all
Defined in:
lib/xamplr/persisters/in-memory.rb

Instance Attribute Summary

Attributes inherited from Persister

#automatic, #block_changes, #cache_hits, #expunged, #format, #last_write_count, #name, #read_count, #rolled_back, #slow_sync, #syncing, #total_cache_hits, #total_read_count, #total_rollback_count, #total_sync_count, #total_write_count, #write_count

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Persister

#busy, #close, #count_changed, #do_sync_write, #done_sync_write, #expunge, #find_known, #find_xampl, #has_changed, #has_not_changed, #introduce, #is_busy, #lazy_load, #lookup, #optimise, #print_stats, #put_changed, #query_implemented, #realise, replace, #represent, #rollback, #shutdown, #start_sync_write, #sync

Constructor Details

#initialize(name = nil, format = nil, capacity = 20) ⇒ InMemoryPersister

Returns a new instance of InMemoryPersister.



7
8
9
10
11
12
13
14
15
# File 'lib/xamplr/persisters/in-memory.rb', line 7

def initialize(name=nil, format=nil, capacity=20)
  capacity ||= 20
  super(name, format)

  @module_map = {}
  @capacity = capacity
  @cache = {}
  @new_cache = {}
end

Class Method Details

.kindObject



17
18
19
# File 'lib/xamplr/persisters/in-memory.rb', line 17

def InMemoryPersister.kind
  :in_memory
end

Instance Method Details

#cache(xampl) ⇒ Object



71
72
73
# File 'lib/xamplr/persisters/in-memory.rb', line 71

def cache(xampl)
  return Xampl.store_in_map(@new_cache, xampl) { xampl }
end

#clear_cacheObject



81
82
83
84
# File 'lib/xamplr/persisters/in-memory.rb', line 81

def clear_cache
  @new_cache = {}
  @cache = {}
end

#fresh_cacheObject



25
26
27
# File 'lib/xamplr/persisters/in-memory.rb', line 25

def fresh_cache
  return XamplCache.new(@capacity)
end

#kindObject



21
22
23
# File 'lib/xamplr/persisters/in-memory.rb', line 21

def kind
  InMemoryPersister.kind
end

#read(klass, pid, target = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/xamplr/persisters/in-memory.rb', line 161

def read(klass, pid, target=nil)
  xampl, target = read_from_cache(klass, pid, target)
  return xampl if xampl and !target

  representation = Xampl.lookup_in_map(@module_map, klass, pid)
  return nil unless representation

  xampl = realise(representation, target)
  Xampl.store_in_cache(@cache, xampl, self) { xampl }
  xampl.introduce_persister(self)

  @read_count = @read_count + 1
  xampl.changes_accepted
  @changed.delete(xampl)
  return xampl
end

#read_from_cache(klass, pid, target = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/xamplr/persisters/in-memory.rb', line 99

def read_from_cache(klass, pid, target=nil)
  xampl = Xampl.lookup_in_map(@cache, klass, pid)
  if xampl then
    if target and target != xampl then
      target.invalidate
      raise XamplException.new(:cache_conflict)
    end
    unless xampl.load_needed then
      @cache_hits = @cache_hits + 1
      return xampl, target
    end
    return xampl, xampl
  end

  xampl = Xampl.lookup_in_map(@new_cache, klass, pid)
  if xampl then
    if target and target != xampl then
      target.invalidate
      raise XamplException.new(:cache_conflict)
    end
    unless xampl.load_needed then
      @cache_hits = @cache_hits + 1
      return xampl, target
    end
    return xampl, xampl
  end

  return nil, target
end

#rollback_cleanupObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/xamplr/persisters/in-memory.rb', line 51

def rollback_cleanup
  @new_cache.each{ | name, map |
    if map then
      map.each{ | name2, map2 |
        if map2 then
          map2.each{ | pid, xampl |
            @changed.delete(xampl)
            xampl.invalidate
          }
        end
      }
    end
  }
  @changed.each{ | xampl, ignore|
    xampl.force_load
  }
  @new_cache = {}
  super
end

#sync_doneObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xamplr/persisters/in-memory.rb', line 29

def sync_done
  if @new_cache then
    @new_cache.each{ | name1, map1 |
      if map1 then
        cache_map1 = @cache[name1]
        @cache[name1] = cache_map1 = {} unless cache_map1
        map1.each{ | name2, map2 |
          if map2 then
            cache_map2 = cache_map1[name2]
            cache_map1[name2] = cache_map2 = self.fresh_cache unless cache_map2

            map2.each{ | pid, xampl |
              cache_map2[pid] = xampl
            }
          end
        }
      end
    }
  end
  @new_cache = {}
end

#uncache(xampl) ⇒ Object



75
76
77
78
79
# File 'lib/xamplr/persisters/in-memory.rb', line 75

def uncache(xampl)
  @changed.delete(xampl)
  Xampl.remove_from_map(@cache, xampl)
  return Xampl.remove_from_map(@new_cache, xampl)
end

#write(xampl) ⇒ Object

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xamplr/persisters/in-memory.rb', line 86

def write(xampl)
  raise XamplException.new(:no_index_so_no_persist) unless xampl.get_the_index
  #return false unless xampl.get_the_index

  if Xampl.store_in_map(@module_map, xampl) { represent(xampl) } then
    @write_count = @write_count + 1
    xampl.changes_accepted
    return true
  else
    return false
  end
end

#xxread_from_cache(klass, pid, target = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/xamplr/persisters/in-memory.rb', line 129

def xxread_from_cache(klass, pid, target=nil)
  xampl = Xampl.lookup_in_map(@cache, klass, pid)
  if (nil != xampl) then
    puts "READ CACHE pid #{pid} -- #{target} #{xampl}" if target and  target != xampl
    if target and target != xampl then
      target.invalidate
      raise XamplException.new(:cache_conflict)
    end
    unless xampl.load_needed then
      @cache_hits = @cache_hits + 1
      return xampl
    end
    target = xampl
  end

  xampl = Xampl.lookup_in_map(@new_cache, klass, pid)
  if (nil != xampl) then
    puts "READ CACHE pid #{pid} -- #{target} #{xampl}" if target and  target != xampl
    if target and target != xampl then
      target.invalidate
      raise XamplException.new(:cache_conflict)
    end
    unless xampl.load_needed then
      @cache_hits = @cache_hits + 1
      return xampl
    end
    target = xampl
  end

  return xampl
end