上一次例子,我们采用了python特有的打开BDB的方式,非常简单,但是我好像没找到怎么指定访问方式的参数,后来继续看例子,找到了一个原生的访问方式,见代码
def common_tree_test(self):
print "############################ Start BTREE TEST ############################# "
home = "db_home"
filename = "test.db"
try:
# create the env folder
os.mkdir(home)
except:
pass
# create DB env
dbenv = bsddb.db.DBEnv()
# open DB env
dbenv.open(home, bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
# Create DB folder/object
d = bsddb.db.DB(dbenv)
# Open the bdb, second one is to define use what kind of struvture to access
# btree: bsddb.db.DB_BTREE
# hash: bsddb.db.DB_HASH
# queue: bsddb.db.DB_QUEUE
# recno: bsddb.db.DB_RECNO
d.open(filename, bsddb.db.DB_BTREE, bsddb.db.DB_CREATE, 0666)
# Insert a record, notice that the key when you use queue, and recno must be number
i = 1
start = time.time()
print start
while(++i < 10000000):
d.put('test1_'+str(i), 'admin'+str(i))
end = time.time()
print end - start
print d.items()
# whenn it close, write the data from memory to file
d.close()
dbenv.close()
def common_queue_test(self):
我们知道BDB有4种访问方式:
1. BTREE: b树的结构速度快
2. Hash: 速度和B树差不多,但是如果是有数据量大的情况下,建议采用Hash
3. Reco:x 相当于我们说的行数据的访问方式
4. Queue: 定长value的访问方式,最值得称赞的是有行锁