Skip to content

Commit

Permalink
Release db cursor eagerly to reduce index locktime
Browse files Browse the repository at this point in the history
frthjf committed Jun 1, 2024
1 parent 839163c commit 442b98c
Showing 2 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

# Unreleased

- Option to hide interfaces from get
- Simplified `**kwargs` arguments in CLI
- Adds save/load_attribute helper
- Adds `@cachable` decorator utility
30 changes: 20 additions & 10 deletions src/machinable/index.py
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
from machinable.utils import is_directory_version, load_file, normjson


def interface_row_factory(cursor, row) -> schema.Interface:
def interface_row_factory(row) -> schema.Interface:
model = getattr(schema, row[1], schema.Interface)
return model(
uuid=row[0],
@@ -80,6 +80,8 @@ def migrate(db: sqlite3.Connection) -> None:
# future migrations
...

cur.close()


def load(database: str, create=False) -> sqlite3.Connection:
if database.startswith("sqlite:///"):
@@ -142,6 +144,7 @@ def commit(self, model: schema.Interface) -> bool:
if cur.execute(
"""SELECT uuid FROM 'index' WHERE uuid=?""", (model.uuid,)
).fetchone():
cur.close()
# already exists
return False
config = copy.deepcopy(model.config)
@@ -185,6 +188,7 @@ def commit(self, model: schema.Interface) -> bool:
),
)
_db.commit()
cur.close()
return True

def create_relation(
@@ -205,6 +209,7 @@ def create_relation(
"""SELECT id FROM 'relations' WHERE uuid=? AND related_uuid=? AND relation=?""",
(uuid, related_uuid, relation),
).fetchone():
cur.close()
# already exists
return
if timestamp is None:
@@ -220,6 +225,7 @@ def create_relation(
(relation, uuid, related_uuid, priority, timestamp),
)
_db.commit()
cur.close()

def find(
self,
@@ -253,15 +259,15 @@ def find_by_id(self, uuid: str) -> Optional[schema.Interface]:
row = cur.execute(
"""SELECT * FROM 'index' WHERE uuid=?""", (uuid,)
).fetchone()
cur.close()
if row is None:
return None
return interface_row_factory(cur, row)
return interface_row_factory(row)

def find_by_context(self, context: Dict) -> List[schema.Interface]:
with db(self.config.database, create=False) as _db:
if not _db:
return []
cur = _db.cursor()

keys = []
equals = []
@@ -279,30 +285,33 @@ def find_by_context(self, context: Dict) -> List[schema.Interface]:
keys.append(f"json_extract(context, '$.{field}')=?")
equals.append(value)

cur = _db.cursor()
if len(keys) > 0:
query = cur.execute(
"""SELECT * FROM 'index' WHERE """ + (" AND ".join(keys)),
[
v if isinstance(v, (str, int, float)) else normjson(v)
for v in equals
],
)
).fetchall()
else:
query = cur.execute("""SELECT * FROM 'index'""")
return [interface_row_factory(cur, row) for row in query.fetchall()]
query = cur.execute("""SELECT * FROM 'index'""").fetchall()
cur.close()
return [interface_row_factory(row) for row in query]

def find_by_hash(self, context_hash: str) -> List[schema.Interface]:
with db(self.config.database, create=False) as _db:
if not _db:
return []
cur = _db.cursor()

cur = _db.cursor()
query = cur.execute(
"""SELECT * FROM 'index' WHERE uuid LIKE ?""",
(f"%{context_hash}",),
)
).fetchall()
cur.close()

return [interface_row_factory(cur, row) for row in query.fetchall()]
return [interface_row_factory(row) for row in query]

def find_related(
self, relation: str, uuid: str, inverse: bool = False
@@ -329,7 +338,8 @@ def find_related(
""",
(uuid, relation),
).fetchall()
return [interface_row_factory(cur, row) for row in rows or []]
cur.close()
return [interface_row_factory(row) for row in rows or []]

def import_directory(
self,

0 comments on commit 442b98c

Please sign in to comment.