Browse Source

Make helper for removing or truncating a file; use it

tags/nilmdb-1.3.0
Jim Paris 11 years ago
parent
commit
7860a6aefb
1 changed files with 21 additions and 12 deletions
  1. +21
    -12
      nilmdb/server/bulkdata.py

+ 21
- 12
nilmdb/server/bulkdata.py View File

@@ -283,6 +283,25 @@ class Table(object):
row = (filenum * self.rows_per_file) + (offset // self.row_size)
return row

def _remove_or_truncate_file(self, subdir, filename, offset = 0):
"""Remove the given file, and remove the subdirectory too
if it's empty. If offset is nonzero, truncate the file
to that size instead."""
# Close potentially open file in file_open LRU cache
self.file_open.cache_remove(self, subdir, filename)
if offset:
# Truncate it
with open(os.path.join(self.root, subdir, filename), "r+b") as f:
f.truncate(offset)
else:
# Remove file
os.remove(os.path.join(self.root, subdir, filename))
# Try deleting subdir, too
try:
os.rmdir(os.path.join(self.root, subdir))
except:
pass

# Cache open files
@nilmdb.utils.lru_cache(size = fd_cache_size,
onremove = lambda f: f.close())
@@ -379,8 +398,7 @@ class Table(object):
"""Helper to mark specific rows as being removed from a
file, and potentially removing or truncating the file itself."""
# Import an existing list of deleted rows for this file
datafile = os.path.join(self.root, subdir, filename)
cachefile = datafile + ".removed"
cachefile = os.path.join(self.root, subdir, filename + ".removed")
try:
with open(cachefile, "rb") as f:
ranges = pickle.load(f)
@@ -418,19 +436,10 @@ class Table(object):
# are generally easier if we don't have to special-case that.
if (len(merged) == 1 and
merged[0][0] == 0 and merged[0][1] == self.rows_per_file):
# Close potentially open file in file_open LRU cache
self.file_open.cache_remove(self, subdir, filename)

# Delete files
os.remove(datafile)
if cachefile_present:
os.remove(cachefile)

# Try deleting subdir, too
try:
os.rmdir(os.path.join(self.root, subdir))
except:
pass
self._remove_or_truncate_file(subdir, filename, 0)
else:
# Update cache. Try to do it atomically.
nilmdb.utils.atomic.replace_file(cachefile,


Loading…
Cancel
Save