leanback 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.rdoc CHANGED
@@ -1,3 +1,18 @@
1
+ =0.4.0
2
+ June 9, 2013
3
+ * added ability to find documents by multiple keys
4
+ Example:
5
+ keys = {:gender => 'male',:age => '34'}
6
+ Couchdb.find_by_keys({:database => 'friends', :keys => keys})
7
+ This will return all documents in the friends database with gender = male and age = 34
8
+ * added the ability to count documents by multiple keys
9
+ Example:
10
+ keys = {:gender => 'male',:age => '40'}
11
+ count = Couchdb.count_by_keys({:database => 'friends', :keys => keys})
12
+ This will return the number of documents in the friends database with gender = male and age = 40.
13
+ See documentation for details.
14
+
15
+
1
16
  =0.3.4
2
17
  June 2, 2013
3
18
  * added options for manipulating query results, the options available are limit, skip, descending, startkey, endkey
data/README.md CHANGED
@@ -14,7 +14,7 @@ This project is still under development. Not complete by any means. I made this
14
14
  Upgraded and tested on CouchDB 1.2.0. CouchDB 1.0.1 should still work fine.
15
15
 
16
16
  ##Changes
17
- (https://github.com/obi-a/leanback/blob/master/Changelog.rdoc)
17
+ View the [Changelog](https://github.com/obi-a/leanback/blob/master/Changelog.rdoc)
18
18
 
19
19
  ##Usage
20
20
 
@@ -26,9 +26,13 @@ Upgraded and tested on CouchDB 1.2.0. CouchDB 1.0.1 should still work fine.
26
26
  + [Basic CouchDB Operations](http://www.whisperservers.com/leanback/basic-couchdb-operations/)
27
27
 
28
28
  + [Find Documents by Key](http://www.whisperservers.com/leanback/find-documents-by-key/)
29
+
30
+ + [Find Document By Multiple Keys] (http://www.whisperservers.com/leanback/find-document-by-multiple-keys/)
29
31
 
30
32
  + [Count Documents by Key](http://www.whisperservers.com/leanback/count-documents-by-key/)
31
33
 
34
+ + [Count Documents by Multiple Keys] (http://www.whisperservers.com/leanback/count-by-multiple-documents/)
35
+
32
36
  + Working with [CouchDB Views](http://www.whisperservers.com/leanback/design-documents-and-permanent-views/)
33
37
 
34
38
  + [Error Handling](http://www.whisperservers.com/leanback/error-handling/)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.4.0
data/leanback.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "leanback"
8
- s.version = "0.3.4"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Obi Akubue"]
12
- s.date = "2013-06-02"
12
+ s.date = "2013-06-09"
13
13
  s.description = "lightweight Ruby interface to CouchDB"
14
14
  s.email = "obioraakubue@yahoo.com"
15
15
  s.extra_rdoc_files = [
data/lib/leanback.rb CHANGED
@@ -396,6 +396,62 @@ def self.add_finder(options,auth_session = "")
396
396
  end
397
397
  end
398
398
 
399
+ #add a multiple finder method to the database
400
+ #this creates a find by keys method
401
+ def self.add_multiple_finder(options,auth_session = "")
402
+ set_address
403
+ db_name = options[:database]
404
+ keys = options[:keys]
405
+ view_name = keys.join("_")
406
+ key = keys.join(",doc.")
407
+ condition = keys.join(" && doc.")
408
+ design_doc_name = view_name + '_keys_finder'
409
+
410
+ view = '{
411
+ "language" : "javascript",
412
+ "views" :{
413
+ "find_by_keys_'+view_name+'" : {
414
+ "map" : "function(doc){ if(doc.'+condition+') emit([doc.'+key+'],doc);}"
415
+ }
416
+ }
417
+ }'
418
+
419
+ begin
420
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:cookies => {"AuthSession" => auth_session}}
421
+ rescue => e
422
+ hash = Yajl::Parser.parse(e.response.to_s)
423
+ raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1]
424
+ end
425
+ end
426
+
427
+ #add a multiple counter method to the database
428
+ #this creates a count by keys method
429
+ def self.add_multiple_counter(options,auth_session = "")
430
+ set_address
431
+ db_name = options[:database]
432
+ keys = options[:keys]
433
+ view_name = keys.join("_")
434
+ key = keys.join(",doc.")
435
+ condition = keys.join(" && doc.")
436
+ design_doc_name = view_name + '_keys_counter'
437
+
438
+ view = '{
439
+ "language" : "javascript",
440
+ "views" :{
441
+ "count_by_keys_'+view_name+'" : {
442
+ "map" : "function(doc){ if(doc.'+condition+') emit([doc.'+key+'],null);}", "reduce": "_count"
443
+ }
444
+ }
445
+ }'
446
+
447
+ begin
448
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:cookies => {"AuthSession" => auth_session}}
449
+ rescue => e
450
+ hash = Yajl::Parser.parse(e.response.to_s)
451
+ raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1]
452
+ end
453
+ end
454
+
399
455
  #add a counter method to the database
400
456
  #this creates a count method that counts documents by key
401
457
  def self.add_counter(options,auth_session = "")
@@ -443,6 +499,40 @@ def self.count(options,auth_session = "")
443
499
  return count.to_i
444
500
  end
445
501
 
502
+ #count by keys
503
+ #example:
504
+ #hash = {:firstname =>'john', :gender => 'male' }
505
+ #Couchdb.count_by_keys({:database => 'contacts', :keys => hash},auth_session)
506
+ def self.count_by_keys(options,auth_session = "", params = {})
507
+ set_address
508
+ db_name = options[:database]
509
+ keys = []
510
+ search_term = []
511
+ hash = options[:keys]
512
+
513
+ hash.each do |k,v|
514
+ keys << k
515
+ search_term << v
516
+ end
517
+ index = keys.join("_")
518
+ design_doc_name = index + '_keys_counter'
519
+ view_name = 'count_by_keys_' + index
520
+ params[:startkey] = search_term
521
+ params[:endkey] = search_term
522
+
523
+ begin
524
+ view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
525
+ docs = find view,auth_session,key=nil,params
526
+ rescue CouchdbException => e
527
+ #add a finder/index if one doesn't already exist in the database
528
+ #then find_by_keys
529
+ add_multiple_counter({:database => db_name, :keys => keys},auth_session)
530
+ docs = find view,auth_session,key=nil,params
531
+ end
532
+ count = docs[0]
533
+ return count.to_i
534
+ end
535
+
446
536
  #find by key
447
537
  def self.find_by(options,auth_session = "", params = {})
448
538
  set_address
@@ -464,6 +554,39 @@ def self.find_by(options,auth_session = "", params = {})
464
554
  return docs
465
555
  end
466
556
 
557
+ #find by keys
558
+ #example:
559
+ #hash = {:firstname =>'john', :gender => 'male' }
560
+ #Couchdb.find_by_keys({:database => 'contacts', :keys => hash},auth_session)
561
+ def self.find_by_keys(options,auth_session = "", params = {})
562
+ set_address
563
+ db_name = options[:database]
564
+ keys = []
565
+ search_term = []
566
+ hash = options[:keys]
567
+
568
+ hash.each do |k,v|
569
+ keys << k
570
+ search_term << v
571
+ end
572
+ index = keys.join("_")
573
+ design_doc_name = index + '_keys_finder'
574
+ view_name = 'find_by_keys_' + index
575
+ params[:startkey] = search_term
576
+ params[:endkey] = search_term
577
+
578
+ begin
579
+ view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
580
+ docs = find view,auth_session,key=nil,params
581
+ rescue CouchdbException => e
582
+ #add a finder/index if one doesn't already exist in the database
583
+ #then find_by_keys
584
+ add_multiple_finder({:database => db_name, :keys => keys},auth_session)
585
+ docs = find view,auth_session,key=nil,params
586
+ end
587
+ return docs
588
+ end
589
+
467
590
  #return a list of all docs in the database
468
591
  def self.docs_from(database_name,auth_session = "")
469
592
  set_address
@@ -64,6 +64,39 @@ it "find items by key" do
64
64
  Couchdb.delete_doc({:database => 'friends', :doc_id => '_design/lastname_finder'})
65
65
  end
66
66
 
67
+ it "should find items by multiple keys" do
68
+ keys = {:gender => 'male',:age => '34'}
69
+ docs = Couchdb.find_by_keys({:database => 'friends', :keys => keys})
70
+ d = docs[0]
71
+ d["age"].should == "34"
72
+ end
73
+
74
+ it "should find items by multiple keys using a single key" do
75
+ keys = {:lastname => 'smith'}
76
+ docs = Couchdb.find_by_keys({:database => 'friends', :keys => keys})
77
+ d = docs[0]
78
+ d["lastname"].should == "smith"
79
+ end
80
+
81
+ it "should find items by multiple keys" do
82
+ keys = {:gender => 'male',:age => '40'}
83
+ docs = Couchdb.find_by_keys({:database => 'friends', :keys => keys})
84
+ docs.should == []
85
+ end
86
+
87
+ it "should count items by multiple keys" do
88
+ keys = {:gender => 'male',:age => '34'}
89
+ count = Couchdb.count_by_keys({:database => 'friends', :keys => keys})
90
+ count.should == 1
91
+ end
92
+
93
+ it "should count items by multiple keys" do
94
+ keys = {:gender => 'male',:age => '40'}
95
+ count = Couchdb.count_by_keys({:database => 'friends', :keys => keys})
96
+ count.should == 0
97
+ end
98
+
99
+
67
100
  it "should query a permanent view that doesn't exist and handle exception" do
68
101
  begin
69
102
  view = { :database => "friends", :design_doc => 'more_views', :view => 'get_user_email'}
@@ -205,6 +238,13 @@ it "should test finder options" do
205
238
  data = {:firstname => 'sam', :gender =>'male', :age => '28', :salary => '97000'}
206
239
  doc = {:database => 'fishes', :doc_id => 'sam', :data => data}
207
240
  Couchdb.create_doc doc
241
+
242
+
243
+ keys = {:age =>'28', :gender => 'male'}
244
+ hash = Couchdb.find_by_keys({:database => 'fishes', :keys => keys},'', options = {:limit => 2, :skip => 1})
245
+ h = hash[0]
246
+ h["firstname"].should == "john"
247
+ hash.length.should == 2
208
248
 
209
249
  #create the design doc to be queryed in the test
210
250
  Couchdb.find_by({:database => 'fishes', :gender => 'male'})
@@ -70,6 +70,37 @@ it "find items by key" do
70
70
  Couchdb.delete_doc({:database => 'contacts', :doc_id => '_design/lastname_finder'},@@auth_session)
71
71
  end
72
72
 
73
+ it "should find items by multiple keys" do
74
+ keys = {:gender => 'male',:age => '34'}
75
+ docs = Couchdb.find_by_keys({:database => 'contacts', :keys => keys},@@auth_session)
76
+ d = docs[0]
77
+ d["age"].should == "34"
78
+ end
79
+
80
+ it "should find items by multiple keys using a single key" do
81
+ keys = {:lastname => 'smith'}
82
+ docs = Couchdb.find_by_keys({:database => 'contacts', :keys => keys},@@auth_session)
83
+ d = docs[0]
84
+ d["lastname"].should == "smith"
85
+ end
86
+
87
+ it "should find items by multiple keys" do
88
+ keys = {:gender => 'male',:age => '40'}
89
+ docs = Couchdb.find_by_keys({:database => 'contacts', :keys => keys},@@auth_session)
90
+ docs.should == []
91
+ end
92
+
93
+ it "should count items by multiple keys" do
94
+ keys = {:gender => 'male',:age => '34'}
95
+ count = Couchdb.count_by_keys({:database => 'contacts', :keys => keys},@@auth_session)
96
+ count.should == 1
97
+ end
98
+
99
+ it "should count items by multiple keys" do
100
+ keys = {:gender => 'male',:age => '40'}
101
+ count = Couchdb.count_by_keys({:database => 'contacts', :keys => keys},@@auth_session)
102
+ count.should == 0
103
+ end
73
104
 
74
105
 
75
106
 
@@ -213,6 +244,13 @@ it "should test finder options" do
213
244
  data = {:firstname => 'sam', :gender =>'male', :age => '28', :salary => '97000'}
214
245
  doc = {:database => 'fishes', :doc_id => 'sam', :data => data}
215
246
  Couchdb.create_doc doc,@@auth_session
247
+
248
+
249
+ keys = {:age =>'28', :gender => 'male'}
250
+ hash = Couchdb.find_by_keys({:database => 'fishes', :keys => keys},@@auth_session, options = {:limit => 2, :skip => 1})
251
+ h = hash[0]
252
+ h["firstname"].should == "john"
253
+ hash.length.should == 2
216
254
 
217
255
  #create the design doc to be queryed in the test
218
256
  Couchdb.find_by({:database => 'fishes', :gender => 'male'},@@auth_session)
@@ -421,7 +459,7 @@ it "should switch to default bind address" do
421
459
  data = {:section => "httpd",
422
460
  :key => "port",
423
461
  :value => "5984" }
424
- Couchdb.set_config(data,@@auth_session)
462
+ Couchdb.set_config(data,@@auth_session)
425
463
 
426
464
  #Couchdb.address = nil
427
465
  #Couchdb.port = nil
data/test/main.rb CHANGED
@@ -44,7 +44,7 @@ view = { :database => "zralph1369728249zeb1292e18b998352",
44
44
  view = { :database => "monitors",
45
45
  :design_doc => 'test_finder',
46
46
  :view => 'find_by_test'}
47
- hash = Couchdb.find view,auth_session,key=nil, options = {:startkey => ["1000","UP"], :endkey => ["6000","UP"]}
47
+ #hash = Couchdb.find view,auth_session,key=nil, options = {:startkey => ["1000","UP"], :endkey => ["6000","UP"]}
48
48
 
49
49
  #hash = Couchdb.find view,auth_session,key=nil, options = {:startkey => ["3300","UP"],:descending => true}
50
50
 
@@ -54,6 +54,33 @@ hash = Couchdb.find view,auth_session,key=nil, options = {:startkey => ["1000","
54
54
 
55
55
  #hash = Couchdb.find view,auth_session,key= '?keys=%5B%228800%22,%22UP%22%5D'
56
56
 
57
+ keys = {:tag =>'ralph', :via => 'ses' }
58
+ hash = Couchdb.find_by_keys({:database => 'monitors', :keys => keys},auth_session)
59
+
60
+ #keys = {:tag =>'ralph', :via => 'ses' }
61
+ #num = Couchdb.count_by_keys({:database => 'monitors', :keys => keys},auth_session)
62
+
63
+ #keys = {:tag =>'ralph', :via => 'ses', :every => '1m'}
64
+ #hash = Couchdb.find_by_keys({:database => 'monitors', :keys => keys},auth_session, options = {:limit => 1, :skip => 1})
65
+
66
+ #keys = {:tag =>'ralph', :via => 'ses', :every => '5m'}
67
+ #num = Couchdb.count_by_keys({:database => 'monitors', :keys => keys},auth_session)
68
+
69
+ #keys = {:tag => 'ralph'}
70
+ #hash = Couchdb.find_by_keys({:database => 'monitors', :keys => keys},auth_session)
71
+
72
+ #keys = {:tag => 'ralph'}
73
+ #num = Couchdb.count_by_keys({:database => 'monitors', :keys => keys},auth_session)
74
+
75
+
76
+ #Couchdb.add_multiple_finder({:database => "monitors", :keys => ["tag","via"]},auth_session)
77
+
78
+ #Couchdb.add_multiple_finder({:database => "monitors", :keys => ["tag","via","state"]},auth_session)
79
+
80
+ #Couchdb.add_multiple_finder({:database => "monitors", :keys => ["tag"]},auth_session)
81
+
82
+ #puts num.to_s
83
+
57
84
  puts hash.inspect
58
85
 
59
86
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leanback
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-02 00:00:00.000000000 Z
12
+ date: 2013-06-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -167,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
167
167
  version: '0'
168
168
  segments:
169
169
  - 0
170
- hash: 887863200366044810
170
+ hash: -3576361140829374907
171
171
  required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  none: false
173
173
  requirements: