1- # $Id: back_metakit.py,v 1.67 2004-03-22 07:45:39 richard Exp $
1+ # $Id: back_metakit.py,v 1.68 2004-03-24 05:33:13 richard Exp $
22'''Metakit backend for Roundup, originally by Gordon McMillan.
33
44Known Current Bugs:
4848from indexer_dbm import Indexer
4949import locking
5050from roundup .date import Range
51+ from blobfiles import files_in_dir
5152
5253# view modes for opening
5354# XXX FIXME BPK -> these don't do anything, they are ignored
@@ -324,6 +325,12 @@ def setid(self, classname, maxid):
324325 ''' No-op in metakit
325326 '''
326327 pass
328+
329+ def numfiles (self ):
330+ '''Get number of files in storage, even across subdirectories.
331+ '''
332+ files_dir = os .path .join (self .config .DATABASE , 'files' )
333+ return files_in_dir (files_dir )
327334
328335_STRINGTYPE = type ('' )
329336_LISTTYPE = type ([])
@@ -377,6 +384,7 @@ def __init__(self, db, classname, **properties):
377384 if view :
378385 self .maxid = view [- 1 ].id + 1
379386 self .uncommitted = {}
387+ self .comactions = []
380388 self .rbactions = []
381389
382390 # people reach inside!!
@@ -1533,11 +1541,15 @@ def _commit(self):
15331541 ''' called post commit of the DB.
15341542 interested subclasses may override '''
15351543 self .uncommitted = {}
1544+ for action in self .comactions :
1545+ action ()
1546+ self .comactions = []
15361547 self .rbactions = []
15371548 self .idcache = {}
15381549 def _rollback (self ):
15391550 ''' called pre rollback of the DB.
15401551 interested subclasses may override '''
1552+ self .comactions = []
15411553 for action in self .rbactions :
15421554 action ()
15431555 self .rbactions = []
@@ -1551,6 +1563,10 @@ def _clear(self):
15511563 iv = self .getindexview (READWRITE )
15521564 if iv :
15531565 iv [:] = []
1566+ def commitaction (self , action ):
1567+ ''' call this to register a callback called on commit
1568+ callback is removed on end of transaction '''
1569+ self .comactions .append (action )
15541570 def rollbackaction (self , action ):
15551571 ''' call this to register a callback called on rollback
15561572 callback is removed on end of transaction '''
@@ -1732,6 +1748,8 @@ def get(self, nodeid, propname, default=_marker, cache=1):
17321748 if propname == 'content' :
17331749 poss_msg = 'Possibly an access right configuration problem.'
17341750 fnm = self .gen_filename (nodeid )
1751+ if not os .path .exists (fnm ):
1752+ fnm = fnm + '.tmp'
17351753 try :
17361754 f = open (fnm , 'rb' )
17371755 except IOError , (strerror ):
@@ -1758,16 +1776,56 @@ def create(self, **propvalues):
17581776
17591777 # figure a filename
17601778 nm = self .gen_filename (newid )
1761- open (nm , 'wb' ).write (content )
1779+ f = open (nm + '.tmp' , 'wb' )
1780+ f .write (content )
1781+ f .close ()
17621782
17631783 mimetype = propvalues .get ('type' , self .default_mime_type )
17641784 self .db .indexer .add_text ((self .classname , newid , 'content' ), content ,
17651785 mimetype )
1786+
1787+ # register commit and rollback actions
1788+ def commit (fnm = nm ):
1789+ os .rename (fnm + '.tmp' , fnm )
1790+ self .commitaction (commit )
17661791 def undo (fnm = nm ):
1767- os .remove (fnm )
1792+ os .remove (fnm + '.tmp' )
17681793 self .rollbackaction (undo )
17691794 return newid
17701795
1796+ def set (self , itemid , ** propvalues ):
1797+ if not propvalues :
1798+ return
1799+ self .fireAuditors ('set' , None , propvalues )
1800+
1801+ content = propvalues .get ('content' , None )
1802+ if content is not None :
1803+ del propvalues ['content' ]
1804+
1805+ propvalues , oldnode = Class .set_inner (self , itemid , ** propvalues )
1806+
1807+ # figure a filename
1808+ if content is not None :
1809+ nm = self .gen_filename (itemid )
1810+ f = open (nm + '.tmp' , 'wb' )
1811+ f .write (content )
1812+ f .close ()
1813+ mimetype = propvalues .get ('type' , self .default_mime_type )
1814+ self .db .indexer .add_text ((self .classname , itemid , 'content' ),
1815+ content , mimetype )
1816+
1817+ # register commit and rollback actions
1818+ def commit (fnm = nm ):
1819+ if os .path .exists (fnm ):
1820+ os .remove (fnm )
1821+ os .rename (fnm + '.tmp' , fnm )
1822+ self .commitaction (commit )
1823+ def undo (fnm = nm ):
1824+ os .remove (fnm + '.tmp' )
1825+ self .rollbackaction (undo )
1826+
1827+ self .fireReactors ('set' , oldnode , propvalues )
1828+
17711829 def index (self , nodeid ):
17721830 Class .index (self , nodeid )
17731831 mimetype = self .get (nodeid , 'type' )
0 commit comments