Python bsddb module operation Berkeley DB database introduction

  • 2020-05-07 19:57:05
  • OfStack

bsddb module is used to operate bdb module, bdb is the famous Berkeley DB, its performance is very good, mysql storage backend engine all support bdb way. Here is a brief introduction to the use of bsddb.

bdb is different from the relational database like 1. The data it stores can only be 1 pair of data composed of key and value. Just like the dictionary 1 of python, it cannot directly represent multiple fields.

The first problem with bsddb is what data access methods to use. bdb supports four: btree, hash, queue, recno. Here is the difference between them. btree is the tree structure used to store the data. The query speed is very fast, and it can store any complex key and value. hash is the hash algorithm. The speed of hash is almost the same as that of btree, but hash should be used when the amount of data is extremely large. queue is a queue operation. It has one limitation. It can only store data of fixed length. But queue can keep data first in, first out, and has special optimizations for inserting data, and provides row-level locking. key of queue must be a number. recno is similar to queue, but it can support longer value, and its key is also a number.

      here first to the four data access methods to open the database, simply insert a data demonstration.
For the bsddb module of python, there are two ways to open the database. 1 is to use the original interface, that is, to open an environment first, and then open a database from this environment, as follows:


import bsddb dbenv = bsddb.db.DBEnv()
dbenv.open(home, bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
d = bsddb.db.DB(dbenv)
d.open(filename, bsddb.db.DB_BTREE, bsddb.db.DB_CREATE, 0666)

There is one other way that is unique to python. This is the packaging of the above process by bsddb module itself, such as opening btree:


import bsddb db = bsddb.btopen('test.db', 'c')

It looks a lot easier than that. However, this approach provides very limited interfaces and very simple functionality, not as flexible as the first, but it is thread-safe in python 2.5. I'm going to introduce 1 here.
Take a look at one example:


#-*- encoding: gb2312 -*-
import os, sys, string
import bsddb, time home = "db_home"
filename = "test.db"
try:
    # create home directory
    os.mkdir(home)
except:
    pass # Creating a database environment
dbenv = bsddb.db.DBEnv()
# Open the database environment
dbenv.open(home, bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
# Creating database objects
d = bsddb.db.DB(dbenv)
# Open database , Here is the first 2 Which parameter specifies which data access method to use
# btree is bsddb.db.DB_BTREE . hash is bsddb.db.DB_HASH
# queu is bsddb.db.DB_QUEUE,  recno is bsddb.db.DB_RECNO
d.open(filename, bsddb.db.DB_BTREE, bsddb.db.DB_CREATE, 0666)
# insert 1 Bar data, attention queue and recno the key It can't be a string, it should be a number
d.put('test1', 'zhaowei')   
print d.items()
# Close, and the data is written back to the file
d.close()
dbenv.close()

Here's one with queue. Notice the difference:

#-*- encoding: gb2312 -*-
import os, sys, string
import bsddb, time home = "db_home"
filename = "testqueue.db"
try:
    os.mkdir(home)
except:
    pass dbenv = bsddb.db.DBEnv()
dbenv.open(home, bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
d = bsddb.db.DB(dbenv)
# queue You have to set 1 a value The length of value Is long
d.set_re_len(40)
d.open(filename, bsddb.db.DB_QUEUE, bsddb.db.DB_CREATE, 0666)
# It's key It has to be a number
d.put(1, 'zhaowei')
print d.items() d.close()
dbenv.close()

The simple second way to use it is much more concise:

import bsddb d = bsddb.hashopen("aaa.db", "c")
d['test1'] = "zhaowei"
print d.items()
d.close()


Related articles: