()
| 30 | |
| 31 | @defer.inlineCallbacks |
| 32 | def 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: |
no test coverage detected