Class: CouchDiff
- Inherits:
-
Object
- Object
- CouchDiff
- Defined in:
- lib/couchdiff.rb
Constant Summary collapse
- VERSION =
'0.0.3'- EXCLUDED_KEYS =
['_rev','revpos']
Instance Attribute Summary collapse
-
#added ⇒ Object
readonly
Returns the value of attribute added.
-
#deleted ⇒ Object
readonly
Returns the value of attribute deleted.
-
#unchanged ⇒ Object
readonly
Returns the value of attribute unchanged.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
Instance Method Summary collapse
-
#attachments_changed(src_doc, dst_doc) ⇒ Object
determine if there are any added/deleted/modified attachments between src and dst.
-
#initialize(src_docs, dst_docs, &changed) ⇒ CouchDiff
constructor
A new instance of CouchDiff.
- #patch(src_db, dst_db, copier = Copier.new(src_db, dst_db)) ⇒ Object
- #updated_as_diff ⇒ Object
Constructor Details
#initialize(src_docs, dst_docs, &changed) ⇒ CouchDiff
Returns a new instance of CouchDiff.
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/couchdiff.rb', line 42 def initialize src_docs, dst_docs, &changed # default behavior is to compare all fields besides the _rev and revpos field changed ||= proc{ |doc1, doc2| doc1.diff(doc2, EXCLUDED_KEYS).size > 0 } @added, @updated, @deleted, @unchanged = [], [], [], [] dst_doc_map, src_doc_map = {}, {} # create hash maps for fast lookup src_docs.each {|doc| src_doc_map[doc['id']] = doc; assert_doc(doc)} dst_docs.each {|doc| dst_doc_map[doc['id']] = doc; assert_doc(doc)} # updated/added docs src_docs.each do |doc| id = doc['id'] if !(dst_doc = dst_doc_map[id]).nil? if changed.yield(doc['doc'], dst_doc['doc']) || (doc['doc'], dst_doc['doc']) @updated << [doc, dst_doc] else @unchanged << doc end else @added << doc end end # deleted docs dst_docs.each do |doc| id = doc['id'] @deleted << doc unless src_doc_map[id] end end |
Instance Attribute Details
#added ⇒ Object (readonly)
Returns the value of attribute added.
36 37 38 |
# File 'lib/couchdiff.rb', line 36 def added @added end |
#deleted ⇒ Object (readonly)
Returns the value of attribute deleted.
38 39 40 |
# File 'lib/couchdiff.rb', line 38 def deleted @deleted end |
#unchanged ⇒ Object (readonly)
Returns the value of attribute unchanged.
39 40 41 |
# File 'lib/couchdiff.rb', line 39 def unchanged @unchanged end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
37 38 39 |
# File 'lib/couchdiff.rb', line 37 def updated @updated end |
Instance Method Details
#attachments_changed(src_doc, dst_doc) ⇒ Object
determine if there are any added/deleted/modified attachments between src and dst
76 77 78 |
# File 'lib/couchdiff.rb', line 76 def src_doc, dst_doc (src_doc['_attachments'] || {}).diff(dst_doc['_attachments'] || {}, EXCLUDED_KEYS).length > 0 end |
#patch(src_db, dst_db, copier = Copier.new(src_db, dst_db)) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/couchdiff.rb', line 80 def patch src_db, dst_db, copier = Copier.new(src_db, dst_db) = ProgressBar.new('Adding docs', @added.size) # ADD new docs until @added.empty? doc_ids = (@added.shift(20)).map {|src| src['id'] } src_docs = src_db.get_bulk(doc_ids)['rows'] src_docs.each do |src_doc| puts "[Adding #{src_doc['doc']['_id']}]" copier.copy src_doc['doc'], nil .inc end end .finish = ProgressBar.new('Updating docs', @updated.size) # UPDATE docs (if they changed) until @updated.empty? # process 25 docs at a time doc_ids = (@updated.shift(25)).map {|src, dst| src['id'] } src_docs = src_db.get_bulk(doc_ids)['rows'] dst_docs = dst_db.get_bulk(doc_ids)['rows'] # update dst_docs content src_docs.each_with_index do |src_doc, index| dst_doc = dst_docs[index] # puts "[Updating #{src_doc['doc']['_id']}]" copier.copy src_doc['doc'], dst_doc['doc'] .inc end end dst_db.bulk_save # flush any pending docs .finish # REMOVE deleted docs = ProgressBar.new('Deleting docs', @deleted.size) dst_db.bulk_save @deleted.map { |doc| {"_id" => doc["id"], "_rev" => doc['doc']['_rev'], "_deleted" => true } } unless @deleted.empty? .finish end |
#updated_as_diff ⇒ Object
124 125 126 |
# File 'lib/couchdiff.rb', line 124 def updated_as_diff @updated.map {|doc1, doc2| doc1['doc'].diff(doc2['doc'], EXCLUDED_KEYS).merge({'id' => doc1['id']}) } end |