-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store filenames #4
Changes from 2 commits
b62105b
76afc74
c762816
5ade9b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,7 +82,7 @@ def __init__(self, data=None, path=None, available_memory=None, | |
# In memory storage | ||
self.inmem = data or dict() | ||
# A set of keys held both in memory or on disk | ||
self._keys = set() | ||
self._keys = {} | ||
# Was a path given or no? If not we'll clean up the directory later | ||
self._explicitly_given_path = path is not None | ||
# Diretory where the on-disk data will be held | ||
|
@@ -101,7 +101,7 @@ def __init__(self, data=None, path=None, available_memory=None, | |
keyfile = os.path.join(self.path, '.keys') | ||
if os.path.exists(keyfile): | ||
with open(keyfile, mode='r'+self.mode) as f: | ||
self._keys = set(self.load(f)) | ||
self._keys = dict(self.load(f)) | ||
|
||
self.lock = Lock() | ||
|
||
|
@@ -123,7 +123,7 @@ def key_to_filename(self, key): | |
def move_to_disk(self, key): | ||
""" Move data from memory onto disk """ | ||
self._on_overflow(key) | ||
fn = self.key_to_filename(key) | ||
fn = self._keys[key] | ||
if not os.path.exists(fn): # Only write if it doesn't exist. | ||
dir = os.path.dirname(fn) | ||
if not os.path.exists(dir): | ||
|
@@ -143,7 +143,7 @@ def get_from_disk(self, key): | |
|
||
self._on_miss(key) | ||
|
||
fn = self.key_to_filename(key) | ||
fn = self._keys[key] | ||
with open(fn, mode='r'+self.mode) as f: | ||
value = self.load(f) | ||
|
||
|
@@ -154,7 +154,7 @@ def __getitem__(self, key): | |
if key in self.inmem: | ||
value = self.inmem[key] | ||
else: | ||
if key not in self._keys: | ||
if key not in self._keys.keys(): | ||
raise KeyError("Key not found: %s" % key) | ||
|
||
self.get_from_disk(key) | ||
|
@@ -176,19 +176,19 @@ def __delitem__(self, key): | |
if key in self.heap: | ||
del self.heap[key] | ||
|
||
fn = self.key_to_filename(key) | ||
fn = self._keys[key] | ||
if os.path.exists(fn): | ||
os.remove(fn) | ||
|
||
self._keys.remove(key) | ||
del self._keys[key] | ||
|
||
def __setitem__(self, key, value): | ||
with self.lock: | ||
if key in self._keys: | ||
if key in self._keys.keys(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Old version is best. |
||
del self[key] | ||
|
||
self.inmem[key] = value | ||
self._keys.add(key) | ||
self._keys[key] = self.key_to_filename(key) | ||
self._update_lru(key) | ||
|
||
with self.lock: | ||
|
@@ -202,13 +202,13 @@ def __del__(self): | |
self.drop() # pragma: no cover | ||
|
||
def __iter__(self): | ||
return iter(self._keys) | ||
return iter(self._keys.keys()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
def __len__(self): | ||
return len(self._keys) | ||
return len(self._keys.keys()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
|
||
def __contains__(self, key): | ||
return key in self._keys | ||
return key in self._keys.keys() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
|
||
@property | ||
def memory_usage(self): | ||
|
@@ -242,7 +242,7 @@ def drop(self): | |
def write_keys(self): | ||
fn = os.path.join(self.path, '.keys') | ||
with open(fn, mode='w'+self.mode) as f: | ||
self.dump(list(self._keys), f) | ||
self.dump(list(iter(self._keys.items())), f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need |
||
|
||
def flush(self): | ||
""" Flush all in-memory storage to disk """ | ||
|
@@ -270,18 +270,18 @@ def update(self, other, overwrite=True): | |
# if already flushed, then this does nothing | ||
self.flush() | ||
other.flush() | ||
for key in other._keys: | ||
for key in other._keys.keys(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
if key in self._keys and overwrite: | ||
del self[key] | ||
elif key in self._keys and not overwrite: | ||
continue | ||
old_fn = os.path.join(other.path, other._key_to_filename(key)) | ||
old_fn = other._keys[key] | ||
new_fn = os.path.join(self.path, self._key_to_filename(key)) | ||
dir = os.path.dirname(new_fn) | ||
if not os.path.exists(dir): | ||
os.makedirs(dir) | ||
os.link(old_fn, new_fn) | ||
self._keys.add(key) | ||
self._keys[key] = new_fn | ||
|
||
|
||
def nbytes(o): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old version is probably best
in my_dict
actually checks for membership in the keys. Adding the.keys()
method creates an explicit list.