def sync_detach_cl(self, cs_name, cl_name, detach_tbl): try: mcs = self.db.get_collection_space(cs_name) mcl = mcs.get_collection(cl_name) mcl.detach_collection(detach_tbl) except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e)
def clean(): print '---begin to remove procedure in clean' try: db.remove_procedure( 'sum' ) except SDBBaseError, e: if ( -233 != e.code and -6 != e.code ): pysequoiadb._print(e.detail) raise e
def clean(username, password): print '---begin to remove user in finally' try: db.remove_user(username, password) except SDBBaseError, e: if (-6 != e.code and -300 != e.code): #standalone throw -6 pysequoiadb._print(e.detail) raise e
def clean( username, password ): print '---begin to remove user in finally' try: db.remove_user( username, password ) except SDBBaseError, e: if ( -6 != e.code and -300 != e.code ): #standalone throw -6 pysequoiadb._print(e.detail) raise e
def clean(): print '---begin to remove procedure in clean' try: db.remove_procedure('sum') except SDBBaseError, e: if (-233 != e.code and -6 != e.code): pysequoiadb._print(e.detail) raise e
def clean( backupName ): print '---begin to remove backup in clean' try: db.remove_backup( {'Name': backupName} ) except SDBBaseError, e: if ( -241 != e.code ): pysequoiadb._print(e.detail) raise e
def clean( cs_name ): print '---begin to drop cs in finally' try: db.drop_collection_space(cs_name) except SDBBaseError, e: if ( -34 != e.code ): pysequoiadb._print(e.detail) raise e
def clean(cs_name): print '---begin to drop cs in finally' try: db.drop_collection_space(cs_name) except SDBBaseError, e: if (-34 != e.code): pysequoiadb._print(e.detail) raise e
def sync_hisub_drpidx(self, main_cs_name, main_cl_name, indexes_objstr, date_fm, etl_date='', dropidx_num=2): try: main_tbl = '%s.%s' % (main_cs_name, main_cl_name) snap8 = self.sync_get_catalog_snapshot(main_tbl) cata_infos = snap8['CataInfo'] if len(cata_infos) <= (dropidx_num + 2): print 'drop index from main cl: %s.%s' % (main_cs_name, main_cl_name) index_infos = self.sync_format_idxkey(indexes_objstr) cl = self.sync_create_cscl(main_cs_name, main_cl_name) for index_info in index_infos: index_name = index_info[0] cl.drop_index(index_name) return True shardkey = snap8['ShardingKey'] key_idx = shardkey.keys()[0] print ' ShardingKey: ' + key_idx # get local sub table #now = datetime.datetime.strptime(etl_date, date_fm) - timedelta(days=1) att_fm = '%s01' % date_fm[0:(len(date_fm) - 2)] etl_dtfm = datetime.datetime.strptime(etl_date, "%Y%m%d") low_bound = etl_dtfm.strftime(att_fm) sub_tbls = [] drop_subnum = 0 if dropidx_num < 2: drop_subnum = dropidx_num else: drop_subnum = dropidx_num / 2 for i in range(drop_subnum): sub_tbls = self.get_up_subtbl(main_tbl, low_bound) low_bound = sub_tbls[-1] sub_tbls.remove(low_bound) for sub_tbl in sub_tbls: sub_cs = sub_tbl.split('.')[0] sub_cl = sub_tbl.split('.')[1] cl = self.sync_create_cscl(sub_cs, sub_cl) index_infos = self.sync_format_idxkey(indexes_objstr) for index_info in index_infos: index_name = index_info[0] # drop index try: print '[DROP INDEX]Sub Table: %s, Index:%s' % ( sub_tbl, index_name) cl.drop_index(index_name) except (SDBTypeError, SDBBaseError), e: pysequoiadb._print("error code: %s, error: %s" % (e.code, e)) except (SDBTypeError, SDBBaseError), e: pysequoiadb._print("error code: %s, error: %s" % (e.code, e))
def _walk_snap_cursor_dict(self, cr): res_dict = {} while 1: try: record = cr.next() except SDBEndOfCursor: break except SDBBaseError as e: pysequoiadb._print(e) else: res_dict.update(record) return res_dict
def _walk_snap_cursor_list(self, cr): res_list = [] while 1: try: record = cr.next() except SDBEndOfCursor: break except SDBBaseError as e: pysequoiadb._print(e) else: res_list.append(record) return res_list
def createLobWithOid( cl, oid ): print '---begin to creat lob, specified oid' obj = cl.create_lob( oid ) data = "1234567891011121314151617181920" obj.write( data, 31 ) obj.close() print '---begin to creat lob, oid has been existed' try: obj = cl.create_lob( oid ) except SDBBaseError, e: if ( -5 != e.code ): pysequoiadb._print( e.detail ) raise e
def createLobWithOid(cl, oid): print '---begin to creat lob, specified oid' obj = cl.create_lob(oid) data = "1234567891011121314151617181920" obj.write(data, 31) obj.close() print '---begin to creat lob, oid has been existed' try: obj = cl.create_lob(oid) except SDBBaseError, e: if (-5 != e.code): pysequoiadb._print(e.detail) raise e
def create_index(self, cs_name, cl_name, indexes_objstr): try: index_infos = self.sync_format_idxkey(indexes_objstr) cl = self.sync_create_cscl(cs_name, cl_name) is_unique = False is_enforced = False for index_info in index_infos: index_name = index_info[0] index_key = index_info[1] cl.create_index(index_key, index_name, is_unique, is_enforced) print "Create index %s OK, full cl: %s.%s, index definition: %s" % ( index_name, cs_name, cl_name, index_key) except (SDBTypeError, SDBBaseError), e: print 'error code: %s' % e.code pysequoiadb._print(e) raise
def create_indexEE(self, cs_name, cl_name, index_def1, **kwargs): sdb = self.db if not sdb.is_valid(): print "Sdb connection is invalid" exit(1) is_unique = False is_enforced = False buffer_size = 64 index_def = eval(index_def1) #index_def = index_def1 #print 'index_def: %s' % index_def if not isinstance(index_def, list): raise SDBTypeError("Index definition must be an instance of tuple") if "is_unique" in kwargs and isinstance(kwargs.get("is_unique"), bool): is_unique = kwargs.get("is_unique") if "is_enforced" in kwargs and isinstance(kwargs.get("is_enforced"), bool): is_enforced = kwargs.get("is_enforced") if "buffer_size" in kwargs and isinstance(kwargs.get("buffer_size"), int): buffer_size = kwargs.get("buffer_size") for i in range(len(index_def)): if not isinstance(index_def[i], dict): print "Every object in index definition must be an instance of dict" print "Please check parameter: %s" % index_def[i] continue index_name = '' for key in index_def[i]: index_name = index_name + key index_name = index_name + '_' index_name = index_name + 'Idx' try: cs = sdb.get_collection_space(cs_name) cl = cs.get_collection(cl_name) cl.create_index(index_def[i], index_name, is_unique, is_enforced, buffer_size) print "Create index %s OK, full cl: %s.%s, index definition: %s" % ( index_name, cs_name, cl_name, index_def[i]) except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e)
def sync_get_subtbl(self, maincs_name, maincl_name, attach_bound=[]): try: main_tbl = '%s.%s' % (maincs_name, maincl_name) cs = self.db.get_collection_space(maincs_name) cl = cs.get_collection(maincl_name) snap8 = self.sync_get_catalog_snapshot(main_tbl) cata_infos = snap8['CataInfo'] shardkey = snap8['ShardingKey'] key_idx = shardkey.keys()[0] subtbl_name = '' for i in range(len(cata_infos)): cata_info_dict = cata_infos[i] try: low_bound = cata_info_dict['LowBound'][key_idx] except: low_bound = cata_info_dict['LowBound'][''] print 'get shard: %s' % key_idx if 0 != len(attach_bound): low_attach = attach_bound[0] if low_bound == low_attach: subtbl_name = cata_info_dict['SubCLName'] else: if '' == subtbl_name: subtbl_name = cata_info_dict['SubCLName'] break return subtbl_name except (SDBTypeError, SDBBaseError), e: if self.log is None: print 'failed to get table: %s.%s\'s sub table, ' \ 'error code: %s' % (maincs_name, maincl_name, e.code) else: self.log.error('failed to get table: %s.%s\'s sub table, ' \ 'error code: %s' % (maincs_name, maincl_name, e.code)) pysequoiadb._print(e) raise
# insert lob bin = "1asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfsdfaasdf" obj = cl.create_lob() obj.write( bin, 30 ) oid = obj.get_oid() obj.close() cr = cl.list_lobs() while True: try: lob_one = cr.next() except SDBEndOfCursor : break except SDBBaseError: raise pysequoiadb._print(lob_one) lob_two = cl.get_lob(oid) pysequoiadb._print(lob_two.get_size()) pysequoiadb._print(lob_two.get_create_time()) datafrom = lob_two.read(20) pysequoiadb._print(datafrom) cl.remove_lob(oid) pysequoiadb._print("remove success") # drop collection cs.drop_collection( cl_name ) del cl # drop collection space db.drop_collection_space(cs_name) del cs
cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize": 0}) # insert single record for i in range(0, 10): basketball = {"Item": "basketball", "id": i} cl.insert(basketball) cr = cl.query_and_update({"$set": { "id": 1000 }}, {"id": 1}, return_new=True) while True: try: record = cr.next() pysequoiadb._print(record) if (1000 == record['id']): print 'find and update ok' except SDBEndOfCursor: break except SDBBaseError: raise cr = cl.query_and_remove(condition={"id": 1000}) while True: try: record = cr.next() pysequoiadb._print(record) if (1000 == record['id']): print 'find and remove failed'
try: # connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) # create a cs cs_name = "gymnasium" cs = db.create_collection_space(cs_name) #create a cl cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize":0}) # get all indexes before create index pysequoiadb._print("Before create index:") cr = cl.get_indexes() # print indexes while True: try: record = cr.next() except SDBEndOfCursor : break except SDBBaseError: raise pysequoiadb._print(record) #create an index index = {'Item':1, 'Rank':-1} index_name = 'idx' cl.create_index(index, index_name, False, False)
cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize":0}) # insert single record basketball = {"Item":"basketball", "id":0} oid = cl.insert(basketball) # insert records records = [] for idx in range(2, 10): sport = {"sport id":idx} records.append(sport) cl.bulk_insert(1, records) # drop collection cs.drop_collection( cl_name ) del cl # drop collection space db.drop_collection_space(cs_name) del cs db.disconnect() del db print("Success") except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e)
records = [] for idx in xrange(0, 10): name = 'SequoiaDB' + str(idx) sport = {"Rank":idx, "Name":name} records.append(sport) cl.bulk_insert(1, [{'idx':i} for i in xrange(10)]) #records full_name = cl.get_full_name() sql1 = "select * from %s" % full_name sql2 = "insert into %s ( Rank, Name ) values( 10000, 'SequoiaDB' )"\ % full_name # execute sql1 cr = db.exec_sql(sql1) pysequoiadb._print("The result are below after execute sql:%s" % sql1) while True: try: record = cr.next() except SDBEndOfCursor : break except SDBBaseError: raise pysequoiadb._print(record) pysequoiadb._print('\n') # execute sql2 db.exec_update(sql2) pysequoiadb._print("The result are below after execute sql:%s" % sql2) cr = cl.query()
# connect to local db, using default args value. # host='localhost', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) # create collection space # try to get a collection space named by cs_name specified cs_name = 'subject' cs = db.create_collection_space( cs_name ) # 1.get collection space # try to get a collection space named by cs_name specified cs = db.get_collection_space( cs_name ) # 2.get collection space # try to get a collection space named by 'sports' use __getitem__ cs = db[cs_name] pysequoiadb._print("get collection space:[%s] success" % cs_name) # 3.get collection space # try to get a collection space named by 'sports' use __getattri__ cs = db.subject pysequoiadb._print("get collection space:[%s] success." % 'subject') # release cs_name = cs.get_collection_space_name() db.drop_collection_space(cs_name) del cs del db except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e)
try: # connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize": 0}) # insert single record basketball = {"Item": "basketball", "id": 0} oid = cl.insert(basketball) pysequoiadb._print("before update") cr = cl.query() while True: try: record = cr.next() except SDBEndOfCursor: break except SDBBaseError: raise pysequoiadb._print(record) # update records update = {'$set': {"Item": "football", "Rank": 1}} cond = {'id': {'$et': 0}} cl.update(update, condition=cond)
def connect_to_hosts(self, hosts, **kwargs): """try to connect a host in specified hosts Parameters: Name Type Info: hosts list The list contains hosts. eg. [ {'host':'localhost', 'service':'11810'}, {'host':'192.168.10.30', 'service':'11810'}, {'host':'192.168.20.63', 'service':11810}, ] **kwargs Useful options are below: - user str The user name to access to database. - password str The user password to access to database. - policy str The policy of select hosts. it must be string of 'random' or 'one_by_one'. Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(hosts, list): raise SDBTypeError("hosts must be an instance of list") if "policy" in kwargs: policy = kwargs.get("policy") else: policy = "random" if not isinstance(policy, str): raise SDBTypeError("policy must be an instance of basestring") if len(hosts) == 0: raise SDBTypeError("hosts must hava at least 1 item", const.INVALIDARG) local = socket.gethostname() localip = self.__get_local_ip() if "user" in kwargs: if not isinstance(kwargs.get("user"), str): raise SDBTypeError("user name in kwargs must be \ an instance of basestring") _user = kwargs.get("user") else: _user = self.USER if "password" in kwargs: if not isinstance(kwargs.get("password"), str): raise SDBTypeError("password in kwargs must be \ an instance of basestring") _psw = kwargs.get("password") else: _psw = self.PSW # connect to localhost first for ip in hosts: if ("localhost" in ip.values() or local in ip.values() or localip in ip.values()): host = ip['host'] svc = ip['service'] if isinstance(host, basestring): self.__host = host else: raise SDBTypeError("policy must be an instance of basestring") if isinstance(svc, int): self.__service = str(svc) elif isinstance(svc, basestring): self.__service = svc else: raise SDBTypeError("policy must be an instance of int or basestring") try: self.connect(self.__host, self.__service, user = _user, password = _psw) except SDBBaseError: continue pysequoiadb._print(self.__repr__()) return # without local host in hosts, check policy size = len(hosts) if 0 == cmp("random", policy): position = random.randint(0, size - 1) elif 0 == cmp("one_by_one", policy): position = 0; else: raise SDBTypeError("policy must be 'random' or 'one_by_one'.") # try to connect to host one by one for index in range(size): ip = hosts[position] host = ip['host'] svc = ip['service'] if isinstance(host, basestring): self.__host = host else: raise SDBTypeError("policy must be an instance of basestring") if isinstance(svc, int): self.__service = str(svc) elif isinstance(svc, basestring): self.__service = svc else: raise SDBTypeError("policy must be an instance of int or str") try: self.connect(self.__host, self.__service, user = _user, password = _psw) except SDBBaseError: position += 1 if position >= size: position %= size continue #with no error pysequoiadb._print(self.__repr__()) return #raise a expection for failed to connect to any host raise SDBBaseError("Failed to connect all specified hosts", rc)
def disconnect(self): try: self.db.disconnect() except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e)
cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize": 0}) # insert single record basketball = {"Item": "basketball", "id": 0} oid = cl.insert(basketball) # insert records records = [] for idx in range(2, 10): sport = {"sport id": idx} records.append(sport) cl.bulk_insert(1, records) # drop collection cs.drop_collection(cl_name) del cl # drop collection space db.drop_collection_space(cs_name) del cs db.disconnect() del db print("Success") except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e)
def test_lob(self): try: #create a new lob,oid = None try: obj = cl.create_lob() obj.write("hello", 5) new_oid = obj.get_oid() #print new_oid obj.close() except SDBBaseError: print 'create oid = None filed' #create a new lob,oid = 54471e5161a3be8176000000 #write data try: obj = cl.create_lob(bson.ObjectId("5448a5181c3eb9e00b000001")) data = "1234567891011121314151617181920" obj.write(data, 31) obj.close() except SDBBaseError: print 'create oid = 54471e5161a3be8176000000 failed' #create a repeat name lob try: obj = cl.create_lob(bson.ObjectId("5448a5181c3eb9e00b000001")) except SDBBaseError: print 'create a repeat name lob' #list lobs i = 0 cr = cl.list_lobs() while True: try: lob = cr.next() i = i + 1 except SDBEndOfCursor: break except SDBBaseError: raise if (i != 2): print 'list lobs failed' #seek and read data from a lob obj = cl.get_lob(bson.ObjectId("5448a5181c3eb9e00b000001")) oid = obj.get_oid() #print oid obj.seek(11, 0) data = obj.read(20) obj.close() if (0 != cmp(data, "11121314151617181920")): print 'seek and read data from a lob failed' #get a exist lob obj = cl.get_lob(bson.ObjectId("5448a5181c3eb9e00b000001")) oid = obj.get_oid() if (0 != cmp(oid, bson.ObjectId("5448a5181c3eb9e00b000001"))): print 'get a exist lob failed' #get a not exist lob try: obj = cl.get_lob(bson.ObjectId("54471e5161a3be8176000002")) print 'get a not exist lob failed' except SDBBaseError: print 'get a not exist lob' #get lob's size if (31 != obj.get_size()): print "get lob's size failed" #get lob's create time pysequoiadb._print(obj.get_create_time()) #remove a exist lob try: cl.remove_lob(obj.get_oid()) cl.remove_lob(new_oid) except SDBBaseError: print 'remove a exist log failed' obj.close() #remove a not exist lob try: cl.remove_lob(bson.ObjectId("54471e5161a3be8176000002")) except SDBBaseError: print 'remove a not exist lob' except Exception as e: print e finally: cl.delete()
try: # connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) # create a cs cs_name = "gymnasium" cs = db.create_collection_space(cs_name) #create a cl cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize": 0}) # get all indexes before create index pysequoiadb._print("Before create index:") cr = cl.get_indexes() # print indexes while True: try: record = cr.next() except SDBEndOfCursor: break except SDBBaseError: raise pysequoiadb._print(record) #create an index index = {'Item': 1, 'Rank': -1} index_name = 'idx' cl.create_index(index, index_name, False, False)
try: # connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize":0}) # insert single record basketball = {"Item":"basketball", "id":0} oid = cl.insert(basketball) pysequoiadb._print("before update") cr = cl.query() while True: try: record = cr.next() except SDBEndOfCursor : break except SDBBaseError: raise pysequoiadb._print(record) # update records update = {'$set':{"Item":"football", "Rank":1 }} cond = {'id':{'$et':0}} cl.update(update, condition = cond)
# connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize": 0}) # insert single record for query() basketball = {"Item": "basketball", "id": 0} oid = cl.insert(basketball) cond = {"id": {'$et': 0}} pysequoiadb._print("query one record, using condition=%s" % cond) cr = cl.query(condition=cond) while True: try: record = cr.next() except SDBEndOfCursor: break except SDBBaseError: raise pysequoiadb._print(record) # bulk_insert records = [] for idx in range(2, 10): sport = {"sport id": idx}
cl_full_name = cs_name + '.' + cl_name cursor = db.list_collections() hasThisCL = False while True: try: record = cursor.next() if ( record['Name'] == cl_full_name ): hasThisCL = True except SDBEndOfCursor: break except ( Exception ), e: raise e if( hasThisCL != True ): print 'excute: db.list_collections(), expect: has cl %s, actual: has not' % ( cl_full_name ) raise Exception( 'CHECK_ERROR' ) print '---begin to drop cl' cs.drop_collection( cl_name ) print '---begin to drop cs' db.drop_collection_space( cs_name ) except SDBBaseError, e: pysequoiadb._print(e.detail) raise e finally: if( locals().has_key('db') ): clean( cs_name ) db.disconnect() del db
def test_lob(self): try: #create a new lob,oid = None try: obj = cl.create_lob() obj.write("hello",5) new_oid = obj.get_oid() #print new_oid obj.close() except SDBBaseError : print 'create oid = None filed' #create a new lob,oid = 54471e5161a3be8176000000 #write data try: obj = cl.create_lob( bson.ObjectId("5448a5181c3eb9e00b000001" )) data = "1234567891011121314151617181920" obj.write( data,31 ) obj.close() except SDBBaseError : print 'create oid = 54471e5161a3be8176000000 failed' #create a repeat name lob try: obj = cl.create_lob( bson.ObjectId("5448a5181c3eb9e00b000001" ) ) except SDBBaseError : print 'create a repeat name lob' #list lobs i = 0 ; cr = cl.list_lobs() while True: try: lob = cr.next() i = i+ 1 except SDBEndOfCursor : break except SDBBaseError : raise if ( i != 2 ): print 'list lobs failed' #seek and read data from a lob obj = cl.get_lob(bson.ObjectId("5448a5181c3eb9e00b000001" ) ) oid = obj.get_oid() #print oid obj.seek( 11,0) data = obj.read(20) obj.close() if( 0 != cmp(data,"11121314151617181920")): print 'seek and read data from a lob failed' #get a exist lob obj = cl.get_lob( bson.ObjectId("5448a5181c3eb9e00b000001" ) ) oid = obj.get_oid() if ( 0 != cmp(oid, bson.ObjectId("5448a5181c3eb9e00b000001" ) )): print 'get a exist lob failed' #get a not exist lob try: obj = cl.get_lob( bson.ObjectId("54471e5161a3be8176000002" ) ) print 'get a not exist lob failed' except SDBBaseError: print 'get a not exist lob' #get lob's size if ( 31 != obj.get_size()): print "get lob's size failed" #get lob's create time pysequoiadb._print(obj.get_create_time()) #remove a exist lob try: cl.remove_lob(obj.get_oid()) cl.remove_lob(new_oid) except SDBBaseError: print 'remove a exist log failed' obj.close() #remove a not exist lob try: cl.remove_lob(bson.ObjectId("54471e5161a3be8176000002" )) except SDBBaseError : print 'remove a not exist lob' except Exception as e: print e finally: cl.delete()
if (-34 != e.code): pysequoiadb._print(e.detail) raise e if __name__ == "__main__": try: parse_option() cs_name = "pydriver_lob_cs" cl_name = "pydriver_lob_cl" db = client(hostname, service) cl = createCL(cs_name, cl_name) oid1 = bson.ObjectId("5448a5181c3eb9e00b000001") createLobWithOid(cl, oid1) oid2 = createLobWithoutOid(cl) listLob(cl) getLob(cl, oid1) removeLob(cl, oid1, oid2) clean(cs_name) except SDBBaseError, e: pysequoiadb._print(e.detail) raise e finally: if (locals().has_key('db')): clean(cs_name) db.disconnect() del db
cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize":0}) # insert single record for i in range(0, 10): basketball = {"Item":"basketball", "id":i} cl.insert(basketball) cr = cl.query_and_update({"$set":{"id":1000}}, {"id": 1}, return_new=True) while True: try: record = cr.next() pysequoiadb._print(record) if (1000 == record['id']): print 'find and update ok' except SDBEndOfCursor : break except SDBBaseError : raise cr = cl.query_and_remove(condition={"id":1000}) while True: try: record = cr.next() pysequoiadb._print(record) if (1000 == record['id']): print 'find and remove failed'
#! /usr/bin/python import pysequoiadb from pysequoiadb import client from pysequoiadb import const from pysequoiadb.error import (SDBTypeError, SDBBaseError, SDBEndOfCursor) if "__main__" == __name__: # connect to local db, using default args value. # host= 'localhost', port= 11810, user= '', password= '' try: db_default = client() pysequoiadb._print( db_default ) del db_default except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e) # connect to db, using default args value. # host= '192.168.20.111', port= 11810, user= '', password= '' # 192.168.20.111 is not a valid try: db_to_1 = client('192.168.20.48', 11810, '', '') del db_to_1 except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e) # connect to db, using default args value. # host= 'localhost', port= 11810, user= '', password= ''
records = [] for idx in xrange(0, 10): name = 'SequoiaDB' + str(idx) sport = {"Rank": idx, "Name": name} records.append(sport) cl.bulk_insert(1, [{'idx': i} for i in xrange(10)]) #records full_name = cl.get_full_name() sql1 = "select * from %s" % full_name sql2 = "insert into %s ( Rank, Name ) values( 10000, 'SequoiaDB' )"\ % full_name # execute sql1 cr = db.exec_sql(sql1) pysequoiadb._print("The result are below after execute sql:%s" % sql1) while True: try: record = cr.next() except SDBEndOfCursor: break except SDBBaseError: raise pysequoiadb._print(record) pysequoiadb._print('\n') # execute sql2 db.exec_update(sql2) pysequoiadb._print("The result are below after execute sql:%s" % sql2) cr = cl.query()
# insert lob bin = "1asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfsdfaasdf" obj = cl.create_lob() obj.write(bin, 30) oid = obj.get_oid() obj.close() cr = cl.list_lobs() while True: try: lob_one = cr.next() except SDBEndOfCursor: break except SDBBaseError: raise pysequoiadb._print(lob_one) lob_two = cl.get_lob(oid) pysequoiadb._print(lob_two.get_size()) pysequoiadb._print(lob_two.get_create_time()) datafrom = lob_two.read(20) pysequoiadb._print(datafrom) cl.remove_lob(oid) pysequoiadb._print("remove success") # drop collection cs.drop_collection(cl_name) del cl # drop collection space db.drop_collection_space(cs_name) del cs
# connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize":0}) # insert single record for query() basketball = {"Item":"basketball", "id":0} oid = cl.insert(basketball) cond = {"id":{'$et':0}} pysequoiadb._print("query one record, using condition=%s" % cond) cr = cl.query(condition=cond) while True: try: record = cr.next() except SDBEndOfCursor : break except SDBBaseError: raise pysequoiadb._print(record) # bulk_insert records = [] for idx in range(2, 10): sport = {"sport id":idx}