Class: StrokeDB::Store

Inherits:
Object show all
Includes:
ChainSync
Defined in:
lib/stores/store.rb,
lib/sync/store_sync.rb,
lib/stores/remote_store.rb

Direct Known Subclasses

SkiplistStore

Instance Method Summary collapse

Methods included from ChainSync

#sync_chains

Instance Method Details

#remote_server(addr, protocol = :drb) ⇒ Object



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

def remote_server(addr, protocol=:drb)
  case protocol
  when :drb
    RemoteStore::DRb::Server.new(self,"#{addr}")
  else
    raise "No #{protocol} protocol"
  end
end

#sync!(docs, _timestamp = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sync/store_sync.rb', line 19

def sync!(docs, _timestamp=nil)
  _timestamp_counter = timestamp.counter
  report = SynchronizationReport.new(self, :store_document => document, :timestamp => _timestamp_counter)
  existing_chain = {}
  docs.group_by {|doc| doc.uuid}.each_pair do |uuid, versions|
    doc = find(uuid)
    existing_chain[uuid] = doc.versions.all_versions if doc 
  end
  case _timestamp
  when Numeric
    @timestamp = LTS.new(_timestamp, timestamp.uuid) 
  when LamportTimestamp
    @timestamp = LTS.new(_timestamp.counter, timestamp.uuid)
  else
  end
  docs.each {|doc| save!(doc) unless exists?(doc.uuid, doc.version)}
  docs.group_by {|doc| doc.uuid}.each_pair do |uuid, versions|
    incoming_chain = find(uuid, versions.last.version).versions.all_versions
    if existing_chain[uuid].nil? or existing_chain[uuid].empty? # It is a new document
      added_doc = find(uuid, versions.last.version)
      save_as_head!(added_doc)
      report.added_documents << added_doc
    else
      begin
        sync = sync_chains(incoming_chain.reverse, existing_chain[uuid].reverse)
      rescue NonMatchingChains
        # raise NonMatchingDocumentCondition.new(uuid) # that will definitely leave garbage in the store (FIXME?)
        non_matching_doc = find(uuid)
        report.non_matching_documents << non_matching_doc
        next
      end
      resolution = sync.is_a?(Array) ? sync.first : sync
      case resolution
      when :up_to_date
        # nothing to do
      when :merge
        report.conflicts << SynchronizationConflict.create!(self, :document => find(uuid), :rev1 => sync[1], :rev2 => sync[2])
      when :fast_forward
        fast_forwarded_doc = find(uuid, sync[1].last)
        save_as_head!(fast_forwarded_doc)
        report.fast_forwarded_documents << fast_forwarded_doc
      else
        raise "Invalid sync resolution #{resolution}"
      end
    end
  end
  report.conflicts.each do |conflict|
    if resolution_strategy = conflict.document.meta[:resolution_strategy]
      conflict.metas << resolution_strategy
      conflict.save!
    end
    conflict.resolve!
  end
  report.save!
end