Commit 6aa4a1f2 authored by Martin v. Löwis's avatar Martin v. Löwis

Import PyBSDDB 3.4.0. Rename historical wrapper to bsddb185.

parent 1d267405
#----------------------------------------------------------------------
# Copyright (c) 1999-2001, Digital Creations, Fredericksburg, VA, USA
# and Andrew Kuchling. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# o Redistributions of source code must retain the above copyright
# notice, this list of conditions, and the disclaimer that follows.
#
# o Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# o Neither the name of Digital Creations nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
# IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
# CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#----------------------------------------------------------------------
"""
This package initialization module provides a compatibility interface
that should enable bsddb3 to be a near drop-in replacement for the original
old bsddb module. The functions and classes provided here are all
wrappers around the new functionality provided in the bsddb3.db module.
People interested in the more advanced capabilites of Berkeley DB 3.x
should use the bsddb3.db module directly.
"""
import _bsddb
# bsddb3 calls it _db
_db = _bsddb
__version__ = _db.__version__
error = _db.DBError # So bsddb3.error will mean something...
#----------------------------------------------------------------------
class _DBWithCursor:
"""
A simple wrapper around DB that makes it look like the bsddbobject in
the old module. It uses a cursor as needed to provide DB traversal.
"""
def __init__(self, db):
self.db = db
self.dbc = None
self.db.set_get_returns_none(0)
def __del__(self):
self.close()
def _checkCursor(self):
if self.dbc is None:
self.dbc = self.db.cursor()
def _checkOpen(self):
if self.db is None:
raise error, "BSDDB object has already been closed"
def isOpen(self):
return self.db is not None
def __len__(self):
self._checkOpen()
return len(self.db)
def __getitem__(self, key):
self._checkOpen()
return self.db[key]
def __setitem__(self, key, value):
self._checkOpen()
self.db[key] = value
def __delitem__(self, key):
self._checkOpen()
del self.db[key]
def close(self):
if self.dbc is not None:
self.dbc.close()
v = 0
if self.db is not None:
v = self.db.close()
self.dbc = None
self.db = None
return v
def keys(self):
self._checkOpen()
return self.db.keys()
def has_key(self, key):
self._checkOpen()
return self.db.has_key(key)
def set_location(self, key):
self._checkOpen()
self._checkCursor()
return self.dbc.set(key)
def next(self):
self._checkOpen()
self._checkCursor()
rv = self.dbc.next()
return rv
def previous(self):
self._checkOpen()
self._checkCursor()
rv = self.dbc.prev()
return rv
def first(self):
self._checkOpen()
self._checkCursor()
rv = self.dbc.first()
return rv
def last(self):
self._checkOpen()
self._checkCursor()
rv = self.dbc.last()
return rv
def sync(self):
self._checkOpen()
return self.db.sync()
#----------------------------------------------------------------------
# Compatibility object factory functions
def hashopen(file, flag='c', mode=0666, pgsize=None, ffactor=None, nelem=None,
cachesize=None, lorder=None, hflags=0):
flags = _checkflag(flag)
d = _db.DB()
d.set_flags(hflags)
if cachesize is not None: d.set_cachesize(0, cachesize)
if pgsize is not None: d.set_pagesize(pgsize)
if lorder is not None: d.set_lorder(lorder)
if ffactor is not None: d.set_h_ffactor(ffactor)
if nelem is not None: d.set_h_nelem(nelem)
d.open(file, _db.DB_HASH, flags, mode)
return _DBWithCursor(d)
#----------------------------------------------------------------------
def btopen(file, flag='c', mode=0666,
btflags=0, cachesize=None, maxkeypage=None, minkeypage=None,
pgsize=None, lorder=None):
flags = _checkflag(flag)
d = _db.DB()
if cachesize is not None: d.set_cachesize(0, cachesize)
if pgsize is not None: d.set_pagesize(pgsize)
if lorder is not None: d.set_lorder(lorder)
d.set_flags(btflags)
if minkeypage is not None: d.set_bt_minkey(minkeypage)
if maxkeypage is not None: d.set_bt_maxkey(maxkeypage)
d.open(file, _db.DB_BTREE, flags, mode)
return _DBWithCursor(d)
#----------------------------------------------------------------------
def rnopen(file, flag='c', mode=0666,
rnflags=0, cachesize=None, pgsize=None, lorder=None,
rlen=None, delim=None, source=None, pad=None):
flags = _checkflag(flag)
d = _db.DB()
if cachesize is not None: d.set_cachesize(0, cachesize)
if pgsize is not None: d.set_pagesize(pgsize)
if lorder is not None: d.set_lorder(lorder)
d.set_flags(rnflags)
if delim is not None: d.set_re_delim(delim)
if rlen is not None: d.set_re_len(rlen)
if source is not None: d.set_re_source(source)
if pad is not None: d.set_re_pad(pad)
d.open(file, _db.DB_RECNO, flags, mode)
return _DBWithCursor(d)
#----------------------------------------------------------------------
def _checkflag(flag):
if flag == 'r':
flags = _db.DB_RDONLY
elif flag == 'rw':
flags = 0
elif flag == 'w':
flags = _db.DB_CREATE
elif flag == 'c':
flags = _db.DB_CREATE
elif flag == 'n':
flags = _db.DB_CREATE | _db.DB_TRUNCATE
else:
raise error, "flags should be one of 'r', 'w', 'c' or 'n'"
return flags | _db.DB_THREAD
#----------------------------------------------------------------------
# This is a silly little hack that allows apps to continue to use the
# DB_THREAD flag even on systems without threads without freaking out
# BerkeleyDB.
#
# This assumes that if Python was built with thread support then
# BerkeleyDB was too.
try:
import thread
del thread
except ImportError:
_db.DB_THREAD = 0
#----------------------------------------------------------------------
#----------------------------------------------------------------------
# Copyright (c) 1999-2001, Digital Creations, Fredericksburg, VA, USA
# and Andrew Kuchling. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# o Redistributions of source code must retain the above copyright
# notice, this list of conditions, and the disclaimer that follows.
#
# o Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# o Neither the name of Digital Creations nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
# IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
# CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#----------------------------------------------------------------------
# This module is just a placeholder for possible future expansion, in
# case we ever want to augment the stuff in _db in any way. For now
# it just simply imports everything from _db.
from _db import *
from _db import __version__
if version() < (3, 1, 0):
raise ImportError, "BerkeleyDB 3.x symbols not found. Perhaps python was statically linked with an older version?"
#-------------------------------------------------------------------------
# This file contains real Python object wrappers for DB and DBEnv
# C "objects" that can be usefully subclassed. The previous SWIG
# based interface allowed this thanks to SWIG's shadow classes.
# -- Gregory P. Smith
#-------------------------------------------------------------------------
#
# (C) Copyright 2001 Autonomous Zone Industries
#
# License: This is free software. You may use this software for any
# purpose including modification/redistribution, so long as
# this header remains intact and that you do not claim any
# rights of ownership or authorship of this software. This
# software has been tested, but no warranty is expressed or
# implied.
#
import db
class DBEnv:
def __init__(self, *args, **kwargs):
self._cobj = apply(db.DBEnv, args, kwargs)
def close(self, *args, **kwargs):
return apply(self._cobj.close, args, kwargs)
def open(self, *args, **kwargs):
return apply(self._cobj.open, args, kwargs)
def remove(self, *args, **kwargs):
return apply(self._cobj.remove, args, kwargs)
def set_cachesize(self, *args, **kwargs):
return apply(self._cobj.set_cachesize, args, kwargs)
def set_data_dir(self, *args, **kwargs):
return apply(self._cobj.set_data_dir, args, kwargs)
def set_flags(self, *args, **kwargs):
return apply(self._cobj.set_flags, args, kwargs)
def set_lg_bsize(self, *args, **kwargs):
return apply(self._cobj.set_lg_bsize, args, kwargs)
def set_lg_dir(self, *args, **kwargs):
return apply(self._cobj.set_lg_dir, args, kwargs)
def set_lg_max(self, *args, **kwargs):
return apply(self._cobj.set_lg_max, args, kwargs)
def set_lk_detect(self, *args, **kwargs):
return apply(self._cobj.set_lk_detect, args, kwargs)
def set_lk_max(self, *args, **kwargs):
return apply(self._cobj.set_lk_max, args, kwargs)
def set_lk_max_locks(self, *args, **kwargs):
return apply(self._cobj.set_lk_max_locks, args, kwargs)
def set_lk_max_lockers(self, *args, **kwargs):
return apply(self._cobj.set_lk_max_lockers, args, kwargs)
def set_lk_max_objects(self, *args, **kwargs):
return apply(self._cobj.set_lk_max_objects, args, kwargs)
def set_mp_mmapsize(self, *args, **kwargs):
return apply(self._cobj.set_mp_mmapsize, args, kwargs)
def set_tmp_dir(self, *args, **kwargs):
return apply(self._cobj.set_tmp_dir, args, kwargs)
def txn_begin(self, *args, **kwargs):
return apply(self._cobj.txn_begin, args, kwargs)
def txn_checkpoint(self, *args, **kwargs):
return apply(self._cobj.txn_checkpoint, args, kwargs)
def txn_stat(self, *args, **kwargs):
return apply(self._cobj.txn_stat, args, kwargs)
def set_tx_max(self, *args, **kwargs):
return apply(self._cobj.set_tx_max, args, kwargs)
def lock_detect(self, *args, **kwargs):
return apply(self._cobj.lock_detect, args, kwargs)
def lock_get(self, *args, **kwargs):
return apply(self._cobj.lock_get, args, kwargs)
def lock_id(self, *args, **kwargs):
return apply(self._cobj.lock_id, args, kwargs)
def lock_put(self, *args, **kwargs):
return apply(self._cobj.lock_put, args, kwargs)
def lock_stat(self, *args, **kwargs):
return apply(self._cobj.lock_stat, args, kwargs)
def log_archive(self, *args, **kwargs):
return apply(self._cobj.log_archive, args, kwargs)
def set_get_returns_none(self, *args, **kwargs):
return apply(self._cobj.set_get_returns_none, args, kwargs)
class DB:
def __init__(self, dbenv, *args, **kwargs):
# give it the proper DBEnv C object that its expecting
self._cobj = apply(db.DB, (dbenv._cobj,) + args, kwargs)
# TODO are there other dict methods that need to be overridden?
def __len__(self):
return len(self._cobj)
def __getitem__(self, arg):
return self._cobj[arg]
def __setitem__(self, key, value):
self._cobj[key] = value
def __delitem__(self, arg):
del self._cobj[arg]
def append(self, *args, **kwargs):
return apply(self._cobj.append, args, kwargs)
def associate(self, *args, **kwargs):
return apply(self._cobj.associate, args, kwargs)
def close(self, *args, **kwargs):
return apply(self._cobj.close, args, kwargs)
def consume(self, *args, **kwargs):
return apply(self._cobj.consume, args, kwargs)
def consume_wait(self, *args, **kwargs):
return apply(self._cobj.consume_wait, args, kwargs)
def cursor(self, *args, **kwargs):
return apply(self._cobj.cursor, args, kwargs)
def delete(self, *args, **kwargs):
return apply(self._cobj.delete, args, kwargs)
def fd(self, *args, **kwargs):
return apply(self._cobj.fd, args, kwargs)
def get(self, *args, **kwargs):
return apply(self._cobj.get, args, kwargs)
def get_both(self, *args, **kwargs):
return apply(self._cobj.get_both, args, kwargs)
def get_byteswapped(self, *args, **kwargs):
return apply(self._cobj.get_byteswapped, args, kwargs)
def get_size(self, *args, **kwargs):
return apply(self._cobj.get_size, args, kwargs)
def get_type(self, *args, **kwargs):
return apply(self._cobj.get_type, args, kwargs)
def join(self, *args, **kwargs):
return apply(self._cobj.join, args, kwargs)
def key_range(self, *args, **kwargs):
return apply(self._cobj.key_range, args, kwargs)
def has_key(self, *args, **kwargs):
return apply(self._cobj.has_key, args, kwargs)
def items(self, *args, **kwargs):
return apply(self._cobj.items, args, kwargs)
def keys(self, *args, **kwargs):
return apply(self._cobj.keys, args, kwargs)
def open(self, *args, **kwargs):
return apply(self._cobj.open, args, kwargs)
def put(self, *args, **kwargs):
return apply(self._cobj.put, args, kwargs)
def remove(self, *args, **kwargs):
return apply(self._cobj.remove, args, kwargs)
def rename(self, *args, **kwargs):
return apply(self._cobj.rename, args, kwargs)
def set_bt_minkey(self, *args, **kwargs):
return apply(self._cobj.set_bt_minkey, args, kwargs)
def set_cachesize(self, *args, **kwargs):
return apply(self._cobj.set_cachesize, args, kwargs)
def set_flags(self, *args, **kwargs):
return apply(self._cobj.set_flags, args, kwargs)
def set_h_ffactor(self, *args, **kwargs):
return apply(self._cobj.set_h_ffactor, args, kwargs)
def set_h_nelem(self, *args, **kwargs):
return apply(self._cobj.set_h_nelem, args, kwargs)
def set_lorder(self, *args, **kwargs):
return apply(self._cobj.set_lorder, args, kwargs)
def set_pagesize(self, *args, **kwargs):
return apply(self._cobj.set_pagesize, args, kwargs)
def set_re_delim(self, *args, **kwargs):
return apply(self._cobj.set_re_delim, args, kwargs)
def set_re_len(self, *args, **kwargs):
return apply(self._cobj.set_re_len, args, kwargs)
def set_re_pad(self, *args, **kwargs):
return apply(self._cobj.set_re_pad, args, kwargs)
def set_re_source(self, *args, **kwargs):
return apply(self._cobj.set_re_source, args, kwargs)
def set_q_extentsize(self, *args, **kwargs):
return apply(self._cobj.set_q_extentsize, args, kwargs)
def stat(self, *args, **kwargs):
return apply(self._cobj.stat, args, kwargs)
def sync(self, *args, **kwargs):
return apply(self._cobj.sync, args, kwargs)
def type(self, *args, **kwargs):
return apply(self._cobj.type, args, kwargs)
def upgrade(self, *args, **kwargs):
return apply(self._cobj.upgrade, args, kwargs)
def values(self, *args, **kwargs):
return apply(self._cobj.values, args, kwargs)
def verify(self, *args, **kwargs):
return apply(self._cobj.verify, args, kwargs)
def set_get_returns_none(self, *args, **kwargs):
return apply(self._cobj.set_get_returns_none, args, kwargs)
"""
File-like objects that read from or write to a bsddb3 record.
This implements (nearly) all stdio methods.
f = DBRecIO(db, key, txn=None)
f.close() # explicitly release resources held
flag = f.isatty() # always false
pos = f.tell() # get current position
f.seek(pos) # set current position
f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF
buf = f.read() # read until EOF
buf = f.read(n) # read up to n bytes
f.truncate([size]) # truncate file at to at most size (default: current pos)
f.write(buf) # write at current position
f.writelines(list) # for line in list: f.write(line)
Notes:
- fileno() is left unimplemented so that code which uses it triggers
an exception early.
- There's a simple test set (see end of this file) - not yet updated
for DBRecIO.
- readline() is not implemented yet.
From:
Itamar Shtull-Trauring <itamar@maxnm.com>
"""
import errno
import string
class DBRecIO:
def __init__(self, db, key, txn=None):
self.db = db
self.key = key
self.txn = txn
self.len = None
self.pos = 0
self.closed = 0
self.softspace = 0
def close(self):
if not self.closed:
self.closed = 1
del self.db, self.txn
def isatty(self):
if self.closed:
raise ValueError, "I/O operation on closed file"
return 0
def seek(self, pos, mode = 0):
if self.closed:
raise ValueError, "I/O operation on closed file"
if mode == 1:
pos = pos + self.pos
elif mode == 2:
pos = pos + self.len
self.pos = max(0, pos)
def tell(self):
if self.closed:
raise ValueError, "I/O operation on closed file"
return self.pos
def read(self, n = -1):
if self.closed:
raise ValueError, "I/O operation on closed file"
if n < 0:
newpos = self.len
else:
newpos = min(self.pos+n, self.len)
dlen = newpos - self.pos
r = self.db.get(key, txn=self.txn, dlen=dlen, doff=self.pos)
self.pos = newpos
return r
__fixme = """
def readline(self, length=None):
if self.closed:
raise ValueError, "I/O operation on closed file"
if self.buflist:
self.buf = self.buf + string.joinfields(self.buflist, '')
self.buflist = []
i = string.find(self.buf, '\n', self.pos)
if i < 0:
newpos = self.len
else:
newpos = i+1
if length is not None:
if self.pos + length < newpos:
newpos = self.pos + length
r = self.buf[self.pos:newpos]
self.pos = newpos
return r
def readlines(self, sizehint = 0):
total = 0
lines = []
line = self.readline()
while line:
lines.append(line)
total += len(line)
if 0 < sizehint <= total:
break
line = self.readline()
return lines
"""
def truncate(self, size=None):
if self.closed:
raise ValueError, "I/O operation on closed file"
if size is None:
size = self.pos
elif size < 0:
raise IOError(errno.EINVAL,
"Negative size not allowed")
elif size < self.pos:
self.pos = size
self.db.put(key, "", txn=self.txn, dlen=self.len-size, doff=size)
def write(self, s):
if self.closed:
raise ValueError, "I/O operation on closed file"
if not s: return
if self.pos > self.len:
self.buflist.append('\0'*(self.pos - self.len))
self.len = self.pos
newpos = self.pos + len(s)
self.db.put(key, s, txn=self.txn, dlen=len(s), doff=self.pos)
self.pos = newpos
def writelines(self, list):
self.write(string.joinfields(list, ''))
def flush(self):
if self.closed:
raise ValueError, "I/O operation on closed file"
"""
# A little test suite
def _test():
import sys
if sys.argv[1:]:
file = sys.argv[1]
else:
file = '/etc/passwd'
lines = open(file, 'r').readlines()
text = open(file, 'r').read()
f = StringIO()
for line in lines[:-2]:
f.write(line)
f.writelines(lines[-2:])
if f.getvalue() != text:
raise RuntimeError, 'write failed'
length = f.tell()
print 'File length =', length
f.seek(len(lines[0]))
f.write(lines[1])
f.seek(0)
print 'First line =', `f.readline()`
here = f.tell()
line = f.readline()
print 'Second line =', `line`
f.seek(-len(line), 1)
line2 = f.read(len(line))
if line != line2:
raise RuntimeError, 'bad result after seek back'
f.seek(len(line2), 1)
list = f.readlines()
line = list[-1]
f.seek(f.tell() - len(line))
line2 = f.read()
if line != line2:
raise RuntimeError, 'bad result after seek back from EOF'
print 'Read', len(list), 'more lines'
print 'File length =', f.tell()
if f.tell() != length:
raise RuntimeError, 'bad length'
f.close()
if __name__ == '__main__':
_test()
"""
#!/bin/env python
#------------------------------------------------------------------------
# Copyright (c) 1997-2001 by Total Control Software
# All Rights Reserved
#------------------------------------------------------------------------
#
# Module Name: dbShelve.py
#
# Description: A reimplementation of the standard shelve.py that
# forces the use of cPickle, and DB.
#
# Creation Date: 11/3/97 3:39:04PM
#
# License: This is free software. You may use this software for any
# purpose including modification/redistribution, so long as
# this header remains intact and that you do not claim any
# rights of ownership or authorship of this software. This
# software has been tested, but no warranty is expressed or
# implied.
#
# 13-Dec-2000: Updated to be used with the new bsddb3 package.
# Added DBShelfCursor class.
#
#------------------------------------------------------------------------
"""
Manage shelves of pickled objects using bsddb3 database files for the
storage.
"""
#------------------------------------------------------------------------
import cPickle
from bsddb3 import db
#------------------------------------------------------------------------
def open(filename, flags=db.DB_CREATE, mode=0660, filetype=db.DB_HASH,
dbenv=None, dbname=None):
"""
A simple factory function for compatibility with the standard
shleve.py module. It can be used like this, where key is a string
and data is a pickleable object:
from bsddb3 import dbshelve
db = dbshelve.open(filename)
db[key] = data
db.close()
"""
if type(flags) == type(''):
sflag = flags
if sflag == 'r':
flags = db.DB_RDONLY
elif sflag == 'rw':
flags = 0
elif sflag == 'w':
flags = db.DB_CREATE
elif sflag == 'c':
flags = db.DB_CREATE
elif sflag == 'n':
flags = db.DB_TRUNCATE | db.DB_CREATE
else:
raise error, "flags should be one of 'r', 'w', 'c' or 'n' or use the bsddb3.db.DB_* flags"
d = DBShelf(dbenv)
d.open(filename, dbname, filetype, flags, mode)
return d
#---------------------------------------------------------------------------
class DBShelf:
"""
A shelf to hold pickled objects, built upon a bsddb3 DB object. It
automatically pickles/unpickles data objects going to/from the DB.
"""
def __init__(self, dbenv=None):
self.db = db.DB(dbenv)
self.binary = 1
def __del__(self):
self.close()
def __getattr__(self, name):
"""Many methods we can just pass through to the DB object. (See below)"""
return getattr(self.db, name)
#-----------------------------------
# Dictionary access methods
def __len__(self):
return len(self.db)
def __getitem__(self, key):
data = self.db[key]
return cPickle.loads(data)
def __setitem__(self, key, value):
data = cPickle.dumps(value, self.binary)
self.db[key] = data
def __delitem__(self, key):
del self.db[key]
def keys(self, txn=None):
if txn != None:
return self.db.keys(txn)
else:
return self.db.keys()
def items(self, txn=None):
if txn != None:
items = self.db.items(txn)
else:
items = self.db.items()
newitems = []
for k, v in items:
newitems.append( (k, cPickle.loads(v)) )
return newitems
def values(self, txn=None):
if txn != None:
values = self.db.values(txn)
else:
values = self.db.values()
return map(cPickle.loads, values)
#-----------------------------------
# Other methods
def append(self, value, txn=None):
data = cPickle.dumps(value, self.binary)
return self.db.append(data, txn)
def associate(self, secondaryDB, callback, flags=0):
def _shelf_callback(priKey, priData, realCallback=callback):
data = cPickle.loads(priData)
return realCallback(priKey, data)
return self.db.associate(secondaryDB, _shelf_callback, flags)
#def get(self, key, default=None, txn=None, flags=0):
def get(self, *args, **kw):
# We do it with *args and **kw so if the default value wasn't
# given nothing is passed to the extension module. That way
# an exception can be raised if set_get_returns_none is turned
# off.
data = apply(self.db.get, args, kw)
try:
return cPickle.loads(data)
except (TypeError, cPickle.UnpicklingError):
return data # we may be getting the default value, or None,
# so it doesn't need unpickled.
def get_both(self, key, value, txn=None, flags=0):
data = cPickle.dumps(value, self.binary)
data = self.db.get(key, data, txn, flags)
return cPickle.loads(data)
def cursor(self, txn=None, flags=0):
c = DBShelfCursor(self.db.cursor(txn, flags))
c.binary = self.binary
return c
def put(self, key, value, txn=None, flags=0):
data = cPickle.dumps(value, self.binary)
return self.db.put(key, data, txn, flags)
def join(self, cursorList, flags=0):
raise NotImplementedError
#----------------------------------------------
# Methods allowed to pass-through to self.db
#
# close, delete, fd, get_byteswapped, get_type, has_key,
# key_range, open, remove, rename, stat, sync,
# upgrade, verify, and all set_* methods.
#---------------------------------------------------------------------------
class DBShelfCursor:
"""
"""
def __init__(self, cursor):
self.dbc = cursor
def __del__(self):
self.close()
def __getattr__(self, name):
"""Some methods we can just pass through to the cursor object. (See below)"""
return getattr(self.dbc, name)
#----------------------------------------------
def dup(self, flags=0):
return DBShelfCursor(self.dbc.dup(flags))
def put(self, key, value, flags=0):
data = cPickle.dumps(value, self.binary)
return self.dbc.put(key, data, flags)
def get(self, *args):
count = len(args) # a method overloading hack
method = getattr(self, 'get_%d' % count)
apply(method, args)
def get_1(self, flags):
rec = self.dbc.get(flags)
return self._extract(rec)
def get_2(self, key, flags):
rec = self.dbc.get(key, flags)
return self._extract(rec)
def get_3(self, key, value, flags):
data = cPickle.dumps(value, self.binary)
rec = self.dbc.get(key, flags)
return self._extract(rec)
def current(self, flags=0): return self.get_1(flags|db.DB_CURRENT)
def first(self, flags=0): return self.get_1(flags|db.DB_FIRST)
def last(self, flags=0): return self.get_1(flags|db.DB_LAST)
def next(self, flags=0): return self.get_1(flags|db.DB_NEXT)
def prev(self, flags=0): return self.get_1(flags|db.DB_PREV)
def consume(self, flags=0): return self.get_1(flags|db.DB_CONSUME)
def next_dup(self, flags=0): return self.get_1(flags|db.DB_NEXT_DUP)
def next_nodup(self, flags=0): return self.get_1(flags|db.DB_NEXT_NODUP)
def prev_nodup(self, flags=0): return self.get_1(flags|db.DB_PREV_NODUP)
def get_both(self, key, value, flags=0):
data = cPickle.dumps(value, self.binary)
rec = self.dbc.get_both(key, flags)
return self._extract(rec)
def set(self, key, flags=0):
rec = self.dbc.set(key, flags)
return self._extract(rec)
def set_range(self, key, flags=0):
rec = self.dbc.set_range(key, flags)
return self._extract(rec)
def set_recno(self, recno, flags=0):
rec = self.dbc.set_recno(recno, flags)
return self._extract(rec)
set_both = get_both
def _extract(self, rec):
if rec is None:
return None
else:
key, data = rec
return key, cPickle.loads(data)
#----------------------------------------------
# Methods allowed to pass-through to self.dbc
#
# close, count, delete, get_recno, join_item
#---------------------------------------------------------------------------
This diff is collapsed.
#------------------------------------------------------------------------
#
# In my performance tests, using this (as in dbtest.py test4) is
# slightly slower than simply compiling _db.c with MYDB_THREAD
# undefined to prevent multithreading support in the C module.
# Using NoDeadlockDb also prevent deadlocks from mutliple processes
# accessing the same database.
#
# Copyright (C) 2000 Autonomous Zone Industries
#
# License: This is free software. You may use this software for any
# purpose including modification/redistribution, so long as
# this header remains intact and that you do not claim any
# rights of ownership or authorship of this software. This
# software has been tested, but no warranty is expressed or
# implied.
#
# Author: Gregory P. Smith <greg@electricrain.com>
#
# Note: I don't know how useful this is in reality since when a
# DBDeadlockError happens the current transaction is supposed to be
# aborted. If it doesn't then when the operation is attempted again
# the deadlock is still happening...
# --Robin
#
#------------------------------------------------------------------------
#
# import the time.sleep function in a namespace safe way to allow
# "from bsddb3.db import *"
#
from time import sleep
_sleep = sleep
del sleep
import _db
_deadlock_MinSleepTime = 1.0/64 # always sleep at least N seconds between retrys
_deadlock_MaxSleepTime = 1.0 # never sleep more than N seconds between retrys
def DeadlockWrap(function, *_args, **_kwargs):
"""DeadlockWrap(function, *_args, **_kwargs) - automatically retries
function in case of a database deadlock.
This is a DeadlockWrapper method which DB calls can be made using to
preform infinite retrys with sleeps in between when a DBLockDeadlockError
exception is raised in a database call:
d = DB(...)
d.open(...)
DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar"
"""
sleeptime = _deadlock_MinSleepTime
while (1) :
try:
return apply(function, _args, _kwargs)
except _db.DBLockDeadlockError:
print 'DeadlockWrap sleeping ', sleeptime
_sleep(sleeptime)
# exponential backoff in the sleep time
sleeptime = sleeptime * 2
if sleeptime > _deadlock_MaxSleepTime :
sleeptime = _deadlock_MaxSleepTime
#------------------------------------------------------------------------
...@@ -393,25 +393,30 @@ GLHACK=-Dclear=__GLclear ...@@ -393,25 +393,30 @@ GLHACK=-Dclear=__GLclear
#gdbm gdbmmodule.c -I/usr/local/include -L/usr/local/lib -lgdbm #gdbm gdbmmodule.c -I/usr/local/include -L/usr/local/lib -lgdbm
# Berkeley DB interface. # Sleepycat Berkeley DB interface.
# #
# This requires the Berkeley DB code, see # This requires the Sleepycat DB code, see http://www.sleepycat.com/
# ftp://ftp.cs.berkeley.edu/pub/4bsd/db.1.85.tar.gz # The minimum supported version of that library is 3.0.
# #
# Edit the variables DB and DBPORT to point to the db top directory # Edit the variables DB and DBLIBVERto point to the db top directory
# and the subdirectory of PORT where you built it. # and the subdirectory of PORT where you built it.
# #DB=/usr/local/BerkeleyDB.4.0
# (See http://electricrain.com/greg/python/bsddb3/ for an interface to #DBLIBVER=4.0
# BSD DB 3.x.) #DBINC=$(DB)/include
#DBLIB=$(DB)/lib
# Note: If a db.h file is found by configure, bsddb will be enabled #_bsddb _bsddb.c -I$(DBINC) -L$(DBLIB) -ldb-$(DBLIBVER
# automatically via Setup.config.in. It only needs to be enabled here
# if it is not automatically enabled there; check the generated
# Setup.config before enabling it here.
# Historical Berkeley DB 1.85
#
# This requires the Berkeley DB code, see
# ftp://ftp.cs.berkeley.edu/pub/4bsd/db.1.85.tar.gz
#
# This module is deprecated; the historical BSDDB library has bugs
# that can cause data corruption. If you can, use the Sleepycat library
# instead.
#DB=/depot/sundry/src/berkeley-db/db.1.85 #DB=/depot/sundry/src/berkeley-db/db.1.85
#DBPORT=$(DB)/PORT/irix.5.3 #DBPORT=$(DB)/PORT/irix.5.3
#bsddb bsddbmodule.c -I$(DBPORT)/include -I$(DBPORT) $(DBPORT)/libdb.a #bsddb185 bsddbmodule.c -I$(DBPORT)/include -I$(DBPORT) $(DBPORT)/libdb.a
......
This diff is collapsed.
...@@ -842,11 +842,11 @@ static PyMethodDef bsddbmodule_methods[] = { ...@@ -842,11 +842,11 @@ static PyMethodDef bsddbmodule_methods[] = {
}; };
PyMODINIT_FUNC PyMODINIT_FUNC
initbsddb(void) { initbsddb185(void) {
PyObject *m, *d; PyObject *m, *d;
Bsddbtype.ob_type = &PyType_Type; Bsddbtype.ob_type = &PyType_Type;
m = Py_InitModule("bsddb", bsddbmodule_methods); m = Py_InitModule("bsddb185", bsddbmodule_methods);
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
BsddbError = PyErr_NewException("bsddb.error", NULL, NULL); BsddbError = PyErr_NewException("bsddb.error", NULL, NULL);
if (BsddbError != NULL) if (BsddbError != NULL)
......
...@@ -437,8 +437,7 @@ class PyBuildExt(build_ext): ...@@ -437,8 +437,7 @@ class PyBuildExt(build_ext):
# Berkeley DB 3.x.) # Berkeley DB 3.x.)
# when sorted in reverse order, keys for this dict must appear in the # when sorted in reverse order, keys for this dict must appear in the
# order you wish to search - e.g., search for db3 before db2, db2 # order you wish to search - e.g., search for db4 before db3
# before db1
db_try_this = { db_try_this = {
'db4': {'libs': ('db-4.3', 'db-4.2', 'db-4.1', 'db-4.0'), 'db4': {'libs': ('db-4.3', 'db-4.2', 'db-4.1', 'db-4.0'),
'libdirs': ('/usr/local/BerkeleyDB.4.3/lib', 'libdirs': ('/usr/local/BerkeleyDB.4.3/lib',
...@@ -460,7 +459,7 @@ class PyBuildExt(build_ext): ...@@ -460,7 +459,7 @@ class PyBuildExt(build_ext):
'/sw/include/db4', '/sw/include/db4',
'/usr/include/db4', '/usr/include/db4',
), ),
'incs': ('db_185.h',)}, 'incs': ('db.h',)},
'db3': {'libs': ('db-3.3', 'db-3.2', 'db-3.1', 'db-3.0'), 'db3': {'libs': ('db-3.3', 'db-3.2', 'db-3.1', 'db-3.0'),
'libdirs': ('/usr/local/BerkeleyDB.3.3/lib', 'libdirs': ('/usr/local/BerkeleyDB.3.3/lib',
'/usr/local/BerkeleyDB.3.2/lib', '/usr/local/BerkeleyDB.3.2/lib',
...@@ -481,35 +480,9 @@ class PyBuildExt(build_ext): ...@@ -481,35 +480,9 @@ class PyBuildExt(build_ext):
'/sw/include/db3', '/sw/include/db3',
'/usr/include/db3', '/usr/include/db3',
), ),
'incs': ('db_185.h',)}, 'incs': ('db.h',)},
'db2': {'libs': ('db2',),
'libdirs': ('/usr/local/lib',
'/sw/lib',
'/usr/lib',
'/lib'),
'incdirs': ('/usr/local/include/db2',
'/sw/include/db2',
'/usr/include/db2'),
'incs': ('db_185.h',)},
# if you are willing to risk hash db file corruption you can
# uncomment the lines below for db1. Note that this will affect
# not only the bsddb module, but the dbhash and anydbm modules
# as well. YOU HAVE BEEN WARNED!!!
##'db1': {'libs': ('db1', 'db'),
## 'libdirs': ('/usr/local/lib',
## '/sw/lib',
## '/usr/lib',
## '/lib'),
## 'incdirs': ('/usr/local/include/db1',
## '/usr/local/include',
## '/usr/include/db1',
## '/usr/include'),
## 'incs': ('db.h',)},
} }
# override this list to affect the library version search order
# for example, if you want to force version 2 to be used:
# db_search_order = ["db2"]
db_search_order = db_try_this.keys() db_search_order = db_try_this.keys()
db_search_order.sort() db_search_order.sort()
db_search_order.reverse() db_search_order.reverse()
...@@ -537,19 +510,11 @@ class PyBuildExt(build_ext): ...@@ -537,19 +510,11 @@ class PyBuildExt(build_ext):
# is usually correct and most trouble free, but may cause problems # is usually correct and most trouble free, but may cause problems
# in some unusual system configurations (e.g. the directory is on # in some unusual system configurations (e.g. the directory is on
# an NFS server that goes away). # an NFS server that goes away).
if dbinc == 'db_185.h': exts.append(Extension('_bsddb', ['_bsddb.c'],
exts.append(Extension('bsddb', ['bsddbmodule.c'], library_dirs=[dblib_dir],
library_dirs=[dblib_dir], runtime_library_dirs=[dblib_dir],
runtime_library_dirs=[dblib_dir], include_dirs=db_incs,
include_dirs=db_incs, libraries=dblibs))
define_macros=[('HAVE_DB_185_H',1)],
libraries=dblibs))
else:
exts.append(Extension('bsddb', ['bsddbmodule.c'],
library_dirs=[dblib_dir],
runtime_library_dirs=[dblib_dir],
include_dirs=db_incs,
libraries=dblibs))
else: else:
db_incs = None db_incs = None
dblibs = [] dblibs = []
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment