[Python-checkins] python/dist/src/Lib/bsddb dbtables.py,1.6,1.6.4.1

bwarsaw@users.sourceforge.net bwarsaw@users.sourceforge.net
2003年1月28日 07:46:28 -0800


Update of /cvsroot/python/python/dist/src/Lib/bsddb
In directory sc8-pr-cvs1:/tmp/cvs-serv31040
Modified Files:
 Tag: bsddb-bsddb3-schizo-branch
	dbtables.py 
Log Message:
Add compatibility idiom, which tries to get bsddb.db first, falling
back to bsddb3.db
Also various code cleanups:
- remove the need for the string module
- always use cPickle module
- fix type comparisons
- more consistent formatting
Index: dbtables.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/bsddb/dbtables.py,v
retrieving revision 1.6
retrieving revision 1.6.4.1
diff -C2 -d -r1.6 -r1.6.4.1
*** dbtables.py	30 Dec 2002 20:52:07 -0000	1.6
--- dbtables.py	28 Jan 2003 15:46:25 -0000	1.6.4.1
***************
*** 18,38 ****
 _cvsid = '$Id$'
 
- import string
- import sys
- try:
- import cPickle
- pickle = cPickle
- except ImportError:
- import pickle
- import whrandom
- import xdrlib
 import re
 import copy
 
! from bsddb.db import *
 
 
! class TableDBError(StandardError): pass
! class TableAlreadyExists(TableDBError): pass
 
 
--- 18,41 ----
 _cvsid = '$Id$'
 
 import re
+ import sys
 import copy
+ import xdrlib
+ import whrandom
+ from types import ListType, StringType
+ import cPickle as pickle
 
! try:
! # For Python 2.3
! from bsddb.db import *
! except ImportError:
! # For earlier Pythons w/distutils pybsddb
! from bsddb3.db import *
 
 
! class TableDBError(StandardError):
! pass
! class TableAlreadyExists(TableDBError):
! pass
 
 
***************
*** 73,79 ****
 chars_to_escape = '.*+()[]?'
 for char in chars_to_escape :
! likestr = string.replace(likestr, char, '\\'+char)
 # convert %s to wildcards
! self.likestr = string.replace(likestr, '%', '.*')
 self.re = re.compile('^'+self.likestr+'$', re_flags)
 def __call__(self, s):
--- 76,82 ----
 chars_to_escape = '.*+()[]?'
 for char in chars_to_escape :
! likestr = likestr.replace(char, '\\'+char)
 # convert %s to wildcards
! self.likestr = likestr.replace('%', '.*')
 self.re = re.compile('^'+self.likestr+'$', re_flags)
 def __call__(self, s):
***************
*** 85,89 ****
 _table_names_key = '__TABLE_NAMES__' # list of the tables in this db
 _columns = '._COLUMNS__' # table_name+this key contains a list of columns
! def _columns_key(table) : return table + _columns
 
 #
--- 88,94 ----
 _table_names_key = '__TABLE_NAMES__' # list of the tables in this db
 _columns = '._COLUMNS__' # table_name+this key contains a list of columns
! 
! def _columns_key(table):
! return table + _columns
 
 #
***************
*** 94,102 ****
 # row in the table. (no data is stored)
 _rowid_str_len = 8 # length in bytes of the unique rowid strings
! def _data_key(table, col, rowid) : return table + _data + col + _data + rowid
! def _search_col_data_key(table, col) : return table + _data + col + _data
! def _search_all_data_key(table) : return table + _data
! def _rowid_key(table, rowid) : return table + _rowid + rowid + _rowid
! def _search_rowid_key(table) : return table + _rowid
 
 def contains_metastrings(s) :
--- 99,117 ----
 # row in the table. (no data is stored)
 _rowid_str_len = 8 # length in bytes of the unique rowid strings
! 
! def _data_key(table, col, rowid):
! return table + _data + col + _data + rowid
! 
! def _search_col_data_key(table, col):
! return table + _data + col + _data
! 
! def _search_all_data_key(table):
! return table + _data
! 
! def _rowid_key(table, rowid):
! return table + _rowid + rowid + _rowid
! 
! def _search_rowid_key(table):
! return table + _rowid
 
 def contains_metastrings(s) :
***************
*** 104,113 ****
 metadata strings that might interfere with dbtables database operation.
 """
! if string.find(s, _table_names_key) >= 0 or \
! string.find(s, _columns) >= 0 or \
! string.find(s, _data) >= 0 or \
! string.find(s, _rowid) >= 0 :
 return 1
! else :
 return 0
 
--- 119,129 ----
 metadata strings that might interfere with dbtables database operation.
 """
! if (s.find(_table_names_key) >= 0 or
! s.find(_columns) >= 0 or
! s.find(_data) >= 0 or
! s.find(_rowid) >= 0):
! # Then
 return 1
! else:
 return 0
 
***************
*** 115,119 ****
 class bsdTableDB :
 def __init__(self, filename, dbhome, create=0, truncate=0, mode=0600,
! recover=0, dbflags=0) :
 """bsdTableDB.open(filename, dbhome, create=0, truncate=0, mode=0600)
 Open database name in the dbhome BerkeleyDB directory.
--- 131,135 ----
 class bsdTableDB :
 def __init__(self, filename, dbhome, create=0, truncate=0, mode=0600,
! recover=0, dbflags=0):
 """bsdTableDB.open(filename, dbhome, create=0, truncate=0, mode=0600)
 Open database name in the dbhome BerkeleyDB directory.
***************
*** 187,191 ****
 try:
 key, data = cur.first()
! while 1 :
 print `{key: data}`
 next = cur.next()
--- 203,207 ----
 try:
 key, data = cur.first()
! while 1:
 print `{key: data}`
 next = cur.next()
***************
*** 203,207 ****
 raises TableDBError if it already exists or for other DB errors.
 """
! assert type(columns) == type([])
 txn = None
 try:
--- 219,223 ----
 raises TableDBError if it already exists or for other DB errors.
 """
! assert isinstance(columns, ListType)
 txn = None
 try:
***************
*** 234,240 ****
 txn.commit()
 txn = None
- 
 except DBError, dberror:
! if txn :
 txn.abort()
 raise TableDBError, dberror[1]
--- 250,255 ----
 txn.commit()
 txn = None
 except DBError, dberror:
! if txn:
 txn.abort()
 raise TableDBError, dberror[1]
***************
*** 245,250 ****
 [] if the table doesn't exist.
 """
! assert type(table) == type('')
! if contains_metastrings(table) :
 raise ValueError, "bad table name: contains reserved metastrings"
 
--- 260,265 ----
 [] if the table doesn't exist.
 """
! assert isinstance(table, StringType)
! if contains_metastrings(table):
 raise ValueError, "bad table name: contains reserved metastrings"
 
***************
*** 274,278 ****
 all of its current columns.
 """
! assert type(columns) == type([])
 try:
 self.CreateTable(table, columns)
--- 289,293 ----
 all of its current columns.
 """
! assert isinstance(columns, ListType)
 try:
 self.CreateTable(table, columns)
***************
*** 332,336 ****
 """Create a new unique row identifier"""
 unique = 0
! while not unique :
 # Generate a random 64-bit row ID string
 # (note: this code has <64 bits of randomness
--- 347,351 ----
 """Create a new unique row identifier"""
 unique = 0
! while not unique:
 # Generate a random 64-bit row ID string
 # (note: this code has <64 bits of randomness
***************
*** 359,370 ****
 txn = None
 try:
! if not self.db.has_key(_columns_key(table)) :
 raise TableDBError, "unknown table"
 
 # check the validity of each column name
! if not self.__tablecolumns.has_key(table) :
 self.__load_column_info(table)
 for column in rowdict.keys() :
! if not self.__tablecolumns[table].count(column) :
 raise TableDBError, "unknown column: "+`column`
 
--- 374,385 ----
 txn = None
 try:
! if not self.db.has_key(_columns_key(table)):
 raise TableDBError, "unknown table"
 
 # check the validity of each column name
! if not self.__tablecolumns.has_key(table):
 self.__load_column_info(table)
 for column in rowdict.keys() :
! if not self.__tablecolumns[table].count(column):
 raise TableDBError, "unknown column: "+`column`
 
***************
*** 374,378 ****
 
 # insert the row values into the table database
! for column, dataitem in rowdict.items() :
 # store the value
 self.db.put(_data_key(table, column, rowid), dataitem, txn=txn)
--- 389,393 ----
 
 # insert the row values into the table database
! for column, dataitem in rowdict.items():
 # store the value
 self.db.put(_data_key(table, column, rowid), dataitem, txn=txn)
***************
*** 393,397 ****
 
 
! def Modify(self, table, conditions={}, mappings={}) :
 """Modify(table, conditions) - Modify in rows matching 'conditions'
 using mapping functions in 'mappings'
--- 408,412 ----
 
 
! def Modify(self, table, conditions={}, mappings={}):
 """Modify(table, conditions) - Modify in rows matching 'conditions'
 using mapping functions in 'mappings'
***************
*** 408,415 ****
 # modify only requested columns
 columns = mappings.keys()
! for rowid in matching_rowids.keys() :
 txn = None
 try:
! for column in columns :
 txn = self.env.txn_begin()
 # modify the requested column
--- 423,430 ----
 # modify only requested columns
 columns = mappings.keys()
! for rowid in matching_rowids.keys():
 txn = None
 try:
! for column in columns:
 txn = self.env.txn_begin()
 # modify the requested column
***************
*** 434,438 ****
 
 except DBError, dberror:
! if txn :
 txn.abort()
 raise
--- 449,453 ----
 
 except DBError, dberror:
! if txn:
 txn.abort()
 raise
***************
*** 441,445 ****
 raise TableDBError, dberror[1]
 
! def Delete(self, table, conditions={}) :
 """Delete(table, conditions) - Delete items matching the given
 conditions from the table.
--- 456,460 ----
 raise TableDBError, dberror[1]
 
! def Delete(self, table, conditions={}):
 """Delete(table, conditions) - Delete items matching the given
 conditions from the table.
***************
*** 453,461 ****
 # delete row data from all columns
 columns = self.__tablecolumns[table]
! for rowid in matching_rowids.keys() :
 txn = None
 try:
 txn = self.env.txn_begin()
! for column in columns :
 # delete the data key
 try:
--- 468,476 ----
 # delete row data from all columns
 columns = self.__tablecolumns[table]
! for rowid in matching_rowids.keys():
 txn = None
 try:
 txn = self.env.txn_begin()
! for column in columns:
 # delete the data key
 try:
***************
*** 474,486 ****
 txn = None
 except DBError, dberror:
! if txn :
 txn.abort()
 raise
- 
 except DBError, dberror:
 raise TableDBError, dberror[1]
 
 
! def Select(self, table, columns, conditions={}) :
 """Select(table, conditions) - retrieve specific row data
 Returns a list of row column->value mapping dictionaries.
--- 489,500 ----
 txn = None
 except DBError, dberror:
! if txn:
 txn.abort()
 raise
 except DBError, dberror:
 raise TableDBError, dberror[1]
 
 
! def Select(self, table, columns, conditions={}):
 """Select(table, conditions) - retrieve specific row data
 Returns a list of row column->value mapping dictionaries.
***************
*** 492,508 ****
 """
 try:
! if not self.__tablecolumns.has_key(table) :
 self.__load_column_info(table)
! if columns is None :
 columns = self.__tablecolumns[table]
 matching_rowids = self.__Select(table, columns, conditions)
 except DBError, dberror:
 raise TableDBError, dberror[1]
- 
 # return the matches as a list of dictionaries
 return matching_rowids.values()
 
 
! def __Select(self, table, columns, conditions) :
 """__Select() - Used to implement Select and Delete (above)
 Returns a dictionary keyed on rowids containing dicts
--- 506,521 ----
 """
 try:
! if not self.__tablecolumns.has_key(table):
 self.__load_column_info(table)
! if columns is None:
 columns = self.__tablecolumns[table]
 matching_rowids = self.__Select(table, columns, conditions)
 except DBError, dberror:
 raise TableDBError, dberror[1]
 # return the matches as a list of dictionaries
 return matching_rowids.values()
 
 
! def __Select(self, table, columns, conditions):
 """__Select() - Used to implement Select and Delete (above)
 Returns a dictionary keyed on rowids containing dicts
***************
*** 514,523 ****
 """
 # check the validity of each column name
! if not self.__tablecolumns.has_key(table) :
 self.__load_column_info(table)
! if columns is None :
 columns = self.tablecolumns[table]
! for column in (columns + conditions.keys()) :
! if not self.__tablecolumns[table].count(column) :
 raise TableDBError, "unknown column: "+`column`
 
--- 527,536 ----
 """
 # check the validity of each column name
! if not self.__tablecolumns.has_key(table):
 self.__load_column_info(table)
! if columns is None:
 columns = self.tablecolumns[table]
! for column in (columns + conditions.keys()):
! if not self.__tablecolumns[table].count(column):
 raise TableDBError, "unknown column: "+`column`
 
***************
*** 525,530 ****
 # column names containing the data for that row and column.
 matching_rowids = {}
! 
! rejected_rowids = {} # keys are rowids that do not match
 
 # attempt to sort the conditions in such a way as to minimize full
--- 538,543 ----
 # column names containing the data for that row and column.
 matching_rowids = {}
! # keys are rowids that do not match
! rejected_rowids = {}
 
 # attempt to sort the conditions in such a way as to minimize full
***************
*** 533,537 ****
 a = atuple[1]
 b = btuple[1]
! if type(a) == type(b) :
 if isinstance(a, PrefixCond) and isinstance(b, PrefixCond):
 # longest prefix first
--- 546,550 ----
 a = atuple[1]
 b = btuple[1]
! if type(a) is type(b):
 if isinstance(a, PrefixCond) and isinstance(b, PrefixCond):
 # longest prefix first
***************
*** 558,586 ****
 cur = self.db.cursor()
 column_num = -1
! for column, condition in conditionlist :
 column_num = column_num + 1
 searchkey = _search_col_data_key(table, column)
 # speedup: don't linear search columns within loop
! if column in columns :
 savethiscolumndata = 1 # save the data for return
! else :
 savethiscolumndata = 0 # data only used for selection
 
 try:
 key, data = cur.set_range(searchkey)
! while key[:len(searchkey)] == searchkey :
 # extract the rowid from the key
 rowid = key[-_rowid_str_len:]
 
! if not rejected_rowids.has_key(rowid) :
 # if no condition was specified or the condition
 # succeeds, add row to our match list.
! if not condition or condition(data) :
! if not matching_rowids.has_key(rowid) :
 matching_rowids[rowid] = {}
! if savethiscolumndata :
 matching_rowids[rowid][column] = data
! else :
! if matching_rowids.has_key(rowid) :
 del matching_rowids[rowid]
 rejected_rowids[rowid] = rowid
--- 571,599 ----
 cur = self.db.cursor()
 column_num = -1
! for column, condition in conditionlist:
 column_num = column_num + 1
 searchkey = _search_col_data_key(table, column)
 # speedup: don't linear search columns within loop
! if column in columns:
 savethiscolumndata = 1 # save the data for return
! else:
 savethiscolumndata = 0 # data only used for selection
 
 try:
 key, data = cur.set_range(searchkey)
! while key[:len(searchkey)] == searchkey:
 # extract the rowid from the key
 rowid = key[-_rowid_str_len:]
 
! if not rejected_rowids.has_key(rowid):
 # if no condition was specified or the condition
 # succeeds, add row to our match list.
! if not condition or condition(data):
! if not matching_rowids.has_key(rowid):
 matching_rowids[rowid] = {}
! if savethiscolumndata:
 matching_rowids[rowid][column] = data
! else:
! if matching_rowids.has_key(rowid):
 del matching_rowids[rowid]
 rejected_rowids[rowid] = rowid
***************
*** 589,593 ****
 
 except DBError, dberror:
! if dberror[0] != DB_NOTFOUND :
 raise
 continue
--- 602,606 ----
 
 except DBError, dberror:
! if dberror[0] != DB_NOTFOUND:
 raise
 continue
***************
*** 600,607 ****
 # extract any remaining desired column data from the
 # database for the matching rows.
! if len(columns) > 0 :
! for rowid, rowdata in matching_rowids.items() :
! for column in columns :
! if rowdata.has_key(column) :
 continue
 try:
--- 613,620 ----
 # extract any remaining desired column data from the
 # database for the matching rows.
! if len(columns) > 0:
! for rowid, rowdata in matching_rowids.items():
! for column in columns:
! if rowdata.has_key(column):
 continue
 try:
***************
*** 609,613 ****
 _data_key(table, column, rowid))
 except DBError, dberror:
! if dberror[0] != DB_NOTFOUND :
 raise
 rowdata[column] = None
--- 622,626 ----
 _data_key(table, column, rowid))
 except DBError, dberror:
! if dberror[0] != DB_NOTFOUND:
 raise
 rowdata[column] = None
***************
*** 617,623 ****
 
 
! def Drop(self, table) :
! """Remove an entire table from the database
! """
 txn = None
 try:
--- 630,635 ----
 
 
! def Drop(self, table):
! """Remove an entire table from the database"""
 txn = None
 try:
***************
*** 631,635 ****
 # delete all keys containing this tables column and row info
 table_key = _search_all_data_key(table)
! while 1 :
 try:
 key, data = cur.set_range(table_key)
--- 643,647 ----
 # delete all keys containing this tables column and row info
 table_key = _search_all_data_key(table)
! while 1:
 try:
 key, data = cur.set_range(table_key)
***************
*** 637,641 ****
 break
 # only delete items in this table
! if key[:len(table_key)] != table_key :
 break
 cur.delete()
--- 649,653 ----
 break
 # only delete items in this table
! if key[:len(table_key)] != table_key:
 break
 cur.delete()
***************
*** 643,647 ****
 # delete all rowids used by this table
 table_key = _search_rowid_key(table)
! while 1 :
 try:
 key, data = cur.set_range(table_key)
--- 655,659 ----
 # delete all rowids used by this table
 table_key = _search_rowid_key(table)
! while 1:
 try:
 key, data = cur.set_range(table_key)
***************
*** 649,653 ****
 break
 # only delete items in this table
! if key[:len(table_key)] != table_key :
 break
 cur.delete()
--- 661,665 ----
 break
 # only delete items in this table
! if key[:len(table_key)] != table_key:
 break
 cur.delete()
***************
*** 670,678 ****
 txn = None
 
! if self.__tablecolumns.has_key(table) :
 del self.__tablecolumns[table]
 
 except DBError, dberror:
! if txn :
 txn.abort()
 raise TableDBError, dberror[1]
--- 682,690 ----
 txn = None
 
! if self.__tablecolumns.has_key(table):
 del self.__tablecolumns[table]
 
 except DBError, dberror:
! if txn:
 txn.abort()
 raise TableDBError, dberror[1]

AltStyle によって変換されたページ (->オリジナル) /