lib/assimilate/batch.rb in assimilate-0.0.4 vs lib/assimilate/batch.rb in assimilate-0.0.5

- old
+ new

@@ -11,11 +11,11 @@ @filename = args[:filename] load_baseline @noops = [] - @changes = [] + @changes = {} @adds = [] @deletes = [] @resolved = false end @@ -43,26 +43,30 @@ current_record = stripped_record_for(key) if current_record if current_record == hash @noops << hash else - @changes << hash + @changes[key] = deltas(current_record, hash) end else @adds << hash end end + def deltas(h1,h2) + (h1.keys | h2.keys).each_with_object({}) {|k,h| h[k] = h2[k] if h1[k] != h2[k]} + end + # compute anything needed before we can write updates to permanent store # * find records that have been deleted def resolve if !@resolved @deleted_keys = (@baseline.keys - @seen.keys).reject {|k| @baseline[k][@catalog.config[:deletion_marker]]} - @updated_field_counts = @changes.each_with_object(Hash.new(0)) do |rec,h| - key = rec[idfield] - diffs = rec.diff(stripped_record_for(key)) + @updated_field_counts = @changes.each_with_object(Hash.new(0)) do |(key,diffs),h| + # key = rec[idfield] + # diffs = deltas(stripped_record_for(key), rec) diffs.keys.each do |f| h[f] += 1 end end @@ -77,12 +81,12 @@ :final_count => @baseline.size + @adds.count, :adds_count => @adds.count, :new_ids => @adds.map {|rec| rec[idfield]}, :deletes_count => @deleted_keys.count, :deleted_ids => @deleted_keys, - :updates_count => @changes.count, - :updated_ids => @changes.map {|rec| rec[idfield]}, + :updates_count => @changes.size, + :updated_ids => @changes.keys, :unchanged_count => @noops.count, :updated_fields => @updated_field_counts } end @@ -126,16 +130,16 @@ @catalog.catalog.insert(decorate(slice)) end end def apply_updates - @changes.each do |rec| + @changes.each do |key, diffs| @catalog.catalog.update( { @domainkey => domain, - idfield => rec[idfield] + idfield => key }, - {"$set" => rec} + {"$set" => diffs} ) end end def decorate(records)