Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Add WAL option for SQLite, closes #488

davidmezzettib30a96d9c2f4
diff --git a/docs/embeddings/configuration/index.md b/docs/embeddings/configuration/index.md
index d184cfb5..6e5d06c6 100644
--- a/docs/embeddings/configuration/index.md
+++ b/docs/embeddings/configuration/index.md
@@ -165,6 +165,15 @@ content: boolean|sqlite|duckdb|custom
Enables content storage. When true, the default storage engine, `sqlite` will be used. Also supports `duckdb`. Add custom storage engines via setting this parameter to the fully resolvable class string.
+Content storage specific settings are set with a corresponding configuration object having the same name as the content storage engine (i.e. duckdb or sqlite). None of these are required and are set to defaults if omitted.
+
+### sqlite
+```yaml
+sqlite:
+ wal: enable write-ahead logging - allows concurrent read/write operations,
+ defaults to false
+```
+
## functions
```yaml
functions: list
diff --git a/src/python/txtai/database/base.py b/src/python/txtai/database/base.py
index 2a4674e6..dc8a0400 100644
--- a/src/python/txtai/database/base.py
+++ b/src/python/txtai/database/base.py
@@ -288,3 +288,22 @@ class Database:
return function(*args)
except Exception as ex:
raise SQLError(ex) from None
+
+ def setting(self, name, default=None):
+ """
+ Looks up database specific setting.
+
+ Args:
+ name: setting name
+ default: default value when setting not found
+
+ Returns:
+ setting value
+ """
+
+ # Get the database-specific config object
+ database = self.config.get(self.config["content"])
+
+ # Get setting value, set default value if not found
+ setting = database.get(name) if database else None
+ return setting if setting else default
diff --git a/src/python/txtai/database/sqlite.py b/src/python/txtai/database/sqlite.py
index 438acf5c..5fe300ae 100644
--- a/src/python/txtai/database/sqlite.py
+++ b/src/python/txtai/database/sqlite.py
@@ -14,7 +14,14 @@ class SQLite(FileDB):
"""
def connect(self, path=""):
- return sqlite3.connect(path, check_same_thread=False)
+ # Create connection
+ connection = sqlite3.connect(path, check_same_thread=False)
+
+ # Enable WAL mode, if necessary
+ if self.setting("wal"):
+ connection.execute("PRAGMA journal_mode=WAL")
+
+ return connection
def getcursor(self):
return self.connection.cursor()
diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py
index c46ae2a3..26aa1943 100644
--- a/test/python/testdatabase/testsql.py
+++ b/test/python/testdatabase/testsql.py
@@ -4,7 +4,7 @@ SQL module tests
import unittest
-from txtai.database import SQL, SQLError, SQLite
+from txtai.database import DatabaseFactory, SQL, SQLError
class TestSQL(unittest.TestCase):
@@ -19,7 +19,7 @@ class TestSQL(unittest.TestCase):
"""
# Create SQL parser for SQLite
- cls.db = SQLite({})
+ cls.db = DatabaseFactory.create({"content": True})
cls.db.initialize()
cls.sql = SQL(cls.db)
diff --git a/test/python/testdatabase/testsqlite.py b/test/python/testdatabase/testsqlite.py
index fb11b0bf..587cc558 100644
--- a/test/python/testdatabase/testsqlite.py
+++ b/test/python/testdatabase/testsqlite.py
@@ -436,6 +436,22 @@ class TestSQLite(unittest.TestCase):
self.embeddings.upsert([(0, "Looking out into the dreadful abyss", None)])
self.assertEqual(self.embeddings.count(), len(self.data))
+ def testSettings(self):
+ """
+ Test custom SQLite settings
+ """
+
+ # Index with write-ahead logging enabled
+ embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2", "content": self.backend, "sqlite": {"wal": True}})
+
+ # Create an index for the list of text
+ embeddings.index([(uid, text, None) for uid, text in enumerate(self.data)])
+
+ # Search for best match
+ result = embeddings.search("feel good story", 1)[0]
+
+ self.assertEqual(result["text"], self.data[4])
+
def testSQL(self):
"""
Test running a SQL query