def test_mutatein(self): count = 0 for durability in Durability: somecontents = {'some': {'path': 'keith'}} key="somekey_{}".format(count) try: self.coll.remove(key) except: pass self.coll.insert(key, somecontents) inserted_value = "inserted_{}".format(count) replacement_value = "replacement_{}".format(count) count += 1 try: self.coll.mutate_in(key, ( SD.replace('some.path', replacement_value), SD.insert('some.other.path', inserted_value, create_parents=True), ), durability_level=durability) somecontents['some']['path'] = replacement_value somecontents['some'].update({'other': {'path': inserted_value}}) self.assertEqual(somecontents, self.coll.get(key).content) except NotSupportedError as e: if not self.is_mock: raise else: logging.error("Assuming failure is due to mock not supporting durability")
def test_mutate_in_simple(self): cas = self.coll.mutate_in(self.KEY, ( SD.upsert("c", "ccc"), SD.replace("b", "XXX"), )).cas self.try_n_times(10, 3, self._cas_matches, self.KEY, cas) result = self.coll.get(self.KEY).content_as[dict] self.assertDictEqual({"a": "aaa", "b": "XXX", "c": "ccc"}, result)
async def sub_doc_operations(collection): key = "hotel_10025" res = await collection.lookup_in(key, [SD.get("reviews[0].ratings")]) print("Review ratings: {}".format(res.content_as[dict](0))) res = await collection.mutate_in( key, [SD.replace("reviews[0].ratings.Rooms", 3.5)]) print("CAS: {}".format(res.cas))
def test_mutate_in_durability(self): if self.is_mock: raise SkipTest( "mock doesn't support getting xattrs (like $document.expiry)") self.assertRaises( DurabilityImpossibleException, self.coll.mutate_in, self.KEY, ( SD.upsert("c", "ccc"), SD.replace("b", "XXX"), ), MutateInOptions(durability=ClientDurability(replicate_to=5)))
def replace(self, client, key='', path='', value=None): try: if self.verbose_func_usage: self.log.info(" replace ----> {0} :: {1}".format(path, value)) if self.use_sdk_client: client.mutate_in(key, SD.replace(path, value, xattr=self.xattr)) else: client.replace_sd(key, path, value) except Exception: raise
def replace(self, client, key = '', path = '', value = None): try: if self.verbose_func_usage: self.log.info(" replace ----> {0} :: {1}".format(path, value)) if self.use_sdk_client: client.mutate_in(key, SD.replace(path, value, xattr=self.xattr)) else: client.replace_sd(key, path, value) except Exception: raise
def offeringPOST(quarter, courseNum, sectionId): # TODO: make redis call if not (quarter and courseNum and sectionId): return make_response( "Missing a parameter. Need quarter, courseNum, sectionId", 400) offering_bucket.mutate_in( quarter, subdoc.replace(courseNum + "." + sectionId, request.get_json())) return make_response( "Updated offering {}/{}-{}".format(quarter, courseNum, sectionId), 200)
def dict_replace(self, client, key = '', path = '', value = None, xattr=None): try: new_path = self.generate_path(self.nesting_level, path) if self.is_sdk_client: # xattr not supported? client.mutate_in(key, SD.replace(path, value, xattr=xattr)) else: client.replace_sd(key, new_path, json.dumps(value)) except Exception as e: self.log.error(e) self.fail("Unable to replace key {0} for path {1} after {2} tries".format(key, path, 1))
def dict_replace(self, client, key = '', path = '', value = None, xattr=None): try: new_path = self.generate_path(self.nesting_level, path) if self.is_sdk_client: # xattr not supported? client.mutate_in(key, SD.replace(path, value, xattr=xattr)) else: client.replace_sd(key, new_path, json.dumps(value)) except Exception as e: self.log.error(e) self.fail("Unable to replace key {0} for path {1} after {2} tries".format(key, path, 1))
def test_mutate_in_expiry(self): if self.is_mock: raise SkipTest("mock doesn't support getting xattrs (like $document.expiry)") cas = self.coll.mutate_in(self.KEY, (SD.upsert("c", "ccc"), SD.replace("b", "XXX"),), MutateInOptions(expiry=timedelta(seconds=1000))).cas self.try_n_times(10, 3, self._cas_matches, self.KEY, cas) result = self.coll.get(self.KEY, GetOptions(with_expiry=True)) expires_in = (result.expiry - datetime.now()).total_seconds() self.assertTrue(0 < expires_in < 1001)
def edit(ids): form = PostForm() da = cb.get(ids).value form.post.data = da['post'] if "currentuser" in session: if form.validate_on_submit(): poo = request.form['post'] if poo != "": res = cb.mutate_in(ids, [SD.replace("post", poo)]) return redirect(url_for('myposts')) else: return redirect("login") return render_template('edit.html', title='Sign In', form=form)
def remove_item(bucket_name, doc_id, item): while True: cb_bucket = cluster.open_bucket(bucket_name) document = cb_bucket.get(doc_id) cur_cas = document.cas ifEncoded = cb_bucket.lookup_in(doc_id, SD.get('isBase64')) ifZiped = cb_bucket.lookup_in(doc_id, SD.get('isZip')) if ifZiped[0] == False and ifEncoded[0] == False: #read body body = cb_bucket.lookup_in(doc_id, SD.get('body'))[0] json_obj = json.loads(body) #print(json.dumps(json_obj, indent=2)) iterate(json_obj, item) try: cb_bucket.mutate_in(doc_id, SD.replace('body', json.dumps(json_obj)), cas=cur_cas) break except KeyExistsError: continue else: bytes_body = decompress( base64.b64decode( cb_bucket.lookup_in(doc_id, SD.get('body'))[0])) json_obj = json.loads(bytes_body) print(json.dumps(json_obj, indent=2)) iterate(json_obj, item) json_obj = base64.b64encode(compress( json.dumps(json_obj))).decode('utf-8') try: cb_bucket.mutate_in(doc_id, SD.replace('body', json.dumps(json_obj)[1:-1]), cas=cur_cas) break except KeyExistsError: continue
def test_mutatein(self, # type: Scenarios dur_name): durability=Durability[dur_name] dur_option = DurabilityOptionBlock(durability=ServerDurability(level=durability)) count = 0 replica_count = self.bucket._bucket.configured_replica_count if dur_name != Durability.NONE and (replica_count == 0 or self.is_mock): raise SkipTest("cluster will not support {}".format(dur_name)) if not self.supports_sync_durability(): dur_option = self.sdk3_to_sdk2_durability(dur_name, replica_count) somecontents = {'some': {'path': 'keith'}} key="{}_{}".format("somekey_{}", count) try: self.coll.remove(key) except: pass self.coll.insert(key, somecontents) inserted_value = "inserted_{}".format(count) replacement_value = "replacement_{}".format(count) count += 1 try: self.coll.mutate_in(key, ( SD.replace('some.path', replacement_value), SD.insert('some.other.path', inserted_value, create_parents=True), ), dur_option) somecontents['some']['path'] = replacement_value somecontents['some'].update({'other': {'path': inserted_value}}) self.assertEqual(somecontents, self.coll.get(key).content) except NotSupportedException as e: if not self.is_mock: raise else: logging.error("Assuming failure is due to mock not supporting durability") except couchbase.exceptions.TimeoutException as e: self.assertIn("Operational",e.message) raise SkipTest("Raised {}, skipped pending further verification".format(e.message))
try: collection.mutate_in( "customer123", [ SD.insert( "purchases.complete", [42, True, "None"])]) except PathExistsException: print("Path exists, cannot use insert.") # end::mutate_in_insert[] # tag::combine_dict[] collection.mutate_in( "customer123", (SD.remove("addresses.billing"), SD.replace( "email", "*****@*****.**"))) # end::combine_dict[] # NOTE: the mutate_in() operation expects a tuple or list # tag::array_append[] collection.mutate_in( "customer123", (SD.array_append( "purchases.complete", 777),)) # purchases.complete is now [339, 976, 442, 666, 777] # end::array_append[] # tag::array_prepend[] collection.mutate_in( "customer123", [
def test_mutate_in(self): cb = self.cb key = self.gen_key('sdstore_upsert') cb.upsert(key, {}) cb.mutate_in(key, SD.upsert('newDict', ['hello'])) result = cb.retrieve_in(key, 'newDict') self.assertEqual(['hello'], result[0]) # Create deep path without create_parents self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key, SD.upsert('path.with.missing.parents', 'value')) # Create deep path using create_parents cb.mutate_in(key, SD.upsert('new.parent.path', 'value', create_parents=True)) result = cb.retrieve_in(key, 'new.parent') self.assertEqual('value', result[0]['path']) # Test CAS operations self.assertTrue(result.cas) self.assertRaises(E.KeyExistsError, cb.mutate_in, key, SD.upsert('newDict', None), cas=result.cas+1) # Try it again, using the CAS result2 = cb.mutate_in(key, SD.upsert('newDict', {}), cas=result.cas) self.assertNotEqual(result.cas, result2.cas) # Test insert, should fail self.assertRaises(E.SubdocPathExistsError, cb.mutate_in, key, SD.insert('newDict', {})) # Test insert on new path, should succeed cb.mutate_in(key, SD.insert('anotherDict', {})) self.assertEqual({}, cb.retrieve_in(key, 'anotherDict')[0]) # Test replace, should not fail cb.mutate_in(key, SD.replace('newDict', {'Hello': 'World'})) self.assertEqual('World', cb.retrieve_in(key, 'newDict')[0]['Hello']) # Test replace with missing value, should fail self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key, SD.replace('nonexist', {})) # Test with empty string (should be OK) cb.mutate_in(key, SD.upsert('empty', '')) self.assertEqual('', cb.retrieve_in(key, 'empty')[0]) # Test with null (None). Should be OK cb.mutate_in(key, SD.upsert('null', None)) self.assertEqual(None, cb.retrieve_in(key, 'null')[0]) # Test with empty path. Should throw some kind of error? self.assertRaises( (E.SubdocCantInsertValueError, E.SubdocEmptyPathError), cb.mutate_in, key, SD.upsert('', {})) cb.mutate_in(key, SD.upsert('array', [1, 2, 3])) self.assertRaises(E.SubdocPathMismatchError, cb.mutate_in, key, SD.upsert('array.newKey', 'newVal')) self.assertRaises(E.SubdocPathInvalidError, cb.mutate_in, key, SD.upsert('array[0]', 'newVal')) self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key, SD.upsert('array[3].bleh', 'newVal'))
def delete(ids): res = cb.mutate_in(ids, [SD.replace("post", None)]) return redirect(url_for('myposts'))
def test_mutate_in(self): cb = self.cb key = self.gen_key('sdstore_upsert') cb.upsert(key, {}) cb.mutate_in(key, SD.upsert('newDict', ['hello'])) result = cb.retrieve_in(key, 'newDict') self.assertEqual(['hello'], result[0]) # Create deep path without create_parents self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key, SD.upsert('path.with.missing.parents', 'value')) # Create deep path using create_parents cb.mutate_in( key, SD.upsert('new.parent.path', 'value', create_parents=True)) result = cb.retrieve_in(key, 'new.parent') self.assertEqual('value', result[0]['path']) # Test CAS operations self.assertTrue(result.cas) self.assertRaises(E.KeyExistsError, cb.mutate_in, key, SD.upsert('newDict', None), cas=result.cas + 1) # Try it again, using the CAS result2 = cb.mutate_in(key, SD.upsert('newDict', {}), cas=result.cas) self.assertNotEqual(result.cas, result2.cas) # Test insert, should fail self.assertRaises(E.SubdocPathExistsError, cb.mutate_in, key, SD.insert('newDict', {})) # Test insert on new path, should succeed cb.mutate_in(key, SD.insert('anotherDict', {})) self.assertEqual({}, cb.retrieve_in(key, 'anotherDict')[0]) # Test replace, should not fail cb.mutate_in(key, SD.replace('newDict', {'Hello': 'World'})) self.assertEqual('World', cb.retrieve_in(key, 'newDict')[0]['Hello']) # Test replace with missing value, should fail self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key, SD.replace('nonexist', {})) # Test with empty string (should be OK) cb.mutate_in(key, SD.upsert('empty', '')) self.assertEqual('', cb.retrieve_in(key, 'empty')[0]) # Test with null (None). Should be OK cb.mutate_in(key, SD.upsert('null', None)) self.assertEqual(None, cb.retrieve_in(key, 'null')[0]) # Test with empty path. Should throw some kind of error? self.assertRaises( (E.SubdocCantInsertValueError, E.SubdocEmptyPathError), cb.mutate_in, key, SD.upsert('', {})) cb.mutate_in(key, SD.upsert('array', [1, 2, 3])) self.assertRaises(E.SubdocPathMismatchError, cb.mutate_in, key, SD.upsert('array.newKey', 'newVal')) self.assertRaises(E.SubdocPathInvalidError, cb.mutate_in, key, SD.upsert('array[0]', 'newVal')) self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key, SD.upsert('array[3].bleh', 'newVal'))
def subdocument_upsert(bucket, document_id, path, subdocument): rv = bucket.mutate_in( document_id, SD.replace(path, subdocument) ) # don't convert the dict to json. the method will automatically return rv[0]