Module: StrokeDB::ChainableStorage

Included in:
Storage
Defined in:
lib/strokedb/stores/chainable_storage.rb

Instance Method Summary collapse

Instance Method Details

#add_chained_storage!(storage) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/strokedb/stores/chainable_storage.rb', line 3

def add_chained_storage!(storage)
  @chained_storages ||= {}
  @chained_storages[storage] = [{},[]]
  class <<self
    alias :save! :save_with_chained_storages!
  end
  storage.add_chained_storage!(self) unless storage.has_chained_storage?(self)
end

#has_chained_storage?(storage) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/strokedb/stores/chainable_storage.rb', line 22

def has_chained_storage?(storage)
  @chained_storages.nil? ? false : !!@chained_storages[storage]
end

#remove_chained_storage!(storage) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/strokedb/stores/chainable_storage.rb', line 12

def remove_chained_storage!(storage)
  @chained_storages.delete(storage)
  storage.remove_chained_storage!(self) if storage.has_chained_storage?(self)
  if @chained_storages.keys.empty?
    class << self
      alias :save! :save_without_chained_storages!
    end
  end
end

#save_with_chained_storages!(document, timestamp, options = {}, source = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/strokedb/stores/chainable_storage.rb', line 48

def save_with_chained_storages!(document, timestamp, options = {},source=nil)
  perform_save!(document, timestamp, options)
  (@chained_storages||{}).each_pair do |storage, accumulator|
    next if storage == source
    hsh = [document,timestamp,options].hash
    hashes, savings = accumulator
    unless hashes.has_key?(hsh) # TODO: here we had a bug (storage == document), spec it
      hashes[hsh] = true
      savings << [document,timestamp,options]      
    end
  end
end

#save_without_chained_storages!(document, timestamp, options = {}, source = nil) ⇒ Object Also known as: save!



44
45
46
# File 'lib/strokedb/stores/chainable_storage.rb', line 44

def save_without_chained_storages!(document, timestamp, options = {},source=nil)
  perform_save!(document, timestamp, options)
end

#sync_chained_storage!(storage) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/strokedb/stores/chainable_storage.rb', line 36

def sync_chained_storage!(storage)
  return unless @chained_storages.is_a?(Hash)
  ((@chained_storages[storage]||[]).last||[]).each do |saving|
    storage.save!(saving[0], saving[1], saving[2], self)
  end
  @chained_storages[storage] = [{},[]]
end

#sync_chained_storages!(origin = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/strokedb/stores/chainable_storage.rb', line 26

def sync_chained_storages!(origin=nil)
  return unless @chained_storages.is_a?(Hash)
  @chained_storages.each_pair do |storage, savings|
    next if storage == origin
    savings.last.each {|saving| storage.save!(saving[0], saving[1], saving[2], self)}
    storage.sync_chained_storages!(self)
    @chained_storages[storage] =  [{},[]]
  end
end