Class: CouchDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/couchdiff.rb

Constant Summary collapse

VERSION =
'0.0.3'
EXCLUDED_KEYS =
['_rev','revpos']

Instance Attribute Summary collapse

Instance Method Summary collapse

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']) || attachments_changed(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

#addedObject (readonly)

Returns the value of attribute added.



36
37
38
# File 'lib/couchdiff.rb', line 36

def added
  @added
end

#deletedObject (readonly)

Returns the value of attribute deleted.



38
39
40
# File 'lib/couchdiff.rb', line 38

def deleted
  @deleted
end

#unchangedObject (readonly)

Returns the value of attribute unchanged.



39
40
41
# File 'lib/couchdiff.rb', line 39

def unchanged
  @unchanged
end

#updatedObject (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 attachments_changed 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)

  p_bar = 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
      p_bar.inc
    end
  end
  p_bar.finish

  p_bar = 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']
      p_bar.inc
    end
  end
  dst_db.bulk_save # flush any pending docs
  p_bar.finish


  # REMOVE deleted docs
  p_bar = 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?
  p_bar.finish

end

#updated_as_diffObject



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