forked from jchris/couchrest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Anderson
committed
Aug 3, 2008
1 parent
66c1f0b
commit fbccbd7
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'rubygems' | ||
require 'couchrest' | ||
|
||
# set the source db and map view | ||
source = CouchRest.new("http://localhost:5984").database('source-db') | ||
source_view = 'mydesign/view-map' | ||
|
||
# set the target db | ||
target = CouchRest.new("http://localhost:5984").database('target-db') | ||
|
||
|
||
pager = CouchRest::Pager.new(source) | ||
|
||
# pager will yield once per uniq key in the source view | ||
|
||
pager.key_reduce(source_view, 10000) do |key, values| | ||
# create a doc from the key and the values | ||
example_doc = { | ||
:key => key, | ||
:values => values.uniq | ||
} | ||
|
||
target.save(example_doc) | ||
|
||
# keep us up to date with progress | ||
puts k if (rand > 0.9) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'rubygems' | ||
require 'couchrest' | ||
|
||
# subset.rb replicates a percentage of a database to a fresh database. | ||
# use it to create a smaller dataset on which to prototype views. | ||
|
||
# specify the source database | ||
source = CouchRest.new("http://localhost:5984").database('source-db') | ||
|
||
# specify the target database | ||
target = CouchRest.new("http://localhost:5984").database('target-db') | ||
|
||
# pager efficiently yields all view rows | ||
pager = CouchRest::Pager.new(source) | ||
|
||
pager.all_docs(1000) do |rows| | ||
docs = rows.collect do |r| | ||
# the percentage of docs to clone | ||
next if rand > 0.1 | ||
doc = source.get(r['id']) | ||
doc.delete('_rev') | ||
doc | ||
end.compact | ||
puts docs.length | ||
next if docs.empty? | ||
|
||
puts docs.first['_id'] | ||
target.bulk_save(docs) | ||
end | ||
|