def test_server_durable_insert(self): if not self.supports_sync_durability(): raise SkipTest("ServerDurability not supported") durability = ServerDurability(level=Durability.PERSIST_TO_MAJORITY) self.cb.insert(self.NOKEY, self.CONTENT, InsertOptions(durability=durability)) result = self.cb.get(self.NOKEY) self.assertEqual(self.CONTENT, result.content_as[dict])
def test_client_durable_insert(self): num_replicas = self.bucket._bucket.configured_replica_count durability = ClientDurability(persist_to=PersistTo.ONE, replicate_to=ReplicateTo(num_replicas)) self.cb.insert(self.NOKEY, self.CONTENT, InsertOptions(durability=durability)) result = self.cb.get(self.NOKEY) self.assertEqual(self.CONTENT, result.content_as[dict])
def insert_with_opts(): print("\n[kv-insert-with-opts]") # tag::kv-insert-with-opts[] document = { "id": 456, "title": "Ardèche", "name": "La Pradella", "address": "rue du village, 07290 Preaux, France", "phone": "+33 4 75 32 08 52", "url": "http://www.lapradella.fr", "country": "France", "city": "Preaux", "state": "Rhône-Alpes", "vacancy": False, } # Insert the document with an expiry time option of 60 seconds. insert_result = hotel_collection.insert( "hotel-456", document, InsertOptions(expiry=timedelta(seconds=60))) # Print the result's CAS metadata to the console. print("CAS:", insert_result.cas)
async def insert(self, key, doc, **kwargs): opts = InsertOptions(expiry=kwargs.get('expiry', None)) return await self._collection.insert(key, doc, opts)
# tag::CASMismatchException[] try: result = collection.get("hotel_10026") collection.replace("hotel_10026", {}, cas=result.cas) except CASMismatchException: # the CAS value has changed pass # end::CASMismatchException[] # tag::DurabilitySyncWriteAmbiguousException[] for i in range(5): try: durability = ServerDurability(level=Durability.PERSIST_TO_MAJORITY) collection.insert("my-key", {"title": "New Hotel"}, InsertOptions(durability=durability)) except ( DocumentExistsException, DurabilitySyncWriteAmbiguousException, ) as ex: # if previously retried and the document now exists, # we can assume it was written successfully by a previous ambiguous exception if isinstance(ex, DocumentExistsException) and i > 0: continue # simply retry the durable operation again if isinstance(ex, DurabilitySyncWriteAmbiguousException): continue # raise the exception if not DocumentExistsException, DurabilitySyncWriteAmbiguousException raise
try: # tag::get_cas_replace[] # Replace document with CAS result = collection.get("document-key") doc = result.content_as[dict] doc["bar"] = "baz" opts = ReplaceOptions(cas=result.cas) result = collection.replace("document-key", doc, opts) # end::get_cas_replace[] except CASMismatchException as ex: print(ex) # tag::insert_w_opts[] # Insert document with options document = {"foo": "bar", "bar": "foo"} opts = InsertOptions(timeout=timedelta(seconds=5)) result = collection.insert("document-key-opts", document, opts, expiry=timedelta(seconds=30)) # end::insert_w_opts[] try: # tag::durability[] # Upsert with Durability (Couchbase Server >= 6.5) level Majority document = dict(foo="bar", bar="foo") opts = UpsertOptions(durability=ServerDurability(Durability.MAJORITY)) result = collection.upsert("document-key", document, opts) # end::durability[] except CouchbaseException as ex: # we expect an exception on local/test host, as Durability requirement