Skip to content
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

Merged
merged 4 commits into from
Feb 20, 2015
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 16 additions & 16 deletions chest/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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):
Expand All @@ -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)

Expand All @@ -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():
Copy link
Member

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.

raise KeyError("Key not found: %s" % key)

self.get_from_disk(key)
Expand All @@ -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():
Copy link
Member

Choose a reason for hiding this comment

The 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:
Expand All @@ -202,13 +202,13 @@ def __del__(self):
self.drop() # pragma: no cover

def __iter__(self):
return iter(self._keys)
return iter(self._keys.keys())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iter(dict) also iterates over keys


def __len__(self):
return len(self._keys)
return len(self._keys.keys())
Copy link
Member

Choose a reason for hiding this comment

The 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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


@property
def memory_usage(self):
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need iter. keeping items is good though.


def flush(self):
""" Flush all in-memory storage to disk """
Expand Down Expand Up @@ -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():
Copy link
Member

Choose a reason for hiding this comment

The 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):
Expand Down