Module: CouchbaseDocumentStore

Defined in:
lib/couchbase_document_store.rb,
lib/couchbase_document_store/version.rb

Constant Summary collapse

CB =
Couchbase.connect(setting_hash)
VERSION =
"0.0.5"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_document(key, value, args = {}) ⇒ Object



123
124
125
126
127
# File 'lib/couchbase_document_store.rb', line 123

def create_document(key, value, args={})
  return nil unless key
  CB.quiet = args[:quiet] || true
  CB.add(key, value, args) # => if !quiet, Generates Couchbase::Error::KeyExists if key already exists
end

.decrease_atomic_count(key, args = {}) ⇒ Object



171
172
173
174
175
# File 'lib/couchbase_document_store.rb', line 171

def decrease_atomic_count(key, args={})
  return nil unless key
  CB.quiet = args[:quiet] || true
  CB.decr(key, args[:amount] || 1)
end

.delete_all_documents!Object



95
96
97
# File 'lib/couchbase_document_store.rb', line 95

def delete_all_documents!
  CB.flush
end

.delete_document(key, args = {}) ⇒ Object



159
160
161
162
163
# File 'lib/couchbase_document_store.rb', line 159

def delete_document(key, args={})
  return nil unless key
  CB.quiet = args[:quiet] || true
  CB.delete(key)
end

.document_exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/couchbase_document_store.rb', line 99

def document_exists?(key)
  return nil unless key

  # Save quiet setting
  tmp = CB.quiet

  # Set quiet to be sure
  CB.quiet = true

  doc = CB.get(key)

  # Restore quiet setting
  CB.quiet = tmp

  !doc.nil?
end

.force_set_document(key, value, args = {}) ⇒ Object

preferred way is to use create/replace instead of this to make sure there are no collisions



178
179
180
181
182
# File 'lib/couchbase_document_store.rb', line 178

def force_set_document(key, value, args={})
  return nil unless key
  CB.quiet = args[:quiet] || true
  CB.set(key, value, args)
end

.get_document(key, args = {}) ⇒ Object



135
136
137
138
139
140
# File 'lib/couchbase_document_store.rb', line 135

def get_document(key, args = {})
  return nil unless key
  CB.quiet = args[:quiet] || true
  doc = CB.get(key, args)
  doc.is_a?(Hash) ? Map.new(doc) : doc
end

.get_documents(keys = [], args = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/couchbase_document_store.rb', line 142

def get_documents(keys = [], args = {})
  return nil unless keys || keys.empty?
  values = CB.get(keys, args)

  if values.is_a? Hash
    tmp = []
    tmp[0] = values
    values = tmp
  end
  # convert hashes to Map (subclass of Hash with *better* indifferent access)
  values.each_with_index do |v, i|
    values[i] = Map.new(v) if v.is_a? Hash
  end

  values
end

.increase_atomic_count(key, args = {}) ⇒ Object



165
166
167
168
169
# File 'lib/couchbase_document_store.rb', line 165

def increase_atomic_count(key, args={} )
  return nil unless key
  CB.quiet = args[:quiet] || true
  CB.incr(key, args[:amount] || 1)
end

.initialize_document(key, value, args = {}) ⇒ Object



116
117
118
119
120
121
# File 'lib/couchbase_document_store.rb', line 116

def initialize_document(key, value, args={})
  return nil unless key
  CB.quiet = true
  doc = DocumentStore.get_document( key )
  (value.is_a?(Fixnum) || value.is_a?(Integer) ? CB.set( key, value ) : CB.add( key, value )) unless doc
end

.replace_document(key, value, args = {}) ⇒ Object



129
130
131
132
133
# File 'lib/couchbase_document_store.rb', line 129

def replace_document(key, value, args = {})
  return nil unless key
  CB.quiet = args[:quiet] || true
  CB.replace(key, value) # => if !quiet, Generates Couchbase::Error::NotFound if key doesn't exist
end

Instance Method Details

#create_document(key, value, args = {}) ⇒ Object

Create a new document (Couchbase#add)



43
44
45
46
# File 'lib/couchbase_document_store.rb', line 43

def create_document(key, value, args={})
  return nil unless key
  CouchbaseDocumentStore.create_document(key, value, args) # => if !quiet, Generates Couchbase::Error::KeyExists if key already exists
end

#decrease_atomic_count(key, args = {}) ⇒ Object



76
77
78
79
# File 'lib/couchbase_document_store.rb', line 76

def decrease_atomic_count(key, args={})
  return nil unless key
  CouchbaseDocumentStore.decrease_atomic_count(key, args)
end

#delete_document(key, args = {}) ⇒ Object



65
66
67
68
# File 'lib/couchbase_document_store.rb', line 65

def delete_document(key, args={})
  return nil unless key
  CouchbaseDocumentStore.delete_document(key, args)
end

#document_exists?(key) ⇒ Boolean

Check if a key/document exists

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/couchbase_document_store.rb', line 31

def document_exists?(key)
  return nil unless key
  CouchbaseDocumentStore.document_exists?(key)
end

#force_set_document(key, value, args = {}) ⇒ Object

preferred way is to use create/replace to make sure there are no collisions



82
83
84
85
# File 'lib/couchbase_document_store.rb', line 82

def force_set_document(key, value, args={})
  return nil unless key
  CouchbaseDocumentStore.force_set_document(key, value, args)
end

#get_document(key, args = {}) ⇒ Object



55
56
57
58
# File 'lib/couchbase_document_store.rb', line 55

def get_document(key, args = {})
  return nil unless key
  CouchbaseDocumentStore.get_document(key, args)
end

#get_documents(keys = [], args = {}) ⇒ Object



60
61
62
63
# File 'lib/couchbase_document_store.rb', line 60

def get_documents(keys = [], args = {})
  return nil unless keys || keys.empty?
  CouchbaseDocumentStore.get_documents(keys, args)
end

#increase_atomic_count(key, args = {}) ⇒ Object

Parameters:

  • args (defaults to: {})

    :amount => Fixnum||Integer, increases by that



71
72
73
74
# File 'lib/couchbase_document_store.rb', line 71

def increase_atomic_count(key, args={})
  return nil unless key
  CouchbaseDocumentStore.increase_atomic_count(key, args)
end

#initialize_document(key, value, args = {}) ⇒ Object

Initialize a document, if it doesn’t exist, create it, if it exists, ignore the call



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

def initialize_document(key, value, args={})
  return nil unless key
  CouchbaseDocumentStore.initialize_document(key, value, args)
end

#replace_document(key, value, args = {}) ⇒ Object

Replace a new document (Couchbase#replace), throws Couchbase::Error

if it doesn’t exist



49
50
51
52
# File 'lib/couchbase_document_store.rb', line 49

def replace_document(key, value, args = {})
  return nil unless key
  CouchbaseDocumentStore.replace_document(key, value, args)
end