MCPcopy Create free account
hub / github.com/couchbase/couchbase-python-client / main

Function main

examples/txcouchbase/txcouchbase_kv_operations.py:32–165  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30
31@defer.inlineCallbacks
32def main():
33 # create a cluster object
34 cluster = Cluster('couchbase://localhost',
35 authenticator=PasswordAuthenticator('Administrator', 'password'))
36
37 # @TODO: if connection fails, this will hang
38 yield cluster.on_connect()
39 # create a bucket object
40 bucket = cluster.bucket('default')
41 yield bucket.on_connect()
42
43 collection = bucket.default_collection()
44
45 try:
46 yield collection.remove("document-key")
47 except CouchbaseException as ex:
48 pass # may not exist in this example
49
50 try:
51 yield collection.remove("document-key-opts")
52 except CouchbaseException as ex:
53 pass # may not exist in this example
54
55 # Insert document
56 document = {"foo": "bar", "bar": "foo"}
57 result = yield collection.insert("document-key", document)
58 print("Result: {}; CAS: {}".format(result, result.cas))
59
60 # Insert document with options
61 document = {"foo": "bar", "bar": "foo"}
62 opts = InsertOptions(timeout=timedelta(seconds=5))
63 result = yield collection.insert("document-key-opts",
64 document,
65 opts,
66 expiry=timedelta(seconds=30))
67
68 try:
69 # Replace document with CAS
70 document = {"foo": "bar", "bar": "foo"}
71 result = yield collection.replace(
72 "document-key",
73 document,
74 cas=12345,
75 timeout=timedelta(
76 minutes=1))
77 except CASMismatchException as ex:
78 # we expect an exception here as the CAS value is chosen
79 # for example purposes
80 print('Caught CAS mismatch: {}'.format(ex))
81
82 try:
83 # Replace document with CAS
84 result = yield collection.get("document-key")
85 doc = result.content_as[dict]
86 doc["bar"] = "baz"
87 opts = ReplaceOptions(cas=result.cas)
88 result = yield collection.replace("document-key", doc, opts)
89 except CouchbaseException as ex:

Callers 1

Calls 15

on_connectMethod · 0.95
bucketMethod · 0.95
ClusterClass · 0.90
InsertOptionsClass · 0.90
ReplaceOptionsClass · 0.90
UpsertOptionsClass · 0.90
ServerDurabilityClass · 0.90
GetOptionsClass · 0.90
RemoveOptionsClass · 0.90
IncrementOptionsClass · 0.90
DeltaValueClass · 0.90

Tested by

no test coverage detected