sqless 3.2.1__tar.gz → 3.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqless
3
- Version: 3.2.1
3
+ Version: 3.4.0
4
4
  Summary: A schema-flexible, zero-abstraction SQLite interface supporting Relational tables, JSON tables, Full-Text Search, and Semantic Search.
5
5
  Home-page: https://github.com/cqian-cs/sqless
6
6
  Author: cqian.cs
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="sqless",
8
- version="3.2.1",
8
+ version="3.4.0",
9
9
  author="cqian.cs",
10
10
  author_email="cqian.cs@qq.com",
11
11
  description="A schema-flexible, zero-abstraction SQLite interface supporting Relational tables, JSON tables, Full-Text Search, and Semantic Search.",
@@ -2,7 +2,7 @@
2
2
  sqless - A schema-flexible, zero-abstraction SQLite interface supporting Relational tables, JSON tables, Full-Text Search, and Semantic Search.
3
3
  """
4
4
 
5
- __version__ = "3.2.1"
5
+ __version__ = "3.4.0"
6
6
  __author__ = "cqian.cs"
7
7
  __email__ = "cqian.cs@qq.com"
8
8
 
@@ -167,7 +167,24 @@ def parse_where(where_str, use_json_path=True):
167
167
 
168
168
  return True, sql, params
169
169
 
170
-
170
+ import functools
171
+ import time
172
+ def retry_on_db_lock(max_retries=5, base_delay=0.05):
173
+ def decorator(func):
174
+ @functools.wraps(func)
175
+ def wrapper(*args, **kwargs):
176
+ for attempt in range(max_retries):
177
+ try:
178
+ return func(*args, **kwargs)
179
+ except sqlite3.OperationalError as e:
180
+ if "locked" in str(e).lower() and attempt < max_retries - 1:
181
+ sleep_time = base_delay * (2 ** attempt)
182
+ time.sleep(sleep_time)
183
+ continue
184
+ raise
185
+ return func(*args, **kwargs)
186
+ return wrapper
187
+ return decorator
171
188
 
172
189
 
173
190
  identifier_re = re.compile(r"^[A-Za-z_][A-Za-z0-9_\-]*[A-Za-z0-9]$")
@@ -195,7 +212,7 @@ class DB:
195
212
  def __init__(self, path_db=f"{path_this}/util_db_vec.sqlite", wal=True):
196
213
  self.path = os.path.realpath(path_db)
197
214
  os.makedirs(os.path.dirname(self.path), exist_ok=True)
198
- self.conn = sqlite3.connect(self.path, detect_types=sqlite3.PARSE_DECLTYPES)
215
+ self.conn = sqlite3.connect(self.path, detect_types=sqlite3.PARSE_DECLTYPES, timeout=15.0)
199
216
  self.tables = {}
200
217
  if _sqlite_vec_available:
201
218
  self.conn.enable_load_extension(True)
@@ -203,6 +220,8 @@ class DB:
203
220
  if wal:
204
221
  cursor = self.conn.cursor()
205
222
  cursor.execute("PRAGMA journal_mode = WAL;")
223
+ cursor.execute("PRAGMA synchronous = NORMAL;")
224
+ cursor.execute("PRAGMA cache_size = -8000;")
206
225
  self.conn.commit()
207
226
 
208
227
  def __del__(self):
@@ -228,6 +247,7 @@ class DB:
228
247
  def close(self):
229
248
  if self.conn:
230
249
  self.conn.close()
250
+ self.conn = None
231
251
 
232
252
  def drop_table(self, table_name):
233
253
  if not valid_identifier(table_name):
@@ -2,7 +2,7 @@ import orjson
2
2
  import re
3
3
  from typing import List, Literal
4
4
 
5
- from .database import parse_where
5
+ from .database import parse_where,retry_on_db_lock
6
6
  from .json_table import JsonTable
7
7
 
8
8
  anyword_re = re.compile(r'[\u2E80-\u9FFF]|[A-Za-z0-9_]+|[^\sA-Za-z0-9_\u2E80-\u9FFF]')
@@ -39,7 +39,8 @@ class FtsTable(JsonTable):
39
39
  return True, 'ok'
40
40
  except Exception as e:
41
41
  return False, f"Ensuring fields error: {e}({sql})"
42
-
42
+
43
+ @retry_on_db_lock(max_retries=6, base_delay=0.05)
43
44
  def upsert(self, key_values: dict, is_delta=False):
44
45
  table = self.name
45
46
  processed_key_values = self.pre_upsert(key_values) if is_delta else key_values
@@ -1,6 +1,6 @@
1
1
  from typing import List, Literal
2
2
 
3
- from .database import parse_where
3
+ from .database import parse_where,retry_on_db_lock
4
4
  import orjson
5
5
 
6
6
  BATCH_SIZE_WRITE = 100_000
@@ -77,6 +77,7 @@ class JsonTable:
77
77
  processed_key_values[key] = new_value
78
78
  return processed_key_values
79
79
 
80
+ @retry_on_db_lock(max_retries=6, base_delay=0.05)
80
81
  def upsert(self, key_values: dict, is_delta=False):
81
82
  table = self.name
82
83
  processed_key_values = self.pre_upsert(key_values) if is_delta else key_values
@@ -129,6 +130,7 @@ class JsonTable:
129
130
  print(f"DB_ERROR|{e}({sql}){values}")
130
131
  return items
131
132
 
133
+ @retry_on_db_lock(max_retries=6, base_delay=0.05)
132
134
  def _get_items_tempjoin(self,keys):
133
135
  cursor = self.db.conn.cursor()
134
136
  items = {k: {'key': k, 'data': None, 'updated_at': None} for k in keys}
@@ -1,6 +1,6 @@
1
1
  from typing import List, Literal
2
2
  import orjson,pickle
3
- from .database import parse_where, valid_identifier
3
+ from .database import parse_where, valid_identifier, retry_on_db_lock
4
4
 
5
5
  type_map = {
6
6
  str: 'TEXT',
@@ -59,7 +59,7 @@ class RelTable:
59
59
  table = self.name
60
60
  try:
61
61
  cursor = self.db.conn.cursor()
62
- sql = f"CREATE TABLE IF NOT EXISTS {table} (key TEXT PRIMARY KEY);"
62
+ sql = f"CREATE TABLE IF NOT EXISTS {table} (key TEXT PRIMARY KEY) WITHOUT ROWID;"
63
63
  cursor.execute(sql)
64
64
  self.db.conn.commit()
65
65
  return True, 'ok'
@@ -134,6 +134,7 @@ class RelTable:
134
134
  return v
135
135
  return encode(v)
136
136
 
137
+ @retry_on_db_lock(max_retries=6, base_delay=0.05)
137
138
  def upsert(self, key_values: dict):
138
139
  if not key_values:
139
140
  return {'suc': True, 'data': "update 0 items."}
@@ -173,7 +174,7 @@ class RelTable:
173
174
  values_mat.append(row)
174
175
  cursor.executemany(sql, values_mat)
175
176
  total += len(items)
176
- self.db.conn.commit()
177
+ self.db.conn.commit()
177
178
  return {'suc': True, 'data': f"update {total} items."}
178
179
  except Exception as e:
179
180
  print(f"DB_ERROR|{e}")
@@ -10,7 +10,7 @@ import sqlite_vec
10
10
  import numpy as np
11
11
  import aiohttp
12
12
  import asyncio
13
- from .database import parse_where
13
+ from .database import parse_where,retry_on_db_lock
14
14
  from .json_table import JsonTable
15
15
 
16
16
 
@@ -175,6 +175,7 @@ class VecTable(JsonTable):
175
175
  print(f"DB_ERROR|{e}({sql}){values}")
176
176
  return {'suc': False, 'data': str(e), 'debug': sql}
177
177
 
178
+ @retry_on_db_lock(max_retries=6, base_delay=0.05)
178
179
  def upsert(self, key_values: dict, is_delta=False):
179
180
  processed_key_values = self.pre_upsert(key_values) if is_delta else key_values
180
181
  if len(processed_key_values) == 0:
@@ -186,6 +187,7 @@ class VecTable(JsonTable):
186
187
  key_vecs = ret['data']
187
188
  return self._upsert(key_texts, key_vecs)
188
189
 
190
+ @retry_on_db_lock(max_retries=6, base_delay=0.05)
189
191
  async def async_upsert(self, key_values: dict, is_delta=False):
190
192
  processed_key_values = self.pre_upsert(key_values) if is_delta else key_values
191
193
  if len(processed_key_values) == 0:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqless
3
- Version: 3.2.1
3
+ Version: 3.4.0
4
4
  Summary: A schema-flexible, zero-abstraction SQLite interface supporting Relational tables, JSON tables, Full-Text Search, and Semantic Search.
5
5
  Home-page: https://github.com/cqian-cs/sqless
6
6
  Author: cqian.cs
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes