Set a row factory instead of nesting the iterator, iterate through the cursor
This commit is contained in:
parent
dd502230a6
commit
e0a62743d7
|
@ -40,14 +40,16 @@ class Inventory():
|
||||||
cur.execute("INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,))
|
cur.execute("INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,))
|
||||||
self._db.commit()
|
self._db.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __object(cursor, 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 __objects(self, cur):
|
def __objects(self, cur):
|
||||||
return (
|
cur.row_factory = self.__object
|
||||||
structure.Object(
|
return cur
|
||||||
expires, obj_type, version, stream, data, offset,
|
|
||||||
tag=tag, vector=vector)
|
|
||||||
for (vector, expires, obj_type, version, stream, tag, data, offset)
|
|
||||||
in cur.fetchall()
|
|
||||||
)
|
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
with shared.objects_lock:
|
with shared.objects_lock:
|
||||||
|
@ -79,7 +81,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:
|
||||||
|
@ -89,7 +91,7 @@ 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')
|
cur = self._db.execute('SELECT * FROM objects')
|
||||||
|
@ -113,14 +115,11 @@ class Inventory():
|
||||||
def __getitem__(self, vector):
|
def __getitem__(self, vector):
|
||||||
cur = self._db.execute(
|
cur = self._db.execute(
|
||||||
'SELECT * FROM objects WHERE vector = ?', (vector,))
|
'SELECT * FROM objects WHERE vector = ?', (vector,))
|
||||||
|
cur.row_factory = self.__object
|
||||||
item = cur.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