Module: RubySync::Connectors::MemoryChangeTracking
- Included in:
- MemoryConnector
- Defined in:
- lib/ruby_sync/connectors/memory_change_tracking.rb
Overview
When included by a connector, tracks changes to the connector using a dbm database of the form path:digest. Digest is a MD5 hash of the record so we can tell if the record has changed. We can’t, however, tell how the record has changed so change events generated will be for the entire record.
Instance Method Summary collapse
- #delete_from_mirror(path) ⇒ Object
- #digest(o) ⇒ Object
-
#each_change ⇒ Object
Subclasses MAY override this to interface with the external system and generate an event for every change that affects items within the scope of this connector.
- #remove_mirror ⇒ Object
- #shadow ⇒ Object
- #update_mirror(path) ⇒ Object
Instance Method Details
#delete_from_mirror(path) ⇒ Object
75 76 77 |
# File 'lib/ruby_sync/connectors/memory_change_tracking.rb', line 75 def delete_from_mirror path shadow.delete(path.to_s) end |
#digest(o) ⇒ Object
67 68 69 |
# File 'lib/ruby_sync/connectors/memory_change_tracking.rb', line 67 def digest(o) Digest::MD5.hexdigest(o.to_yaml) end |
#each_change ⇒ Object
Subclasses MAY override this to interface with the external system and generate an event for every change that affects items within the scope of this connector.
The default behaviour is to compare a hash of each entry in the database with a stored hash of its previous value and generate add, modify and delete events appropriately. This is normally a very inefficient way to operate so overriding this method is highly recommended if you can detect changes in a more efficient manner.
This method will be called repeatedly until the connector is stopped.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ruby_sync/connectors/memory_change_tracking.rb', line 43 def each_change # scan existing entries to see if any new or modified each_entry do |path, entry| digest = digest(entry) unless stored_digest = shadow[path.to_s] and digest == stored_digest operations = create_operations_for(entry) yield RubySync::Event.add(self, path, nil, operations) shadow[path.to_s] = digest end end # scan shadow to find deleted shadow.each do |key, stored_hash| unless self[key] yield RubySync::Event.delete(self, key) shadow.delete key if is_vault? and @pipeline association = association_for @pipeline.association_context, key remove_association association end end end end |
#remove_mirror ⇒ Object
71 72 73 |
# File 'lib/ruby_sync/connectors/memory_change_tracking.rb', line 71 def remove_mirror @shadow = nil end |
#shadow ⇒ Object
27 28 29 |
# File 'lib/ruby_sync/connectors/memory_change_tracking.rb', line 27 def shadow @shadow ||= {} end |
#update_mirror(path) ⇒ Object
79 80 81 82 |
# File 'lib/ruby_sync/connectors/memory_change_tracking.rb', line 79 def update_mirror path entry = self[path.to_s] shadow[path.to_s] = digest(entry) end |