Skip to content

Commit

Permalink
Fix bug whereby it is not possible to iterate over an empty Lmdb data…
Browse files Browse the repository at this point in the history
…base
  • Loading branch information
JohnVinyard committed Apr 1, 2016
1 parent f7b904c commit 6c64176
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion featureflow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.5.2'
__version__ = '0.5.3'

from model import BaseModel

Expand Down
6 changes: 5 additions & 1 deletion featureflow/lmdbstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ def size(self, key):
return len(buf)

def iter_ids(self):
db = self.dbs.values()[0]
try:
db = self.dbs.values()[0]
except IndexError:
return

with self.env.begin() as txn:
cursor = txn.cursor(db)
for _id in cursor.iternext(keys=True, values=False):
Expand Down
16 changes: 14 additions & 2 deletions featureflow/test_lmdbstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,44 @@ def setUp(self):
key_builder=self.key_builder)
self.value = os.urandom(1000)
self.key = self.key_builder.build('id', 'feature')
with self.db.write_stream(self.key, 'application/octet-stream') as ws:
ws.write(self.value)

def tearDown(self):
shutil.rmtree(self.path, ignore_errors=True)

def write_key(self):
with self.db.write_stream(self.key, 'application/octet-stream') as ws:
ws.write(self.value)

def test_can_seek_to_beginning_of_value(self):
self.write_key()
with self.db.read_stream(self.key) as rs:
rs.read(100)
rs.seek(0, os.SEEK_SET)
self.assertEqual(0, rs.tell())
self.assertEqual(rs.read(100), self.value[:100])

def test_can_seek_relative_to_current_position(self):
self.write_key()
with self.db.read_stream(self.key) as rs:
rs.read(100)
rs.seek(100, os.SEEK_CUR)
self.assertEqual(200, rs.tell())
self.assertEqual(rs.read(100), self.value[200:300])

def test_can_seek_relative_to_end_of_value(self):
self.write_key()
with self.db.read_stream(self.key) as rs:
rs.seek(-100, os.SEEK_END)
self.assertEqual(900, rs.tell())
self.assertEqual(rs.read(100), self.value[-100:])

def test_invalid_seek_argument_raises(self):
self.write_key()
with self.db.read_stream(self.key) as rs:
self.assertRaises(IOError, lambda: rs.seek(0, 999))

def test_can_iterate_over_empty_database(self):
_ids = list(self.db.iter_ids())
self.assertEqual(0, len(_ids))


0 comments on commit 6c64176

Please sign in to comment.