WIP: Implementing sqlite objects storage #13
|
@ -39,15 +39,16 @@ class Inventory():
|
||||||
cur.execute('VACUUM')
|
cur.execute('VACUUM')
|
||||||
cur.execute("INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,))
|
cur.execute("INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,))
|
||||||
self._db.commit()
|
self._db.commit()
|
||||||
|
self._db.row_factory = self.__object
|
||||||
|
|
||||||
def __objects(self, cur):
|
@staticmethod
|
||||||
return (
|
def __object(cursor, row):
|
||||||
structure.Object(
|
if len(cursor.description) != 8:
|
||||||
expires, obj_type, version, stream, data, offset,
|
return row
|
||||||
tag=tag, vector=vector)
|
vector, expires, obj_type, version, stream, tag, data, offset = row
|
||||||
for (vector, expires, obj_type, version, stream, tag, data, offset)
|
return structure.Object(
|
||||||
in cur.fetchall()
|
expires, obj_type, version, stream, data, offset,
|
||||||
)
|
tag=tag, vector=vector)
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
"""Remove expired objects"""
|
"""Remove expired objects"""
|
||||||
|
@ -74,7 +75,7 @@ class Inventory():
|
||||||
cur = self._db.execute(
|
cur = self._db.execute(
|
||||||
'SELECT * FROM objects WHERE ' # nosec B608
|
'SELECT * FROM objects WHERE ' # nosec B608
|
||||||
+ ' AND '.join(clauses), params)
|
+ ' AND '.join(clauses), params)
|
||||||
return self.__objects(cur)
|
return cur
|
||||||
|
|
||||||
def vectors_to_send(self, stream=None):
|
def vectors_to_send(self, stream=None):
|
||||||
cur = self._db.execute(
|
cur = self._db.execute(
|
||||||
|
@ -82,7 +83,7 @@ class Inventory():
|
||||||
' ORDER BY random()',
|
' ORDER BY random()',
|
||||||
(int(time.time()), stream or shared.stream)
|
(int(time.time()), stream or shared.stream)
|
||||||
)
|
)
|
||||||
return [v for v, in cur.fetchall()]
|
return [v for v, in cur]
|
||||||
|
|
||||||
def get(self, vector, default=None):
|
def get(self, vector, default=None):
|
||||||
try:
|
try:
|
||||||
|
@ -92,11 +93,10 @@ class Inventory():
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
cur = self._db.execute('SELECT vector FROM objects')
|
cur = self._db.execute('SELECT vector FROM objects')
|
||||||
return (v for v, in cur.fetchall())
|
return (v for v, in cur)
|
||||||
|
|
||||||
def values(self):
|
def values(self):
|
||||||
cur = self._db.execute('SELECT * FROM objects')
|
return self._db.execute('SELECT * FROM objects')
|
||||||
return self.__objects(cur)
|
|
||||||
|
|
||||||
def popitem(self):
|
def popitem(self):
|
||||||
if not self.rowid:
|
if not self.rowid:
|
||||||
|
@ -114,16 +114,11 @@ class Inventory():
|
||||||
return cur.fetchone()
|
return cur.fetchone()
|
||||||
|
|
||||||
def __getitem__(self, vector):
|
def __getitem__(self, vector):
|
||||||
cur = self._db.execute(
|
item = self._db.execute(
|
||||||
'SELECT * FROM objects WHERE vector = ?', (vector,))
|
'SELECT * FROM objects WHERE vector = ?', (vector,)).fetchone()
|
||||||
item = cur.fetchone()
|
|
||||||
if item is None:
|
if item is None:
|
||||||
raise KeyError(vector)
|
raise KeyError(vector)
|
||||||
vector, expires, obj_type, version, stream, tag, data, offset = item
|
return item
|
||||||
return structure.Object(
|
|
||||||
expires, obj_type, version, stream, data, offset,
|
|
||||||
tag=tag, vector=vector
|
|
||||||
)
|
|
||||||
|
|
||||||
def __delitem__(self, vector):
|
def __delitem__(self, vector):
|
||||||
with shared.objects_lock: # KeyError
|
with shared.objects_lock: # KeyError
|
||||||
|
|
Loading…
Reference in New Issue
Block a user