topfunky-couchrest 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/LICENSE +176 -0
  2. data/README.rdoc +51 -0
  3. data/Rakefile +87 -0
  4. data/THANKS +15 -0
  5. data/bin/couchdir +20 -0
  6. data/bin/couchview +48 -0
  7. data/examples/word_count/markov +38 -0
  8. data/examples/word_count/views/books/chunked-map.js +3 -0
  9. data/examples/word_count/views/books/united-map.js +1 -0
  10. data/examples/word_count/views/markov/chain-map.js +6 -0
  11. data/examples/word_count/views/markov/chain-reduce.js +7 -0
  12. data/examples/word_count/views/word_count/count-map.js +6 -0
  13. data/examples/word_count/views/word_count/count-reduce.js +3 -0
  14. data/examples/word_count/word_count.rb +67 -0
  15. data/examples/word_count/word_count_query.rb +39 -0
  16. data/lib/couchrest.rb +92 -0
  17. data/lib/couchrest/commands/generate.rb +71 -0
  18. data/lib/couchrest/commands/push.rb +99 -0
  19. data/lib/couchrest/core/database.rb +105 -0
  20. data/lib/couchrest/core/server.rb +49 -0
  21. data/lib/couchrest/helper/file_manager.rb +223 -0
  22. data/lib/couchrest/helper/pager.rb +103 -0
  23. data/lib/couchrest/helper/streamer.rb +29 -0
  24. data/lib/couchrest/monkeypatches.rb +22 -0
  25. data/spec/couchrest_spec.rb +92 -0
  26. data/spec/database_spec.rb +429 -0
  27. data/spec/file_manager_spec.rb +116 -0
  28. data/spec/fixtures/attachments/test.html +11 -0
  29. data/spec/fixtures/views/lib.js +3 -0
  30. data/spec/fixtures/views/test_view/lib.js +3 -0
  31. data/spec/fixtures/views/test_view/only-map.js +4 -0
  32. data/spec/fixtures/views/test_view/test-map.js +3 -0
  33. data/spec/fixtures/views/test_view/test-reduce.js +3 -0
  34. data/spec/pager_spec.rb +122 -0
  35. data/spec/spec.opts +6 -0
  36. data/spec/spec_helper.rb +4 -0
  37. data/spec/streamer_spec.rb +23 -0
  38. data/utils/remap.rb +27 -0
  39. data/utils/subset.rb +30 -0
  40. metadata +124 -0
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Test</title>
5
+ </head>
6
+ <body>
7
+ <p>
8
+ Test
9
+ </p>
10
+ </body>
11
+ </html>
@@ -0,0 +1,3 @@
1
+ function globalLib() {
2
+ return "fixture";
3
+ };
@@ -0,0 +1,3 @@
1
+ function justThisView() {
2
+ return "fixture";
3
+ };
@@ -0,0 +1,4 @@
1
+ function(doc) {
2
+ //include-lib
3
+ emit(null, null);
4
+ };
@@ -0,0 +1,3 @@
1
+ function(doc) {
2
+ emit(null, null);
3
+ };
@@ -0,0 +1,3 @@
1
+ function(ks,vs,co) {
2
+ return vs.length;
3
+ };
@@ -0,0 +1,122 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe CouchRest::Pager do
4
+ before(:all) do
5
+ @cr = CouchRest.new(COUCHHOST)
6
+ @db = @cr.database(TESTDB)
7
+ @db.delete! rescue nil
8
+ @db = @cr.create_db(TESTDB) rescue nil
9
+ @pager = CouchRest::Pager.new(@db)
10
+ end
11
+
12
+ after(:all) do
13
+ begin
14
+ @db.delete!
15
+ rescue RestClient::Request::RequestFailed
16
+ end
17
+ end
18
+
19
+ it "should store the db" do
20
+ @pager.db.should == @db
21
+ end
22
+
23
+ describe "paging all docs" do
24
+ before(:all) do
25
+ @docs = []
26
+ 100.times do |i|
27
+ @docs << ({:number => (i % 10)})
28
+ end
29
+ @db.bulk_save(@docs)
30
+ end
31
+ it "should yield total_docs / count times" do
32
+ n = 0
33
+ @pager.all_docs(10) do |doc|
34
+ n += 1
35
+ end
36
+ n.should == 10
37
+ end
38
+ it "should yield each docrow group without duplicate docs" do
39
+ docids = {}
40
+ @pager.all_docs(10) do |docrows|
41
+ docrows.each do |row|
42
+ docids[row['id']].should be_nil
43
+ docids[row['id']] = true
44
+ end
45
+ end
46
+ docids.keys.length.should == 100
47
+ end
48
+ it "should yield each docrow group" do
49
+ @pager.all_docs(10) do |docrows|
50
+ doc = @db.get(docrows[0]['id'])
51
+ doc['number'].class.should == Fixnum
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "Pager with a view and docs" do
57
+ before(:all) do
58
+ @docs = []
59
+ 100.times do |i|
60
+ @docs << ({:number => (i % 10)})
61
+ end
62
+ @db.bulk_save(@docs)
63
+ @db.save({
64
+ '_id' => '_design/magic',
65
+ 'views' => {
66
+ 'number' => {
67
+ 'map' => 'function(doc){emit(doc.number,null)}'
68
+ }
69
+ }
70
+ })
71
+ end
72
+
73
+ it "should have docs" do
74
+ @docs.length.should == 100
75
+ @db.documents['rows'].length.should == 101
76
+ end
77
+
78
+ it "should have a view" do
79
+ @db.view('magic/number', :count => 10)['rows'][0]['key'].should == 0
80
+ end
81
+
82
+ it "should yield once per key" do
83
+ results = {}
84
+ @pager.key_reduce('magic/number', 20) do |k,vs|
85
+ results[k] = vs.length
86
+ end
87
+ results[0].should == 10
88
+ results[3].should == 10
89
+ end
90
+
91
+ it "with a small step size should yield once per key" do
92
+ results = {}
93
+ @pager.key_reduce('magic/number', 7) do |k,vs|
94
+ results[k] = vs.length
95
+ end
96
+ results[0].should == 10
97
+ results[3].should == 10
98
+ results[9].should == 10
99
+ end
100
+ it "with a large step size should yield once per key" do
101
+ results = {}
102
+ @pager.key_reduce('magic/number', 1000) do |k,vs|
103
+ results[k] = vs.length
104
+ end
105
+ results[0].should == 10
106
+ results[3].should == 10
107
+ results[9].should == 10
108
+ end
109
+ it "with a begin and end should only yield in the range (and leave out the lastkey)" do
110
+ results = {}
111
+ @pager.key_reduce('magic/number', 1000, 4, 7) do |k,vs|
112
+ results[k] = vs.length
113
+ end
114
+ results[0].should be_nil
115
+ results[4].should == 10
116
+ results[6].should == 10
117
+ results[7].should be_nil
118
+ results[8].should be_nil
119
+ results[9].should be_nil
120
+ end
121
+ end
122
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,4 @@
1
+ require File.dirname(__FILE__) + '/../lib/couchrest'
2
+
3
+ COUCHHOST = "http://localhost:5984"
4
+ TESTDB = 'couchrest-test'
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe CouchRest::Streamer do
4
+ before(:all) do
5
+ @cr = CouchRest.new(COUCHHOST)
6
+ @db = @cr.database(TESTDB)
7
+ @db.delete! rescue nil
8
+ @db = @cr.create_db(TESTDB) rescue nil
9
+ @streamer = CouchRest::Streamer.new(@db)
10
+ @docs = (1..1000).collect{|i| {:integer => i, :string => i.to_s}}
11
+ @db.bulk_save(@docs)
12
+ end
13
+
14
+ it "should yield each row in a view" do
15
+ count = 0
16
+ sum = 0
17
+ @streamer.view("_all_docs") do |row|
18
+ count += 1
19
+ end
20
+ count.should == 1000
21
+ end
22
+
23
+ end
data/utils/remap.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'couchrest'
3
+
4
+ # set the source db and map view
5
+ source = CouchRest.new("http://localhost:5984").database('source-db')
6
+ source_view = 'mydesign/view-map'
7
+
8
+ # set the target db
9
+ target = CouchRest.new("http://localhost:5984").database('target-db')
10
+
11
+
12
+ pager = CouchRest::Pager.new(source)
13
+
14
+ # pager will yield once per uniq key in the source view
15
+
16
+ pager.key_reduce(source_view, 10000) do |key, values|
17
+ # create a doc from the key and the values
18
+ example_doc = {
19
+ :key => key,
20
+ :values => values.uniq
21
+ }
22
+
23
+ target.save(example_doc)
24
+
25
+ # keep us up to date with progress
26
+ puts k if (rand > 0.9)
27
+ end
data/utils/subset.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'couchrest'
3
+
4
+ # subset.rb replicates a percentage of a database to a fresh database.
5
+ # use it to create a smaller dataset on which to prototype views.
6
+
7
+ # specify the source database
8
+ source = CouchRest.new("http://localhost:5984").database('source-db')
9
+
10
+ # specify the target database
11
+ target = CouchRest.new("http://localhost:5984").database('target-db')
12
+
13
+ # pager efficiently yields all view rows
14
+ pager = CouchRest::Pager.new(source)
15
+
16
+ pager.all_docs(1000) do |rows|
17
+ docs = rows.collect do |r|
18
+ # the percentage of docs to clone
19
+ next if rand > 0.1
20
+ doc = source.get(r['id'])
21
+ doc.delete('_rev')
22
+ doc
23
+ end.compact
24
+ puts docs.length
25
+ next if docs.empty?
26
+
27
+ puts docs.first['_id']
28
+ target.bulk_save(docs)
29
+ end
30
+
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: topfunky-couchrest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.4
5
+ platform: ruby
6
+ authors:
7
+ - J. Chris Anderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.2
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: rest-client
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0.5"
32
+ version:
33
+ description: CouchRest provides a simple interface on top of CouchDB's RESTful HTTP API, as well as including some utility scripts for managing views and attachments.
34
+ email: jchris@grabb.it
35
+ executables:
36
+ - couchview
37
+ - couchdir
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.rdoc
42
+ - LICENSE
43
+ - THANKS
44
+ files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - THANKS
49
+ - bin/couchdir
50
+ - bin/couchview
51
+ - examples/word_count
52
+ - examples/word_count/markov
53
+ - examples/word_count/views
54
+ - examples/word_count/views/books
55
+ - examples/word_count/views/books/chunked-map.js
56
+ - examples/word_count/views/books/united-map.js
57
+ - examples/word_count/views/markov
58
+ - examples/word_count/views/markov/chain-map.js
59
+ - examples/word_count/views/markov/chain-reduce.js
60
+ - examples/word_count/views/word_count
61
+ - examples/word_count/views/word_count/count-map.js
62
+ - examples/word_count/views/word_count/count-reduce.js
63
+ - examples/word_count/word_count.rb
64
+ - examples/word_count/word_count_query.rb
65
+ - lib/couchrest
66
+ - lib/couchrest/commands
67
+ - lib/couchrest/commands/generate.rb
68
+ - lib/couchrest/commands/push.rb
69
+ - lib/couchrest/core
70
+ - lib/couchrest/core/database.rb
71
+ - lib/couchrest/core/server.rb
72
+ - lib/couchrest/helper
73
+ - lib/couchrest/helper/file_manager.rb
74
+ - lib/couchrest/helper/pager.rb
75
+ - lib/couchrest/helper/streamer.rb
76
+ - lib/couchrest/monkeypatches.rb
77
+ - lib/couchrest.rb
78
+ - spec/couchrest_spec.rb
79
+ - spec/database_spec.rb
80
+ - spec/file_manager_spec.rb
81
+ - spec/fixtures
82
+ - spec/fixtures/attachments
83
+ - spec/fixtures/attachments/test.html
84
+ - spec/fixtures/views
85
+ - spec/fixtures/views/lib.js
86
+ - spec/fixtures/views/test_view
87
+ - spec/fixtures/views/test_view/lib.js
88
+ - spec/fixtures/views/test_view/only-map.js
89
+ - spec/fixtures/views/test_view/test-map.js
90
+ - spec/fixtures/views/test_view/test-reduce.js
91
+ - spec/pager_spec.rb
92
+ - spec/spec.opts
93
+ - spec/spec_helper.rb
94
+ - spec/streamer_spec.rb
95
+ - utils/remap.rb
96
+ - utils/subset.rb
97
+ has_rdoc: "true"
98
+ homepage: http://github.com/jchris/couchrest
99
+ post_install_message:
100
+ rdoc_options: []
101
+
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: "0"
109
+ version:
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: "0"
115
+ version:
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.2.0
120
+ signing_key:
121
+ specification_version: 2
122
+ summary: Lean and RESTful interface to CouchDB.
123
+ test_files: []
124
+