Module: StrokeDB

Defined in:
lib/util/java_util.rb,
lib/init.rb,
lib/sync/diff.rb,
lib/util/util.rb,
lib/view/view.rb,
lib/stores/store.rb,
lib/config/config.rb,
lib/document/meta.rb,
lib/document/slot.rb,
lib/document/util.rb,
lib/document/delete.rb,
lib/sync/chain_sync.rb,
lib/sync/store_sync.rb,
lib/util/lazy_array.rb,
lib/document/callback.rb,
lib/document/document.rb,
lib/document/versions.rb,
lib/document/coercions.rb,
lib/util/serialization.rb,
lib/document/virtualize.rb,
lib/stores/remote_store.rb,
lib/document/validations.rb,
lib/document/associations.rb,
lib/sync/stroke_diff/hash.rb,
lib/sync/lamport_timestamp.rb,
lib/sync/stroke_diff/array.rb,
lib/util/lazy_mapping_hash.rb,
lib/sync/stroke_diff/string.rb,
lib/util/lazy_mapping_array.rb,
lib/data_structures/skiplist.rb,
lib/stores/chainable_storage.rb,
lib/sync/stroke_diff/default.rb,
lib/data_structures/point_query.rb,
lib/stores/skiplist_store/chunk.rb,
lib/sync/stroke_diff/stroke_diff.rb,
lib/data_structures/inverted_list.rb,
lib/stores/skiplist_store/chunk_storage.rb,
lib/stores/skiplist_store/skiplist_store.rb,
lib/stores/skiplist_store/file_chunk_storage.rb,
lib/stores/skiplist_store/memory_chunk_storage.rb,
lib/stores/inverted_list_index/inverted_list_index.rb,
lib/stores/inverted_list_index/inverted_list_file_storage.rb

Overview

Some java overrides

Defined Under Namespace

Modules: Associations, ChainSync, ChainableStorage, Coercions, ImmutableDocument, JsonSerializationMethod, MarshalSerializationMethod, Meta, RemoteStore, Util, Validations, VersionedDocument, Virtualizations Classes: ArraySlotValue, Callback, Chunk, ChunkStorage, Config, DefaultSlotDiff, Document, DocumentDeletionError, DocumentReferenceValue, FileChunkStorage, HashSlotValue, InvalidDocumentError, InvertedList, InvertedListFileStorage, InvertedListIndex, LamportTimestamp, LazyArray, LazyMappingArray, LazyMappingHash, MemoryChunkStorage, NoDefaultStoreError, PointQuery, Skiplist, SkiplistStore, Slot, SlotDiffStrategy, SlotNotFoundError, Store, UnknownIndexTypeError, UnknownStorageTypeError, UnknownStoreTypeError

Constant Summary collapse

VERSION =
'0.0.2.1' + (RUBY_PLATFORM =~ /java/ ? '-java' : '')
UUID_RE =

UUID regexp (like 1e3d02cc-0769-4bd8-9113-e033b246b013)

/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/
VERSION_RE =

document version regexp

UUID_RE
NIL_UUID =

so called Nil UUID, should be used as special UUID for Meta meta

"00000000-0000-0000-0000-000000000000"
DELETED_DOCUMENT_UUID =

UUID used for DeletedDocument meta

'e5e0ef20-e10f-4269-bff3-3040a90e194e'
STORE_INFO_UUID =

UUID used for StoreInfo meta

"23e11d2e-e3d3-4c24-afd2-b3316403dd03"
DIFF_UUID =

UUID used for Diff meta

"5704bd39-4a01-405e-bc72-3650ddd89ca4"
SYNCHRONIZATION_REPORT_UUID =

UUID used for SynchronizationReport meta

"8dbaf160-addd-401a-9c29-06b03f70df93"
SYNCHRONIZATION_CONFLICT_UUID =

UUID used for SynchronizationConflict meta

"36fce59c-ee3d-4566-969b-7b152814a314"
VIEW_UUID =

UUID used for View meta

"ced0ad12-7419-4db1-a9f4-bc35e9b64112"
VIEWCUT_UUID =

UUID used for ViewCut meta

"2975630e-c877-4eab-b86c-732e1de1adf5"
Diff =
Meta.new(:uuid => DIFF_UUID) do

  on_initialization do |diff|
    diff.added_slots = {} unless diff[:added_slots]
    diff.removed_slots = {} unless diff[:removed_slots]
    diff.updated_slots = {} unless diff[:updated_slots] 
    diff.send!(:compute_diff) if diff.new?
  end

  def different?
    !updated_slots.empty? || !removed_slots.empty? || !added_slots.empty?
  end

  def patch!(document)
    added_slots.each_pair do |addition, value|
      document[addition] = value
    end
    removed_slots.keys.each do |removal|
      document.remove_slot!(removal)
    end
    updated_slots.each_pair do |update, value|
      if sk = strategy_class_for(update)
        document[update] = sk.patch(document[update], value)
      else
        document[update] =value
      end
    end
  end


  protected

  def compute_diff
    additions = to.slotnames - from.slotnames 
    additions.each do |addition|
      self.added_slots[addition] = to[addition]
    end
    removals = from.slotnames - to.slotnames
    removals.each do |removal|
      self.removed_slots[removal] = from[removal]
    end
    updates = (to.slotnames - additions - ['version']).select {|slotname| to[slotname] != from[slotname]}
    updates.each do |update|
      unless sk = strategy_class_for(update)
        self.updated_slots[update] = to[update]
      else
        self.updated_slots[update] = sk.diff(from[update], to[update]) 
      end
    end
  end

  def strategy_class_for(slotname)
    if from.meta && strategy = from.meta["diff_strategy_#{slotname}"]
      _strategy_class = strategy.camelize.constantize rescue nil
      return _strategy_class if _strategy_class && _strategy_class.ancestors.include?(SlotDiffStrategy)
    end
    false
  end

end
View =
Meta.new(:uuid => VIEW_UUID) do
  attr_accessor :map_with_proc
  attr_reader :reduce_with_proc

  on_initialization do |view|
    view.map_with_proc = proc {|doc, *args| doc } 
  end

  def reduce_with(&block)
    @reduce_with_proc = block
    self
  end

  def map_with(&block)
    @map_with_proc = block
    self
  end

  def emit(*args) 
    ViewCut.new(store, :view => self, :args => args, :timestamp_state => LTS.zero.counter).emit
  end

end
ViewCut =
Meta.new(:uuid => VIEWCUT_UUID) do

  on_new_document do |cut|
    cut.instance_eval do
      if view.is_a?(View)
        @map_with_proc = view.map_with_proc
        @reduce_with_proc = view.reduce_with_proc
      end
    end
  end

  before_save do |cut|
    view = cut.view
    view.last_cut = cut if view[:last_cut].nil? or (cut[:previous] && view.last_cut == cut.previous)
    view.save!
  end


  def emit
    mapped = []
    store.each(:after_timestamp => timestamp_state, :include_versions => view[:include_versions]) do |doc| 
      mapped << @map_with_proc.call(doc,*args) 
    end
    documents = (@reduce_with_proc ? mapped.select {|doc| @reduce_with_proc.call(doc,*args) } : mapped).map{|d| d.is_a?(Document) ? d.extend(VersionedDocument) : d}
    ViewCut.new(store, :documents => documents, :view => view, :args => args, :timestamp_state => store.timestamp.counter, :previous => timestamp_state == 0 ? nil : self)
  end
  def to_a
    documents
  end
end
StoreInfo =
Meta.new(:uuid => STORE_INFO_UUID)
DeletedDocument =
Meta.new(:uuid => DELETED_DOCUMENT_UUID) do
  on_load do |doc|
    doc.make_immutable!
  end
  
  def undelete!
    deleted_version = versions.previous
    store.save_as_head!(deleted_version)
    self.class.find(uuid)
  end
end
SynchronizationReport =
Meta.new(:uuid => SYNCHRONIZATION_REPORT_UUID) do
  on_new_document do |report|
    report.conflicts = []
    report.added_documents = []
    report.fast_forwarded_documents = []
    report.non_matching_documents = []
  end
end
SynchronizationConflict =
Meta.new(:uuid => SYNCHRONIZATION_CONFLICT_UUID) do
  def resolve!
    # by default, do nothing
  end
end
DOCREF =

Slots which contain references to another documents are matched with these regexps.

/^@##{UUID_RE}$/
VERSIONREF =
/^@##{UUID_RE}\.#{VERSION_RE}$/
LTS =
LamportTimestamp
PATCH_REPLACE =
'R'.freeze
PATCH_PLUS =
'+'.freeze
PATCH_MINUS =
'-'.freeze
PATCH_DIFF =
'D'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_storeObject



37
38
39
# File 'lib/init.rb', line 37

def default_store
  StrokeDB.default_config.stores[:default] rescue nil
end

.default_store=(store) ⇒ Object



40
41
42
43
44
# File 'lib/init.rb', line 40

def default_store=(store)
  cfg = Config.new
  cfg.stores[:default] = store
  StrokeDB.default_config = cfg
end

.serialization_method=(method_name) ⇒ Object



22
23
24
# File 'lib/util/serialization.rb', line 22

def self.serialization_method=(method_name)
  StrokeDB.extend StrokeDB.const_get("#{method_name.to_s.camelize}SerializationMethod")
end

.use_global_default_config!Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/config/config.rb', line 148

def use_global_default_config!
  class <<self
    def default_config
      $strokedb_default_config
    end
    def default_config=(config)
      $strokedb_default_config = config
    end
  end
end

.use_perthread_default_config!Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/config/config.rb', line 138

def use_perthread_default_config!
  class <<self
    def default_config
      Thread.current['StrokeDB.default_config']
    end
    def default_config=(config)
      Thread.current['StrokeDB.default_config'] = config
    end
  end
end

Instance Method Details

#DEBUGObject



48
49
50
# File 'lib/init.rb', line 48

def DEBUG
  yield
end