def test_create_doc(self):
        cb = self.cb
        key = self.gen_key('create_doc')
        cb.mutate_in(key, SD.upsert('new.path', 'newval'), upsert_doc=True)
        self.assertEqual('newval', cb.retrieve_in(key, 'new.path')[0])

        # Check 'insert_doc'
        self.assertRaises(E.KeyExistsError, cb.mutate_in,
                          key, SD.upsert('new.path', 'newval'), insert_doc=True)

        cb.remove(key)
        cb.mutate_in(key, SD.upsert('new.path', 'newval'), insert_doc=True)
        self.assertEqual('newval', cb.retrieve_in(key, 'new.path')[0])
Beispiel #2
0
    def __load_chain(self, start_num=0):
        for i, cluster in enumerate(self.get_cb_clusters()):
            if self._rdirection == REPLICATION_DIRECTION.BIDIRECTION:
                if i > len(self.get_cb_clusters()) - 1:
                    break
            else:
                if i >= len(self.get_cb_clusters()) - 1:
                    break
            if not self._dgm_run:
                for bucket in cluster.get_buckets():
                    client = SDKClient(scheme="couchbase", hosts=[cluster.get_master_node().ip],
                                            bucket=bucket.name).cb
                    for i in xrange(start_num, start_num + self._num_items):
                        key = 'k_%s_%s' % (i, str(cluster).replace(' ','_').
                                           replace('.','_').replace(',','_').replace(':','_'))
                        value = {'xattr_%s' % i:'value%s' % i}
                        client.upsert(key, value)
                        client.mutate_in(key, SD.upsert('xattr_%s' % i, 'value%s' % i,
                                                             xattr=True,
                                                             create_parents=True))
                        partition = bucket.kvs[1].acquire_partition(key)#["partition"]
                        if self.only_store_hash:
                            value = str(crc32.crc32_hash(value))
                        res = client.get(key)
                        partition.set(key, json.dumps(value), 0, res.flags)
                        bucket.kvs[1].release_partition(key)

            else:
                cluster.load_all_buckets_till_dgm(
                    active_resident_threshold=self._active_resident_threshold,
                    items=self._num_items)
    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_enhanced_err_present_authorisation(self):
        import couchbase.subdocument as SD
        users=[('writer',('s3cr3t',[('data_reader', 'default'), ('data_writer', 'default')])),
              ('reader',('s3cr3t',[('data_reader', 'default')]))]
        #self.mockclient._do_request("SET_ENHANCED_ERRORS",{"enabled":True})
        for user in users:
            print(str(user))
            (userid, password, roles) = user[0],user[1][0],user[1][1]
            # add user
            self.admin.user_upsert(AuthDomain.Local, userid, password, roles)
            time.sleep(1)
            try:
                connection = self.make_connection(username=userid,password=password)

                key = self.gen_key('create_doc')
                connection.mutate_in(key, SD.upsert('new.path', 'newval'), upsert_doc=True)
            except CouchbaseError as e:
                print(str(e))
                if userid=="writer":
                    raise e
                else:
                    self.assertRegexpMatches(e.context,r".*Authorization failure.*","doesn't have correct Context field")
                    self.assertRegexpMatches(e.ref,r"(.*?)-(.*?)-.*","doesn't have correct Ref field")
                    self.assertRegexpMatches(str(e),r".*Context=Authorization failure.*,.*Ref=.*","exception as string doesn't contain both fields")
            finally:
                #remove user
                self.admin.user_remove(AuthDomain.Local, userid)
 def test_eventing_processes_mutations_when_mutated_through_subdoc_api_and_set_expiry_through_sdk(self):
     # set expiry pager interval
     ClusterOperationHelper.flushctl_set(self.master, "exp_pager_stime", 1, bucket=self.src_bucket_name)
     url = 'couchbase://{ip}/{name}'.format(ip=self.master.ip, name=self.src_bucket_name)
     bucket = Bucket(url, username="******", password="******")
     for docid in ['customer123', 'customer1234', 'customer12345']:
         bucket.insert(docid, {'some': 'value'})
     body = self.create_save_function_body(self.function_name, self.handler_code,
                                           dcp_stream_boundary="from_now")
     # deploy eventing function
     self.deploy_function(body)
     # upserting a new sub-document
     bucket.mutate_in('customer123', SD.upsert('fax', '775-867-5309'))
     # inserting a sub-document
     bucket.mutate_in('customer1234', SD.insert('purchases.complete', [42, True, None], create_parents=True))
     # Creating and populating an array document
     bucket.mutate_in('customer12345', SD.array_append('purchases.complete', ['Hello'], create_parents=True))
     self.verify_eventing_results(self.function_name, 3, skip_stats_validation=True)
     for docid in ['customer123', 'customer1234', 'customer12345']:
         # set expiry on all the docs created using sub doc API
         bucket.touch(docid, ttl=5)
     self.sleep(10, "wait for expiry of the documents")
     # Wait for eventing to catch up with all the expiry mutations and verify results
     self.verify_eventing_results(self.function_name, 0, skip_stats_validation=True)
     self.undeploy_and_delete_function(body)
Beispiel #6
0
 def set_xattr(self, sdk_conn):
     try:
         k = "sdk_1"
         sdk_conn.upsert(k, {})
         sdk_conn.mutate_in(k, SD.upsert('my', {'value': 1}, xattr=True))
         return True
     except Exception as e:
         log.info("Exception is from set_xattr function {0}".format(e))
         return False
 def dict_upsert(self, client, key='', path='', value=None):
     try:
         if self.verbose_func_usage:
             self.log.info(" dict_upsert ----> {0} :: {1}".format(path, value))
         if self.use_sdk_client:
             client.mutate_in(key, SD.upsert(path, value, xattr=self.xattr))
         else:
             client.dict_upsert_sd(key, path, value)
     except Exception:
         raise
 def dict_upsert(self, client, key='', path='', value=None, xattr=None, create_parents=None):
     try:
         new_path = self.generate_path(self.nesting_level, path)
         if self.is_sdk_client:
             client.mutate_in(key, SD.upsert(path, value, xattr=xattr, create_parents=create_parents))
         else:
             client.dict_upsert_sd(key, new_path, json.dumps(value))
     except Exception as e:
         self.log.error(e)
         self.fail("Unable to add key {0} for path {1} after {2} tries".format(key, path, 1))
    def create_xattr_data(self, type="system"):
        cluster = Cluster('couchbase://'+str(self.master.ip))
        authenticator = PasswordAuthenticator(self.username, self.password)
        cluster.authenticate(authenticator)
        cb = cluster.open_bucket('default')
        docs = self.get_meta_ids()
        self.log.info("Docs: " + str(docs[0:5]))
        xattr_data = []
        self.log.info("Adding xattrs to data")
        val = 0
        for doc in docs:
            if type == "system":
                rv = cb.mutate_in(doc["id"], SD.upsert('_system1', val, xattr=True, create_parents=True))
                xattr_data.append({'_system1': val})
                rv = cb.mutate_in(doc["id"], SD.upsert('_system2', {'field1': val, 'field2': val*val}, xattr=True, create_parents=True))
                xattr_data.append({'_system2': {'field1': val, 'field2': val*val}})
                rv = cb.mutate_in(doc["id"], SD.upsert('_system3', {'field1': {'sub_field1a': val, 'sub_field1b': val*val}, 'field2': {'sub_field2a': 2*val, 'sub_field2b': 2*val*val}}, xattr=True, create_parents=True))
                xattr_data.append({'_system3': {'field1': {'sub_field1a': val, 'sub_field1b': val*val}, 'field2': {'sub_field2a': 2*val, 'sub_field2b': 2*val*val}}})
            if type == "user":
                rv = cb.mutate_in(doc["id"], SD.upsert('user1', val, xattr=True, create_parents=True))
                xattr_data.append({'user1': val})
                rv = cb.mutate_in(doc["id"], SD.upsert('user2', {'field1': val, 'field2': val*val}, xattr=True, create_parents=True))
                xattr_data.append({'user2': {'field1': val, 'field2': val*val}})
                rv = cb.mutate_in(doc["id"], SD.upsert('user3', {'field1': {'sub_field1a': val, 'sub_field1b': val*val}, 'field2': {'sub_field2a': 2*val, 'sub_field2b': 2*val*val}}, xattr=True, create_parents=True))
                xattr_data.append({'user3': {'field1': {'sub_field1a': val, 'sub_field1b': val*val}, 'field2': {'sub_field2a': 2*val, 'sub_field2b': 2*val*val}}})
            val = val + 1

        self.log.info("Completed adding " + type + "xattrs to data to " + str(val) + " docs")
        return xattr_data
Beispiel #10
0
 def test_eventing_processes_mutation_when_xattrs_is_updated(self):
     url = 'couchbase://{ip}/{name}'.format(ip=self.master.ip, name=self.src_bucket_name)
     bucket = Bucket(url, username="******", password="******")
     for docid in ['customer123', 'customer1234', 'customer12345']:
         bucket.upsert(docid, {})
     body = self.create_save_function_body(self.function_name, self.handler_code,
                                           dcp_stream_boundary="from_now")
     # deploy eventing function
     self.deploy_function(body)
     # update multiple xattrs and update the documents
     for docid in ['customer123', 'customer1234', 'customer12345']:
         bucket.mutate_in(docid, SD.upsert('my1', {'value': 1}, xattr=True))
         bucket.mutate_in(docid, SD.upsert('my2', {'value': 2}, xattr=True))
         bucket.mutate_in(docid, SD.upsert('fax', '775-867-5309'))
     self.verify_eventing_results(self.function_name, 3, skip_stats_validation=True)
     # add new multiple xattrs , delete old xattrs and delete the documents
     for docid in ['customer123', 'customer1234', 'customer12345']:
         bucket.mutate_in(docid, SD.upsert('my3', {'value': 3}, xattr=True))
         bucket.mutate_in(docid, SD.upsert('my4', {'value': 4}, xattr=True))
         bucket.mutate_in(docid, SD.remove('my3', xattr=True))
         bucket.mutate_in(docid, SD.remove('my4', xattr=True))
         bucket.remove(docid)
     self.verify_eventing_results(self.function_name, 0, skip_stats_validation=True)
     self.undeploy_and_delete_function(body)
 def test_eventing_processes_mutations_when_mutated_through_subdoc_api_and_set_expiry_through_sdk(
         self):
     # set expiry pager interval
     ClusterOperationHelper.flushctl_set(self.master,
                                         "exp_pager_stime",
                                         1,
                                         bucket=self.src_bucket_name)
     url = 'couchbase://{ip}/{name}'.format(ip=self.master.ip,
                                            name=self.src_bucket_name)
     bucket = Bucket(url, username="******", password="******")
     for docid in ['customer123', 'customer1234', 'customer12345']:
         bucket.insert(docid, {'some': 'value'})
     body = self.create_save_function_body(self.function_name,
                                           self.handler_code,
                                           dcp_stream_boundary="from_now")
     # deploy eventing function
     self.deploy_function(body)
     # upserting a new sub-document
     bucket.mutate_in('customer123', SD.upsert('fax', '775-867-5309'))
     # inserting a sub-document
     bucket.mutate_in(
         'customer1234',
         SD.insert('purchases.complete', [42, True, None],
                   create_parents=True))
     # Creating and populating an array document
     bucket.mutate_in(
         'customer12345',
         SD.array_append('purchases.complete', ['Hello'],
                         create_parents=True))
     self.verify_eventing_results(self.function_name,
                                  3,
                                  skip_stats_validation=True)
     for docid in ['customer123', 'customer1234', 'customer12345']:
         # set expiry on all the docs created using sub doc API
         bucket.touch(docid, ttl=5)
     self.sleep(10, "wait for expiry of the documents")
     # Wait for eventing to catch up with all the expiry mutations and verify results
     self.verify_eventing_results(self.function_name,
                                  0,
                                  skip_stats_validation=True)
     self.undeploy_and_delete_function(body)
Beispiel #12
0
    def test_fulldoc(self):
        cb = self.cb
        key = self.gen_key('fulldoc')
        cb.mutate_in(key,
                     SD.upsert_fulldoc({'val': True}),
                     SD.upsert('my.xattr',
                               'attrval',
                               create_parents=True,
                               xattr=True),
                     insert_doc=True)
        self.assertEqual(True, cb.retrieve_in(key, 'val')[0])

        self.assertEqual('attrval',
                         cb.lookup_in(key, SD.get('my.xattr', xattr=True))[0])

        rv = cb.lookup_in(key, SD.get('my.xattr'))
        self.assertFalse(rv.exists(0))

        # Get the document back
        rv = cb.lookup_in(key, SD.get_fulldoc())
        self.assertEqual(True, rv[0]['val'])
Beispiel #13
0
    def test_scenario_B(self):
        """
          Scenario B:

        1) fetch a document fragment which is a json array with elements
        2) make modifications to the content
        3) replace the fragment in the original document

        """
        self.coll.upsert("id", {'someArray': ['wibble', 'gronk']})
        subdoc = self.coll.get("id", GetOptions().project("someArray"))
        result = None
        if subdoc:
            arr = subdoc.content_as_array()
            arr.append("foo")

            result = self.coll.mutate_in(
                "id", [SD.upsert("someArray", arr)],
                MutateInOptions().timeout(Seconds(10)))

        self.assertIsInstance(result, MutateInResult)
Beispiel #14
0
    def test_xattrs_basic(self):
        cb = self.cb
        k = self.gen_key('xattrs')
        cb.upsert(k, {})

        # Try to upsert a single xattr
        rv = cb.mutate_in(
            k, SD.upsert('my.attr', 'value', xattr=True, create_parents=True))
        self.assertTrue(rv.success)

        body = cb.get(k)
        self.assertFalse('my' in body.value)
        self.assertFalse('my.attr' in body.value)

        # Try using lookup_in
        rv = cb.retrieve_in(k, 'my.attr')
        self.assertFalse(rv.exists('my.attr'))

        # Finally, use lookup_in with 'xattrs' attribute enabled
        rv = cb.lookup_in(k, SD.get('my.attr', xattr=True))
        self.assertTrue(rv.exists('my.attr'))
        self.assertEqual('value', rv['my.attr'])
    def test_xattrs_basic(self):
        cb = self.cb
        k = self.gen_key('xattrs')
        cb.upsert(k, {})

        # Try to upsert a single xattr
        rv = cb.mutate_in(k, SD.upsert('my.attr', 'value',
                                       xattr=True,
                                       create_parents=True))
        self.assertTrue(rv.success)

        body = cb.get(k)
        self.assertFalse('my' in body.value)
        self.assertFalse('my.attr' in body.value)

        # Try using lookup_in
        rv = cb.retrieve_in(k, 'my.attr')
        self.assertFalse(rv.exists('my.attr'))

        # Finally, use lookup_in with 'xattrs' attribute enabled
        rv = cb.lookup_in(k, SD.get('my.attr', xattr=True))
        self.assertTrue(rv.exists('my.attr'))
        self.assertEqual('value', rv['my.attr'])
Beispiel #16
0
    def __load_chain(self, start_num=0):
        for i, cluster in enumerate(self.get_cb_clusters()):
            if self._rdirection == REPLICATION_DIRECTION.BIDIRECTION:
                if i > len(self.get_cb_clusters()) - 1:
                    break
            else:
                if i >= len(self.get_cb_clusters()) - 1:
                    break
            if not self._dgm_run:
                for bucket in cluster.get_buckets():
                    client = SDKClient(scheme="couchbase",
                                       hosts=[cluster.get_master_node().ip],
                                       bucket=bucket.name).cb
                    for i in range(start_num, start_num + self._num_items):
                        key = 'k_%s_%s' % (i, str(cluster).replace(
                            ' ', '_').replace('.', '_').replace(
                                ',', '_').replace(':', '_'))
                        value = {'xattr_%s' % i: 'value%s' % i}
                        client.upsert(key, value)
                        client.mutate_in(
                            key,
                            SD.upsert('xattr_%s' % i,
                                      'value%s' % i,
                                      xattr=True,
                                      create_parents=True))
                        partition = bucket.kvs[1].acquire_partition(
                            key)  #["partition"]
                        if self.only_store_hash:
                            value = str(crc32.crc32_hash(value))
                        res = client.get(key)
                        partition.set(key, json.dumps(value), 0, res.flags)
                        bucket.kvs[1].release_partition(key)

            else:
                cluster.load_all_buckets_till_dgm(
                    active_resident_threshold=self._active_resident_threshold,
                    items=self._num_items)
Beispiel #17
0
    def set_doc(self, client, key, value, exp, flags, xattr=None):
        if self.is_sdk_client:
            if xattr:
                # for verification xattr in subdoc
                client.cb.set(key, {}, exp, flags, )
                if isinstance(value, dict):
                    for k, v in value.items():
                        print(k, ":", v)
                        rv = client.cb.mutate_in(key, SD.upsert(k, v, xattr=xattr))
                        self.log.info("xattr '%s' added successfully?: %s" % (k, rv.success))
                        self.assertTrue(rv.success)

                        rv = client.cb.lookup_in(key, SD.exists(k, xattr=xattr))
                        self.log.info("xattr '%s' exists?: %s" % (k, rv.success))
                        try:
                            self.assertTrue(rv.success)
                        except Exception as e:
                            raise e
                else:
                    self.fail("Unable to handle non-json docs. Please review behavior")
            else:
                client.set(key, value, exp, flags,)
        else:
            client.set(key, exp, flags, value)
    def set_doc(self, client, key, value, exp, flags, xattr=None):
        if self.is_sdk_client:
            if xattr:
                # for verification xattr in subdoc
                client.cb.set(key, {}, exp, flags, )
                if isinstance(value, dict):
                    for k, v in value.iteritems():
                        print k, ":", v
                        rv = client.cb.mutate_in(key, SD.upsert(k, v, xattr=xattr))
                        self.log.info("xattr '%s' added successfully?: %s" % (k, rv.success))
                        self.assertTrue(rv.success)

                        rv = client.cb.lookup_in(key, SD.exists(k, xattr=xattr))
                        self.log.info("xattr '%s' exists?: %s" % (k, rv.success))
                        try:
                            self.assertTrue(rv.success)
                        except Exception as e:
                            raise e
                else:
                    self.fail("Unable to handle non-json docs. Please review behavior")
            else:
                client.set(key, value, exp, flags,)
        else:
            client.set(key, exp, flags, value)
Beispiel #19
0
 def update(self, key, subdoc_fields, size):
     newdoc = NewDocument(size)
     alphabet = newdoc._build_alphabet(key)
     for field in subdoc_fields.split(','):
         new_field_value = getattr(newdoc, '_build_' + field)(alphabet)
         self.client.mutate_in(key, SD.upsert(field, new_field_value))
Beispiel #20
0
 def update_xattr(self, key: str, field: str, doc: dict):
     self.client.mutate_in(key, subdocument.upsert(path=field,
                                                   value=doc,
                                                   xattr=True,
                                                   create_parents=True))
Beispiel #21
0
 def update_xattr(self, key: str, field: str, doc: dict):
     self.client.mutate_in(key, subdocument.upsert(path=field,
                                                   value=doc,
                                                   xattr=True,
                                                   create_parents=True))
    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'))
bucket_name = settings.BUCKET_NAME
user = settings.USERNAME
password = settings.PASSWORD
node = settings.CLUSTER_NODES[0]

SDK_CLIENT = Bucket('couchbase://{0}/{1}'.format(node, bucket_name),
                    username=user,
                    password=password)

SDK_CLIENT.timeout = 15

while True:
    try:
        print "."
        results = SDK_CLIENT.n1ql_query(
            'SELECT symbol,price FROM {} WHERE symbol IS NOT MISSING AND price IS NOT MISSING'
            .format(bucket_name, ))
        for row in results:
            stock_key = "stock:" + (row['symbol'])
            # perturb the price and round it to 2 decimal places
            price_multiplier = random.normalvariate(1, 0.025)
            if row['symbol'] == "CBSE" and price_multiplier < 1:
                price_multiplier = 1
            new_price = float(row['price']) * price_multiplier
            new_price = round(new_price, 2)
            SDK_CLIENT.mutate_in(stock_key, SD.upsert('price', new_price))
    except Exception:
        pass

    time.sleep(8)
except PathNotFoundException:
    print("Path does not exist")

# tag::lookup_in_multi[]
result = collection.lookup_in(
    "customer123",
    [SD.get("addresses.delivery.country"),
     SD.exists("purchases.complete[-1]")])

print("{0}".format(result.content_as[str](0)))
print("Path exists: {}.".format(result.exists(1)))
# path exists: True.
# end::lookup_in_multi[]

# tag::mutate_in_upsert[]
collection.mutate_in("customer123", [SD.upsert("fax", "311-555-0151")])
# end::mutate_in_upsert[]

# tag::mutate_in_insert[]
collection.mutate_in(
    "customer123", [SD.insert("purchases.pending", [42, True, "None"])])

try:
    collection.mutate_in(
        "customer123", [
            SD.insert(
                "purchases.complete",
                [42, True, "None"])])
except PathExistsException:
    print("Path exists, cannot use insert.")
# end::mutate_in_insert[]
Beispiel #25
0
 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)))
Beispiel #26
0
    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'))
Beispiel #27
0
 def update(self, key: str, field: str, doc: dict):
     new_field_value = doc[field]
     self.client.mutate_in(key, subdocument.upsert(path=field,
                                                   value=new_field_value))
def zero_enrollment_count(quarterId, courseNum, offeringId):
    return list(
        offering_bucket.mutate_in(
            quarterId,
            subdoc.upsert(courseNum + '.' + offeringId + '.enrolled', 0)))[0]
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])
Beispiel #30
0
 def update(self, key: str, field: str, doc: dict):
     new_field_value = doc[field]
     self.client.mutate_in(
         key, subdocument.upsert(path=field, value=new_field_value))
Beispiel #31
0
 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)
Beispiel #32
0
 def update(self, key, subdoc_fields, size):
     newdoc = NewDocument(size)
     alphabet = newdoc._build_alphabet(key)
     for field in subdoc_fields.split(','):
         new_field_value = getattr(newdoc, '_build_' + field)(alphabet)
         self.client.mutate_in(key, SD.upsert(field, new_field_value))
Beispiel #33
0
 def sd_upsert(self, key, path, doc, cas=None):
     if cas is not None:
         return self._cb.mutate_in(key, SD.upsert(path, doc, True), cas=cas)
     else:
         return self._cb.mutate_in(key, SD.upsert(path, doc, True))
Beispiel #34
0
print("Path exists? {0}", result.content_as[bool](1))
#end::combine[]
"""
----

== Mutating

Mutation operations modify one or more paths in the document.
    The simplest of these operations is _subdoc-upsert_, which, similar to the fulldoc-level _upsert_, will either modify the value of an existing path or create it if it does not exist:

.Upserting a new sub-document
[source,csharp]
                 ----
"""
#tag::upsert[]
collection.mutate_in("customer123", [SD.upsert("fax", "311-555-0151")])
#end::upsert[]
"""
----

Likewise, the _subdoc-insert_ operation will only add the new value to the path if it does not exist:

.Inserting a sub-document
[source,csharp]
             ----
"""
#tag::insert[]
collection.mutate_in("customer123",
                     [SD.insert("purchases.complete", [42, True, "None"])])

# SubdocPathExistsError