Module: RubySync::Connectors::DbmChangeTracking

Defined in:
lib/ruby_sync/connectors/dbm_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

Instance Method Details

#dbm_pathObject

set a default dbm path



31
32
33
34
35
# File 'lib/ruby_sync/connectors/dbm_change_tracking.rb', line 31

def dbm_path()
  p = "#{base_path}/db"
  ::FileUtils.mkdir_p p
  ::File.join(p,name)
end

#delete_from_mirror(path) ⇒ Object



91
92
93
94
95
# File 'lib/ruby_sync/connectors/dbm_change_tracking.rb', line 91

def delete_from_mirror path
  DBM.open(self.mirror_dbm_filename) do |dbm|
    dbm.delete(path)
  end
end

#digest(o) ⇒ Object



81
82
83
# File 'lib/ruby_sync/connectors/dbm_change_tracking.rb', line 81

def digest(o)
  Digest::MD5.hexdigest(o.to_yaml)
end

#each_changeObject

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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby_sync/connectors/dbm_change_tracking.rb', line 55

def each_change
  DBM.open(self.mirror_dbm_filename) do |dbm|
    # scan existing entries to see if any new or modified
    each_entry do |path, entry|
	digest = digest(entry)
	unless stored_digest = dbm[path.to_s] and digest == stored_digest
 operations = create_operations_for(entry)
 yield RubySync::Event.add(self, path, nil, operations) 
 dbm[path.to_s] = digest
	end
    end
        
    # scan dbm to find deleted
    dbm.each do |key, stored_hash|
	unless self[key]
 yield RubySync::Event.delete(self, key)
 dbm.delete key
 if is_vault? and @pipeline
   association = association_for @pipeline.association_context, key
   remove_association association
 end
	end
    end
  end        
end

#mirror_dbm_filenameObject

Stores a hash for each entry so we can tell when entries are added, deleted or modified



39
40
41
# File 'lib/ruby_sync/connectors/dbm_change_tracking.rb', line 39

def mirror_dbm_filename
  dbm_path + "_mirror"
end

#remove_mirrorObject



87
88
89
# File 'lib/ruby_sync/connectors/dbm_change_tracking.rb', line 87

def remove_mirror
  File.delete_if_exists(["#{mirror_dbm_filename}.db"])
end

#update_mirror(path) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/ruby_sync/connectors/dbm_change_tracking.rb', line 97

def update_mirror path
  if entry = self[path]
    DBM.open(self.mirror_dbm_filename) do |dbm|
	dbm[path.to_s] = digest(entry)
    end
  end
end