def create_db(self, lib=0, db_path=None): create_sql = "create table person(lastname, firstname);" SQLiteCacheDB.initDB(db_path, None, create_sql, lib=lib, check_version=False, busytimeout=10000)
def read_data(self): db = SQLiteCacheDB.getInstance() one = db.fetchone('select * from person') assert one == ('a','b'), str(one) one = db.fetchone("select lastname from person where firstname == 'b'") assert one == 'a' one = db.fetchone("select lastname from person where firstname == 'c'") assert one == None all = db.fetchall('select * from person') assert len(all) == 101, len(all) num = db.size('person') assert num == 101 db.insert('person', lastname='1', firstname='abc') one = db.fetchone("select firstname from person where lastname == '1'") assert one == '1' or one == 'abc' all = db.fetchall("select firstname from person where lastname == '1'") assert len(all) == 2 db.update('person', "lastname == '2'", firstname='56') one = db.fetchone("select firstname from person where lastname == '2'") assert one == '56', one db.update('person', "lastname == '3'", firstname=65) one = db.fetchone("select firstname from person where lastname == '3'") assert one == 65, one db.update('person', "lastname == '4'", firstname=654, lastname=44) one = db.fetchone("select firstname from person where lastname == 44") assert one == 654, one db.close()
def keep_writing_data(self, period): db = SQLiteCacheDB.getInstance() st = time() print >> sys.stderr, "begin write", self.getName( ), period, time() begin_time = time() try: while True: db.begin() values = [] for i in range(10): value = (str(i), str(i**2)) values.append(value) print ">>start write", self.getName(), time() db.insertMany('person', values) print ">>end write", self.getName(), time() db.commit() print ">>committed", self.getName(), time() sleep(0.01) et = time() if et - st > period: break except Exception, msg: print_exc() print >> sys.stderr, "On Error", time( ), begin_time, time() - begin_time, Exception, msg
def _test_open_close_db(self): sqlite_test = SQLiteCacheDB.getInstance() sqlite_test.openDB(self.db_path, 0) sqlite_test.close() sqlite_test.close() sqlite_test.openDB(self.db_path, 0) sqlite_test.close()
def read_data(self): db = SQLiteCacheDB.getInstance() one = db.fetchone('select * from person') assert one == ('a', 'b'), str(one) one = db.fetchone("select lastname from person where firstname == 'b'") assert one == 'a' one = db.fetchone("select lastname from person where firstname == 'c'") assert one == None all = db.fetchall('select * from person') assert len(all) == 101, len(all) num = db.size('person') assert num == 101 db.insert('person', lastname='1', firstname='abc') one = db.fetchone("select firstname from person where lastname == '1'") assert one == '1' or one == 'abc' all = db.fetchall("select firstname from person where lastname == '1'") assert len(all) == 2 db.update('person', "lastname == '2'", firstname='56') one = db.fetchone("select firstname from person where lastname == '2'") assert one == '56', one db.update('person', "lastname == '3'", firstname=65) one = db.fetchone("select firstname from person where lastname == '3'") assert one == 65, one db.update('person', "lastname == '4'", firstname=654, lastname=44) one = db.fetchone("select firstname from person where lastname == 44") assert one == 654, one db.close()
def keep_reading_data(self, period=5): db = SQLiteCacheDB.getInstance() st = time() while True: all = db.fetchall("select * from person where lastname='37'") num37 = len(all) print num37, et = time() if et - st > period: print break db.close()
def keep_reading_data(self, period=5): db = SQLiteCacheDB.getInstance() st = time() while True: all = db.fetchall("select * from person where lastname='37'") num37 = len(all) print num37, et = time() if et-st > period: print break db.close()
def write_data(self): db = SQLiteCacheDB.getInstance() db.begin() db.insert('person', lastname='a', firstname='b') values = [] for i in range(100): value = (str(i), str(i**2)) values.append(value) db.insertMany('person', values) db.commit() db.begin() db.commit() db.commit() db.close()
def basic_funcs(self, lib=0): db = SQLiteCacheDB.getInstance() create_sql = "create table person(lastname, firstname);" db.createDB(create_sql, lib=lib) db.insert('person', lastname='a', firstname='b') one = db.fetchone('select * from person') assert one == ('a', 'b') one = db.fetchone("select lastname from person where firstname == 'b'") assert one == 'a' one = db.fetchone("select lastname from person where firstname == 'c'") assert one == None values = [] for i in range(100): value = (str(i), str(i**2)) values.append(value) db.insertMany('person', values) all = db.fetchall('select * from person') assert len(all) == 101 all = db.fetchall("select * from person where lastname=='101'") assert all == [] num = db.size('person') assert num == 101 db.insert('person', lastname='1', firstname='abc') one = db.fetchone("select firstname from person where lastname == '1'") assert one == '1' or one == 'abc' all = db.fetchall("select firstname from person where lastname == '1'") assert len(all) == 2 db.update('person', "lastname == '2'", firstname='56') one = db.fetchone("select firstname from person where lastname == '2'") assert one == '56', one db.update('person', "lastname == '3'", firstname=65) one = db.fetchone("select firstname from person where lastname == '3'") assert one == 65, one db.update('person', "lastname == '4'", firstname=654, lastname=44) one = db.fetchone("select firstname from person where lastname == 44") assert one == 654, one db.close()
def basic_funcs(self, lib=0): db = SQLiteCacheDB.getInstance() create_sql = "create table person(lastname, firstname);" db.createDB(create_sql, lib=lib) db.insert('person', lastname='a', firstname='b') one = db.fetchone('select * from person') assert one == ('a','b') one = db.fetchone("select lastname from person where firstname == 'b'") assert one == 'a' one = db.fetchone("select lastname from person where firstname == 'c'") assert one == None values = [] for i in range(100): value = (str(i), str(i**2)) values.append(value) db.insertMany('person', values) all = db.fetchall('select * from person') assert len(all) == 101 all = db.fetchall("select * from person where lastname=='101'") assert all == [] num = db.size('person') assert num == 101 db.insert('person', lastname='1', firstname='abc') one = db.fetchone("select firstname from person where lastname == '1'") assert one == '1' or one == 'abc' all = db.fetchall("select firstname from person where lastname == '1'") assert len(all) == 2 db.update('person', "lastname == '2'", firstname='56') one = db.fetchone("select firstname from person where lastname == '2'") assert one == '56', one db.update('person', "lastname == '3'", firstname=65) one = db.fetchone("select firstname from person where lastname == '3'") assert one == 65, one db.update('person', "lastname == '4'", firstname=654, lastname=44) one = db.fetchone("select firstname from person where lastname == 44") assert one == 654, one db.close()
def test_convert_db(self): try: bsddb2sqlite = Bsddb2Sqlite(BSDDB_DIR, self.db_path, CREATE_SQL_FILE) bsddb2sqlite.run(torrent_dir=BSDDB_DIR) sdb = SQLiteCacheDB.getInstance() sdb.openDB(self.db_path, 0) nconnpeers = sdb.fetchone( 'select count(*) from Peer where connected_times>0;') assert nconnpeers == 1466 ntorrents = sdb.fetchone('select count(*) from torrent;') assert ntorrents == 42934 ntorrents = sdb.fetchone( 'select count(torrent_file_name) from torrent;') assert ntorrents == 4848 nprefs = sdb.fetchone('select count(*) from Preference;') assert nprefs == 60634 nmyprefs = sdb.fetchone('select count(*) from MyPreference') assert nmyprefs == 12 nfriends = sdb.fetchone( 'select count(*) from peer where friend==1') assert nfriends == 2 nfriends = sdb.fetchone('select count(*) from Friend') assert nfriends == 2 nsuperpeers = sdb.fetchone( 'select count(*) from peer where superpeer==1') assert nsuperpeers == 8 nsuperpeers = sdb.fetchone('select count(*) from SuperPeer') assert nsuperpeers == 8 nbc = sdb.fetchone('select count(*) from BarterCast') assert nbc == 584 torrent_dir = sdb.fetchone( "select value from MyInfo where entry='torrent_dir'") assert torrent_dir == BSDDB_DIR, torrent_dir sdb.close() finally: bsddb2sqlite.close()
def test_insertPeer(self): create_sql = """ CREATE TABLE Peer ( peer_id integer PRIMARY KEY AUTOINCREMENT NOT NULL, permid text NOT NULL, name text, ip text, port integer, thumbnail text, oversion integer, similarity numeric, friend integer, superpeer integer, last_seen numeric, last_connected numeric, last_buddycast numeric, connected_times integer, buddycast_times integer, num_peers integer, num_torrents integer, num_prefs integer, num_queries integer ); """ db = SQLiteCacheDB.getInstance() db.createDB(create_sql) assert db.size('Peer') == 0 fake_permid_x = 'fake_permid_x' + '0R0\x10\x06\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04' peer_x = { 'permid': fake_permid_x, 'ip': '1.2.3.4', 'port': 234, 'name': 'fake peer x' } permid = peer_x.pop('permid') db.insertPeer(permid, update=False, **peer_x) assert db.size('Peer') == 1 assert db.getOne('Peer', 'name', peer_id=1) == peer_x['name'] peer_x['port'] = 456 db.insertPeer(permid, update=False, **peer_x) assert db.getOne('Peer', 'port', peer_id=1) == 234 db.insertPeer(permid, update=True, **peer_x) assert db.getOne('Peer', 'port', peer_id=1) == 456
def test_convert_db(self): try: bsddb2sqlite = Bsddb2Sqlite(BSDDB_DIR, self.db_path, CREATE_SQL_FILE) bsddb2sqlite.run(torrent_dir=BSDDB_DIR) sdb = SQLiteCacheDB.getInstance() sdb.openDB(self.db_path, 0) nconnpeers = sdb.fetchone('select count(*) from Peer where connected_times>0;') assert nconnpeers == 1466 ntorrents = sdb.fetchone('select count(*) from torrent;') assert ntorrents == 42934 ntorrents = sdb.fetchone('select count(torrent_file_name) from torrent;') assert ntorrents == 4848 nprefs = sdb.fetchone('select count(*) from Preference;') assert nprefs == 60634 nmyprefs = sdb.fetchone('select count(*) from MyPreference') assert nmyprefs == 12 nfriends = sdb.fetchone('select count(*) from peer where friend==1') assert nfriends == 2 nfriends = sdb.fetchone('select count(*) from Friend') assert nfriends == 2 nsuperpeers = sdb.fetchone('select count(*) from peer where superpeer==1') assert nsuperpeers == 8 nsuperpeers = sdb.fetchone('select count(*) from SuperPeer') assert nsuperpeers == 8 nbc = sdb.fetchone('select count(*) from BarterCast') assert nbc == 584 torrent_dir = sdb.fetchone("select value from MyInfo where entry='torrent_dir'") assert torrent_dir == BSDDB_DIR, torrent_dir sdb.close() finally: bsddb2sqlite.close()
def keep_reading_data(self, period, sleeptime): db = SQLiteCacheDB.getInstance() st = time() all = db.fetchall("select * from person where lastname='7'") if not all: oldnum = 0 else: oldnum = len(all) print "begin read", period while True: all = db.fetchall("select * from person where lastname='7'") num = len(all) print "----------- read", self.getName(), num assert num>=oldnum, (num, oldnum) et = time() #sleep(0) #sleep(sleeptime) if et-st > period: break db.close() if period > 1: assert num>oldnum
def test_insertPeer(self): create_sql = """ CREATE TABLE Peer ( peer_id integer PRIMARY KEY AUTOINCREMENT NOT NULL, permid text NOT NULL, name text, ip text, port integer, thumbnail text, oversion integer, similarity numeric, friend integer, superpeer integer, last_seen numeric, last_connected numeric, last_buddycast numeric, connected_times integer, buddycast_times integer, num_peers integer, num_torrents integer, num_prefs integer, num_queries integer ); """ db = SQLiteCacheDB.getInstance() db.createDB(create_sql) assert db.size('Peer') == 0 fake_permid_x = 'fake_permid_x'+'0R0\x10\x06\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04' peer_x = {'permid':fake_permid_x, 'ip':'1.2.3.4', 'port':234, 'name':'fake peer x'} permid = peer_x.pop('permid') db.insertPeer(permid, update=False, **peer_x) assert db.size('Peer') == 1 assert db.getOne('Peer', 'name', peer_id=1) == peer_x['name'] peer_x['port']=456 db.insertPeer(permid, update=False, **peer_x) assert db.getOne('Peer', 'port', peer_id=1) == 234 db.insertPeer(permid, update=True, **peer_x) assert db.getOne('Peer', 'port', peer_id=1) == 456
def keep_reading_data(self, period, sleeptime): db = SQLiteCacheDB.getInstance() st = time() all = db.fetchall("select * from person where lastname='7'") if not all: oldnum = 0 else: oldnum = len(all) print "begin read", period while True: all = db.fetchall( "select * from person where lastname='7'") num = len(all) print "----------- read", self.getName(), num assert num >= oldnum, (num, oldnum) et = time() #sleep(0) #sleep(sleeptime) if et - st > period: break db.close() if period > 1: assert num > oldnum
def keep_writing_data(self, period): db = SQLiteCacheDB.getInstance() st = time() print >> sys.stderr, "begin write", self.getName(), period, time() begin_time = time() try: while True: db.begin() values = [] for i in range(10): value = (str(i), str(i**2)) values.append(value) print ">>start write", self.getName(), time() db.insertMany('person', values) print ">>end write", self.getName(), time() db.commit() print ">>committed", self.getName(), time() sleep(0.01) et = time() if et-st > period: break except Exception, msg: print_exc() print >> sys.stderr, "On Error", time(), begin_time, time()-begin_time, Exception, msg
def tearDown(self): db = SQLiteCacheDB.getInstance() db.close(clean=True) del db if os.path.exists(self.db_path): os.remove(self.db_path)
def _test_create_temp_db(self): sqlite_test = SQLiteCacheDB.getInstance() sql = "create table person(lastname, firstname);" sqlite_test.createDB(sql, self.db_path) sqlite_test.close()
def __init__(self): self.db = SQLiteCacheDB.getInstance()