1515# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717#
18- #$Id: back_anydbm.py,v 1.159 2004-07-01 03:55:47 richard Exp $
18+ #$Id: back_anydbm.py,v 1.160 2004-07-02 05:22:09 richard Exp $
1919'''This module defines a backend that saves the hyperdatabase in a
2020database chosen by anydbm. It is guaranteed to always be available in python
2121versions >2.1.1 (the dumbdbm fallback in 2.1.1 and earlier has several
@@ -124,14 +124,10 @@ def __repr__(self):
124124 def __getattr__ (self , classname ):
125125 '''A convenient way of calling self.getclass(classname).'''
126126 if self .classes .has_key (classname ):
127- if __debug__ :
128- print >> hyperdb .DEBUG , '__getattr__' , (self , classname )
129127 return self .classes [classname ]
130128 raise AttributeError , classname
131129
132130 def addclass (self , cl ):
133- if __debug__ :
134- print >> hyperdb .DEBUG , 'addclass' , (self , cl )
135131 cn = cl .classname
136132 if self .classes .has_key (cn ):
137133 raise ValueError , cn
@@ -145,8 +141,6 @@ def addclass(self, cl):
145141
146142 def getclasses (self ):
147143 '''Return a list of the names of all existing classes.'''
148- if __debug__ :
149- print >> hyperdb .DEBUG , 'getclasses' , (self ,)
150144 l = self .classes .keys ()
151145 l .sort ()
152146 return l
@@ -156,8 +150,6 @@ def getclass(self, classname):
156150
157151 If 'classname' is not a valid class name, a KeyError is raised.
158152 '''
159- if __debug__ :
160- print >> hyperdb .DEBUG , 'getclass' , (self , classname )
161153 try :
162154 return self .classes [classname ]
163155 except KeyError :
@@ -169,8 +161,7 @@ def getclass(self, classname):
169161 def clear (self ):
170162 '''Delete all database contents
171163 '''
172- if __debug__ :
173- print >> hyperdb .DEBUG , 'clear' , (self ,)
164+ self .config .logging .getLogger ('hyperdb' ).info ('clear' )
174165 for cn in self .classes .keys ():
175166 for dummy in 'nodes' , 'journals' :
176167 path = os .path .join (self .dir , 'journals.%s' % cn )
@@ -183,8 +174,6 @@ def getclassdb(self, classname, mode='r'):
183174 ''' grab a connection to the class db that will be used for
184175 multiple actions
185176 '''
186- if __debug__ :
187- print >> hyperdb .DEBUG , 'getclassdb' , (self , classname , mode )
188177 return self .opendb ('nodes.%s' % classname , mode )
189178
190179 def determine_db_type (self , path ):
@@ -205,17 +194,14 @@ def opendb(self, name, mode):
205194 '''Low-level database opener that gets around anydbm/dbm
206195 eccentricities.
207196 '''
208- if __debug__ :
209- print >> hyperdb .DEBUG , 'opendb' , (self , name , mode )
210-
211197 # figure the class db type
212198 path = os .path .join (os .getcwd (), self .dir , name )
213199 db_type = self .determine_db_type (path )
214200
215201 # new database? let anydbm pick the best dbm
216202 if not db_type :
217203 if __debug__ :
218- print >> hyperdb . DEBUG , "opendb anydbm.open(%r, 'c')" % path
204+ self . config . logging . getLogger ( ' hyperdb' ). debug ( "opendb anydbm.open(%r, 'c')" % path )
219205 return anydbm .open (path , 'c' )
220206
221207 # open the database with the correct module
@@ -226,8 +212,8 @@ def opendb(self, name, mode):
226212 "Couldn't open database - the required module '%s'" \
227213 " is not available" % db_type
228214 if __debug__ :
229- print >> hyperdb . DEBUG , "opendb %r.open(%r, %r)" % (db_type , path ,
230- mode )
215+ self . config . logging . getLogger ( ' hyperdb' ). debug ( "opendb %r.open(%r, %r)" % (db_type , path ,
216+ mode ))
231217 return dbm .open (path , mode )
232218
233219 #
@@ -261,9 +247,6 @@ def setid(self, classname, setid):
261247 def addnode (self , classname , nodeid , node ):
262248 ''' add the specified node to its class's db
263249 '''
264- if __debug__ :
265- print >> hyperdb .DEBUG , 'addnode' , (self , classname , nodeid , node )
266-
267250 # we'll be supplied these props if we're doing an import
268251 if not node .has_key ('creator' ):
269252 # add in the "calculated" properties (dupe so we don't affect
@@ -280,8 +263,6 @@ def addnode(self, classname, nodeid, node):
280263 def setnode (self , classname , nodeid , node ):
281264 ''' change the specified node
282265 '''
283- if __debug__ :
284- print >> hyperdb .DEBUG , 'setnode' , (self , classname , nodeid , node )
285266 self .dirtynodes .setdefault (classname , {})[nodeid ] = 1
286267
287268 # update the activity time (dupe so we don't affect
@@ -298,7 +279,7 @@ def savenode(self, classname, nodeid, node):
298279 ''' perform the saving of data specified by the set/addnode
299280 '''
300281 if __debug__ :
301- print >> hyperdb . DEBUG , 'savenode' , ( self , classname , nodeid , node )
282+ self . config . logging . getLogger ( 'hyperdb' ). debug ( 'save %s%s %r' % ( classname , nodeid , node ) )
302283 self .transactions .append ((self .doSaveNode , (classname , nodeid , node )))
303284
304285 def getnode (self , classname , nodeid , db = None , cache = 1 ):
@@ -307,22 +288,18 @@ def getnode(self, classname, nodeid, db=None, cache=1):
307288 Note the "cache" parameter is not used, and exists purely for
308289 backward compatibility!
309290 '''
310- if __debug__ :
311- print >> hyperdb .DEBUG , 'getnode' , (self , classname , nodeid , db )
312-
313291 # try the cache
314292 cache_dict = self .cache .setdefault (classname , {})
315293 if cache_dict .has_key (nodeid ):
316294 if __debug__ :
317- print >> hyperdb .TRACE , 'get %s %s cached' % (classname ,
318- nodeid )
295+ self .config .logging .getLogger ('hyperdb' ).debug ('get %s%s cached' % (classname , nodeid ))
319296 self .stats ['cache_hits' ] += 1
320297 return cache_dict [nodeid ]
321298
322299 if __debug__ :
323300 self .stats ['cache_misses' ] += 1
324301 start_t = time .time ()
325- print >> hyperdb . TRACE , 'get %s %s' % (classname , nodeid )
302+ self . config . logging . getLogger ( ' hyperdb' ). debug ( 'get %s%s' % (classname , nodeid ) )
326303
327304 # get from the database and save in the cache
328305 if db is None :
@@ -354,8 +331,7 @@ def destroynode(self, classname, nodeid):
354331 '''Remove a node from the database. Called exclusively by the
355332 destroy() method on Class.
356333 '''
357- if __debug__ :
358- print >> hyperdb .DEBUG , 'destroynode' , (self , classname , nodeid )
334+ self .config .logging .getLogger ('hyperdb' ).info ('destroy %s%s' % (classname , nodeid ))
359335
360336 # remove from cache and newnodes if it's there
361337 if (self .cache .has_key (classname ) and
@@ -380,8 +356,6 @@ def serialise(self, classname, node):
380356 '''Copy the node contents, converting non-marshallable data into
381357 marshallable data.
382358 '''
383- if __debug__ :
384- print >> hyperdb .DEBUG , 'serialise' , classname , node
385359 properties = self .getclass (classname ).getprops ()
386360 d = {}
387361 for k , v in node .items ():
@@ -407,8 +381,6 @@ def serialise(self, classname, node):
407381 def unserialise (self , classname , node ):
408382 '''Decode the marshalled node data
409383 '''
410- if __debug__ :
411- print >> hyperdb .DEBUG , 'unserialise' , classname , node
412384 properties = self .getclass (classname ).getprops ()
413385 d = {}
414386 for k , v in node .items ():
@@ -436,17 +408,10 @@ def unserialise(self, classname, node):
436408 def hasnode (self , classname , nodeid , db = None ):
437409 ''' determine if the database has a given node
438410 '''
439- if __debug__ :
440- print >> hyperdb .DEBUG , 'hasnode' , (self , classname , nodeid , db )
441-
442411 # try the cache
443412 cache = self .cache .setdefault (classname , {})
444413 if cache .has_key (nodeid ):
445- if __debug__ :
446- print >> hyperdb .TRACE , 'has %s %s cached' % (classname , nodeid )
447414 return 1
448- if __debug__ :
449- print >> hyperdb .TRACE , 'has %s %s' % (classname , nodeid )
450415
451416 # not in the cache - check the database
452417 if db is None :
@@ -455,9 +420,6 @@ def hasnode(self, classname, nodeid, db=None):
455420 return res
456421
457422 def countnodes (self , classname , db = None ):
458- if __debug__ :
459- print >> hyperdb .DEBUG , 'countnodes' , (self , classname , db )
460-
461423 count = 0
462424
463425 # include the uncommitted nodes
@@ -490,16 +452,16 @@ def addjournal(self, classname, nodeid, action, params, creator=None,
490452 'retire' -- 'params' is None
491453 '''
492454 if __debug__ :
493- print >> hyperdb . DEBUG , 'addjournal' , ( self , classname , nodeid ,
494- action , params , creator , creation )
455+ self . config . logging . getLogger ( ' hyperdb' ). debug ( 'addjournal %s%s %s %r %s %r' % ( classname ,
456+ nodeid , action , params , creator , creation ) )
495457 self .transactions .append ((self .doSaveJournal , (classname , nodeid ,
496458 action , params , creator , creation )))
497459
498460 def setjournal (self , classname , nodeid , journal ):
499461 '''Set the journal to the "journal" list.'''
500462 if __debug__ :
501- print >> hyperdb . DEBUG , 'setjournal' , ( self , classname , nodeid ,
502- journal )
463+ self . config . logging . getLogger ( ' hyperdb' ). debug ( 'setjournal %s%s %r' % ( classname ,
464+ nodeid , journal ) )
503465 self .transactions .append ((self .doSetJournal , (classname , nodeid ,
504466 journal )))
505467
@@ -509,9 +471,6 @@ def getjournal(self, classname, nodeid):
509471 Raise IndexError if the node doesn't exist (as per history()'s
510472 API)
511473 '''
512- if __debug__ :
513- print >> hyperdb .DEBUG , 'getjournal' , (self , classname , nodeid )
514-
515474 # our journal result
516475 res = []
517476
@@ -562,11 +521,9 @@ def getjournal(self, classname, nodeid):
562521 def pack (self , pack_before ):
563522 ''' Delete all journal entries except "create" before 'pack_before'.
564523 '''
565- if __debug__ :
566- print >> hyperdb .DEBUG , 'packjournal' , (self , pack_before )
567-
568524 pack_before = pack_before .serialise ()
569525 for classname in self .getclasses ():
526+ packed = 0
570527 # get the journal db
571528 db_name = 'journals.%s' % classname
572529 path = os .path .join (os .getcwd (), self .dir , classname )
@@ -586,7 +543,13 @@ def pack(self, pack_before):
586543 # create entry, then it stays
587544 if date_stamp > pack_before or action == 'create' :
588545 l .append (entry )
546+ else :
547+ packed += 1
589548 db [key ] = marshal .dumps (l )
549+
550+ self .config .logging .getLogger ('hyperdb' ).info ('packed %d %s items' % (packed ,
551+ classname ))
552+
590553 if db_type == 'gdbm' :
591554 db .reorganize ()
592555 db .close ()
@@ -598,8 +561,8 @@ def pack(self, pack_before):
598561 def commit (self ):
599562 ''' Commit the current transactions.
600563 '''
601- if __debug__ :
602- print >> hyperdb . DEBUG , 'commit' , (self , )
564+ self . config . logging . getLogger ( 'hyperdb' ). info ( 'commit %s transactions' % (
565+ len (self . transactions )) )
603566
604567 # keep a handle to all the database files opened
605568 self .databases = {}
@@ -617,7 +580,6 @@ def commit(self):
617580
618581 # reindex the nodes that request it
619582 for classname , nodeid in filter (None , reindex .keys ()):
620- print >> hyperdb .DEBUG , 'commit.reindex' , (classname , nodeid )
621583 self .getclass (classname ).index (nodeid )
622584
623585 # save the indexer state
@@ -643,10 +605,6 @@ def getCachedClassDB(self, classname):
643605 return self .databases [db_name ]
644606
645607 def doSaveNode (self , classname , nodeid , node ):
646- if __debug__ :
647- print >> hyperdb .DEBUG , 'doSaveNode' , (self , classname , nodeid ,
648- node )
649-
650608 db = self .getCachedClassDB (classname )
651609
652610 # now save the marshalled data
@@ -685,9 +643,6 @@ def doSaveJournal(self, classname, nodeid, action, params, creator,
685643 # create the journal entry
686644 entry = (nodeid , journaldate , journaltag , action , params )
687645
688- if __debug__ :
689- print >> hyperdb .DEBUG , 'doSaveJournal' , entry
690-
691646 db = self .getCachedJournalDB (classname )
692647
693648 # now insert the journal entry
@@ -714,9 +669,6 @@ def doSetJournal(self, classname, nodeid, journal):
714669 db [nodeid ] = marshal .dumps (l )
715670
716671 def doDestroyNode (self , classname , nodeid ):
717- if __debug__ :
718- print >> hyperdb .DEBUG , 'doDestroyNode' , (self , classname , nodeid )
719-
720672 # delete from the class database
721673 db = self .getCachedClassDB (classname )
722674 if db .has_key (nodeid ):
@@ -733,8 +685,9 @@ def doDestroyNode(self, classname, nodeid):
733685 def rollback (self ):
734686 ''' Reverse all actions from the current transaction.
735687 '''
736- if __debug__ :
737- print >> hyperdb .DEBUG , 'rollback' , (self , )
688+ self .config .logging .getLogger ('hyperdb' ).info ('rollback %s transactions' % (
689+ len (self .transactions )))
690+
738691 for method , args in self .transactions :
739692 # delete temporary files
740693 if method == self .doStoreFile :
@@ -1552,9 +1505,6 @@ def list(self):
15521505 def getnodeids (self , db = None ):
15531506 ''' Return a list of ALL nodeids
15541507 '''
1555- if __debug__ :
1556- print >> hyperdb .DEBUG , 'getnodeids' , (self , self .classname , db )
1557-
15581508 res = []
15591509
15601510 # start off with the new nodes
0 commit comments