Updated to be cooler and allow float maxmods.

This commit is contained in:
Jeremy Fincher 2004-09-30 04:13:28 +00:00
parent 69dce4761d
commit ec9258a371

View File

@ -65,23 +65,23 @@ def dump(map, fd=sys.stdout):
for (key, value) in map.iteritems(): for (key, value) in map.iteritems():
fd.write('+%s,%s:%s->%s\n' % (len(key), len(value), key, value)) fd.write('+%s,%s:%s->%s\n' % (len(key), len(value), key, value))
def open(filename, mode='r'): def open(filename, mode='r', **kwargs):
"""Opens a database; used for compatibility with other database modules.""" """Opens a database; used for compatibility with other database modules."""
if mode == 'r': if mode == 'r':
return Reader(filename) return Reader(filename, **kwargs)
elif mode == 'w': elif mode == 'w':
return ReaderWriter(filename) return ReaderWriter(filename, **kwargs)
elif mode == 'c': elif mode == 'c':
if os.path.exists(filename): if os.path.exists(filename):
return ReaderWriter(filename) return ReaderWriter(filename, **kwargs)
else: else:
maker = Maker(filename) maker = Maker(filename)
maker.finish() maker.finish()
return ReaderWriter(filename) return ReaderWriter(filename, **kwargs)
elif mode == 'n': elif mode == 'n':
maker = Maker(filename) maker = Maker(filename)
maker.finish() maker.finish()
return ReaderWriter(filename) return ReaderWriter(filename, **kwargs)
else: else:
raise ValueError, 'Invalid flag: %s' % mode raise ValueError, 'Invalid flag: %s' % mode
@ -334,8 +334,7 @@ class ReaderWriter(utils.IterableMap):
except IOError: except IOError:
pass pass
if removals or adds: if removals or adds:
tempfilename = utils.mktemp('.db') maker = Maker(self.filename)
maker = Maker(tempfilename)
cdb = Reader(self.filename) cdb = Reader(self.filename)
for (key, value) in cdb.iteritems(): for (key, value) in cdb.iteritems():
if key in removals: if key in removals:
@ -352,7 +351,6 @@ class ReaderWriter(utils.IterableMap):
maker.add(key, value) maker.add(key, value)
cdb.close() cdb.close()
maker.finish() maker.finish()
os.rename(tempfilename, self.filename)
if os.path.exists(self.journalName): if os.path.exists(self.journalName):
os.remove(self.journalName) os.remove(self.journalName)
@ -367,9 +365,15 @@ class ReaderWriter(utils.IterableMap):
def _flushIfOverLimit(self): def _flushIfOverLimit(self):
if self.maxmods: if self.maxmods:
if self.mods > self.maxmods: if isinstance(self.maxmods, int):
self.flush() if self.mods > self.maxmods:
self.mods = 0 self.flush()
self.mods = 0
elif isinstance(self.maxmods, float):
assert 0 <= self.maxmods
if self.mods / max(len(self.cdb), 100) > self.maxmods:
self.flush()
self.mods = 0
def __getitem__(self, key): def __getitem__(self, key):
if key in self.removals: if key in self.removals: