'''
对BerkeleyDB DB_QUEUE与DB_Recno数据结构的封装 用于优化与提高代码效率
'''
class BDBQueue(object):
"""docstring for Bqueue"""
def __init__(self,dbFile=None,cachefilename=None):
#创建DB
home = C["DB_PATH"]
self.lock = threading.Lock()
self.dbenv=bsddb.db.DBEnv()
self.dbenv.open(home,bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
self.mydb = bsddb.db.DB(self.dbenv)
if dbFile:
self.dbFile = home + os.sep + dbFile
self.mydb.set_re_len(1024)
self.mydb.open(self.dbFile,dbtype = bsddb.db.DB_QUEUE, flags = bsddb.db.DB_CREATE)
if cachefilename:
self.cachefilename = home + os.sep + cachefilename
self.mydb.open(self.cachefilename,dbtype=db.DB_RECNO,flags=db.DB_CREATE)
def put(self,item):
try:
itemStr = json.dumps(item)
self.lock.acquire()
self.mydb.append(itemStr)
self.lock.release()
#self.mydb.sync()
except Exception, e:
raise e
def get(self):
try:
self.lock.acquire()
itemStr = self.mydb.consume()
self.lock.release()
except Exception, e:
raise e
if itemStr:
return json.loads(itemStr[1])
def qsize(self):
try:
self.lock.acquire()
dbStat = self.mydb.stat()
ndata = dbStat['ndata']
except Exception, e:
ndata = -1
finally:
self.lock.release()
return ndata
def putdata(self,item):
try:
db=json.dumps(item)
self.lock.acquire()
self.mydb.append(db)
self.lock.release()
except Exception, e:
raise e
def getOne(self):
self.lock.acquire()
cur = self.mydb.cursor()
val = cur.first()
if val:
print self.mydb.stat()
cur.delete()
self.lock.release()
return val
#清空缓存
def sync(self):
self.lock.acquire()
self.dbenv.memp_sync()
self.lock.release()
#关闭队列
def close(self):
"TODO:关闭数据库,队列不再可用"
print "CLOSING QUEUE.."
self.mydb.close()
self.dbenv.close()
#有待进一步优化:对BDB的信息监控,需要实时掌握BDB的详细信息。线程安全问题以及性能提升问题......
更多资料 · 微信公众号搜索【CTO Plus】关注后,获取更多,我们一起学习交流。
关于公众号的描述访问如下链接