altcodepro-polydb-python 2.5.20__py3-none-any.whl → 2.5.22__py3-none-any.whl

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: altcodepro-polydb-python
3
- Version: 2.5.20
3
+ Version: 2.5.22
4
4
  Summary: Production-ready multi-cloud database abstraction layer with connection pooling, retry logic, and thread safety
5
5
  Author: AltCodePro
6
6
  Project-URL: Homepage, https://github.com/altcodepro/polydb-python
@@ -1,12 +1,12 @@
1
- altcodepro_polydb_python-2.5.20.dist-info/licenses/LICENSE,sha256=9X8GLocsBwy-5aR5JGOt2SAMDDPs9Qv-YnqmHBHOXrw,1067
1
+ altcodepro_polydb_python-2.5.22.dist-info/licenses/LICENSE,sha256=9X8GLocsBwy-5aR5JGOt2SAMDDPs9Qv-YnqmHBHOXrw,1067
2
2
  polydb/PolyDB.py,sha256=Lp0QtG7D89Z6P6nt9fVoT8rVHAy8xlE2kFRSS70r-mk,23672
3
- polydb/__init__.py,sha256=fNrKwNBgwMJKrBhs_eaRu4XpQ3olREipIOiwBmv38b8,1876
3
+ polydb/__init__.py,sha256=5nO3ONrpIkzQDi4oka8UgcO4jAWVyAvt9gAmj7mPNdQ,1876
4
4
  polydb/advanced_query.py,sha256=M9sKmdF2PsLZWSc-zKpXRcwaDumbYYTsibmL_IAjKP4,6931
5
5
  polydb/aio.py,sha256=0EQ6hXrf6N4If6pcht6L9B7qz0PU2yGahyGzEss2eQg,8340
6
6
  polydb/batch.py,sha256=ulvSdtVeofsOiVNVAQwrE9tXIIaP8fjiW1Sbpz3dcv0,4093
7
7
  polydb/cache.py,sha256=ArI0zRbuhAYd6kn4TPVMBHBptdRmuZyy05_hwIATUsQ,11784
8
8
  polydb/cloudDatabaseFactory.py,sha256=eJiNKycdaC9nO2BKWPjkig3GaNGkNjoJci6yVJC7A_c,25301
9
- polydb/databaseFactory.py,sha256=0gjc5wk0zkOsCqOY7ET8klaNG8wZMPJhKOvzmdYTwr0,59610
9
+ polydb/databaseFactory.py,sha256=_faf37970W9T5z26q5IRuRUiXpnvABBkRX0H_5aV-jo,59746
10
10
  polydb/errors.py,sha256=AtXCLdzkHE2ncUaE1P0wH5HmIyW3NXx9KHL-fCejjA4,1832
11
11
  polydb/json_safe.py,sha256=jj3ZA6gImGwgTO0dCZVcckjeUHtewa2HP6OA1-u6VZ0,137
12
12
  polydb/models.py,sha256=Pq_63FXu1IiTxl-RshZ2vmoe-5m5k6qft-tXpHLn-70,12600
@@ -19,15 +19,15 @@ polydb/registry.py,sha256=RD_elvFXcmhTdCyZDm2f3ej0elxqhArnSJ2aO9k5VCU,2352
19
19
  polydb/retry.py,sha256=0rILN3-auuzomOXPveIAvgnhBbtXXGNKvxg6qcRgN4g,4926
20
20
  polydb/schema.py,sha256=v3lnD9jUoBippMj2eLNwvmykPygO0nFx806X7cmeb8Q,9840
21
21
  polydb/security.py,sha256=IF8Oh6Y6YU9xSjz_pi6lotfJuhIUJgP7cCmpGc8bz6w,20158
22
- polydb/types.py,sha256=Kx_d9_XJP9UnIbABaVCOcWiBEem_cfUrDwiPc3uaxCY,2825
22
+ polydb/types.py,sha256=UbO5tQ2oPfviszWAvZvhuYv6PMp_WhgPFgUa5tTVgKI,3026
23
23
  polydb/utils.py,sha256=YS-BhBpQfQsOf-z_i1zw_kvMYwJ05Zn1S2Xt6K4_2ak,3171
24
24
  polydb/validation.py,sha256=DK9-AFndTr0cLodpc3KX0wnABDpw2vPB4H6hp67BJKE,4570
25
25
  polydb/adapters/AWSSecretsManagerAdapter.py,sha256=W4B3xhQFcBAlNlwQ4uTgcDmRrOmZr6v-suC4qihTpJw,2747
26
26
  polydb/adapters/AzureBlobStorageAdapter.py,sha256=ZTovSC_CVduGnIoIQUUeNxDQlf-IbFDBRzjGDk8U9Io,7084
27
27
  polydb/adapters/AzureFileStorageAdapter.py,sha256=VZNprqlBXCuWUgtqClNT-NrQmRf-XFYEiRA2BLbf-Sc,7046
28
- polydb/adapters/AzureKeyVaultAdapter.py,sha256=7Nizs9qGh6cytbZNgV78f-odOO4Z6sUlwm_HQYqt-pc,2339
28
+ polydb/adapters/AzureKeyVaultAdapter.py,sha256=EtsrxI3X75nORnO0KbvKTVXJfliHd-gt3y9Mo8Npugs,3365
29
29
  polydb/adapters/AzureQueueAdapter.py,sha256=1e2HiFvkH1BI_JKS9fgays2J41kOfH6pUPbZDe1wmWA,13658
30
- polydb/adapters/AzureTableStorageAdapter.py,sha256=u2DsUIMOjYnBX_2G4abaLCjRaVOCORQQnwC4e0XrJ0Q,35567
30
+ polydb/adapters/AzureTableStorageAdapter.py,sha256=W0MU4Q2v9ArlI_wz-8stqGehstPJnPkQr8jz6zNTnV4,39218
31
31
  polydb/adapters/BlockchainBlobAdapter.py,sha256=85H3PxiL9rGDtK-sTsW26jOuUQ_SGipLSPu3s-juZRA,3298
32
32
  polydb/adapters/BlockchainFileAdapter.py,sha256=lZbRyC18LghZcVh5QyIRP7tCftNF_Ep1FKG0KRkvMmk,8335
33
33
  polydb/adapters/BlockchainKVAdapter.py,sha256=pvpiPshDE5Z_IRcSBvBwgAXT--86lgEyIsEUeqcmoE8,5422
@@ -57,18 +57,19 @@ polydb/audit/__init__.py,sha256=VANh47kW5V9gi7anUCZ7LklQ6ofaFRmBKrPVitRxdw0,245
57
57
  polydb/audit/context.py,sha256=jbw58vDoslY2mEcS0qUBlEhYg54EoMk4Xcfuq1L3FuY,1945
58
58
  polydb/audit/manager.py,sha256=DgBkDcZlf_EotUm-RlKA1CWAoVi3DIUlRCsN4wzsfgc,1237
59
59
  polydb/audit/models.py,sha256=XHT-te2XpmdywfakKm1cVH09yi3C9Fa4FaBTZaJ5vqY,7044
60
- polydb/base/NoSQLKVAdapter.py,sha256=9nG--KZ1Q0-KTc6n_i1nBIGDlSgOVEei0-QhBmqC_tI,17175
60
+ polydb/base/NoSQLKVAdapter.py,sha256=0-yu9yO4DNT1Ya608Lh3J6v3PZ8sCWi5hEuzUUNVUK4,18533
61
61
  polydb/base/ObjectStorageAdapter.py,sha256=mr6UMGmAFOBKlJeoyubaUJ7x7BDZMlylqnuggIatEv0,2289
62
62
  polydb/base/QueueAdapter.py,sha256=PzTlEuc37TXEnYNdsrrmxzFworbieWBxljDXJ-qq4XQ,8206
63
63
  polydb/base/SecretsAdapter.py,sha256=RaWejh21oQji0TdEBa7P5JgD9y6N06M1aCBbLvzTQU0,990
64
64
  polydb/base/SharedFilesAdapter.py,sha256=YHoPMZ5T9Bm5S1oStDvIEzGEgxx7TeXqqr74Yt4KRiE,852
65
65
  polydb/base/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
66
+ polydb/base/constraints.py,sha256=meIH7dnqYl1zX5PgAmeggNKWxQbsvz00BtWMi6wrH_8,13269
66
67
  polydb/observability/__init__.py,sha256=ZIMx5ODCBZI_FnROJFb11Q9E5kRq0ZcCp6_RmlwG53k,122
67
68
  polydb/observability/logging.py,sha256=FWYky-Mg1IhLpwuaKuLIsTROBZc9bI_-AO-y53nvES0,3911
68
69
  polydb/services/__init__.py,sha256=Zs3PZbHutgPdPjJatygvtYvKPZMBt1qKSqESbFRPHJs,241
69
70
  polydb/services/compliance_service.py,sha256=UMjCbQ4z13-w6pT4RLQpMi_u1zbShEpiz8kawA2WsSU,5075
70
71
  polydb/services/security_service.py,sha256=gGo_j9Ca_rgvxS7s6C5JUktugQBj1PV1DXpMnOYAGbI,4992
71
- altcodepro_polydb_python-2.5.20.dist-info/METADATA,sha256=1UKkHliLCxQix8uakLf4qjXErKRQHzbJnlBZmqYZy3w,15599
72
- altcodepro_polydb_python-2.5.20.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
73
- altcodepro_polydb_python-2.5.20.dist-info/top_level.txt,sha256=WgLFWJoYjUhwvyPxJFl6jYLrVFuBJDX3OABf4ocwk_E,7
74
- altcodepro_polydb_python-2.5.20.dist-info/RECORD,,
72
+ altcodepro_polydb_python-2.5.22.dist-info/METADATA,sha256=26GBAk1iot-WHk6DimfIK7Lzshb77-_xsPPOKg0lWIQ,15599
73
+ altcodepro_polydb_python-2.5.22.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
74
+ altcodepro_polydb_python-2.5.22.dist-info/top_level.txt,sha256=WgLFWJoYjUhwvyPxJFl6jYLrVFuBJDX3OABf4ocwk_E,7
75
+ altcodepro_polydb_python-2.5.22.dist-info/RECORD,,
polydb/__init__.py CHANGED
@@ -4,7 +4,7 @@ PolyDB - Enterprise Cloud-Independent Database Abstraction
4
4
  Full LINQ support, field-level audit, cache, soft delete, overflow storage
5
5
  """
6
6
 
7
- __version__ = "2.5.20"
7
+ __version__ = "2.5.22"
8
8
 
9
9
  from .aio import AsyncDatabaseFactory, AsyncPolyDB
10
10
  from .audit.context import AuditContext
@@ -19,10 +19,24 @@ class AzureKeyVaultAdapter(SecretsAdapter):
19
19
  self._initialize_client()
20
20
 
21
21
  def _initialize_client(self) -> None:
22
- from azure.identity import DefaultAzureCredential
23
22
  from azure.keyvault.secrets import SecretClient
24
23
 
25
- self._client = SecretClient(vault_url=self.vault_url, credential=DefaultAzureCredential())
24
+ self._client = SecretClient(vault_url=self.vault_url, credential=self._build_credential())
25
+
26
+ @staticmethod
27
+ def _build_credential():
28
+ """Inside Azure Container Apps / App Service / Functions a managed identity is available
29
+ (IDENTITY_ENDPOINT / MSI_ENDPOINT). DefaultAzureCredential tries EnvironmentCredential FIRST, and a deployment that
30
+ also exports AZURE_CLIENT_ID + AZURE_CLIENT_SECRET (for some other purpose) then fails with AADSTS7000232
31
+ "MSI identity should not use ClientSecretCredential". So when a managed identity exists it goes first
32
+ (AZURE_CLIENT_ID, if set, names a user-assigned identity); the normal chain stays as the fallback, so local
33
+ development (az login, environment variables) behaves as before."""
34
+ from azure.identity import ChainedTokenCredential, DefaultAzureCredential, ManagedIdentityCredential
35
+
36
+ if os.getenv("IDENTITY_ENDPOINT") or os.getenv("MSI_ENDPOINT"):
37
+ client_id = os.getenv("AZURE_CLIENT_ID") or None
38
+ return ChainedTokenCredential(ManagedIdentityCredential(client_id=client_id), DefaultAzureCredential())
39
+ return DefaultAzureCredential()
26
40
 
27
41
  # Key Vault secret names may only contain alphanumerics and dashes --
28
42
  # our secret keys (e.g. "tenant-42/stripe/api_key") use slashes, so
@@ -40,6 +40,10 @@ _BASE64_RE = re.compile(r"^[A-Za-z0-9+/]*={0,2}$")
40
40
  # ensures model isolation across the same table
41
41
  _MODEL_FIELD = "__polydb_model__"
42
42
 
43
+
44
+ class _AlreadyExists(Exception):
45
+ """create_entity found the (PartitionKey, RowKey) taken. Deliberately NOT a NoSQLError so `@retry` does not retry it."""
46
+
43
47
  _CONNECT_TIMEOUT = float(os.getenv("AZURE_TABLE_CONNECT_TIMEOUT", "10"))
44
48
  _READ_TIMEOUT = float(os.getenv("AZURE_TABLE_READ_TIMEOUT", "30"))
45
49
  _RETRY_TOTAL = int(os.getenv("AZURE_TABLE_RETRY_TOTAL", "3"))
@@ -47,6 +51,59 @@ _RETRY_TOTAL = int(os.getenv("AZURE_TABLE_RETRY_TOTAL", "3"))
47
51
  logging.getLogger("azure").setLevel(logging.ERROR)
48
52
 
49
53
 
54
+
55
+ def _is_table_missing(exc: BaseException) -> bool:
56
+ text = f"{type(exc).__name__} {exc}"
57
+ return "TableNotFound" in text or "The table specified does not exist" in text
58
+
59
+
60
+ class _SelfHealingTableClient:
61
+ """Wraps an Azure TableClient so a table that disappears while the process is running (deleted by hand, a data
62
+ reset, a restore) is created again and the operation retried once, instead of failing with TableNotFound until
63
+ the process restarts. Lazy iterators (query_entities / list_entities) are healed only if the failure happens
64
+ before the first row was produced."""
65
+
66
+ _LAZY = ("query_entities", "list_entities")
67
+
68
+ def __init__(self, adapter: "AzureTableStorageAdapter", table_name: str, client: Any):
69
+ self._adapter = adapter
70
+ self._table_name = table_name
71
+ self._client = client
72
+
73
+ def _heal(self) -> None:
74
+ self._adapter._ensured_tables.discard(self._table_name)
75
+ self._adapter._client.create_table_if_not_exists(self._table_name)
76
+ self._adapter._ensured_tables.add(self._table_name)
77
+ logger.warning(f"Azure Table {self._table_name} was missing; created it again")
78
+
79
+ def __getattr__(self, name: str):
80
+ target = getattr(self._client, name)
81
+ if not callable(target):
82
+ return target
83
+ if name in self._LAZY:
84
+ def lazy(*args, **kwargs):
85
+ produced = False
86
+ try:
87
+ for item in target(*args, **kwargs):
88
+ produced = True
89
+ yield item
90
+ except Exception as exc: # noqa: BLE001
91
+ if produced or not _is_table_missing(exc):
92
+ raise
93
+ self._heal()
94
+ yield from getattr(self._client, name)(*args, **kwargs)
95
+ return lazy
96
+
97
+ def call(*args, **kwargs):
98
+ try:
99
+ return target(*args, **kwargs)
100
+ except Exception as exc: # noqa: BLE001
101
+ if not _is_table_missing(exc):
102
+ raise
103
+ self._heal()
104
+ return getattr(self._client, name)(*args, **kwargs)
105
+ return call
106
+
50
107
  class AzureTableStorageAdapter(NoSQLKVAdapter):
51
108
  """
52
109
  Azure Table Storage adapter with:
@@ -441,17 +498,21 @@ class AzureTableStorageAdapter(NoSQLKVAdapter):
441
498
  try:
442
499
  self._client.create_table_if_not_exists(table_name)
443
500
  logger.info(f"✅ Azure Table ensured/created: {table_name}")
501
+ self._ensured_tables.add(table_name)
444
502
  except Exception as e:
445
503
  msg = str(e)
446
- if "TableAlreadyExists" not in msg and "already exists" not in msg.lower():
447
- logger.warning(f"Could not create table {table_name}: {e}")
448
- self._ensured_tables.add(table_name)
504
+ if "TableAlreadyExists" in msg or "already exists" in msg.lower():
505
+ self._ensured_tables.add(table_name)
506
+ else:
507
+ # Do NOT remember a failed creation as "ensured": the next call tries again instead of
508
+ # failing with TableNotFound for the rest of the process's life.
509
+ logger.error(f"Could not create table {table_name}: {e}")
449
510
 
450
511
  if cached is None:
451
512
  cached = self._client.get_table_client(table_name=table_name)
452
513
  self._table_clients_cache[table_name] = cached
453
514
 
454
- return cached
515
+ return _SelfHealingTableClient(self, table_name, cached)
455
516
 
456
517
  def _restore_overflow_properties(self, entity_dict: JsonDict) -> JsonDict:
457
518
  restored = {}
@@ -500,7 +561,9 @@ class AzureTableStorageAdapter(NoSQLKVAdapter):
500
561
  return restored
501
562
 
502
563
  @retry(max_attempts=3, delay=1.0, exceptions=(NoSQLError,))
503
- def _put_raw(self, model: type, pk: str, rk: str, data: JsonDict) -> JsonDict:
564
+ def _put_raw(
565
+ self, model: type, pk: str, rk: str, data: JsonDict, *, _insert_only: bool = False
566
+ ) -> JsonDict:
504
567
  try:
505
568
  table_client = self._get_table_client(model)
506
569
  safe_pk = self._sanitize_pk_rk(pk)
@@ -575,7 +638,10 @@ class AzureTableStorageAdapter(NoSQLKVAdapter):
575
638
  else:
576
639
  reference_entity[k] = v
577
640
 
578
- table_client.upsert_entity(reference_entity)
641
+ if _insert_only:
642
+ table_client.create_entity(reference_entity) # 409 when the key is taken: atomic claim
643
+ else:
644
+ table_client.upsert_entity(reference_entity)
579
645
 
580
646
  restored = self._unpack_entity(model, entity)
581
647
  # Mirror the write-side condition above (line ~521): only
@@ -602,11 +668,27 @@ class AzureTableStorageAdapter(NoSQLKVAdapter):
602
668
  return restored
603
669
 
604
670
  except Exception as e:
671
+ if _insert_only and (
672
+ type(e).__name__ == "ResourceExistsError" or "EntityAlreadyExists" in str(e)
673
+ ):
674
+ raise _AlreadyExists() from e
605
675
  if "PropertyValueTooLarge" in str(e):
606
676
  raise NoSQLError("Azure Table limit exceeded → must use blob overflow") from e
607
677
 
608
678
  raise NoSQLError(f"Azure Table put failed: {str(e)}")
609
679
 
680
+ def _put_new_raw(self, model: type, pk: str, rk: str, data: JsonDict) -> bool:
681
+ """Atomic insert-if-absent via Table Storage's `create_entity` (HTTP 409 when the key exists)."""
682
+ try:
683
+ self._put_raw(model, pk, rk, data, _insert_only=True)
684
+ return True
685
+ except _AlreadyExists:
686
+ return False
687
+
688
+ def _query_partition_raw(self, model: type, partition: str, limit: Optional[int]) -> List[JsonDict]:
689
+ """One partition -- a PartitionKey filter, not a table scan."""
690
+ return self._query_raw(model, {"partition_key": partition}, limit)
691
+
610
692
  @retry(max_attempts=3, delay=1.0, exceptions=(NoSQLError,))
611
693
  def _get_raw(self, model: type, pk: str, rk: str) -> Optional[JsonDict]:
612
694
  try:
@@ -12,6 +12,7 @@ from ..json_safe import json_safe
12
12
  from ..query import Operator, QueryBuilder
13
13
  from ..retry import retry
14
14
  from ..types import JsonDict, Lookup
15
+ from .constraints import NoSQLConstraintsMixin, UniqueConstraintError # noqa: F401 (re-exported)
15
16
 
16
17
  if TYPE_CHECKING:
17
18
  from ..models import PartitionConfig
@@ -46,7 +47,7 @@ def select_columns(
46
47
  return None
47
48
 
48
49
 
49
- class NoSQLKVAdapter:
50
+ class NoSQLKVAdapter(NoSQLConstraintsMixin):
50
51
  """Base with auto-overflow and LINQ support"""
51
52
 
52
53
  # An adapter sets this True when its `_query_raw` accepts `select=[property names]` and really sends it to the store (Azure
@@ -266,8 +267,16 @@ class NoSQLKVAdapter:
266
267
  # Protocol implementation
267
268
  def put(self, model: type, data: JsonDict) -> JsonDict:
268
269
  pk, rk = self._get_pk_rk(model, data)
269
- store_data, _ = self._check_overflow(data)
270
- return self._put_raw(model, pk, rk, store_data)
270
+
271
+ def _write() -> JsonDict:
272
+ store_data, _ = self._check_overflow(data)
273
+ return self._put_raw(model, pk, rk, store_data)
274
+
275
+ uq, ix = self._constraint_fields(model)
276
+ if not (uq or ix):
277
+ return _write()
278
+ previous = self._get_raw(model, pk, rk)
279
+ return self._constrained_write(model, pk, rk, previous, {**(previous or {}), **data}, _write)
271
280
 
272
281
  def query(
273
282
  self,
@@ -279,6 +288,9 @@ class NoSQLKVAdapter:
279
288
  fields: Optional[List[str]] = None,
280
289
  omit: Optional[List[str]] = None,
281
290
  ) -> List[JsonDict]:
291
+ indexed = self._indexed_query(model, query or {}, limit)
292
+ if indexed is not None:
293
+ return project_rows([self._retrieve_overflow(r) for r in indexed], fields, omit)
282
294
  select = select_columns(fields, omit, getattr(model, "__polydb__", {}).get("columns"))
283
295
  if select is not None and self.SUPPORTS_SELECT_PUSHDOWN:
284
296
  results = self._query_raw(model, query or {}, limit, select=select)
@@ -318,15 +330,25 @@ class NoSQLKVAdapter:
318
330
  else:
319
331
  pk, rk = self._get_pk_rk(model, {"id": entity_id, **data})
320
332
 
321
- if not replace:
333
+ uq, ix = self._constraint_fields(model)
334
+ constrained = bool(uq or ix) and pk is not None
335
+ previous: Optional[JsonDict] = None
336
+ if not replace or constrained:
322
337
  existing = self._get_raw(model, pk, rk) # type: ignore
323
338
  if existing:
324
339
  existing = self._retrieve_overflow(existing)
325
- existing.update(data)
326
- data = existing
340
+ previous = dict(existing)
341
+ if not replace:
342
+ existing.update(data)
343
+ data = existing
344
+
345
+ def _write() -> JsonDict:
346
+ store_data, _ = self._check_overflow(data)
347
+ return self._put_raw(model, pk, rk, store_data) # type: ignore
327
348
 
328
- store_data, _ = self._check_overflow(data)
329
- return self._put_raw(model, pk, rk, store_data) # type: ignore
349
+ if not constrained:
350
+ return _write()
351
+ return self._constrained_write(model, pk, rk, previous, data, _write) # type: ignore[arg-type]
330
352
 
331
353
  def upsert(self, model: type, data: JsonDict, *, replace: bool = False) -> JsonDict:
332
354
  return self.put(model, data)
@@ -340,7 +362,13 @@ class NoSQLKVAdapter:
340
362
  else:
341
363
  pk, rk = self._get_pk_rk(model, {"id": entity_id})
342
364
 
343
- return self._delete_raw(model, pk, rk, etag) # type: ignore
365
+ uq, ix = self._constraint_fields(model)
366
+ if not (uq or ix) or pk is None:
367
+ return self._delete_raw(model, pk, rk, etag) # type: ignore
368
+ existing = self._get_raw(model, pk, rk) # type: ignore
369
+ result = self._delete_raw(model, pk, rk, etag) # type: ignore
370
+ self._release_all(model, pk, rk, existing)
371
+ return result
344
372
 
345
373
  def query_linq_rows(self, model: type, builder: QueryBuilder) -> List[JsonDict]:
346
374
  """
@@ -0,0 +1,260 @@
1
+ # src/polydb/base/constraints.py
2
+ """
3
+ Unique constraints and equality lookup indexes for key/value NoSQL stores.
4
+
5
+ Table Storage (and every other KV backend here) enforces uniqueness only for the (PartitionKey, RowKey) pair and can only be
6
+ queried efficiently by those two keys. A model that declares `unique_fields` / `index_fields` in `__polydb__` gets both from this
7
+ mixin, on every NoSQL adapter, with no per-provider code beyond one primitive (`_put_new_raw`, insert-if-absent).
8
+
9
+ How it works -- two hidden side tables per model (`<table>uq`, `<table>ix`):
10
+
11
+ * UNIQUE one "claim" row per (field, value): PartitionKey = u<2 hex of the hash>, RowKey = sha256(field, value). The claim is
12
+ written with an INSERT-IF-ABSENT before the entity, so two writers racing for one value cannot both succeed. A
13
+ duplicate raises UniqueConstraintError. The claim names its owner entity; a claim whose owner no longer exists, or
14
+ whose owner moved to another value (a crash between the claim and the entity write), is taken over.
15
+ * INDEX one row per (field, value, entity): PartitionKey = i<hash of (field, value)>, RowKey = hash of the entity key. An
16
+ equality query on an indexed field reads that one partition, then point-reads each owner and re-checks every filter,
17
+ so a stale index row can never produce a wrong result (it is skipped).
18
+
19
+ Ordering: claims and index rows are written BEFORE the entity and removed AFTER a delete / value change, so a visible entity
20
+ always has its rows. The residue of a crash is a stale row, which is harmless (verified on read, taken over on the next claim).
21
+
22
+ Not covered: rows written before a field was declared have no claim/index row -- run `rebuild_constraints()` once. Backends
23
+ without a native conditional insert fall back to a non-atomic get-then-put (documented on `_put_new_raw`).
24
+ """
25
+
26
+ from __future__ import annotations
27
+
28
+ import hashlib
29
+ import re
30
+ from typing import Any, Dict, List, Optional, Tuple
31
+
32
+ from ..errors import NoSQLError
33
+ from ..types import JsonDict
34
+
35
+ _UNSAFE_KEY = re.compile(r"[\\/#\?\x00-\x1f\x7f:+ ]")
36
+ _UQ, _IX = "uq", "ix"
37
+
38
+
39
+ class UniqueConstraintError(NoSQLError):
40
+ """A write would create a second row with a value a `unique_fields` field already holds."""
41
+
42
+ def __init__(self, field: str, value: Any):
43
+ super().__init__(f"unique constraint violated on {field!r}")
44
+ self.field = field
45
+ self.value = value
46
+
47
+
48
+ def normalize_value(value: Any) -> Optional[str]:
49
+ """The string two values are compared as. None/empty/containers carry no constraint."""
50
+ if value is None or isinstance(value, (dict, list, tuple, set)):
51
+ return None
52
+ if isinstance(value, bool):
53
+ return "true" if value else "false"
54
+ text = str(value)
55
+ return text if text != "" else None
56
+
57
+
58
+ def _digest(*parts: str) -> str:
59
+ return hashlib.sha256("\x1f".join(parts).encode("utf-8")).hexdigest()
60
+
61
+
62
+ class NoSQLConstraintsMixin:
63
+ """Mixed into NoSQLKVAdapter. Needs `_get_raw`, `_put_raw`, `_delete_raw`, `_query_raw`, `_get_pk_rk`."""
64
+
65
+ # -- metadata ---------------------------------------------------------------------------------------------------------
66
+
67
+ @staticmethod
68
+ def _constraint_fields(model: type) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
69
+ meta = getattr(model, "__polydb__", None) or {}
70
+ return tuple(meta.get("unique_fields") or ()), tuple(meta.get("index_fields") or ())
71
+
72
+ def _side_model(self, model: type, suffix: str) -> type:
73
+ cache = self.__dict__.setdefault("_side_models", {})
74
+ meta = getattr(model, "__polydb__", None) or {}
75
+ base = meta.get("collection") or meta.get("collection_name") or meta.get("table") or getattr(model, "__name__", "model")
76
+ base = re.sub(r"[^A-Za-z0-9]", "", str(base)).lower()[:58] or "model"
77
+ key = (base, suffix)
78
+ if key not in cache:
79
+ name = f"{base}{suffix}"
80
+ cache[key] = type(name, (), {"__polydb__": {"storage": "nosql", "table": name, "pk_field": "bucket", "rk_field": "id"}})
81
+ return cache[key]
82
+
83
+ # -- insert-if-absent primitive ----------------------------------------------------------------------------------------
84
+
85
+ def _put_new_raw(self, model: type, pk: str, rk: str, data: JsonDict) -> bool:
86
+ """Insert only when (pk, rk) is absent. True = inserted, False = already there. This default is get-then-put and is NOT
87
+ atomic; an adapter with a native conditional create (Azure `create_entity`, Dynamo `attribute_not_exists`, ...) overrides."""
88
+ if self._get_raw(model, pk, rk) is not None: # type: ignore[attr-defined]
89
+ return False
90
+ self._put_raw(model, pk, rk, data) # type: ignore[attr-defined]
91
+ return True
92
+
93
+ def _query_partition_raw(self, model: type, partition: str, limit: Optional[int]) -> List[JsonDict]:
94
+ """Every row of one partition. Portable default: a property equality on the mirrored `ix_bucket` field."""
95
+ return self._query_raw(model, {"ix_bucket": partition}, limit) # type: ignore[attr-defined]
96
+
97
+ # -- side-row helpers --------------------------------------------------------------------------------------------------
98
+
99
+ def _safe_delete(self, model: type, pk: str, rk: str) -> None:
100
+ try:
101
+ self._delete_raw(model, pk, rk, None) # type: ignore[attr-defined]
102
+ except Exception: # already gone, or the row was never written
103
+ pass
104
+
105
+ def _claim(self, model: type, pk: str, rk: str, field: str, value: str) -> None:
106
+ side = self._side_model(model, _UQ)
107
+ h = _digest(field, value)
108
+ spk = f"u{h[:2]}"
109
+ row = {"bucket": spk, "ix_bucket": spk, "id": h, "field": field, "owner_pk": pk, "owner_rk": rk}
110
+ for _ in range(4):
111
+ if self._put_new_raw(side, spk, h, row):
112
+ return
113
+ cur = self._get_raw(side, spk, h) # type: ignore[attr-defined]
114
+ if cur is None: # released between our insert and our read
115
+ continue
116
+ if (cur.get("owner_pk"), cur.get("owner_rk")) == (pk, rk):
117
+ return # our own claim (an idempotent retry)
118
+ owner = self._get_raw(model, cur.get("owner_pk"), cur.get("owner_rk")) # type: ignore[attr-defined]
119
+ if owner is not None and normalize_value(owner.get(field)) == value:
120
+ raise UniqueConstraintError(field, value)
121
+ self._safe_delete(side, spk, h) # orphaned or moved-on claim: take it over
122
+ raise UniqueConstraintError(field, value)
123
+
124
+ def _release(self, model: type, pk: str, rk: str, field: str, value: str) -> None:
125
+ side = self._side_model(model, _UQ)
126
+ h = _digest(field, value)
127
+ spk = f"u{h[:2]}"
128
+ cur = self._get_raw(side, spk, h) # type: ignore[attr-defined]
129
+ if cur is not None and (cur.get("owner_pk"), cur.get("owner_rk")) == (pk, rk):
130
+ self._safe_delete(side, spk, h)
131
+
132
+ def _index_key(self, field: str, value: str, pk: str, rk: str) -> Tuple[str, str]:
133
+ return f"i{_digest(field, value)}", _digest(str(pk), str(rk))
134
+
135
+ def _index_put(self, model: type, pk: str, rk: str, field: str, value: str) -> None:
136
+ side = self._side_model(model, _IX)
137
+ spk, srk = self._index_key(field, value, pk, rk)
138
+ row = {"bucket": spk, "ix_bucket": spk, "id": srk, "field": field, "owner_pk": pk, "owner_rk": rk}
139
+ self._put_raw(side, spk, srk, row) # type: ignore[attr-defined]
140
+
141
+ def _index_del(self, model: type, pk: str, rk: str, field: str, value: str) -> None:
142
+ spk, srk = self._index_key(field, value, pk, rk)
143
+ self._safe_delete(self._side_model(model, _IX), spk, srk)
144
+
145
+ # -- write path --------------------------------------------------------------------------------------------------------
146
+
147
+ def _constrained_write(self, model: type, pk: str, rk: str, previous: Optional[JsonDict], new: JsonDict, write):
148
+ """Claim -> index -> entity write -> release/remove the old rows. `new` is the row as it will exist afterwards."""
149
+ uq, ix = self._constraint_fields(model)
150
+ prev = previous or {}
151
+ claimed: List[Tuple[str, str]] = []
152
+ try:
153
+ for f in uq:
154
+ nv, ov = normalize_value(new.get(f)), normalize_value(prev.get(f))
155
+ if nv is not None and nv != ov:
156
+ self._claim(model, pk, rk, f, nv)
157
+ claimed.append((f, nv))
158
+ for f in ix:
159
+ nv, ov = normalize_value(new.get(f)), normalize_value(prev.get(f))
160
+ if nv is not None and nv != ov:
161
+ self._index_put(model, pk, rk, f, nv)
162
+ result = write()
163
+ except Exception:
164
+ for f, v in claimed:
165
+ self._release(model, pk, rk, f, v)
166
+ raise
167
+ for f in uq:
168
+ nv, ov = normalize_value(new.get(f)), normalize_value(prev.get(f))
169
+ if ov is not None and ov != nv:
170
+ self._release(model, pk, rk, f, ov)
171
+ for f in ix:
172
+ nv, ov = normalize_value(new.get(f)), normalize_value(prev.get(f))
173
+ if ov is not None and ov != nv:
174
+ self._index_del(model, pk, rk, f, ov)
175
+ return result
176
+
177
+ def _release_all(self, model: type, pk: str, rk: str, row: Optional[JsonDict]) -> None:
178
+ if not row:
179
+ return
180
+ uq, ix = self._constraint_fields(model)
181
+ for f in uq:
182
+ v = normalize_value(row.get(f))
183
+ if v is not None:
184
+ self._release(model, pk, rk, f, v)
185
+ for f in ix:
186
+ v = normalize_value(row.get(f))
187
+ if v is not None:
188
+ self._index_del(model, pk, rk, f, v)
189
+
190
+ # -- read path ---------------------------------------------------------------------------------------------------------
191
+
192
+ def _lookup_owners(self, model: type, field: str, value: str, limit: Optional[int]) -> List[Tuple[str, str]]:
193
+ uq, _ = self._constraint_fields(model)
194
+ h = _digest(field, value)
195
+ if field in uq:
196
+ cur = self._get_raw(self._side_model(model, _UQ), f"u{h[:2]}", h) # type: ignore[attr-defined]
197
+ return [(cur["owner_pk"], cur["owner_rk"])] if cur else []
198
+ rows = self._query_partition_raw(self._side_model(model, _IX), f"i{h}", limit)
199
+ return [(r["owner_pk"], r["owner_rk"]) for r in rows if r.get("owner_pk") is not None]
200
+
201
+ @staticmethod
202
+ def _filter_matches(row: JsonDict, query: Dict[str, Any], pk: str, rk: str) -> bool:
203
+ for k, want in query.items():
204
+ if k in ("partition_key", "PartitionKey"):
205
+ if _UNSAFE_KEY.sub("_", str(pk)) != str(want):
206
+ return False
207
+ elif k in ("row_key", "RowKey"):
208
+ if _UNSAFE_KEY.sub("_", str(rk)) != str(want):
209
+ return False
210
+ elif want is None:
211
+ if row.get(k) is not None:
212
+ return False
213
+ elif normalize_value(row.get(k)) != normalize_value(want):
214
+ return False
215
+ return True
216
+
217
+ def _indexed_query(self, model: type, query: Dict[str, Any], limit: Optional[int]) -> Optional[List[JsonDict]]:
218
+ """Rows for a query that has an equality on a unique/indexed field, or None when the index cannot serve it."""
219
+ uq, ix = self._constraint_fields(model)
220
+ if not query or not (uq or ix):
221
+ return None
222
+ if any(isinstance(v, (dict, list, tuple, set)) for v in query.values()):
223
+ return None
224
+ for f in (*uq, *ix):
225
+ nv = normalize_value(query.get(f)) if f in query else None
226
+ if nv is None:
227
+ continue
228
+ out: List[JsonDict] = []
229
+ for opk, ork in self._lookup_owners(model, f, nv, None):
230
+ row = self._get_raw(model, opk, ork) # type: ignore[attr-defined]
231
+ if row is not None and self._filter_matches(row, query, opk, ork):
232
+ out.append(row)
233
+ if limit and len(out) >= limit:
234
+ break
235
+ return out
236
+ return None
237
+
238
+ def rebuild_constraints(self, model: type) -> Dict[str, int]:
239
+ """One-off backfill for rows written before a field was declared unique/indexed. Claims every existing value (the
240
+ first holder keeps it; a duplicate is reported, not overwritten) and writes every index row."""
241
+ uq, ix = self._constraint_fields(model)
242
+ done = {"rows": 0, "claims": 0, "indexed": 0, "duplicates": 0}
243
+ for row in self._query_raw(model, {}, None): # type: ignore[attr-defined]
244
+ pk, rk = self._get_pk_rk(model, row) # type: ignore[attr-defined]
245
+ done["rows"] += 1
246
+ for f in uq:
247
+ v = normalize_value(row.get(f))
248
+ if v is None:
249
+ continue
250
+ try:
251
+ self._claim(model, pk, rk, f, v)
252
+ done["claims"] += 1
253
+ except UniqueConstraintError:
254
+ done["duplicates"] += 1
255
+ for f in ix:
256
+ v = normalize_value(row.get(f))
257
+ if v is not None:
258
+ self._index_put(model, pk, rk, f, v)
259
+ done["indexed"] += 1
260
+ return done
polydb/databaseFactory.py CHANGED
@@ -167,6 +167,8 @@ def _extract_meta(model: Union[type, str]) -> ModelMeta:
167
167
  cache=raw.get("cache", False),
168
168
  cache_ttl=raw.get("cache_ttl"),
169
169
  encrypted_fields=tuple(raw.get("encrypted_fields") or ()),
170
+ unique_fields=tuple(raw.get("unique_fields") or ()),
171
+ index_fields=tuple(raw.get("index_fields") or ()),
170
172
  )
171
173
  return ModelMeta(storage="nosql", table=None, collection=None)
172
174
 
polydb/types.py CHANGED
@@ -26,6 +26,9 @@ class ModelMeta:
26
26
  cache_ttl: Optional[int] = None
27
27
  # Fields stored encrypted when the factory has encryption enabled (a tuple: the dataclass is frozen/hashable).
28
28
  encrypted_fields: Tuple[str, ...] = ()
29
+ # NoSQL: fields that must stay unique across the model / that get an equality lookup index (see base/constraints.py).
30
+ unique_fields: Tuple[str, ...] = ()
31
+ index_fields: Tuple[str, ...] = ()
29
32
 
30
33
 
31
34
  @runtime_checkable