couchbase-docstore 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +1 -1
- data/couchbase_doc_store.gemspec +22 -0
- data/lib/couchbase_doc_store.rb +190 -0
- data/lib/couchbase_doc_store/version.rb +3 -0
- metadata +5 -2
data/README.md
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'couchbase_doc_store/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "couchbase-docstore"
|
8
|
+
gem.version = CouchbaseDocStore::VERSION
|
9
|
+
gem.authors = ["Jasdeep Jaitla"]
|
10
|
+
gem.email = ["jasdeep@scalabl3.com"]
|
11
|
+
gem.description = %q{A Convenient Wrapper for the Couchbase gem, uses Map gem and adds new functionality}
|
12
|
+
gem.summary = %q{You simply use the Couchbase gem, or you can use this wrapper that encapsulates the gem and adds some subjective conveniences. }
|
13
|
+
gem.homepage = "https://github.com/scalabl3/couchbase-docstore"
|
14
|
+
|
15
|
+
gem.add_dependency('couchbase-settings', '>= 0.1.0')
|
16
|
+
gem.add_dependency('couchbase', '>= 1.2.0.z.beta3')
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'couchbase'
|
2
|
+
require 'map'
|
3
|
+
require 'couchbase_settings'
|
4
|
+
require "couchbase_doc_store/version"
|
5
|
+
|
6
|
+
module CouchbaseDocStore
|
7
|
+
|
8
|
+
Rails.logger.debug(CouchbaseSetting.pm)
|
9
|
+
|
10
|
+
setting_hash = {}
|
11
|
+
if (CouchbaseSetting.respond_to?("servers") && CouchbaseSetting.servers && !CouchbaseSetting.servers.empty?)
|
12
|
+
setting_hash[:node_list] = CouchbaseSetting.servers
|
13
|
+
elsif CouchbaseSetting.respond_to?("server")
|
14
|
+
|
15
|
+
setting_hash[:hostname] = CouchbaseSetting.server
|
16
|
+
else
|
17
|
+
raise ArgumentError, "You didn't set a Couchbase Server in your /config/couchbase.yml file!"
|
18
|
+
end
|
19
|
+
setting_hash[:pool] = "default"
|
20
|
+
setting_hash[:bucket] = CouchbaseSetting.bucket
|
21
|
+
setting_hash[:port] = 8091
|
22
|
+
|
23
|
+
if (CouchbaseSetting.respond_to?("password") && CouchbaseSetting.password && !CouchbaseSetting.password.blank?)
|
24
|
+
setting_hash[:username] = CouchbaseSetting.bucket
|
25
|
+
setting_hash[:password] = CouchbaseSetting.password
|
26
|
+
end
|
27
|
+
|
28
|
+
CB = Couchbase.connect(setting_hash)
|
29
|
+
|
30
|
+
#### INSTANCE METHODS
|
31
|
+
|
32
|
+
# Check if a key/document exists
|
33
|
+
def document_exists?(key)
|
34
|
+
return nil unless key
|
35
|
+
CouchbaseDocStore.document_exists?(key)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Initialize a document, if it doesn't exist, create it, if it exists, ignore the call
|
39
|
+
def initialize_document(key, value, args={})
|
40
|
+
return nil unless key
|
41
|
+
CouchbaseDocStore.initialize_document(key, value, args)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Create a new document (Couchbase#add)
|
45
|
+
def create_document(key, value, args={})
|
46
|
+
return nil unless key
|
47
|
+
CouchbaseDocStore.create_document(key, value, args) # => if !quiet, Generates Couchbase::Error::KeyExists if key already exists
|
48
|
+
end
|
49
|
+
|
50
|
+
# Replace a new document (Couchbase#replace), throws Couchbase::Error:: if it doesn't exist
|
51
|
+
def replace_document(key, value, args = {})
|
52
|
+
return nil unless key
|
53
|
+
CouchbaseDocStore.replace_document(key, value, args)
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
def get_document(key, args = {})
|
58
|
+
return nil unless key
|
59
|
+
CouchbaseDocStore.get_document(key, args)
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_documents(keys = [], args = {})
|
63
|
+
return nil unless keys || keys.empty?
|
64
|
+
CouchbaseDocStore.get_documents(keys, args)
|
65
|
+
end
|
66
|
+
|
67
|
+
def delete_document(key, args={})
|
68
|
+
return nil unless key
|
69
|
+
CouchbaseDocStore.delete_document(key, args)
|
70
|
+
end
|
71
|
+
|
72
|
+
# @param args :amount => Fixnum||Integer, increases by that
|
73
|
+
def increase_atomic_count(key, args={})
|
74
|
+
return nil unless key
|
75
|
+
CouchbaseDocStore.increase_atomic_count(key, args)
|
76
|
+
end
|
77
|
+
|
78
|
+
def decrease_atomic_count(key, args={})
|
79
|
+
return nil unless key
|
80
|
+
CouchbaseDocStore.decrease_atomic_count(key, args)
|
81
|
+
end
|
82
|
+
|
83
|
+
# preferred way is to use create/replace to make sure there are no collisions
|
84
|
+
def force_set_document(key, value, args={})
|
85
|
+
return nil unless key
|
86
|
+
CouchbaseDocStore.force_set_document(key, value, args)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
# end Instance Methods
|
92
|
+
#####################################################################
|
93
|
+
#### CLASS METHODS
|
94
|
+
|
95
|
+
class << self
|
96
|
+
|
97
|
+
def delete_all_documents!
|
98
|
+
CB.flush
|
99
|
+
end
|
100
|
+
|
101
|
+
def document_exists?(key)
|
102
|
+
return nil unless key
|
103
|
+
|
104
|
+
# Save quiet setting
|
105
|
+
tmp = CB.quiet
|
106
|
+
|
107
|
+
# Set quiet to be sure
|
108
|
+
CB.quiet = true
|
109
|
+
|
110
|
+
doc = CB.get(key)
|
111
|
+
|
112
|
+
# Restore quiet setting
|
113
|
+
CB.quiet = tmp
|
114
|
+
|
115
|
+
!doc.nil?
|
116
|
+
end
|
117
|
+
|
118
|
+
def initialize_document(key, value, args={})
|
119
|
+
return nil unless key
|
120
|
+
CB.quiet = true
|
121
|
+
doc = CouchbaseDocStore.get_document( key )
|
122
|
+
(value.is_a?(Fixnum) || value.is_a?(Integer) ? CB.set( key, value ) : CB.add( key, value )) unless doc
|
123
|
+
end
|
124
|
+
|
125
|
+
def create_document(key, value, args={})
|
126
|
+
return nil unless key
|
127
|
+
CB.quiet = args[:quiet] || true
|
128
|
+
CB.add(key, value, args) # => if !quiet, Generates Couchbase::Error::KeyExists if key already exists
|
129
|
+
end
|
130
|
+
|
131
|
+
def replace_document(key, value, args = {})
|
132
|
+
return nil unless key
|
133
|
+
CB.quiet = args[:quiet] || true
|
134
|
+
CB.replace(key, value) # => if !quiet, Generates Couchbase::Error::NotFound if key doesn't exist
|
135
|
+
end
|
136
|
+
|
137
|
+
def get_document(key, args = {})
|
138
|
+
return nil unless key
|
139
|
+
CB.quiet = args[:quiet] || true
|
140
|
+
doc = CB.get(key, args)
|
141
|
+
doc.is_a?(Hash) ? Map.new(doc) : doc
|
142
|
+
end
|
143
|
+
|
144
|
+
def get_documents(keys = [], args = {})
|
145
|
+
return nil unless keys || keys.empty?
|
146
|
+
values = CB.get(keys, args)
|
147
|
+
|
148
|
+
if values.is_a? Hash
|
149
|
+
tmp = []
|
150
|
+
tmp[0] = values
|
151
|
+
values = tmp
|
152
|
+
end
|
153
|
+
# convert hashes to Map (subclass of Hash with *better* indifferent access)
|
154
|
+
values.each_with_index do |v, i|
|
155
|
+
values[i] = Map.new(v) if v.is_a? Hash
|
156
|
+
end
|
157
|
+
|
158
|
+
values
|
159
|
+
end
|
160
|
+
|
161
|
+
def delete_document(key, args={})
|
162
|
+
return nil unless key
|
163
|
+
CB.quiet = args[:quiet] || true
|
164
|
+
CB.delete(key)
|
165
|
+
end
|
166
|
+
|
167
|
+
def increase_atomic_count(key, args={} )
|
168
|
+
return nil unless key
|
169
|
+
CB.quiet = args[:quiet] || true
|
170
|
+
CB.incr(key, args[:amount] || 1)
|
171
|
+
end
|
172
|
+
|
173
|
+
def decrease_atomic_count(key, args={})
|
174
|
+
return nil unless key
|
175
|
+
CB.quiet = args[:quiet] || true
|
176
|
+
CB.decr(key, args[:amount] || 1)
|
177
|
+
end
|
178
|
+
|
179
|
+
# preferred way is to use create/replace instead of this to make sure there are no collisions
|
180
|
+
def force_set_document(key, value, args={})
|
181
|
+
return nil unless key
|
182
|
+
CB.quiet = args[:quiet] || true
|
183
|
+
CB.set(key, value, args)
|
184
|
+
end
|
185
|
+
|
186
|
+
end# end ClassMethods
|
187
|
+
|
188
|
+
#####################################################################
|
189
|
+
|
190
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couchbase-docstore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -56,7 +56,10 @@ files:
|
|
56
56
|
- LICENSE.txt
|
57
57
|
- README.md
|
58
58
|
- Rakefile
|
59
|
-
|
59
|
+
- couchbase_doc_store.gemspec
|
60
|
+
- lib/couchbase_doc_store.rb
|
61
|
+
- lib/couchbase_doc_store/version.rb
|
62
|
+
homepage: https://github.com/scalabl3/couchbase-docstore
|
60
63
|
licenses: []
|
61
64
|
post_install_message:
|
62
65
|
rdoc_options: []
|