Class: Couchbase::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/collection.rb,
lib/couchbase/collection_options.rb

Defined Under Namespace

Classes: ExistsOptions, ExistsResult, GetAllReplicasOptions, GetAndLockOptions, GetAndTouchOptions, GetAnyReplicaOptions, GetOptions, GetReplicaResult, GetResult, InsertOptions, LookupInOptions, LookupInResult, MutateInOptions, MutateInResult, MutationResult, RemoveOptions, ReplaceOptions, TouchOptions, UnlockOptions, UpsertOptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, bucket_name, scope_name, collection_name) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • backend (Couchbase::Backend)
  • bucket_name (String)

    name of the bucket

  • scope_name (String, :_default)

    name of the scope

  • collection_name (String, :_default)

    name of the collection



31
32
33
34
35
36
# File 'lib/couchbase/collection.rb', line 31

def initialize(backend, bucket_name, scope_name, collection_name)
  @backend = backend
  @bucket_name = bucket_name
  @scope_name = scope_name
  @name = collection_name
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



21
22
23
# File 'lib/couchbase/collection.rb', line 21

def bucket_name
  @bucket_name
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/couchbase/collection.rb', line 23

def name
  @name
end

#scope_nameObject (readonly)

Returns the value of attribute scope_name.



22
23
24
# File 'lib/couchbase/collection.rb', line 22

def scope_name
  @scope_name
end

Instance Method Details

#binaryBinaryCollection

Provides access to the binary APIs, not used for JSON documents

Returns:



41
42
43
# File 'lib/couchbase/collection.rb', line 41

def binary
  BinaryCollection.new(self)
end

#exists(id, options = ExistsOptions.new) ⇒ ExistsResult

Checks if the given document ID exists on the active partition.

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • options (ExistsOptions) (defaults to: ExistsOptions.new)

    request customization

Returns:



126
127
128
129
130
131
132
133
# File 'lib/couchbase/collection.rb', line 126

def exists(id, options = ExistsOptions.new)
  resp = @backend.document_exists(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout)
  ExistsResult.new do |res|
    res.status = resp[:status]
    res.partition_id = resp[:partition_id]
    res.cas = resp[:cas] if res.status != :not_found
  end
end

#get(id, options = GetOptions.new) ⇒ GetResult

Fetches the full document from the collection

Parameters:

  • id (String)

    the document id which is used to uniquely identify it

  • options (GetOptions) (defaults to: GetOptions.new)

    request customization

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/couchbase/collection.rb', line 51

def get(id, options = GetOptions.new)
  resp = if options.need_projected_get?
           @backend.document_get_projected(bucket_name, "#{@scope_name}.#{@name}", id,
                                           options.timeout,
                                           options.with_expiry,
                                           options.projections,
                                           options.preserve_array_indexes)
         else
           @backend.document_get(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout)
         end
  GetResult.new do |res|
    res.transcoder = options.transcoder
    res.cas = resp[:cas]
    res.flags = resp[:flags]
    res.encoded = resp[:content]
    res.expiry = resp[:expiry] if resp.key?(:expiry)
  end
end

#get_all_replicas(id, options = GetAllReplicasOptions.new) ⇒ Array<GetReplicaResult>

Reads from all available replicas and the active node and returns the results

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • options (GetAllReplicasOptions) (defaults to: GetAllReplicasOptions.new)

    request customization

Returns:



110
# File 'lib/couchbase/collection.rb', line 110

def get_all_replicas(id, options = GetAllReplicasOptions.new) end

#get_and_lock(id, lock_time, options = GetAndLockOptions.new) ⇒ GetResult

Fetches the full document and write-locks it for the given duration

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • lock_time (Integer)

    how long to lock the document (values over 30 seconds will be capped)

  • options (GetAndLockOptions) (defaults to: GetAndLockOptions.new)

    request customization

Returns:



77
78
79
80
81
82
83
84
85
# File 'lib/couchbase/collection.rb', line 77

def get_and_lock(id, lock_time, options = GetAndLockOptions.new)
  resp = @backend.document_get_and_lock(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, lock_time)
  GetResult.new do |res|
    res.transcoder = options.transcoder
    res.cas = resp[:cas]
    res.flags = resp[:flags]
    res.encoded = resp[:content]
  end
end

#get_and_touch(id, expiry, options = GetAndTouchOptions.new) ⇒ GetResult

Fetches a full document and resets its expiration time to the duration provided

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • expiry (Integer)

    the new expiration time for the document

  • options (GetAndTouchOptions) (defaults to: GetAndTouchOptions.new)

    request customization

Returns:



94
95
96
97
98
99
100
101
102
# File 'lib/couchbase/collection.rb', line 94

def get_and_touch(id, expiry, options = GetAndTouchOptions.new)
  resp = @backend.document_get_and_touch(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, expiry)
  GetResult.new do |res|
    res.transcoder = options.transcoder
    res.cas = resp[:cas]
    res.flags = resp[:flags]
    res.encoded = resp[:content]
  end
end

#get_any_replica(id, options = GetAnyReplicaOptions.new) ⇒ GetReplicaResult

Reads all available replicas, and returns the first found

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • options (GetAnyReplicaOptions) (defaults to: GetAnyReplicaOptions.new)

    request customization

Returns:



118
# File 'lib/couchbase/collection.rb', line 118

def get_any_replica(id, options = GetAnyReplicaOptions.new) end

#insert(id, content, options = InsertOptions.new) ⇒ MutationResult

Inserts a full document which does not exist yet

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • content (Object)

    the document content to insert

  • options (InsertOptions) (defaults to: InsertOptions.new)

    request customization

Returns:



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/couchbase/collection.rb', line 158

def insert(id, content, options = InsertOptions.new)
  blob, flags = options.transcoder.encode(content)
  resp = @backend.document_insert(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, blob, flags, {
    durability_level: options.durability_level,
    expiry: options.expiry,
  })
  MutationResult.new do |res|
    res.cas = resp[:cas]
    res.mutation_token = extract_mutation_token(resp)
  end
end

#lookup_in(id, specs, options = LookupInOptions.new) ⇒ LookupInResult

Performs lookups to document fragments

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • specs (Array<LookupInSpec>)

    the list of specifications which describe the types of the lookups to perform

  • options (LookupInOptions) (defaults to: LookupInOptions.new)

    request customization

Returns:



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/couchbase/collection.rb', line 241

def lookup_in(id, specs, options = LookupInOptions.new)
  resp = @backend.document_lookup_in(
    bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, options.access_deleted,
    specs.map do |s|
      {
        opcode: s.type,
        xattr: s.xattr?,
        path: s.path,
      }
    end
  )
  LookupInResult.new do |res|
    res.transcoder = options.transcoder
    res.cas = resp[:cas]
    res.encoded = resp[:fields].map do |field|
      SubDocumentField.new do |f|
        f.exists = field[:exists]
        f.index = field[:index]
        f.path = field[:path]
        f.status = field[:status]
        f.error = field[:error]
        f.value = field[:value]
        f.type = field[:type]
      end
    end
  end
end

#mutate_in(id, specs, options = MutateInOptions.new) ⇒ MutateInResult

Performs mutations to document fragments

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • specs (Array<MutateInSpec>)

    the list of specifications which describe the types of the lookups to perform

  • options (MutateInOptions) (defaults to: MutateInOptions.new)

    request customization

Returns:



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/couchbase/collection.rb', line 276

def mutate_in(id, specs, options = MutateInOptions.new)
  resp = @backend.document_mutate_in(
    bucket_name, "#{@scope_name}.#{@name}", id, options.timeout,
    specs.map do |s|
      {
        opcode: s.type,
        path: s.path,
        param: s.param,
        xattr: s.xattr?,
        expand_macros: s.expand_macros?,
        create_path: s.create_path?,
      }
    end,
    {
      durability_level: options.durability_level,
      store_semantics: options.store_semantics,
      access_deleted: options.access_deleted,
      cas: options.cas,
      expiry: options.expiry,
    }
  )
  result = MutateInResult.new do |res|
    res.transcoder = options.transcoder
    res.cas = resp[:cas]
    res.mutation_token = extract_mutation_token(resp)
    res.first_error_index = resp[:first_error_index]
    res.encoded = resp[:fields].map do |field|
      SubDocumentField.new do |f|
        f.exists = field[:exists]
        f.path = field[:path]
        f.status = field[:status]
        f.error = field[:error]
        f.value = field[:value]
        f.type = field[:type]
      end
    end
  end
  raise result.first_error unless result.success?

  result
end

#remove(id, options = RemoveOptions.new) ⇒ MutationResult

Removes a document from the collection

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • options (RemoveOptions) (defaults to: RemoveOptions.new)

    request customization

Returns:



141
142
143
144
145
146
147
148
149
# File 'lib/couchbase/collection.rb', line 141

def remove(id, options = RemoveOptions.new)
  resp = @backend.document_remove(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, {
    durability_level: options.durability_level,
  })
  MutationResult.new do |res|
    res.cas = resp[:cas]
    res.mutation_token = extract_mutation_token(resp)
  end
end

#replace(id, content, options = ReplaceOptions.new) ⇒ MutationResult

Replaces a full document which already exists

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • content (Object)

    the document content to upsert

  • options (ReplaceOptions) (defaults to: ReplaceOptions.new)

    request customization

Returns:



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/couchbase/collection.rb', line 196

def replace(id, content, options = ReplaceOptions.new)
  blob, flags = options.transcoder.encode(content)
  resp = @backend.document_replace(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, blob, flags, {
    durability_level: options.durability_level,
    expiry: options.expiry,
    cas: options.cas,
  })
  MutationResult.new do |res|
    res.cas = resp[:cas]
    res.mutation_token = extract_mutation_token(resp)
  end
end

#touch(id, expiry, options = TouchOptions.new) ⇒ MutationResult

Update the expiration of the document with the given id

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • expiry (Integer)

    new expiration time for the document

  • options (TouchOptions) (defaults to: TouchOptions.new)

    request customization

Returns:



216
217
218
219
220
221
# File 'lib/couchbase/collection.rb', line 216

def touch(id, expiry, options = TouchOptions.new)
  resp = @backend.document_touch(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, expiry)
  MutationResult.new do |res|
    res.cas = resp[:cas]
  end
end

#unlock(id, cas, options = UnlockOptions.new) ⇒ Object

Unlocks a document if it has been locked previously

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • cas (Integer)

    CAS value which is needed to unlock the document

  • options (UnlockOptions) (defaults to: UnlockOptions.new)

    request customization

Raises:



230
231
232
# File 'lib/couchbase/collection.rb', line 230

def unlock(id, cas, options = UnlockOptions.new)
  @backend.document_unlock(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, cas)
end

#upsert(id, content, options = UpsertOptions.new) ⇒ MutationResult

Upserts (inserts or updates) a full document which might or might not exist yet

Parameters:

  • id (String)

    the document id which is used to uniquely identify it.

  • content (Object)

    the document content to upsert

  • options (UpsertOptions) (defaults to: UpsertOptions.new)

    request customization

Returns:



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/couchbase/collection.rb', line 177

def upsert(id, content, options = UpsertOptions.new)
  blob, flags = options.transcoder.encode(content)
  resp = @backend.document_upsert(bucket_name, "#{@scope_name}.#{@name}", id, options.timeout, blob, flags, {
    durability_level: options.durability_level,
    expiry: options.expiry,
  })
  MutationResult.new do |res|
    res.cas = resp[:cas]
    res.mutation_token = extract_mutation_token(resp)
  end
end