Module: CouchbaseDocStore

Defined in:
lib/couchbase-docstore.rb,
lib/couchbase_doc_store/railtie.rb,
lib/couchbase_doc_store/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connect!(setting_hash = {hostname: "127.0.0.1", bucket: "default"}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/couchbase-docstore.rb', line 79

def connect!(setting_hash = {hostname: "127.0.0.1", bucket: "default"})
  puts "---------------------------------------------------"
  if defined? (CouchbaseSetting)

    puts "CouchbaseSetting found..."
    
    if (CouchbaseSetting.respond_to?("servers") && CouchbaseSetting.servers && !CouchbaseSetting.servers.empty?)
      setting_hash[:node_list] = CouchbaseSetting.servers 
    elsif CouchbaseSetting.respond_to?("server")
      setting_hash[:hostname] = CouchbaseSetting.server 
    else
      raise ArgumentError, "You didn't set Couchbase Server(s)/Bucket in your /config/couchbase.yml file!"
    end
    
    setting_hash[:pool] = "default"
    setting_hash[:bucket] = CouchbaseSetting.bucket
    setting_hash[:port] = 8091

    if (CouchbaseSetting.respond_to?("password") && CouchbaseSetting.password && !CouchbaseSetting.password.blank?)
      setting_hash[:username] = CouchbaseSetting.bucket
      setting_hash[:password] = CouchbaseSetting.password
    end
  end

  $cb = Couchbase.connect(setting_hash)
  
  puts "CouchbaseDocStore connected..."
  puts $cb.inspect
  puts "---------------------------------------------------"
end

.connectionObject



75
76
77
# File 'lib/couchbase-docstore.rb', line 75

def connection
  $cb
end

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



138
139
140
141
142
# File 'lib/couchbase-docstore.rb', line 138

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



186
187
188
189
190
# File 'lib/couchbase-docstore.rb', line 186

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



110
111
112
# File 'lib/couchbase-docstore.rb', line 110

def delete_all_documents!
  $cb.flush
end

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



174
175
176
177
178
# File 'lib/couchbase-docstore.rb', line 174

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

.document_exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/couchbase-docstore.rb', line 114

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



193
194
195
196
197
# File 'lib/couchbase-docstore.rb', line 193

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



150
151
152
153
154
155
# File 'lib/couchbase-docstore.rb', line 150

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/couchbase-docstore.rb', line 157

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



180
181
182
183
184
# File 'lib/couchbase-docstore.rb', line 180

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



131
132
133
134
135
136
# File 'lib/couchbase-docstore.rb', line 131

def initialize_document(key, value, args={})
  return nil unless key
  $cb.quiet = true
  doc = CouchbaseDocStore.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



144
145
146
147
148
# File 'lib/couchbase-docstore.rb', line 144

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)



23
24
25
26
# File 'lib/couchbase-docstore.rb', line 23

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

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



56
57
58
59
# File 'lib/couchbase-docstore.rb', line 56

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

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



45
46
47
48
# File 'lib/couchbase-docstore.rb', line 45

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

#document_exists?(key) ⇒ Boolean

Check if a key/document exists

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/couchbase-docstore.rb', line 11

def document_exists?(key)
  return nil unless key
  CouchbaseDocStore.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



62
63
64
65
# File 'lib/couchbase-docstore.rb', line 62

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

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



35
36
37
38
# File 'lib/couchbase-docstore.rb', line 35

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

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



40
41
42
43
# File 'lib/couchbase-docstore.rb', line 40

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

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

Parameters:

  • args (defaults to: {})

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



51
52
53
54
# File 'lib/couchbase-docstore.rb', line 51

def increase_atomic_count(key, args={})
  return nil unless key
  CouchbaseDocStore.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



17
18
19
20
# File 'lib/couchbase-docstore.rb', line 17

def initialize_document(key, value, args={})
  return nil unless key
  CouchbaseDocStore.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



29
30
31
32
# File 'lib/couchbase-docstore.rb', line 29

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