Set a row factory instead of nesting the iterator, iterate through the cursor

This commit is contained in:
Lee Miller 2024-09-11 02:35:09 +03:00
parent 98f22a8f52
commit a8db11199b
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

View File

@ -39,15 +39,16 @@ class Inventory():
cur.execute('VACUUM')
cur.execute("INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,))
self._db.commit()
self._db.row_factory = self.__object
def __objects(self, cur):
return (
structure.Object(
expires, obj_type, version, stream, data, offset,
tag=tag, vector=vector)
for (vector, expires, obj_type, version, stream, tag, data, offset)
in cur.fetchall()
)
@staticmethod
def __object(cursor, row):
if len(cursor.description) != 8:
return row
vector, expires, obj_type, version, stream, tag, data, offset = row
return structure.Object(
expires, obj_type, version, stream, data, offset,
tag=tag, vector=vector)
def cleanup(self):
with shared.objects_lock:
@ -72,7 +73,7 @@ class Inventory():
cur = self._db.execute(
'SELECT * FROM objects WHERE ' # nosec B608
+ ' AND '.join(clauses), params)
return self.__objects(cur)
return cur
def vectors_to_send(self, stream=None):
cur = self._db.execute(
@ -80,7 +81,7 @@ class Inventory():
' ORDER BY random()',
(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):
try:
@ -90,11 +91,10 @@ class Inventory():
def keys(self):
cur = self._db.execute('SELECT vector FROM objects')
return (v for v, in cur.fetchall())
return (v for v, in cur)
def values(self):
cur = self._db.execute('SELECT * FROM objects')
return self.__objects(cur)
return self._db.execute('SELECT * FROM objects')
def popitem(self):
if not self.rowid:
@ -112,16 +112,11 @@ class Inventory():
return cur.fetchone()
def __getitem__(self, vector):
cur = self._db.execute(
'SELECT * FROM objects WHERE vector = ?', (vector,))
item = cur.fetchone()
item = self._db.execute(
'SELECT * FROM objects WHERE vector = ?', (vector,)).fetchone()
if item is None:
raise KeyError(vector)
vector, expires, obj_type, version, stream, tag, data, offset = item
return structure.Object(
expires, obj_type, version, stream, data, offset,
tag=tag, vector=vector
)
return item
def __delitem__(self, vector):
with shared.objects_lock: # KeyError