def counter(self, client, key='', path='', value=None): try: if self.verbose_func_usage: self.log.info(" counter ----> {0} :: {1} + {2}".format(key, path, value)) if self.use_sdk_client: client.cb.mutate_in(key, SD.counter(path, int(value), xattr=self.xattr)) else: client.counter_sd(key, path, value) except Exception: raise
def counter(self, client, key = '', path = '', value = None): try: if self.verbose_func_usage: self.log.info(" counter ----> {0} :: {1} + {2}".format(key, path, value)) if self.use_sdk_client: client.cb.mutate_in(key, SD.counter(path, int(value), xattr=self.xattr)) else: client.counter_sd(key, path, value) except Exception: raise
def counter(self, client, key = '', path = '', value = None, xattr=None, create_parents=None): try: new_path = self.generate_path(self.nesting_level, path) self.log.info(new_path) if self.is_sdk_client: client.cb.mutate_in(key, SD.counter(new_path, int(value), xattr=xattr, create_parents=create_parents)) else: client.counter_sd(key, new_path, value) except Exception as e: self.log.error(e) msg = "Unable to add key {0} for path {1} after {2} tries".format(key, path, 1) return msg
def test_counter_in(self): cb = self.cb key = self.gen_key('sdcounter') cb.upsert(key, {}) rv = cb.mutate_in(key, SD.counter('counter', 100)) self.assertTrue(rv.success) self.assertFalse(rv.cas == 0) self.assertEqual(100, rv[0]) self.assertRaises(E.SubdocBadDeltaError, cb.mutate_in, key, SD.counter('not_a_counter', 'blah')) # Do an upsert cb.mutate_in(key, SD.upsert('not_a_counter', 'blah')) self.assertRaises(E.SubdocPathMismatchError, cb.mutate_in, key, SD.counter('not_a_counter', 25)) self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key, SD.counter('path.to.newcounter', 99)) rv = cb.mutate_in(key, SD.counter('path.to.newcounter', 99, create_parents=True)) self.assertEqual(99, rv[0]) # Increment first counter again rv = cb.mutate_in(key, SD.counter('counter', -25)) self.assertEqual(75, rv[0]) self.assertRaises(ValueError, SD.counter, 'counter', 0)
def test_counter_in(self): cb = self.cb key = self.gen_key('sdcounter') cb.upsert(key, {}) rv = cb.mutate_in(key, SD.counter('counter', 100)) self.assertTrue(rv.success) self.assertFalse(rv.cas == 0) self.assertEqual(100, rv[0]) self.assertRaises(E.SubdocBadDeltaError, cb.mutate_in, key, SD.counter('not_a_counter', 'blah')) # Do an upsert cb.mutate_in(key, SD.upsert('not_a_counter', 'blah')) self.assertRaises(E.SubdocPathMismatchError, cb.mutate_in, key, SD.counter('not_a_counter', 25)) self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key, SD.counter('path.to.newcounter', 99)) rv = cb.mutate_in( key, SD.counter('path.to.newcounter', 99, create_parents=True)) self.assertEqual(99, rv[0]) # Increment first counter again rv = cb.mutate_in(key, SD.counter('counter', -25)) self.assertEqual(75, rv[0]) self.assertRaises(ValueError, SD.counter, 'counter', 0)
def test_mutate_in(self): res = None self.bucket.upsert( 'king_arthur', { 'name': 'Arthur', 'email': '*****@*****.**', 'interests': ['Holy Grail', 'African Swallows'] }) with tracer.start_active_span('test'): res = self.bucket.mutate_in( 'king_arthur', SD.array_addunique('interests', 'Cats'), SD.counter('updates', 1)) assert (res) self.assertTrue(res.success) spans = self.recorder.queued_spans() self.assertEqual(2, len(spans)) test_span = get_first_span_by_name(spans, 'sdk') assert (test_span) self.assertEqual(test_span.data["sdk"]["name"], 'test') cb_span = get_first_span_by_name(spans, 'couchbase') assert (cb_span) # Same traceId and parent relationship self.assertEqual(test_span.t, cb_span.t) self.assertEqual(cb_span.p, test_span.s) assert (cb_span.stack) self.assertIsNone(cb_span.ec) self.assertEqual(cb_span.data["couchbase"]["hostname"], "%s:8091" % testenv['couchdb_host']) self.assertEqual(cb_span.data["couchbase"]["bucket"], 'travel-sample') self.assertEqual(cb_span.data["couchbase"]["type"], 'mutate_in')
def change_enrollment_count(quarterId, courseNum, offeringId, delta): return list( offering_bucket.mutate_in( quarterId, subdoc.counter(courseNum + '.' + offeringId + '.enrolled', delta)))[0]
collection.mutate_in("array", [SD.array_insert("[1]", "cruel")]) # end::array_insert[] # exception raised if attempt to insert in out of bounds position try: collection.mutate_in("array", [SD.array_insert("[6]", "!")]) except PathNotFoundException: print("Cannot insert to out of bounds index.") # can insert into nested arrays as long as the path is appropriate collection.mutate_in("array", [SD.array_append("", ["another", "array"])]) collection.mutate_in("array", [SD.array_insert("[3][2]", "!")]) # tag::counter1[] result = collection.mutate_in("customer123", (SD.counter("logins", 1),)) num_logins = collection.get("customer123").content_as[dict]["logins"] print('Number of logins: {}.'.format(num_logins)) # Number of logins: 1. # end::counter1[] # tag::counter2[] collection.upsert("player432", {"gold": 1000}) collection.mutate_in("player432", (SD.counter("gold", -150),)) result = collection.lookup_in("player432", (SD.get("gold"),)) print("{} has {} gold remaining.".format( "player432", result.content_as[int](0))) # player432 has 850 gold remaining. # end::counter2[]
def like(ids): res = cb.mutate_in(ids, [SD.counter("likes", 1, create_parents=True)]) return redirect(url_for('posts'))
from couchbase.bucket import Bucket import couchbase.exceptions as E import couchbase.subdocument as SD cb = Bucket('couchbase://localhost/default') cb.upsert('docid', { 'name': 'Mark', 'email': '*****@*****.**', 'array': [1, 2, 3, 4] }) cb.mutate_in('docid', # Add 42 as a new element to 'array' SD.array_append('array', '42'), # Increment the numeric value of the first element by 99 SD.counter('array[0]', 99), # Add a new 'description' field SD.upsert('description', 'just a dev')) print('Document is now:', cb.get('docid').value) try: cb.mutate_in('docid', SD.upsert('deep.nested.path', 'some-value')) except E.SubdocPathNotFoundError as e: print('Caught exception', e) # Use `create` cb.mutate_in('docid', SD.upsert( 'deep.nested.path', 'some-value', create_parents=True)) print('Getting value back:', cb.retrieve_in('docid', 'deep.nested.path')[0])