Class: Couchbase::Collection
- Inherits:
-
Object
- Object
- Couchbase::Collection
- Defined in:
- lib/couchbase/collection.rb,
lib/couchbase/collection_options.rb,
/Users/sergey.auseyau/code/couchbase-ruby-client/lib/couchbase/collection.rb,
/Users/sergey.auseyau/code/couchbase-ruby-client/lib/couchbase/collection_options.rb more...
Overview
Provides access to all collection APIs
Defined Under Namespace
Classes: ExistsResult, GetReplicaResult, GetResult, LookupInResult, MutateInResult, MutationResult
Instance Attribute Summary collapse
-
#bucket_name ⇒ Object
readonly
Returns the value of attribute bucket_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#scope_name ⇒ Object
readonly
Returns the value of attribute scope_name.
Instance Method Summary collapse
-
#binary ⇒ BinaryCollection
Provides access to the binary APIs, not used for JSON documents.
-
#exists(id, options = Options::Exists::DEFAULT) ⇒ ExistsResult
Checks if the given document ID exists on the active partition.
-
#get(id, options = Options::Get::DEFAULT) ⇒ GetResult
Fetches the full document from the collection.
-
#get_all_replicas(id, options = Options::GetAllReplicas::DEFAULT) ⇒ Array<GetReplicaResult>
Reads from all available replicas and the active node and returns the results.
-
#get_and_lock(id, lock_time, options = Options::GetAndLock::DEFAULT) ⇒ GetResult
Fetches the full document and write-locks it for the given duration.
-
#get_and_touch(id, expiry, options = Options::GetAndTouch::DEFAULT) ⇒ GetResult
Fetches a full document and resets its expiration time to the duration provided.
-
#get_any_replica(id, options = Options::GetAnyReplica::DEFAULT) ⇒ GetReplicaResult
Reads all available replicas and active, and returns the first found.
-
#get_multi(ids, options = Options::GetMulti::DEFAULT) ⇒ Array<GetResult>
Fetches multiple documents from the collection.
-
#initialize(backend, bucket_name, scope_name, collection_name) ⇒ Collection
constructor
A new instance of Collection.
-
#insert(id, content, options = Options::Insert::DEFAULT) ⇒ MutationResult
Inserts a full document which does not exist yet.
-
#lookup_in(id, specs, options = Options::LookupIn::DEFAULT) ⇒ LookupInResult
Performs lookups to document fragments.
-
#mutate_in(id, specs, options = Options::MutateIn::DEFAULT) ⇒ MutateInResult
Performs mutations to document fragments.
- #query_indexes ⇒ Management::CollectionQueryIndexManager
-
#remove(id, options = Options::Remove::DEFAULT) ⇒ MutationResult
Removes a document from the collection.
-
#remove_multi(ids, options = Options::RemoveMulti::DEFAULT) ⇒ Array<MutationResult>
Removes a list of the documents from the collection.
-
#replace(id, content, options = Options::Replace::DEFAULT) ⇒ MutationResult
Replaces a full document which already exists.
-
#touch(id, expiry, options = Options::Touch::DEFAULT) ⇒ MutationResult
Update the expiration of the document with the given id.
-
#unlock(id, cas, options = Options::Unlock::DEFAULT) ⇒ void
Unlocks a document if it has been locked previously.
-
#upsert(id, content, options = Options::Upsert::DEFAULT) ⇒ MutationResult
Upserts (inserts or updates) a full document which might or might not exist yet.
-
#upsert_multi(id_content, options = Options::UpsertMulti::DEFAULT) ⇒ Array<MutationResult>
Upserts (inserts or updates) a list of documents which might or might not exist yet.
Constructor Details
#initialize(backend, bucket_name, scope_name, collection_name) ⇒ Collection
Returns a new instance of Collection.
32 33 34 35 36 37 |
# File 'lib/couchbase/collection.rb', line 32 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_name ⇒ Object (readonly)
Returns the value of attribute bucket_name.
22 23 24 |
# File 'lib/couchbase/collection.rb', line 22 def bucket_name @bucket_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/couchbase/collection.rb', line 24 def name @name end |
#scope_name ⇒ Object (readonly)
Returns the value of attribute scope_name.
23 24 25 |
# File 'lib/couchbase/collection.rb', line 23 def scope_name @scope_name end |
Instance Method Details
#binary ⇒ BinaryCollection
Provides access to the binary APIs, not used for JSON documents
42 43 44 |
# File 'lib/couchbase/collection.rb', line 42 def binary BinaryCollection.new(self) end |
#exists(id, options = Options::Exists::DEFAULT) ⇒ ExistsResult
Checks if the given document ID exists on the active partition.
227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/couchbase/collection.rb', line 227 def exists(id, = Options::Exists::DEFAULT) resp = @backend.document_exists(bucket_name, @scope_name, @name, id, .to_backend) ExistsResult.new do |res| res.deleted = resp[:deleted] res.exists = resp[:exists] res.expiry = resp[:expiry] res.flags = resp[:flags] res.sequence_number = resp[:sequence_number] res.datatype = resp[:datatype] res.cas = resp[:cas] end end |
#get(id, options = Options::Get::DEFAULT) ⇒ GetResult
Fetches the full document from the collection
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/couchbase/collection.rb', line 77 def get(id, = Options::Get::DEFAULT) resp = if .need_projected_get? @backend.document_get_projected(bucket_name, @scope_name, @name, id, .to_backend) else @backend.document_get(bucket_name, @scope_name, @name, id, .to_backend) end GetResult.new do |res| res.transcoder = .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 = Options::GetAllReplicas::DEFAULT) ⇒ Array<GetReplicaResult>
Reads from all available replicas and the active node and returns the results
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/couchbase/collection.rb', line 176 def get_all_replicas(id, = Options::GetAllReplicas::DEFAULT) resp = @backend.document_get_all_replicas(@bucket_name, @scope_name, @name, id, .to_backend) resp.map do |entry| GetReplicaResult.new do |res| res.transcoder = .transcoder res.cas = entry[:cas] res.flags = entry[:flags] res.encoded = entry[:content] res.is_replica = entry[:is_replica] end end end |
#get_and_lock(id, lock_time, options = Options::GetAndLock::DEFAULT) ⇒ GetResult
Fetches the full document and write-locks it for the given duration
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/couchbase/collection.rb', line 136 def get_and_lock(id, lock_time, = Options::GetAndLock::DEFAULT) resp = @backend.document_get_and_lock(bucket_name, @scope_name, @name, id, lock_time.respond_to?(:in_seconds) ? lock_time.public_send(:in_seconds) : lock_time, .to_backend) GetResult.new do |res| res.transcoder = .transcoder res.cas = resp[:cas] res.flags = resp[:flags] res.encoded = resp[:content] end end |
#get_and_touch(id, expiry, options = Options::GetAndTouch::DEFAULT) ⇒ GetResult
Fetches a full document and resets its expiration time to the duration provided
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/couchbase/collection.rb', line 158 def get_and_touch(id, expiry, = Options::GetAndTouch::DEFAULT) resp = @backend.document_get_and_touch(bucket_name, @scope_name, @name, id, Utils::Time.extract_expiry_time(expiry), .to_backend) GetResult.new do |res| res.transcoder = .transcoder res.cas = resp[:cas] res.flags = resp[:flags] res.encoded = resp[:content] end end |
#get_any_replica(id, options = Options::GetAnyReplica::DEFAULT) ⇒ GetReplicaResult
Reads all available replicas and active, and returns the first found.
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/couchbase/collection.rb', line 206 def get_any_replica(id, = Options::GetAnyReplica::DEFAULT) resp = @backend.document_get_any_replica(@bucket_name, @scope_name, @name, id, .to_backend) GetReplicaResult.new do |res| res.transcoder = .transcoder res.cas = resp[:cas] res.flags = resp[:flags] res.encoded = resp[:content] res.is_replica = resp[:is_replica] end end |
#get_multi(ids, options = Options::GetMulti::DEFAULT) ⇒ Array<GetResult>
that it will not generate Error::DocumentNotFound exceptions in this case. The caller should check Couchbase::Collection::GetResult#error property of the result
Fetches multiple documents from the collection.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/couchbase/collection.rb', line 106 def get_multi(ids, = Options::GetMulti::DEFAULT) resp = @backend.document_get_multi(ids.map { |id| [bucket_name, @scope_name, @name, id] }, .to_backend) resp.map do |entry| GetResult.new do |res| res.transcoder = .transcoder res.id = entry[:id] res.cas = entry[:cas] res.flags = entry[:flags] res.encoded = entry[:content] res.error = entry[:error] end end end |
#insert(id, content, options = Options::Insert::DEFAULT) ⇒ MutationResult
Inserts a full document which does not exist yet
326 327 328 329 330 331 332 333 |
# File 'lib/couchbase/collection.rb', line 326 def insert(id, content, = Options::Insert::DEFAULT) blob, flags = .transcoder ? .transcoder.encode(content) : [content, 0] resp = @backend.document_insert(bucket_name, @scope_name, @name, id, blob, flags, .to_backend) MutationResult.new do |res| res.cas = resp[:cas] res.mutation_token = extract_mutation_token(resp) end end |
#lookup_in(id, specs, options = Options::LookupIn::DEFAULT) ⇒ LookupInResult
Performs lookups to document fragments
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/couchbase/collection.rb', line 463 def lookup_in(id, specs, = Options::LookupIn::DEFAULT) resp = @backend.document_lookup_in( bucket_name, @scope_name, @name, id, specs.map do |s| { opcode: s.type, xattr: s.xattr?, path: s.path, } end, .to_backend ) LookupInResult.new do |res| res.transcoder = .transcoder res.cas = resp[:cas] res.deleted = resp[:deleted] res.encoded = resp[:fields].map do |field| SubDocumentField.new do |f| f.exists = field[:exists] f.index = field[:index] f.path = field[:path] f.value = field[:value] end end end end |
#mutate_in(id, specs, options = Options::MutateIn::DEFAULT) ⇒ MutateInResult
Performs mutations to document fragments
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/couchbase/collection.rb', line 509 def mutate_in(id, specs, = Options::MutateIn::DEFAULT) resp = @backend.document_mutate_in( bucket_name, @scope_name, @name, id, specs.map do |s| { opcode: s.type, path: s.path, param: s.param, xattr: s.xattr?, expand_macros: s., create_path: s.create_path?, } end, .to_backend ) MutateInResult.new do |res| res.transcoder = .transcoder res.cas = resp[:cas] res.deleted = resp[:deleted] res.mutation_token = extract_mutation_token(resp) res.encoded = resp[:fields].map do |field| SubDocumentField.new do |f| f.index = field[:index] f.path = field[:path] f.value = field[:value] end end end end |
#query_indexes ⇒ Management::CollectionQueryIndexManager
47 48 49 |
# File 'lib/couchbase/collection.rb', line 47 def query_indexes Management::CollectionQueryIndexManager.new(@backend, @bucket_name, @scope_name, @name) end |
#remove(id, options = Options::Remove::DEFAULT) ⇒ MutationResult
Removes a document from the collection
260 261 262 263 264 265 266 |
# File 'lib/couchbase/collection.rb', line 260 def remove(id, = Options::Remove::DEFAULT) resp = @backend.document_remove(bucket_name, @scope_name, @name, id, .to_backend) MutationResult.new do |res| res.cas = resp[:cas] res.mutation_token = extract_mutation_token(resp) end end |
#remove_multi(ids, options = Options::RemoveMulti::DEFAULT) ⇒ Array<MutationResult>
that it will not generate Error::DocumentNotFound or Error::CasMismatch exceptions in this case. The caller should check Couchbase::Collection::MutationResult#error property of the result
Removes a list of the documents from the collection
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/couchbase/collection.rb', line 286 def remove_multi(ids, = Options::RemoveMulti::DEFAULT) resp = @backend.document_remove_multi(bucket_name, @scope_name, @name, ids.map do |id| case id when String [id, nil] when Array id else raise ArgumentError, "id argument of remove_multi must be a String or Array<String, Integer>, given: #{id.inspect}" end end, .to_backend) resp.map do |entry| MutationResult.new do |res| res.cas = entry[:cas] res.mutation_token = extract_mutation_token(entry) res.error = entry[:error] res.id = entry[:id] end end end |
#replace(id, content, options = Options::Replace::DEFAULT) ⇒ MutationResult
Replaces a full document which already exists
400 401 402 403 404 405 406 407 |
# File 'lib/couchbase/collection.rb', line 400 def replace(id, content, = Options::Replace::DEFAULT) blob, flags = .transcoder ? .transcoder.encode(content) : [content, 0] resp = @backend.document_replace(bucket_name, @scope_name, @name, id, blob, flags, .to_backend) MutationResult.new do |res| res.cas = resp[:cas] res.mutation_token = extract_mutation_token(resp) end end |
#touch(id, expiry, options = Options::Touch::DEFAULT) ⇒ MutationResult
Update the expiration of the document with the given id
419 420 421 422 423 424 425 426 |
# File 'lib/couchbase/collection.rb', line 419 def touch(id, expiry, = Options::Touch::DEFAULT) resp = @backend.document_touch(bucket_name, @scope_name, @name, id, Utils::Time.extract_expiry_time(expiry), .to_backend) MutationResult.new do |res| res.cas = resp[:cas] end end |
#unlock(id, cas, options = Options::Unlock::DEFAULT) ⇒ void
This method returns an undefined value.
Unlocks a document if it has been locked previously
441 442 443 |
# File 'lib/couchbase/collection.rb', line 441 def unlock(id, cas, = Options::Unlock::DEFAULT) @backend.document_unlock(bucket_name, @scope_name, @name, id, cas, .to_backend) end |
#upsert(id, content, options = Options::Upsert::DEFAULT) ⇒ MutationResult
Upserts (inserts or updates) a full document which might or might not exist yet
346 347 348 349 350 351 352 353 |
# File 'lib/couchbase/collection.rb', line 346 def upsert(id, content, = Options::Upsert::DEFAULT) blob, flags = .transcoder ? .transcoder.encode(content) : [content, 0] resp = @backend.document_upsert(bucket_name, @scope_name, @name, id, blob, flags, .to_backend) MutationResult.new do |res| res.cas = resp[:cas] res.mutation_token = extract_mutation_token(resp) end end |
#upsert_multi(id_content, options = Options::UpsertMulti::DEFAULT) ⇒ Array<MutationResult>
that it will not generate exceptions in this case. The caller should check Couchbase::Collection::MutationResult#error property of the result
Upserts (inserts or updates) a list of documents which might or might not exist yet
373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/couchbase/collection.rb', line 373 def upsert_multi(id_content, = Options::UpsertMulti::DEFAULT) resp = @backend.document_upsert_multi(bucket_name, @scope_name, @name, id_content.map do |(id, content)| blob, flags = .transcoder ? .transcoder.encode(content) : [content, 0] [id, blob, flags] end, .to_backend) resp.map do |entry| MutationResult.new do |res| res.cas = entry[:cas] res.mutation_token = extract_mutation_token(entry) res.error = entry[:error] res.id = entry[:id] end end end |