Skip to content

Commit

Permalink
Minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
knutin committed Feb 17, 2016
1 parent cbf6866 commit c6cef91
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions traildb/traildb.py
Expand Up @@ -29,7 +29,6 @@ class tdb_event(Structure):
("items", tdb_item * 0)]



api(lib.tdb_cons_open, [tdb_cons, c_char_p, POINTER(c_char_p), c_uint64], tdb_error)
api(lib.tdb_cons_close, [tdb_cons])
api(lib.tdb_cons_add,
Expand All @@ -41,9 +40,9 @@ class tdb_event(Structure):
api(lib.tdb_open, [tdb, c_char_p], tdb_error)
api(lib.tdb_close, [tdb])

api(lib.tdb_lexicon_size, [tdb, tdb_field], c_int)
api(lib.tdb_lexicon_size, [tdb, tdb_field], tdb_error)

api(lib.tdb_get_field, [tdb, c_char_p], c_uint)
api(lib.tdb_get_field, [tdb, c_char_p], tdb_error)
api(lib.tdb_get_field_name, [tdb, tdb_field], c_char_p)

api(lib.tdb_get_item, [tdb, tdb_field, c_char_p, c_uint64], tdb_item)
Expand All @@ -53,13 +52,16 @@ class tdb_event(Structure):
api(lib.tdb_get_uuid, [tdb, c_uint64], POINTER(c_ubyte))
api(lib.tdb_get_trail_id, [tdb, POINTER(c_ubyte)], c_uint64)

api(lib.tdb_error_str, [tdb_error], c_char_p)

api(lib.tdb_num_trails, [tdb], c_uint64)
api(lib.tdb_num_events, [tdb], c_uint64)
api(lib.tdb_num_fields, [tdb], c_uint64)
api(lib.tdb_min_timestamp, [tdb], c_uint64)
api(lib.tdb_max_timestamp, [tdb], c_uint64)

api(lib.tdb_version, [tdb], c_uint64)

api(lib.tdb_cursor_new, [tdb], tdb_cursor)
api(lib.tdb_cursor_free, [tdb])
api(lib.tdb_cursor_next, [tdb_cursor], POINTER(tdb_event))
Expand Down Expand Up @@ -110,8 +112,9 @@ def __init__(self, path, ofields=()):
n = len(ofields)

ofield_names = (c_char_p * n)(*[name + '\x00' for name in ofields])
ofield_names_p = cast(ofield_names, POINTER(c_char_p))
self._cons = lib.tdb_cons_init()
if lib.tdb_cons_open(self._cons, path, cast(ofield_names, POINTER(c_char_p)), n) != 0:
if lib.tdb_cons_open(self._cons, path, ofield_names_p, n) != 0:
raise TrailDBError("Cannot open constructor")

self.path = path
Expand All @@ -127,7 +130,8 @@ def add(self, cookie, time, values=()):
n = len(self.ofields)
value_array = (c_char_p * n)(*values)
value_lengths = (c_uint64 * n)(*[len(v) for v in values])
f = lib.tdb_cons_add(self._cons, rawcookie(cookie), time, value_array, value_lengths)
f = lib.tdb_cons_add(self._cons, rawcookie(cookie), time, value_array,
value_lengths)
if f:
raise TrailDBError("Too many values: %s" % values[f])

Expand All @@ -152,7 +156,8 @@ def __init__(self, cursor, cls, valuefun):
self.valuefun = valuefun

def __del__(self):
lib.tdb_cursor_free(self.cursor)
if self.cursor:
lib.tdb_cursor_free(self.cursor)

def __iter__(self):
return self
Expand Down Expand Up @@ -184,21 +189,13 @@ def __init__(self, path):
self.num_fields = lib.tdb_num_fields(db)
self.fields = [lib.tdb_get_field_name(db, i) for i in xrange(self.num_fields)]
self._evcls = namedtuple('event', self.fields, rename=True)
self._trail_buf_size = 0
self._grow_buffer()

self._cursor = None

def __del__(self):
if self._cursor:
lib.tdb_cursor_free(self._cursor)
lib.tdb_close(self._db)

def _grow_buffer(self, increment=1000000):
self._trail_buf_size += increment
self._trail_buf = (c_uint64 * self._trail_buf_size)()
return self._trail_buf, self._trail_buf_size

def __contains__(self, cookieish):
try:
self[cookieish]
Expand All @@ -222,11 +219,11 @@ def crumbs(self, **kwds):

def trail(self, i):
if self._cursor:
raise Exception("cursor already created")
raise TrailDBError("Cursor already created")

cursor = lib.tdb_cursor_new(self._db)
if lib.tdb_get_trail(cursor, i) != 0:
raise Exception("foo")
raise TrailDBError("Failed to create cursor")

return TrailDBCursor(cursor, self._evcls, self.value)

Expand Down Expand Up @@ -373,7 +370,3 @@ def construct_clause(clause_arr):
i += 1 + clause_len

return q




0 comments on commit c6cef91

Please sign in to comment.