Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Open lazy SQLite connections before saving indexes (#1290)

LimbC-C81f908874b8a
diff --git a/src/python/txtai/ann/dense/sqlite.py b/src/python/txtai/ann/dense/sqlite.py
index 36000c14..b890b292 100644
--- a/src/python/txtai/ann/dense/sqlite.py
+++ b/src/python/txtai/ann/dense/sqlite.py
@@ -73,6 +73,9 @@ class SQLite(ANN):
return self.cursor.fetchone()[0]
def save(self, path):
+ # Loading defers opening the connection until the first database operation.
+ self.database()
+
# Temporary database
if not self.path:
# Save temporary database
diff --git a/test/python/testann/testdense.py b/test/python/testann/testdense.py
index df8ec858..32568b1e 100644
--- a/test/python/testann/testdense.py
+++ b/test/python/testann/testdense.py
@@ -745,6 +745,32 @@ class TestDense(unittest.TestCase):
self.assertEqual(ann.quantize, expected)
@unittest.skipIf(platform.system() == "Darwin", "SQLite extensions not supported on macOS")
+ def testSQLiteSaveWithoutQuery(self):
+ """
+ Saving after load must not require an earlier query to open the connection.
+ """
+
+ for quantize in (None, 1, 8):
+ with self.subTest(quantize=quantize), tempfile.TemporaryDirectory() as directory:
+ source, target = (os.path.join(directory, name) for name in ("source", "target"))
+ ann = ANNFactory.create({"backend": "sqlite", "dimensions": 8, "sqlite": {"quantize": quantize}})
+ data = np.ones((2, 8), dtype=np.float32)
+ data[1, :4] = -1
+ try:
+ ann.index(data)
+ expected = ann.search(data[:1], 2)
+ ann.save(source)
+ for path in (source, target):
+ ann.close()
+ ann.load(source)
+ self.assertIsNone(ann.connection)
+ ann.save(path)
+ ann.close()
+ ann.load(path)
+ self.assertEqual(ann.count(), 2)
+ self.assertEqual(ann.search(data[:1], 2), expected)
+ finally:
+ ann.close()
def testSQLiteBinaryScores(self):
"""
Binary similarity is the fraction of matching bits, not one minus their distance.