Exemple #1
0
   def test_index(self):
      try:
         #create index
         index_def = {'name':1,'age':-1}
         idx_name = 'nameIndex'
         is_unique = True
         is_enforce = True
         try:
            cl.create_index(index_def,idx_name,is_unique,is_enforce)
         except Exception as e:
            print 'create index error'

         idx_name = 'nameIndex'
         #get index name
         cursor = cl.get_indexes(idx_name)
         while True:
            try:
               record = cursor.next()
               if(0 != cmp( record['IndexDef']['name'],idx_name)):
                  print 'get index failed!'
            except SDBEndOfCursor :
                  break
            except SDBBaseError :
                  raise

         #get all index name
         flag  = False
         idx_name = ''
         cursor = cl.get_indexes(idx_name)
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               if (0 == cmp(record['IndexDef']['name'],'nameIndex')):
                  flag = True
                  break
            except SDBEndOfCursor:
               break
            except SDBBaseError:
               raise

         if flag == False:
            print 'get all index failed!'

         #remove index
         idx_name = 'nameIndex'
         try:
            cl.drop_index(idx_name)
         except Exception as e:
            print 'drop index error'

      except Exception as e:
         print e
      finally:
         cl.delete()
    def test_index(self):
        try:
            #create index
            index_def = {'name': 1, 'age': -1}
            idx_name = 'nameIndex'
            is_unique = True
            is_enforce = True
            try:
                cl.create_index(index_def, idx_name, is_unique, is_enforce)
            except Exception as e:
                print 'create index error'

            idx_name = 'nameIndex'
            #get index name
            cursor = cl.get_indexes(idx_name)
            while True:
                try:
                    record = cursor.next()
                    if (0 != cmp(record['IndexDef']['name'], idx_name)):
                        print 'get index failed!'
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #get all index name
            flag = False
            idx_name = ''
            cursor = cl.get_indexes(idx_name)
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    if (0 == cmp(record['IndexDef']['name'], 'nameIndex')):
                        flag = True
                        break
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            if flag == False:
                print 'get all index failed!'

            #remove index
            idx_name = 'nameIndex'
            try:
                cl.drop_index(idx_name)
            except Exception as e:
                print 'drop index error'

        except Exception as e:
            print e
        finally:
            cl.delete()
Exemple #3
0
def query( cl ):   
   print '---begin to query'
   
   condition = {'age':{'$gt':96}}
   selector = {'age':'','name':''}
   order = {'age':-1}
   cursor = cl.query(condition=condition,selector=selector,order_by=order)
     
   i = 0      
   ageVal = 99
   while True:
      try:
         record = cursor.next()
         if( record['age'] != ageVal ):
            print 'exeute: cl.query, condition=%s, selector=%s, order_by=%s' % (condition, selector, order)     
            print 'expect: {age:%d, name:"mike"%d}, actual: %s' % ( ageVal, ageVal, record )
            raise  Exception( 'CHECK_ERROR' )
         ageVal = ageVal - 1 
         i = i + 1
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( i != 3 ):
      print 'exeute: cl.query, condition=%s, selector=%s, order_by=%s' % (condition, selector, order)
      print 'return record number, expect: 3, actual: %d' % ( i )
      raise  Exception( 'COUNT_ERROR' ) 
Exemple #4
0
def aggregate( cl ): 
   print '---begin to aggregate'
   match = SON({'$match':{'no':{'$exists':1}}})
   group = SON({'$group':{'_id':'$major','avg_score':{'$avg':'$score'},'major':{'$first':'$major'}}})
   sort  = SON({'$sort':{'avg_score':1}})
   skip  = {'$skip':1}
   limit = {'$limit':1}
   option = [ match, group, sort, skip, limit ]
   cursor = cl.aggregate( option )
   
   i = 0
   while True:
      try:
         rec = cursor.next()
         i = i + 1
         
         # check field value
         if ( rec['major'] != 'Math' or rec['avg_score'] != 75.0 ):
            print 'expect: {major:"Math", avg_score:75.0}, actual: %s' % ( rec )
            raise  Exception( 'CHECK_FIELD_VALUE_ERROR' )
            
         # check field number
         fieldNum = len(rec)
         if ( fieldNum != 2 ):
            print 'expect: {major:"Math", avg_score:75.0}, actual: %s' % ( rec )
            raise  Exception( 'CHECK_FIELD_NUMBER_ERROR' )   
      except SDBEndOfCursor:
         break
      except ( Exception ), e:
         raise e
Exemple #5
0
def query(cl):
    print '---begin to query'

    condition = {'age': {'$gt': 96}}
    selector = {'age': '', 'name': ''}
    order = {'age': -1}
    cursor = cl.query(condition=condition, selector=selector, order_by=order)

    i = 0
    ageVal = 99
    while True:
        try:
            record = cursor.next()
            if (record['age'] != ageVal):
                print 'exeute: cl.query, condition=%s, selector=%s, order_by=%s' % (
                    condition, selector, order)
                print 'expect: {age:%d, name:"mike"%d}, actual: %s' % (
                    ageVal, ageVal, record)
                raise Exception('CHECK_ERROR')
            ageVal = ageVal - 1
            i = i + 1
        except SDBEndOfCursor:
            break
        except SDBBaseError:
            raise e
    if (i != 3):
        print 'exeute: cl.query, condition=%s, selector=%s, order_by=%s' % (
            condition, selector, order)
        print 'return record number, expect: 3, actual: %d' % (i)
        raise Exception('COUNT_ERROR')
Exemple #6
0
def findAndUpdate(cl):
    print '---begin to findAndUpdate'
    cursor = cl.query_and_update({"$set": {
        "item": "updated_item"
    }}, {"_id": 1},
                                 return_new=True)

    # check return value
    i = 0
    while True:
        try:
            record = cursor.next()
            if ("updated_item" != record['item'] or 1 != record['_id']):
                print 'return value, expect: {_id:1,item:"updated_item"}, atual: '
                print record
                raise Exception('CHECK_ERROR')
            i = i + 1
        except SDBEndOfCursor:
            break
        except SDBBaseError:
            raise e
    if (1 != i):
        print 'return value, expect: 1 record, atual: %d record' % (i)
        raise Exception('CHECK_ERROR')
    cursor.close()
Exemple #7
0
   def test_query_sort(self):
      try:
         for i in range(1,100):
            cl.insert({'age':i,'name':'mike'+str(i)})

         gt = {'$gt':96}
         condition = {'age':gt}
         selector = {'age':'','name':''}
         order = {'age':-1}
         cursor = cl.query(condition=condition,selector=selector,order_by=order)
         while True:
            try:
               record = cursor.next()
               print record['age']
               if record['age'] != 99:
                  print 'query sort error!'
               break
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise

      except Exception as e:
         print  e
      finally:
         cl.delete()
    def test_query_sort(self):
        try:
            for i in range(1, 100):
                cl.insert({'age': i, 'name': 'mike' + str(i)})

            gt = {'$gt': 96}
            condition = {'age': gt}
            selector = {'age': '', 'name': ''}
            order = {'age': -1}
            cursor = cl.query(condition=condition,
                              selector=selector,
                              order_by=order)
            while True:
                try:
                    record = cursor.next()
                    print record['age']
                    if record['age'] != 99:
                        print 'query sort error!'
                    break
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

        except Exception as e:
            print e
        finally:
            cl.delete()
    def test_list(self):
        try:
            try:
                cursor = sdb.get_list(0)
            except Exception as e:
                print 'list (0) error'
            temp = {'$gt': 1}
            query = {'SessionID': temp}
            select = {'Contexts': 'aa'}
            order = {'SessionID': 1}
            #list by options
            cursor = sdb.get_list(0,
                                  condition=query,
                                  selector=select,
                                  order_by=order)
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    print record['Contexts']
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #list has no options
            try:
                cursor = sdb.get_list(4)
            except Exception as e:
                print 'list has no options error'
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    print record['Name']
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise
        except Exception as e:
            print e
        finally:
            cl.delete()
Exemple #10
0
def backup( dataRGName, backupName ):   
   print '---begin to backup data group: %s' % (dataRGName)   
   option = {'GroupName': dataRGName, 'Name': backupName, 'OverWrite': True}
   db.backup_offline( option )

   print '---begin to list backup'
   option = {'Name': backupName}
   cursor = db.list_backup( option )
   i = 0        
   while True:
      try:
         record = cursor.next()
         i = i + 1
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( i != 1 ):
      print 'exeute: list_backup( %d )' % ( option )
      print 'return record number, expect: 1, actual: %d' % ( i )
      raise  Exception( 'COUNT_ERROR' )     
   
   print '---begin to remove backup: %s' % (backupName)
   db.remove_backup( {'Name': backupName} )
   
   option = {'Name': backupName}
   cursor = db.list_backup( option )
   i = 0        
   while True:
      try:
         record = cursor.next()
         i = i + 1
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( i != 0 ):
      print 'exeute: remove_backup, list_backup( %s )' % ( option )
      print 'return record number, expect: 0, actual: %d' % ( i )
      raise  Exception( 'CHECK_ERROR' )      
Exemple #11
0
   def test_list(self):
      try:
         try:
            cursor = sdb.get_list(0)
         except Exception as e:
            print 'list (0) error'
         temp = {'$gt':1}
         query = {'SessionID':temp}
         select = {'Contexts':'aa'}
         order = {'SessionID':1}
         #list by options
         cursor = sdb.get_list(0,condition=query,selector=select,order_by=order)
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               print record['Contexts']
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise

         #list has no options
         try:
            cursor = sdb.get_list(4)
         except Exception as e:
            print 'list has no options error'
         #rc,record = cursor.next()
         while True :
            try :
               record = cursor.next()
               print record['Name']
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise
      except Exception as e:
         print  e
      finally:
         cl.delete()
    def test_snapshot(self):
        try:
            try:
                cursor = sdb.get_snapshot(0)
            except Exception as e:
                print 'get snapshot(0) error'
            query = {'SessionID': {'$ne': 1}}
            select = {'SessionID': 1}
            order = {'SessionID': 1}
            #snapshot by options
            cursor = sdb.get_snapshot(0,
                                      condition=query,
                                      selector=select,
                                      order_by=order)
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    print record
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #snapshot has no options
            cursor = sdb.get_snapshot(5)
            rc = const.SDB_OK
            while True:
                try:
                    record = cursor.next()
                    print record['PageSize']
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise
        except Exception as e:
            print e
        finally:
            cl.delete()
Exemple #13
0
   def test_snapshot(self):
      try:
         try:
            cursor = sdb.get_snapshot(0)
         except Exception as e:
            print 'get snapshot(0) error'
         query = {'SessionID':{'$ne':1}}
         select = {'SessionID':1}
         order = {'SessionID':1}
         #snapshot by options 
         cursor = sdb.get_snapshot(0,condition=query,selector=select,order_by=order)
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               print record
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise


         #snapshot has no options
         cursor = sdb.get_snapshot(5)
         rc = const.SDB_OK
         while True:
            try:
               record = cursor.next()
               print record['PageSize']
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise
      except Exception as e:
         print  e
      finally:
         cl.delete()
Exemple #14
0
def snapshot_5( cl ):   
   print '---begin to snapshot(5)'
   cond = {'Name': cs_name }
   cursor = db.get_snapshot( 5, condition=cond )
    
   i = 0        
   while True:
      try:
         record = cursor.next()
         i = i + 1
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( i != 1 ):
      print 'exeute: db.get_snapshot( 5, condition=%s )' % ( cond )
      print 'return record number, expect: 1, actual: %d' % ( i )
      raise  Exception( 'COUNT_ERROR' ) 
Exemple #15
0
def get_list( cs_name, cl_name ):   
   print '---begin to list(4)'
   cond = {'Name': cs_name + '.' + cl_name }
   cursor = db.get_list( 4, condition=cond )
    
   i = 0        
   while True:
      try:
         record = cursor.next()
         i = i + 1
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( i != 1 ):
      print 'exeute: db.get_list( 4, condition=%s )' % ( cond )
      print 'return record number, expect: 1, actual: %d' % ( i )
      raise  Exception( 'COUNT_ERROR' ) 
    def test_backup(self):
        try:
            #create backup
            option = {
                'GroupName': 'datagroup',
                'Name': 'datagroup_backup',
                'OverWrite': True
            }
            try:
                sdb.backup_offline(option)
            except Exception as e:
                print 'backup_offline error'

            #list backup
            option = {'GroupName': 'datagroup'}
            condition = {'NodeName': {'$ne': 'aa'}}
            selector = {'Name': 1}
            cursor = sdb.list_backup(option,
                                     condition=condition,
                                     selector=selector)
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    if (0 != cmp(record['Name'], 'datagroup_backup')):
                        print 'list backup failed!'
                        break
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #delete backup
            option = {'Name': 'datagroup_backup'}
            try:
                sdb.remove_backup(option)
            except Exception as e:
                print 'remove backup error'

        except Exception as e:
            print e
        finally:
            cl.delete()
Exemple #17
0
def aggregate(cl):
    print '---begin to aggregate'
    match = SON({'$match': {'no': {'$exists': 1}}})
    group = SON({
        '$group': {
            '_id': '$major',
            'avg_score': {
                '$avg': '$score'
            },
            'major': {
                '$first': '$major'
            }
        }
    })
    sort = SON({'$sort': {'avg_score': 1}})
    skip = {'$skip': 1}
    limit = {'$limit': 1}
    option = [match, group, sort, skip, limit]
    cursor = cl.aggregate(option)

    i = 0
    while True:
        try:
            rec = cursor.next()
            i = i + 1

            # check field value
            if (rec['major'] != 'Math' or rec['avg_score'] != 75.0):
                print 'expect: {major:"Math", avg_score:75.0}, actual: %s' % (
                    rec)
                raise Exception('CHECK_FIELD_VALUE_ERROR')

            # check field number
            fieldNum = len(rec)
            if (fieldNum != 2):
                print 'expect: {major:"Math", avg_score:75.0}, actual: %s' % (
                    rec)
                raise Exception('CHECK_FIELD_NUMBER_ERROR')
        except SDBEndOfCursor:
            break
        except (Exception), e:
            raise e
Exemple #18
0
def findAndRemove( cl ):
   print '---begin to findAndRemove'
   cursor = cl.query_and_remove( condition={"item":"updated_item"} )
   
   # check return value
   i = 0
   while True:
      try:
         record = cursor.next()
         if( "updated_item" != record['item'] or 1 != record['_id'] ):
            print 'return value, expect: {_id:1,item:"updated_item"}, atual: '
            print record
            raise Exception( 'CHECK_ERROR' )
         i = i + 1   
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( 1 != i ):
      print 'return value, expect: 1 record, atual: %d record' % (i)
      raise  Exception( 'CHECK_ERROR' )       
   cursor.close()
Exemple #19
0
def query(cl):
    print '---begin to query'

    # check count
    cnt = cl.get_count()
    if (2 != cnt):
        print 'excute: cl.get_count(), expect: 2, actual: %d' % (cnt)
        raise Exception('COUNT_ERROR')

    # check record {name:"kate_new", age:24}
    cond = {'age': {'$et': 24}}
    cursor = cl.query(condition=cond)
    while True:
        try:
            record = cursor.next()
            if (record['name'] != 'kate_new'):
                print 'excute: cl.query(%s), expect: {name:"kate_new", age:24}, actual: %s' % (
                    cond, record)
                raise Exception('RECORD_ERROR')
        except SDBEndOfCursor:
            break
        except (Exception), e:
            raise e
Exemple #20
0
   def test_backup(self):
      try:
         #create backup
         option = {'GroupName':'datagroup','Name':'datagroup_backup','OverWrite':True}
         try:
            sdb.backup_offline(option)
         except Exception as e:
            print 'backup_offline error'

         #list backup
         option = {'GroupName':'datagroup'}
         condition = {'NodeName':{'$ne':'aa'}}
         selector = {'Name':1}
         cursor = sdb.list_backup(option,condition=condition,selector=selector)
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               if(0 != cmp(record['Name'],'datagroup_backup')):
                  print 'list backup failed!'
                  break
            except SDBEndOfCursor :
               break
            except SDBBaseError:
               raise

         #delete backup
         option = {'Name':'datagroup_backup'}
         try:
            sdb.remove_backup(option)
         except Exception as e:
            print 'remove backup error'

      except Exception as e:
         print  e
      finally:
         cl.delete()
Exemple #21
0
   def test_aggregate(self):
      try:
         data1 = {'no':1000,'score':80,'interest':['basketball','football'],'major':'computer th','dep':'computer','info':{'name':'tom','age':25,'gender':'man'}}
         data2 = {'no':1001,'score':90,'interest':['basketball','football'],'major':'computer sc','dep':'computer','info':{'name':'mike','age':24,'gender':'lady'}}
         data3 = {'no':1002,'score':85,'interest':['basketball','football'],'major':'computer en','dep':'computer','info':{'name':'kkk','age':25,'gender':'man'}}
         data4 = {'no':1003,'score':92,'interest':['basketball','football'],'major':'computer en','dep':'computer','info':{'name':'mmm','age':25,'gender':'man'}}
         data5 = {'no':1004,'score':88,'interest':['basketball','football'],'major':'computer sc','dep':'computer','info':{'name':'ttt','age':25,'gender':'man'}}

         cl.insert(data1)
         cl.insert(data2)
         cl.insert(data3)
         cl.insert(data4)
         cl.insert(data5)
         match = SON({'$match':{'interest':{'$exists':1}}})
         group = SON({'$group':{'_id':'$major','avg_age':{'$avg':'$info.age'},'major':{'$first':'$major'}}})
         sort = SON({'$sort':{'avg_age':1, 'major':-1}})
         skip = {'$skip':0}
         limit = {'$limit':5}

         option = [match,group,sort,skip,limit]
         cursor = cl.aggregate(option)
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               print record
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise


      except Exception as e:
         print e
      finally:
         cl.delete()
    def test_replgroup(self):
        try:
            #create replgroup
            try:
                rg = sdb.create_replica_group('test_group')
            except SDBBaseError as e:
                print 'create replicate group error'
                print e.code
                print e.detail

            #get replgroup by name
            try:
                rg = sdb.get_replica_group_by_name('test_group')
            except SDBBaseError as e:
                print e.code
                print e.detail
                print 'get replica group by name error'

            query = {'GroupName': {'$et': 'test_group'}}
            select = {'GroupID': 1}
            order = {'GroupID': 1}
            rg = sdb.get_list(7, condition=query, selector=select)
            #rc,record = rg.next()
            while True:
                try:
                    record = rg.next()
                    groupid = record['GroupID']
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #list replgroup
            cursor = sdb.list_replica_groups()
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    print record['GroupID']
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #get replgroup by id
            try:
                print 'groupid=' + str(groupid)
                rg = sdb.get_replica_group_by_id(groupid)
            except SDBBaseError as e:
                print e.code
                print e.detail
                print 'get replgroup by id  error'

            #create node
            try:
                rg.create_node(hostname, '18000',
                               '/opt/trunk/database/data/18000')
                print 'create 18000 end'
                rg.create_node(hostname, '19000',
                               '/opt/trunk/database/data/19000')
                print 'create 19000 end'
            except SDBBaseError as e:
                print e.code
                print e.detail
                print 'create node error'

            #start group
            try:
                rg.start()
                #print 'aa'
            except Exception as e:
                print 'start group error'

            #time.sleep(10)

            #stop group
            try:
                rg.stop()
            except SDBBaseError as e:
                print e.code
                print e.detail
                print 'stop group error'

            #remove node
            try:
                rg.remove_node(hostname, '18000')
            except Exception as e:
                print 'remove node error.'

            #remove replgroup
            try:
                sdb.remove_replica_group('test_group')
            except Exception as e:
                print 'remove replica group error'

        except Exception as e:
            print e
        finally:
            cl.delete()
Exemple #23
0
def procedure():
    print '---begin to create procedure'
    code = 'function sum(x,y){return x+y;}'
    db.create_procedure(code)

    print '---begin to list procedure'
    cond = {'name': 'sum'}
    cursor = db.list_procedures(condition=cond)

    i = 0
    while True:
        try:
            record = cursor.next()
            i = i + 1
        except SDBEndOfCursor:
            break
        except SDBBaseError:
            raise e
    if (i != 1):
        print 'exeute: db.list_procedures( condition=%s )' % (cond)
        print 'return record number, expect: 1, actual: %d' % (i)
        raise Exception('COUNT_ERROR')

    print '---begin to exec procedure'
    cursor = db.eval_procedure('sum(1,2)')

    i = 0
    while True:
        try:
            result = cursor.next()
            if (result['value'] != 3):
                print 'exeute: eval_procedure("sum(1,2)")'
                print 'expect: 3, actual: %d' % (result['value'])
                raise Exception('RESULT_ERROR')
            i = i + 1
        except SDBEndOfCursor:
            break
        except SDBBaseError:
            raise e
    if (i != 1):
        print 'exeute: db.list_procedures( condition=%s )' % (cond)
        print 'return record number, expect: 1, actual: %d' % (i)
        raise Exception('COUNT_ERROR')

    print '---begin to remove procedure'
    db.remove_procedure('sum')

    cond = {'name': 'sum'}
    cursor = db.list_procedures(condition=cond)
    i = 0
    while True:
        try:
            record = cursor.next()
            i = i + 1
        except SDBEndOfCursor:
            break
        except SDBBaseError:
            raise e
    if (i != 0):
        print 'exeute: remove, list_procedures( condition=%s )' % (cond)
        print 'return record number, expect: 0, actual: %d' % (i)
        raise Exception('COUNT_ERROR')
    def test_partition(self):
        try:
            #range partition
            cursor = sdb.get_snapshot(
                4, condition={'Name': {
                    '$et': cs_global + '.' + range_name
                }})
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    if (0 == cmp(record['Details'][0]['GroupName'],
                                 'datagroup')):
                        src = 'datagroup'
                        dst = 'datagroup1'
                        break
                    else:
                        src = 'datagroup1'
                        dst = 'datagroup'
                        break
                #rc,record = cursor.next()
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise
            print 'src=' + src + ',dst=' + dst

            for i in range(0, 100):
                range_cl.insert({'age': i, 'name': 'mike' + str(i)})
            range_cl.split_by_condition(src, dst, {'age': 10}, {'age': 60})
            time.sleep(3)

            temp_sdb = client(hostname, data_port)
            temp_cs = temp_sdb.get_collection_space(cs_global)
            temp_cl = temp_cs.get_collection(range_name)
            count = temp_cl.get_count()
            print 'count=' + count.__str__()
            if count != 50:
                print 'range partion failed!'

            #hash partition
            cursor = sdb.get_snapshot(
                4, condition={'Name': {
                    '$et': cs_global + '.' + hash_name
                }})
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    if (0 == cmp(record['Details'][0]['GroupName'],
                                 'datagroup')):
                        src = 'datagroup'
                        dst = 'datagroup1'
                        break
                    else:
                        src = 'datagroup1'
                        dst = 'datagroup'
                        break
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise
            print 'src=' + src + ',dst=' + dst
            for i in range(0, 100):
                hash_cl.insert({'age': i, 'name': 'mike' + str(i)})
            hash_cl.split_by_condition(src, dst, {'Partition': 4},
                                       {'Partition': 8})
            temp_cl = temp_cs.get_collection(hash_name)
            count = temp_cl.get_count()
            print 'count=' + count.__str__()
            if count != 57 and count != 43:
                print 'hash partion failed!'

            #precent
            cursor = sdb.get_snapshot(
                4, condition={'Name': {
                    '$et': cs_global + '.' + percent_name
                }})
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    if (0 == cmp(record['Details'][0]['GroupName'],
                                 'datagroup')):
                        src = 'datagroup'
                        dst = 'datagroup1'
                        break
                    else:
                        src = 'datagroup1'
                        dst = 'datagroup'
                        break
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise
            print 'src=' + src + ',dst=' + dst
            for i in range(0, 100):
                percent_cl.insert({'age': i, 'name': 'mike' + str(i)})
            percent_cl.split_by_percent(src, dst, 50.0)

            temp_cl = temp_cs.get_collection(percent_name)
            count = temp_cl.get_count()
            print 'count=' + count.__str__()
            if count != 50:
                print 'percent partion failed!'

        except Exception as e:
            print e
        finally:
            range_cl.delete()
            hash_cl.delete()
            percent_cl.delete()
Exemple #25
0
   def test_cs_cl(self):
      try:
         #create a repeat name cs
         try:
            test_cs = sdb.create_collection_space(cs_global)
         except Exception as e:
            print 'create repeat name cs'

         #create cs name include '$'
         cs_name = '$hello'
         try:
            test_cs = sdb.create_collection_space(cs_name)
         except Exception as e:
            print 'cs name include "$" '

         #create cs name's length > 128
         cs_name ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
         try:
            test_cs = sdb.create_collection_space(cs_name)
         except Exception as e:
            print "cs name's length greater than 128"

         #get one not exist cs
         try:
            test_cs = sdb.get_collection_space('not_exist_cs')
         except Exception as e:
            print 'get a not exist cs'

         #get one not exist cl
         try:
            test_cl = cs.get_collection('not_exist_cl')
         except Exception as e:
            print 'get a not exist cl'

         #list all cs
         cursor = sdb.list_collection_spaces()
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               print record['Name']
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise

         #list all cl
         cursor = sdb.list_collections()
         while True:
            try:
               record = cursor.next()
               print record['Name']
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise

      except SDBBaseError as e:
         print e.code
         print e.detail
      finally:
         cl.delete()
    def test_procedure(self):
        try:

            #create normal procedure
            code = 'function sum(x,y){return x+y;}'
            sdb.create_procedure(code)

            #create abnormal procedure
            code = 'func sum(x,y){return x+z;}'
            try:
                sdb.create_procedure(code)
            except Exception as e:
                print 'create a error procedure'

            #create repeat name procedure
            code = 'function sum(x,y){return x+y;}'
            try:
                sdb.create_procedure(code)
            except Exception as e:
                print 'create repeat procedure'

            #list procedure
            condition = {'name': 'sum'}
            cursor = sdb.list_procedures(condition=condition)
            flag = False
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    print record['name']
                    if (0 == cmp(record['name'], 'sum')):
                        flag = True
                        break
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            if flag == False:
                print 'list procedure failed!'

            #exec normal procedure
            result = sdb.eval_procedure('sum(1,2)')
            #rc,record = result.next()
            while True:
                #print record['value']
                try:
                    record = result.next()
                    if record['value'] != 3:
                        print 'eval procedure failed!'
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise
                #break

            #print 'world'

            #exec abnormal procedure
            try:
                result = sdb.eval_procedure('no_exist(1,2)')
            except Exception as e:
                print 'eval not exist procedure '

            #del normal procedure
            sdb.remove_procedure('sum')

            #del abnormal procedure
            try:
                sdb.remove_procedure('not_exist')
            except Exception as e:
                print 'remove not exist procedure'

        except SDBBaseError as e:
            print e.code
            print e.detail
        finally:
            cl.delete()
    def test_cs_cl(self):
        try:
            #create a repeat name cs
            try:
                test_cs = sdb.create_collection_space(cs_global)
            except Exception as e:
                print 'create repeat name cs'

            #create cs name include '$'
            cs_name = '$hello'
            try:
                test_cs = sdb.create_collection_space(cs_name)
            except Exception as e:
                print 'cs name include "$" '

            #create cs name's length > 128
            cs_name = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
            try:
                test_cs = sdb.create_collection_space(cs_name)
            except Exception as e:
                print "cs name's length greater than 128"

            #get one not exist cs
            try:
                test_cs = sdb.get_collection_space('not_exist_cs')
            except Exception as e:
                print 'get a not exist cs'

            #get one not exist cl
            try:
                test_cl = cs.get_collection('not_exist_cl')
            except Exception as e:
                print 'get a not exist cl'

            #list all cs
            cursor = sdb.list_collection_spaces()
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    print record['Name']
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #list all cl
            cursor = sdb.list_collections()
            while True:
                try:
                    record = cursor.next()
                    print record['Name']
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

        except SDBBaseError as e:
            print e.code
            print e.detail
        finally:
            cl.delete()
Exemple #28
0
   def test_procedure(self):
      try:

         #create normal procedure
         code = 'function sum(x,y){return x+y;}'
         sdb.create_procedure(code)

         #create abnormal procedure
         code = 'func sum(x,y){return x+z;}'
         try:
            sdb.create_procedure(code)
         except Exception as e:
            print 'create a error procedure'

         #create repeat name procedure
         code = 'function sum(x,y){return x+y;}'
         try:
            sdb.create_procedure(code)
         except Exception as e:
            print 'create repeat procedure'

         #list procedure
         condition = {'name':'sum'}
         cursor = sdb.list_procedures(condition=condition)
         flag = False
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               print record['name']
               if ( 0 == cmp(record['name'],'sum')):
                  flag = True
                  break
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise

         if flag == False:
            print 'list procedure failed!'


         #exec normal procedure
         result = sdb.eval_procedure('sum(1,2)')
         #rc,record = result.next()
         while True:
            #print record['value']
            try:
               record = result.next()
               if record['value'] != 3:
                  print 'eval procedure failed!'
            except SDBEndOfCursor:
               break
            except SDBBaseError:
               raise
            #break

         #print 'world'

         #exec abnormal procedure
         try:
            result = sdb.eval_procedure('no_exist(1,2)')
         except Exception as e:
            print 'eval not exist procedure '

         #del normal procedure
         sdb.remove_procedure('sum')

         #del abnormal procedure
         try:
            sdb.remove_procedure('not_exist')
         except Exception as e:
            print 'remove not exist procedure'

      except SDBBaseError as e:
         print e.code
         print e.detail
      finally:
         cl.delete()
    def test_connection(self):
        sdb = client(hostname, service)
        cs = None
        if not sdb.is_valid():
            print 'create connection failed!'
            return

        hosts = [
            {
                'host': '192.168.10.30',
                'service': 11200,
            },
            {
                'host': '192.168.10.30',
                'service': 11200,
            },
            {
                'host': hostname,
                'service': service,
            },
        ]

        sdb.connect_to_hosts(hosts, user="", password="", policy="random")

        sdb.connect(hostname, service, user="", password="")

        try:
            cs = sdb.create_collection_space(cs_name, 0)
            test_cs = sdb.get_collection_space(cs_name)
            if (0 != cmp(cs.get_collection_space_name(),
                         test_cs.get_collection_space_name())):
                print 'get_collection_space failed!'
                return
            cl = cs.create_collection(cl_name)
            test_cl = cs.get_collection(cl_name)
            if (0 != cmp(cl.get_collection_name(),
                         test_cl.get_collection_name())):
                print 'get_collection failed!'
                return

            #insert data
            data1 = {'age': 1, 'name': 'tom'}
            cl.insert(data1)
            time.sleep(0.5)

            insert_sql = 'insert into ' + cs_name + '.' + cl_name + '(age,name) values(24,\'kate\')'
            sdb.exec_update(insert_sql)
            time.sleep(0.5)

            #update data
            cs = sdb.get_collection_space(cs_name)
            cl = cs.get_collection(cl_name)

            update_sql = 'update ' + cs_name + '.' + cl_name + ' set name = \'tom_new\' where age = 1'
            sdb.exec_update(update_sql)
            rule = {'$set': {'name': 'kate_new'}}
            cond = {'age': {'$et': 24}}
            cl.update(rule, condition=cond)
            time.sleep(0.5)

            #search data
            cursor = cl.query(condition=cond)
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    if (0 != cmp(record['name'], 'kate_new')):
                        print 'search data failed1!'
                        return
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            select_sql = 'select name from ' + cs_name + '.' + cl_name + ' where age=1'
            cursor = sdb.exec_sql(select_sql)
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    if (0 != cmp(record['name'], 'tom_new')):
                        print 'search data failed2!'
                        return

                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #delete data
            delete_sql = 'delete from ' + cs_name + '.' + cl_name + ' where age=1'
            sdb.exec_update(delete_sql)
            cl.delete(condition=cond)
            time.sleep(0.5)

            count = cl.get_count()
            if 0 != count:
                print 'delete data failed!'
                return

        except Exception as e:
            print e
        finally:
            sdb.drop_collection_space(cs_name)
            time.sleep(0.5)
            if sdb.is_valid():
                sdb.disconnect()
            time.sleep(1)
            if sdb.is_valid():
                print 'disconnect is failed!'
    def test_transaction(self):
        try:
            """
         rc,cl = cs.get_collection(cl_name)
         option = {'ReplSize':2}
         if rc != const.SDB_OK:
            rc = const.SDB_OK
            rc,cl = cs.create_collection(cl_name,option)
            time.sleep(1)
         """
            data1 = {'name': 'mike', 'score': 98}

            #insert transaction
            sdb.transaction_begin()
            cl.insert(data1)
            sdb.transaction_commit()
            time.sleep(0.1)
            count = cl.get_count()
            #print count.__str__()
            if 1 != count:
                print 'insert data failed!'
                return

            #update transaction
            rule = {'$set': {'score': 60}}
            cond = {'name': {'$et': 'mike'}}
            sdb.transaction_begin()
            cl.update(rule, condition=cond)
            sdb.transaction_commit()
            time.sleep(0.1)
            cursor = cl.query(condition=cond)
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    if 60 != record['score']:
                        print 'update data failed!'
                        return
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

            #delete transaction rollback
            sdb.transaction_begin()
            cl.delete(condition=cond)
            sdb.transaction_rollback()
            time.sleep(0.1)
            count = cl.get_count()
            if 1 != count:
                print 'rollback failed!'
                return

            #delete transaction
            sdb.transaction_begin()
            cl.delete(confition=cond)
            sdb.transaction_commit()
            time.sleep(0.1)
            count = cl.get_count()
            if 0 != count:
                print 'delete data failed!'
                return

        except SDBBaseError as e:
            print e.code
            print e.detail
        finally:
            cl.delete()
Exemple #31
0
   def test_replgroup(self):
      try:
         #create replgroup
         try:
            rg = sdb.create_replica_group('test_group')
         except SDBBaseError as e:
            print 'create replicate group error'
            print e.code
            print e.detail

         #get replgroup by name
         try:
            rg = sdb.get_replica_group_by_name('test_group')
         except SDBBaseError as e:
            print e.code
            print e.detail
            print 'get replica group by name error'

         query = {'GroupName':{'$et':'test_group'}}
         select = {'GroupID':1}
         order = {'GroupID':1}
         rg = sdb.get_list(7,condition=query,selector=select)
         #rc,record = rg.next()
         while True:
            try:
               record = rg.next()
               groupid = record['GroupID']
            except SDBEndOfCursor :
               break
            except SDBBaseError:
               raise

         #list replgroup
         cursor = sdb.list_replica_groups()
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               print record['GroupID']
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise

         #get replgroup by id
         try:
            print 'groupid='+str(groupid)
            rg = sdb.get_replica_group_by_id(groupid)
         except SDBBaseError as e:
            print e.code
            print e.detail
            print 'get replgroup by id  error'

         #create node
         try:
            rg.create_node(hostname,'18000','/opt/trunk/database/data/18000')
            print 'create 18000 end'
            rg.create_node(hostname,'19000','/opt/trunk/database/data/19000')
            print 'create 19000 end'
         except SDBBaseError as e:
            print e.code
            print e.detail
            print 'create node error'

         #start group
         try:
            rg.start()
            #print 'aa'
         except Exception as e:
            print 'start group error'


         #time.sleep(10)

         #stop group
         try:
            rg.stop()
         except SDBBaseError as e:
            print e.code
            print e.detail
            print 'stop group error'


         #remove node
         try:
            rg.remove_node(hostname,'18000')
         except Exception as e:
            print 'remove node error.'

         #remove replgroup
         try:
            sdb.remove_replica_group('test_group')
         except Exception as e:
            print 'remove replica group error'

      except Exception as e:
         print  e
      finally:
         cl.delete()
Exemple #32
0
         else:
            raise e
      if( hasErr33 == False ):
         print 'expect: return error -33, actual: return no error'
         raise  Exception( 'RETURN_CODE_ERROR' )
         
      print '---begin to create cl and get cl'     
      cs.create_collection( cl_name )
      cl = cs.get_collection( cl_name )
      
      print '---begin to list cs'
      cursor = db.list_collection_spaces()
      hasThisCS = False
      while True:
         try:
            record = cursor.next()
            if ( record['Name'] == cs_name ):
               hasThisCS = True
         except SDBEndOfCursor:
            break
         except ( Exception ), e:
            raise e
      if( hasThisCS != True ):
         print 'excute: db.list_collection_spaces(), expect: has cs %s, actual: has not' % ( cs_name )
         raise  Exception( 'CHECK_ERROR' )

      print '---begin to list cl'
      cl_full_name = cs_name + '.' + cl_name 
      cursor = db.list_collections()
      hasThisCL = False
      while True:
Exemple #33
0
def procedure():     
   print '---begin to create procedure'
   code = 'function sum(x,y){return x+y;}'
   db.create_procedure( code )
   
   print '---begin to list procedure'
   cond = {'name': 'sum'}
   cursor = db.list_procedures( condition=cond )
   
   i = 0        
   while True:
      try:
         record = cursor.next()
         i = i + 1
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( i != 1 ):
      print 'exeute: db.list_procedures( condition=%s )' % ( cond )
      print 'return record number, expect: 1, actual: %d' % ( i )
      raise  Exception( 'COUNT_ERROR' ) 
   
   print '---begin to exec procedure'
   cursor = db.eval_procedure( 'sum(1,2)' )
   
   i = 0        
   while True:
      try:
         result = cursor.next()
         if( result['value'] != 3 ):
            print 'exeute: eval_procedure("sum(1,2)")'
            print 'expect: 3, actual: %d' % ( result['value'] )
            raise  Exception( 'RESULT_ERROR' )
         i = i + 1
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( i != 1 ):
      print 'exeute: db.list_procedures( condition=%s )' % ( cond )
      print 'return record number, expect: 1, actual: %d' % ( i )
      raise  Exception( 'COUNT_ERROR' ) 
    
   print '---begin to remove procedure'
   db.remove_procedure('sum')
   
   cond = {'name': 'sum'}
   cursor = db.list_procedures( condition=cond )  
   i = 0        
   while True:
      try:
         record = cursor.next()
         i = i + 1
      except SDBEndOfCursor :
         break
      except SDBBaseError :
         raise e
   if( i != 0 ):
      print 'exeute: remove, list_procedures( condition=%s )' % ( cond )
      print 'return record number, expect: 0, actual: %d' % ( i )
      raise  Exception( 'COUNT_ERROR' )     
Exemple #34
0
   def test_partition(self):
      try:
         #range partition
         cursor = sdb.get_snapshot(4,condition={'Name':{'$et':cs_global+'.'+range_name}})
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               if( 0 == cmp( record['Details'][0]['GroupName'],'datagroup')):
                  src = 'datagroup'
                  dst = 'datagroup1'
                  break
               else:
                  src = 'datagroup1'
                  dst = 'datagroup'
                  break
            #rc,record = cursor.next()
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise
         print 'src='+src+',dst='+dst

         for i in range(0,100):
            range_cl.insert({'age':i,'name':'mike'+str(i)})
         range_cl.split_by_condition(src,dst,{'age':10},{'age':60})
         time.sleep(3)

         temp_sdb = client(hostname,data_port)
         temp_cs = temp_sdb.get_collection_space(cs_global)
         temp_cl = temp_cs.get_collection(range_name)
         count = temp_cl.get_count()
         print 'count='+count.__str__()
         if count != 50:
            print 'range partion failed!'

         #hash partition
         cursor = sdb.get_snapshot(4,condition={'Name':{'$et':cs_global+'.'+hash_name}})
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               if( 0 == cmp( record['Details'][0]['GroupName'],'datagroup')):
                  src = 'datagroup'
                  dst = 'datagroup1'
                  break
               else:
                  src = 'datagroup1'
                  dst = 'datagroup'
                  break
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise
         print 'src='+src+',dst='+dst
         for i in range(0,100):
            hash_cl.insert({'age':i,'name':'mike'+str(i)})
         hash_cl.split_by_condition(src,dst,{'Partition':4},{'Partition':8})
         temp_cl = temp_cs.get_collection(hash_name)
         count = temp_cl.get_count()
         print 'count='+count.__str__()
         if count != 57 and count != 43:
            print 'hash partion failed!'

         #precent
         cursor = sdb.get_snapshot(4,condition={'Name':{'$et':cs_global+'.'+percent_name}})
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               if( 0 == cmp( record['Details'][0]['GroupName'],'datagroup')):
                  src = 'datagroup'
                  dst = 'datagroup1'
                  break
               else:
                  src = 'datagroup1'
                  dst = 'datagroup'
                  break
            except SDBEndOfCursor :
                  break
            except SDBBaseError :
                  raise
         print 'src='+src+',dst='+dst
         for i in range(0,100):
            percent_cl.insert({'age':i,'name':'mike'+str(i)})
         percent_cl.split_by_percent(src,dst,50.0)

         temp_cl = temp_cs.get_collection(percent_name)
         count = temp_cl.get_count()
         print 'count='+count.__str__()
         if count != 50:
            print 'percent partion failed!'

      except Exception as e:
         print  e
      finally:
         range_cl.delete()
         hash_cl.delete()
         percent_cl.delete()
Exemple #35
0
   def test_transaction(self):
      try:
         """
         rc,cl = cs.get_collection(cl_name)
         option = {'ReplSize':2}
         if rc != const.SDB_OK:
            rc = const.SDB_OK
            rc,cl = cs.create_collection(cl_name,option)
            time.sleep(1)
         """
         data1 = {'name':'mike','score':98}

         #insert transaction
         sdb.transaction_begin()
         cl.insert(data1)
         sdb.transaction_commit()
         time.sleep(0.1)
         count = cl.get_count()
         #print count.__str__()
         if 1 != count:
            print 'insert data failed!'
            return

         #update transaction
         rule = {'$set':{'score':60}}
         cond = {'name':{'$et':'mike'}}
         sdb.transaction_begin()
         cl.update(rule,condition = cond)
         sdb.transaction_commit()
         time.sleep(0.1)
         cursor = cl.query(condition=cond)
         #rc,record = cursor.next()
         while True :
            try:
               record = cursor.next()
               if 60 != record['score']:
                  print 'update data failed!'
                  return
            except SDBEndOfCursor :
                  break
            except SDBBaseError :
                  raise


         #delete transaction rollback
         sdb.transaction_begin()
         cl.delete(condition=cond)
         sdb.transaction_rollback()
         time.sleep(0.1)
         count = cl.get_count()
         if 1!= count:
            print 'rollback failed!'
            return

         #delete transaction
         sdb.transaction_begin()
         cl.delete(confition=cond)
         sdb.transaction_commit()
         time.sleep(0.1)
         count = cl.get_count()
         if 0 != count:
            print 'delete data failed!'
            return

      except SDBBaseError as e:
         print e.code
         print e.detail
      finally:
         cl.delete()
    def test_aggregate(self):
        try:
            data1 = {
                'no': 1000,
                'score': 80,
                'interest': ['basketball', 'football'],
                'major': 'computer th',
                'dep': 'computer',
                'info': {
                    'name': 'tom',
                    'age': 25,
                    'gender': 'man'
                }
            }
            data2 = {
                'no': 1001,
                'score': 90,
                'interest': ['basketball', 'football'],
                'major': 'computer sc',
                'dep': 'computer',
                'info': {
                    'name': 'mike',
                    'age': 24,
                    'gender': 'lady'
                }
            }
            data3 = {
                'no': 1002,
                'score': 85,
                'interest': ['basketball', 'football'],
                'major': 'computer en',
                'dep': 'computer',
                'info': {
                    'name': 'kkk',
                    'age': 25,
                    'gender': 'man'
                }
            }
            data4 = {
                'no': 1003,
                'score': 92,
                'interest': ['basketball', 'football'],
                'major': 'computer en',
                'dep': 'computer',
                'info': {
                    'name': 'mmm',
                    'age': 25,
                    'gender': 'man'
                }
            }
            data5 = {
                'no': 1004,
                'score': 88,
                'interest': ['basketball', 'football'],
                'major': 'computer sc',
                'dep': 'computer',
                'info': {
                    'name': 'ttt',
                    'age': 25,
                    'gender': 'man'
                }
            }

            cl.insert(data1)
            cl.insert(data2)
            cl.insert(data3)
            cl.insert(data4)
            cl.insert(data5)
            match = SON({'$match': {'interest': {'$exists': 1}}})
            group = SON({
                '$group': {
                    '_id': '$major',
                    'avg_age': {
                        '$avg': '$info.age'
                    },
                    'major': {
                        '$first': '$major'
                    }
                }
            })
            sort = SON({'$sort': {'avg_age': 1, 'major': -1}})
            skip = {'$skip': 0}
            limit = {'$limit': 5}

            option = [match, group, sort, skip, limit]
            cursor = cl.aggregate(option)
            #rc,record = cursor.next()
            while True:
                try:
                    record = cursor.next()
                    print record
                except SDBEndOfCursor:
                    break
                except SDBBaseError:
                    raise

        except Exception as e:
            print e
        finally:
            cl.delete()
Exemple #37
0
   def test_connection(self):
      sdb = client(hostname,service)
      cs = None
      if not sdb.is_valid():
         print 'create connection failed!'
         return


      hosts = [{'host':'192.168.10.30','service':11200,},
               {'host':'192.168.10.30','service':11200,},
               {'host':hostname,'service':service,},]

      sdb.connect_to_hosts(hosts,user="",password="",policy="random")


      sdb.connect(hostname,service,user="",password="")


      try:
         cs = sdb.create_collection_space(cs_name,0)
         test_cs = sdb.get_collection_space(cs_name)
         if(0!=cmp(cs.get_collection_space_name(),test_cs.get_collection_space_name())):
            print 'get_collection_space failed!'
            return
         cl = cs.create_collection(cl_name)
         test_cl = cs.get_collection(cl_name)
         if(0 !=cmp(cl.get_collection_name(),test_cl.get_collection_name())):
            print 'get_collection failed!'
            return

         #insert data
         data1 = {'age':1,'name':'tom'}
         cl.insert(data1)
         time.sleep(0.5)

         insert_sql = 'insert into '+cs_name+'.'+cl_name+'(age,name) values(24,\'kate\')'
         sdb.exec_update(insert_sql)
         time.sleep(0.5)

         #update data
         cs = sdb.get_collection_space(cs_name)
         cl = cs.get_collection(cl_name)

         update_sql = 'update '+cs_name+'.'+cl_name+' set name = \'tom_new\' where age = 1'
         sdb.exec_update(update_sql)
         rule = {'$set':{'name':'kate_new'}}
         cond = {'age':{'$et':24}}
         cl.update(rule,condition=cond)
         time.sleep(0.5)


         #search data
         cursor = cl.query(condition=cond)
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               if (0!=cmp(record['name'],'kate_new')):
                  print 'search data failed1!'
                  return
            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise

         select_sql = 'select name from '+cs_name+'.'+cl_name+' where age=1'
         cursor = sdb.exec_sql(select_sql)
         #rc,record = cursor.next()
         while True:
            try:
               record = cursor.next()
               if (0!=cmp(record['name'],'tom_new')):
                  print 'search data failed2!'
                  return

            except SDBEndOfCursor :
               break
            except SDBBaseError :
               raise


          #delete data
         delete_sql = 'delete from '+cs_name+'.'+cl_name +' where age=1'
         sdb.exec_update(delete_sql)
         cl.delete(condition=cond)
         time.sleep(0.5)

         count = cl.get_count()
         if 0 != count:
            print 'delete data failed!'
            return

      except Exception  as e:
         print e
      finally:
         sdb.drop_collection_space(cs_name)
         time.sleep(0.5)
         if sdb.is_valid():
            sdb.disconnect()
         time.sleep(1)
         if sdb.is_valid():
            print 'disconnect is failed!'