umf-access 0.1.0.dev8__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.
- umf_access/__init__.py +109 -0
- umf_access/adoption.py +41 -0
- umf_access/build/__init__.py +2 -0
- umf_access/build/approximate.py +34 -0
- umf_access/build/catalog.py +1626 -0
- umf_access/build/commands.py +394 -0
- umf_access/build/pg_privileges.py +1544 -0
- umf_access/build/privileges.py +134 -0
- umf_access/build/search.py +365 -0
- umf_access/build/unity_search.py +104 -0
- umf_access/bulk.py +227 -0
- umf_access/canonical.py +23 -0
- umf_access/capture.py +410 -0
- umf_access/catalog.py +325 -0
- umf_access/codec_support.py +427 -0
- umf_access/commands.py +93 -0
- umf_access/delta_read.py +758 -0
- umf_access/dsl/__init__.py +148 -0
- umf_access/dsl/_context.py +36 -0
- umf_access/dsl/_expressions.py +625 -0
- umf_access/dsl/_joined.py +4 -0
- umf_access/dsl/_nodes.py +207 -0
- umf_access/dsl/_query.py +5559 -0
- umf_access/dsl/_selection.py +143 -0
- umf_access/dsl/navigation.py +302 -0
- umf_access/dsl/render.py +516 -0
- umf_access/dsl/search.py +343 -0
- umf_access/engine_catalog.py +563 -0
- umf_access/generated_core.py +5 -0
- umf_access/lifecycle/__init__.py +46 -0
- umf_access/lifecycle/_darwin_process_observer.c +306 -0
- umf_access/lifecycle/approval.py +366 -0
- umf_access/lifecycle/backfills.py +79 -0
- umf_access/lifecycle/catalog.py +289 -0
- umf_access/lifecycle/cli.py +231 -0
- umf_access/lifecycle/concurrent.py +196 -0
- umf_access/lifecycle/conversion_policy.py +188 -0
- umf_access/lifecycle/correspondence.py +203 -0
- umf_access/lifecycle/generation_authority_bootstrap.py +1491 -0
- umf_access/lifecycle/generation_authority_session.py +125 -0
- umf_access/lifecycle/generation_enrollment_authority.py +1108 -0
- umf_access/lifecycle/generation_enrollment_ledger.py +698 -0
- umf_access/lifecycle/generation_ingress.py +185 -0
- umf_access/lifecycle/generation_root_registry.py +138 -0
- umf_access/lifecycle/head.py +250 -0
- umf_access/lifecycle/head_view.py +81 -0
- umf_access/lifecycle/host.py +1183 -0
- umf_access/lifecycle/ingress_custody.py +750 -0
- umf_access/lifecycle/ingress_local_postgres.py +7990 -0
- umf_access/lifecycle/ingress_store.py +1488 -0
- umf_access/lifecycle/ledger.py +276 -0
- umf_access/lifecycle/ledger_schema.py +116 -0
- umf_access/lifecycle/mixed.py +593 -0
- umf_access/lifecycle/outcome_schema.py +136 -0
- umf_access/lifecycle/owner_approval_core.py +1810 -0
- umf_access/lifecycle/owner_authority.py +472 -0
- umf_access/lifecycle/owner_bootstrap.py +2077 -0
- umf_access/lifecycle/owner_wire.py +161 -0
- umf_access/lifecycle/phase_fence.py +52 -0
- umf_access/lifecycle/phase_policy.py +422 -0
- umf_access/lifecycle/privileges.py +145 -0
- umf_access/lifecycle/probe.py +39 -0
- umf_access/lifecycle/progress.py +497 -0
- umf_access/lifecycle/roles.py +96 -0
- umf_access/lifecycle/row_originals.py +1712 -0
- umf_access/lifecycle/row_originals_creation.py +759 -0
- umf_access/lifecycle/row_originals_receipt.py +413 -0
- umf_access/lifecycle/seeds.py +99 -0
- umf_access/lifecycle/sql_containment.py +358 -0
- umf_access/lifecycle/sql_containment_cli.py +33 -0
- umf_access/lifecycle/sql_containment_parser.py +20 -0
- umf_access/lifecycle/supervisor_journal_codec.py +1251 -0
- umf_access/lifecycle/table_boundary.py +22 -0
- umf_access/lifecycle/target_custody_codec.py +788 -0
- umf_access/lifecycle/witness.py +72 -0
- umf_access/managed_actions.py +271 -0
- umf_access/managed_bulk.py +353 -0
- umf_access/managed_delta.py +150 -0
- umf_access/managed_support.py +166 -0
- umf_access/manifest_commands.py +278 -0
- umf_access/native_actions.py +1007 -0
- umf_access/native_program.py +570 -0
- umf_access/pages.py +125 -0
- umf_access/postgres_bulk.py +509 -0
- umf_access/postgres_read.py +1230 -0
- umf_access/postgres_search.py +639 -0
- umf_access/protocols.py +665 -0
- umf_access/py.typed +0 -0
- umf_access/related_keys.py +233 -0
- umf_access/reviewed.py +706 -0
- umf_access/search_catalog.py +122 -0
- umf_access/targets.py +312 -0
- umf_access/types.py +220 -0
- umf_access/unified/__init__.py +187 -0
- umf_access/unified/_binding.py +654 -0
- umf_access/unified/head_session.py +144 -0
- umf_access/unified/native_binding.py +143 -0
- umf_access/unified/operation_values.py +173 -0
- umf_access/unified/outcome_values.py +76 -0
- umf_access/unified/session_async.py +161 -0
- umf_access/unified/session_binding.py +108 -0
- umf_access/unified/session_policy.py +152 -0
- umf_access/unified/session_psycopg2.py +70 -0
- umf_access/unified/session_sqlalchemy.py +161 -0
- umf_access/unified/session_sync.py +148 -0
- umf_access/unified/table_reads.py +314 -0
- umf_access/unified/table_writes.py +193 -0
- umf_access/unity_search.py +312 -0
- umf_access/values.py +280 -0
- umf_access/verification.py +64 -0
- umf_access-0.1.0.dev8.dist-info/METADATA +15 -0
- umf_access-0.1.0.dev8.dist-info/RECORD +117 -0
- umf_access-0.1.0.dev8.dist-info/WHEEL +4 -0
- umf_access-0.1.0.dev8.dist-info/entry_points.txt +4 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE-APACHE +201 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE-MIT +21 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE.md +239 -0
umf_access/pages.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from collections.abc import Mapping
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from dataclasses import field as dataclass_field
|
|
7
|
+
from typing import Generic, TypeVar
|
|
8
|
+
|
|
9
|
+
from .types import SnapshotVersions, validate_snapshot_versions
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"ReadContinuation",
|
|
13
|
+
"ReadObligationUnmet",
|
|
14
|
+
"ReadPage",
|
|
15
|
+
"SnapshotVersions",
|
|
16
|
+
"StaleContinuation",
|
|
17
|
+
"StreamLimitReached",
|
|
18
|
+
"stream_page_size",
|
|
19
|
+
"validate_snapshot_versions",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
R = TypeVar("R")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class ReadContinuation:
|
|
27
|
+
values: tuple[object, ...]
|
|
28
|
+
identity_digest: str
|
|
29
|
+
owner_cursor: bytes | None = dataclass_field(default=None, repr=False)
|
|
30
|
+
|
|
31
|
+
snapshot_versions: SnapshotVersions = ()
|
|
32
|
+
|
|
33
|
+
def __post_init__(self) -> None:
|
|
34
|
+
validate_snapshot_versions(self.snapshot_versions)
|
|
35
|
+
if self.owner_cursor is not None and (
|
|
36
|
+
type(self.owner_cursor) is not bytes
|
|
37
|
+
or not 1 <= len(self.owner_cursor) <= 65536
|
|
38
|
+
):
|
|
39
|
+
raise TypeError("bounded immutable original engine cursor required")
|
|
40
|
+
if type(self.values) is not tuple or type(self.identity_digest) is not str:
|
|
41
|
+
raise TypeError(
|
|
42
|
+
"continuations require immutable values and an identity digest"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass(frozen=True)
|
|
47
|
+
class ReadPage(Generic[R]):
|
|
48
|
+
items: tuple[R, ...]
|
|
49
|
+
next_after: ReadContinuation | None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class ReadObligationUnmet(RuntimeError):
|
|
53
|
+
pass
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class StaleContinuation(ReadObligationUnmet):
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class StreamLimitReached(RuntimeError):
|
|
61
|
+
"""The row bound was reached before natural exhaustion was established.
|
|
62
|
+
|
|
63
|
+
truncated=True proves buffered rows remain. None means the original cursor
|
|
64
|
+
may have more rows; reaching an exact full-page boundary does not prove it.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
def __init__(self, max_rows: int, *, truncated: bool | None):
|
|
68
|
+
self.max_rows = max_rows
|
|
69
|
+
self.emitted = max_rows
|
|
70
|
+
self.truncated = truncated
|
|
71
|
+
super().__init__(
|
|
72
|
+
"stream reached max_rows before natural exhaustion was established"
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def stream_page_size(
|
|
77
|
+
artifact: Mapping[str, object], requested: int | None
|
|
78
|
+
) -> int | None:
|
|
79
|
+
"""Validate the original owner's optional bounded stream-size parameter."""
|
|
80
|
+
if requested is not None and (
|
|
81
|
+
type(requested) is not int or not 1 <= requested <= 1000
|
|
82
|
+
):
|
|
83
|
+
raise ValueError("page_size must be an integer from 1 through 1000")
|
|
84
|
+
obligations = artifact.get("obligations")
|
|
85
|
+
domain = (
|
|
86
|
+
obligations.get("streamPageSize") if isinstance(obligations, Mapping) else None
|
|
87
|
+
)
|
|
88
|
+
if domain is None:
|
|
89
|
+
if requested is not None:
|
|
90
|
+
raise ReadObligationUnmet(
|
|
91
|
+
"STREAM_PAGE_SIZE_UNSUPPORTED: original owner parameter required"
|
|
92
|
+
)
|
|
93
|
+
return None
|
|
94
|
+
if (
|
|
95
|
+
type(domain) is not dict
|
|
96
|
+
or set(domain) != {"parameter", "minimum", "maximum"}
|
|
97
|
+
or domain["parameter"] != "__umf_page_size"
|
|
98
|
+
or type(domain["minimum"]) is not int
|
|
99
|
+
or domain["minimum"] != 1
|
|
100
|
+
or type(domain["maximum"]) is not int
|
|
101
|
+
or not 1 <= domain["maximum"] <= 1000
|
|
102
|
+
or type(obligations.get("rowCap")) is not int
|
|
103
|
+
or domain["maximum"] > obligations["rowCap"]
|
|
104
|
+
):
|
|
105
|
+
raise ReadObligationUnmet("invalid original stream page-size obligation")
|
|
106
|
+
parameters = artifact.get("cursorParameters")
|
|
107
|
+
slots = (
|
|
108
|
+
[
|
|
109
|
+
p
|
|
110
|
+
for p in parameters
|
|
111
|
+
if type(p) is dict and p.get("name") == "__umf_page_size"
|
|
112
|
+
]
|
|
113
|
+
if type(parameters) is list
|
|
114
|
+
else []
|
|
115
|
+
)
|
|
116
|
+
if (
|
|
117
|
+
len(slots) != 1
|
|
118
|
+
or slots[0].get("carrier") != "integer"
|
|
119
|
+
or slots[0].get("nullable", False) is not False
|
|
120
|
+
):
|
|
121
|
+
raise ReadObligationUnmet("original stream page-size parameter differs")
|
|
122
|
+
size = domain["maximum"] if requested is None else requested
|
|
123
|
+
if size > domain["maximum"]:
|
|
124
|
+
raise ValueError("page_size exceeds the original compiled maximum")
|
|
125
|
+
return size
|
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
2
|
+
"""Native set-based bulk driver over the original action settlement machinery."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
from collections.abc import Mapping
|
|
8
|
+
from datetime import timedelta
|
|
9
|
+
from typing import Any, cast
|
|
10
|
+
|
|
11
|
+
from pydantic import TypeAdapter
|
|
12
|
+
|
|
13
|
+
from .bulk import AsyncAtomicBulkPlan, AtomicBulkPlan, BulkOutcome, BulkPreview
|
|
14
|
+
from .commands import CommandVariant
|
|
15
|
+
from .native_actions import (
|
|
16
|
+
AsyncNativeAdmission,
|
|
17
|
+
AsyncPostgresActionExecutor,
|
|
18
|
+
NativeAdmission,
|
|
19
|
+
PostgresActionExecutor,
|
|
20
|
+
_State,
|
|
21
|
+
native_input_value,
|
|
22
|
+
)
|
|
23
|
+
from .native_program import NativeProgram, canonical_json
|
|
24
|
+
from .protocols import (
|
|
25
|
+
ActionConflict,
|
|
26
|
+
ActionDenied,
|
|
27
|
+
ActionIndeterminate,
|
|
28
|
+
ActionRefused,
|
|
29
|
+
ActionTimedOut,
|
|
30
|
+
Committed,
|
|
31
|
+
Conflict,
|
|
32
|
+
Denied,
|
|
33
|
+
Indeterminate,
|
|
34
|
+
Receipt,
|
|
35
|
+
TimedOut,
|
|
36
|
+
)
|
|
37
|
+
from .reviewed import validate_list_parameters
|
|
38
|
+
from .targets import AsyncPostgresTarget, PostgresConfiguration, PostgresTarget
|
|
39
|
+
from .types import ActionRef, StrictModel
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _validate_parameters(
|
|
43
|
+
parameters: list[Any], inputs: Mapping[str, object], action_inputs: Mapping[str, str]
|
|
44
|
+
) -> None:
|
|
45
|
+
"""Check original parameter values without granting execution authority."""
|
|
46
|
+
from decimal import Decimal
|
|
47
|
+
|
|
48
|
+
carriers = {"string": str, "boolean": bool, "integer": int, "decimal": Decimal}
|
|
49
|
+
validate_list_parameters(parameters, inputs)
|
|
50
|
+
for parameter in parameters:
|
|
51
|
+
if parameter.get("container") == "list":
|
|
52
|
+
if parameter["name"] in action_inputs.values():
|
|
53
|
+
raise ActionRefused("NATIVE_BULK_ACTION_INPUT_SCALAR")
|
|
54
|
+
continue
|
|
55
|
+
annotation = carriers[parameter["carrier"]]
|
|
56
|
+
TypeAdapter(
|
|
57
|
+
cast(Any, annotation | None if parameter["nullable"] else annotation)
|
|
58
|
+
).validate_python(inputs[parameter["name"]], strict=True)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _bulk_input_value(value: object) -> Any:
|
|
62
|
+
"""Retain bounded selection lists; action scalar conversion stays unchanged."""
|
|
63
|
+
return (
|
|
64
|
+
[native_input_value(item) for item in value]
|
|
65
|
+
if type(value) is list
|
|
66
|
+
else native_input_value(value)
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class _BulkState(_State):
|
|
71
|
+
def __init__(
|
|
72
|
+
self,
|
|
73
|
+
action: ActionRef[Any, Any],
|
|
74
|
+
program: NativeProgram,
|
|
75
|
+
inputs: Mapping[str, object],
|
|
76
|
+
preview: bool,
|
|
77
|
+
):
|
|
78
|
+
program.bind(action)
|
|
79
|
+
self.action, self.program, self.inputs = action, program.checked(), dict(inputs)
|
|
80
|
+
if (
|
|
81
|
+
self.program["profile"] != "umf.actions.native-bulk/0.1"
|
|
82
|
+
or action.source is None
|
|
83
|
+
or self.program["source"]["action"] != action.action
|
|
84
|
+
or self.program["source"]["module"] != action.module
|
|
85
|
+
or self.program["source"]["documentSha256"] != action.source.document_sha256
|
|
86
|
+
or getattr(action.input_type, "UMF_BUNDLE_INPUT_DIGEST", None)
|
|
87
|
+
!= program.bundle_input_digest
|
|
88
|
+
):
|
|
89
|
+
raise ActionRefused("NATIVE_BULK_SOURCE_MISMATCH")
|
|
90
|
+
parameters = self.program["parameters"]
|
|
91
|
+
if set(inputs) != {p["name"] for p in parameters}:
|
|
92
|
+
raise ActionRefused("NATIVE_BULK_PARAMETER_INVENTORY")
|
|
93
|
+
_validate_parameters(parameters, inputs, self.program["actionInputs"])
|
|
94
|
+
# Mutation inputs use their original generated Field validators, including
|
|
95
|
+
# constraints, rather than only the SQL scalar carrier.
|
|
96
|
+
for source, name in self.program["actionInputs"].items():
|
|
97
|
+
python_name = next(
|
|
98
|
+
python for original, python in action.input_fields if original == source
|
|
99
|
+
)
|
|
100
|
+
field = action.input_type.model_fields[python_name]
|
|
101
|
+
from typing import Annotated
|
|
102
|
+
|
|
103
|
+
TypeAdapter(
|
|
104
|
+
cast(
|
|
105
|
+
Any,
|
|
106
|
+
Annotated[(field.annotation, *field.metadata)]
|
|
107
|
+
if field.metadata
|
|
108
|
+
else field.annotation,
|
|
109
|
+
)
|
|
110
|
+
).validate_python(inputs[name], strict=True)
|
|
111
|
+
self.statements = json.loads(program.statements_json)
|
|
112
|
+
self.pre = {}
|
|
113
|
+
self.post = {}
|
|
114
|
+
self.scope = None
|
|
115
|
+
self.receipt = None
|
|
116
|
+
self.markers = []
|
|
117
|
+
self.verification = []
|
|
118
|
+
self.changes = []
|
|
119
|
+
self.parameters = dict(inputs)
|
|
120
|
+
self.request = canonical_json(
|
|
121
|
+
{
|
|
122
|
+
"program": self.program,
|
|
123
|
+
"inputDigest": program.bundle_input_digest,
|
|
124
|
+
"parameters": {
|
|
125
|
+
name: _bulk_input_value(value)
|
|
126
|
+
for name, value in self.inputs.items()
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
)
|
|
130
|
+
if len(self.request.encode()) > 4_194_304:
|
|
131
|
+
raise ActionRefused("NATIVE_REQUEST_BUDGET")
|
|
132
|
+
self.rows: list[Any] = []
|
|
133
|
+
self.read_only = preview
|
|
134
|
+
|
|
135
|
+
def resolve(self, source: Any) -> Any:
|
|
136
|
+
if "bulkInput" in source:
|
|
137
|
+
return self.inputs[source["bulkInput"]]
|
|
138
|
+
if "bulkKeys" in source:
|
|
139
|
+
field = self.program["entity"]["key"]["fields"][source["bulkKeys"]][
|
|
140
|
+
"reference"
|
|
141
|
+
]
|
|
142
|
+
index = next(
|
|
143
|
+
i
|
|
144
|
+
for i, f in enumerate(self.program["selectionFields"])
|
|
145
|
+
if f["reference"] == field
|
|
146
|
+
)
|
|
147
|
+
return [row[index] for row in self.rows]
|
|
148
|
+
return super().resolve(source)
|
|
149
|
+
|
|
150
|
+
def decode_key(self, values: tuple[Any, ...]) -> StrictModel:
|
|
151
|
+
ref = self.program["entity"]["key"]["reference"]
|
|
152
|
+
model = next(
|
|
153
|
+
k.model
|
|
154
|
+
for k in self.action.key_types
|
|
155
|
+
if (k.module, k.element, k.key)
|
|
156
|
+
== (ref["module"], ref["element"], ref["key"])
|
|
157
|
+
)
|
|
158
|
+
fields = self.program["entity"]["key"]["fields"]
|
|
159
|
+
by_ref = {
|
|
160
|
+
(f["reference"]["module"], f["reference"]["element"]): value
|
|
161
|
+
for f, value in zip(fields, values, strict=True)
|
|
162
|
+
}
|
|
163
|
+
return model.model_validate(
|
|
164
|
+
{
|
|
165
|
+
name: by_ref[(module, element)]
|
|
166
|
+
for module, element, name in model.UMF_FIELDS
|
|
167
|
+
},
|
|
168
|
+
strict=True,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
def execute(self, validate: bool):
|
|
172
|
+
rows = yield ("query", *self.statement("/admission"))
|
|
173
|
+
if len(rows) != 1 or len(rows[0]) != 4 or self.scope is None:
|
|
174
|
+
raise ActionRefused("NATIVE_BULK_ADMISSION_MARKER")
|
|
175
|
+
allowed, authenticated, effective, xid = rows[0]
|
|
176
|
+
if (
|
|
177
|
+
(authenticated, effective)
|
|
178
|
+
!= (self.scope.authenticated, self.scope.effective)
|
|
179
|
+
or type(xid) is not str
|
|
180
|
+
or not xid.isdecimal()
|
|
181
|
+
):
|
|
182
|
+
raise ActionRefused("NATIVE_SESSION_CHANGED")
|
|
183
|
+
self.receipt = Receipt(self.scope.store, self.scope.epoch, xid)
|
|
184
|
+
if allowed is not True:
|
|
185
|
+
yield ("rollback",)
|
|
186
|
+
return Denied("ROLE_DENIED", self.details("rolled-back"))
|
|
187
|
+
if not self.read_only:
|
|
188
|
+
for sql in self.program["lockSql"]:
|
|
189
|
+
yield ("query", sql, None)
|
|
190
|
+
yield from self.schema_admissions()
|
|
191
|
+
self.rows = yield (
|
|
192
|
+
"query",
|
|
193
|
+
*self.statement("/preview" if self.read_only else "/selection"),
|
|
194
|
+
self.program["maxItems"] + 2,
|
|
195
|
+
)
|
|
196
|
+
if len(self.rows) > self.program["maxItems"]:
|
|
197
|
+
raise ActionRefused("BULK_LIMIT")
|
|
198
|
+
fields = self.program["selectionFields"]
|
|
199
|
+
entity = self.program["entity"]
|
|
200
|
+
positions = [
|
|
201
|
+
next(i for i, f in enumerate(fields) if f["reference"] == k["reference"])
|
|
202
|
+
for k in entity["key"]["fields"]
|
|
203
|
+
]
|
|
204
|
+
keys = tuple(
|
|
205
|
+
self.decode_key(tuple(row[i] for i in positions)) for row in self.rows
|
|
206
|
+
)
|
|
207
|
+
if len({key.model_dump_json() for key in keys}) != len(keys):
|
|
208
|
+
raise ActionRefused("NATIVE_BULK_DUPLICATE_KEY")
|
|
209
|
+
for row in self.rows:
|
|
210
|
+
if len(row) != len(fields):
|
|
211
|
+
raise ActionRefused("NATIVE_BULK_ROW_SHAPE")
|
|
212
|
+
if self.read_only:
|
|
213
|
+
yield from self.schema_admissions()
|
|
214
|
+
yield ("rollback",)
|
|
215
|
+
return BulkPreview(
|
|
216
|
+
keys, "one bounded source-bound native SET; preview is advisory"
|
|
217
|
+
)
|
|
218
|
+
expected = 0
|
|
219
|
+
for row in self.rows:
|
|
220
|
+
different = False
|
|
221
|
+
for field, value in self.program["values"]:
|
|
222
|
+
index = next(
|
|
223
|
+
i
|
|
224
|
+
for i, f in enumerate(fields)
|
|
225
|
+
if f["reference"] == field["reference"]
|
|
226
|
+
)
|
|
227
|
+
source = (
|
|
228
|
+
{"bulkInput": self.program["actionInputs"][value["input"]]}
|
|
229
|
+
if "input" in value
|
|
230
|
+
else value
|
|
231
|
+
)
|
|
232
|
+
different = different or row[index] != self.resolve(source)
|
|
233
|
+
if different:
|
|
234
|
+
expected += 1
|
|
235
|
+
changed = yield (
|
|
236
|
+
"query",
|
|
237
|
+
*self.statement("/update"),
|
|
238
|
+
self.program["maxItems"] + 1,
|
|
239
|
+
)
|
|
240
|
+
if len(changed) != expected:
|
|
241
|
+
raise ActionDenied("NATIVE_BULK_EFFECT_DENIED", self.details("pending"))
|
|
242
|
+
returned = tuple(self.decode_key(tuple(row)) for row in changed)
|
|
243
|
+
if len({key.model_dump_json() for key in returned}) != len(returned) or any(
|
|
244
|
+
key not in keys for key in returned
|
|
245
|
+
):
|
|
246
|
+
raise ActionRefused("NATIVE_BULK_RETURNED_KEY")
|
|
247
|
+
self.markers = [{"selected": len(keys), "changed": len(returned)}]
|
|
248
|
+
self.changes = [
|
|
249
|
+
{"kind": "updated", "key": key.model_dump_json()} for key in returned
|
|
250
|
+
]
|
|
251
|
+
details = self.details("committed")
|
|
252
|
+
result = Committed(
|
|
253
|
+
BulkOutcome(len(keys), len(returned), self.receipt, details),
|
|
254
|
+
self.receipt,
|
|
255
|
+
(),
|
|
256
|
+
returned,
|
|
257
|
+
details,
|
|
258
|
+
)
|
|
259
|
+
yield from self.schema_admissions()
|
|
260
|
+
yield ("commit", result)
|
|
261
|
+
return result
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
class _Driver(PostgresActionExecutor):
|
|
265
|
+
def __init__(
|
|
266
|
+
self,
|
|
267
|
+
connection: Any,
|
|
268
|
+
program: NativeProgram,
|
|
269
|
+
action: ActionRef[Any, Any],
|
|
270
|
+
inputs: Mapping[str, object],
|
|
271
|
+
admission: NativeAdmission,
|
|
272
|
+
default_timeout: timedelta,
|
|
273
|
+
reconnect: Any,
|
|
274
|
+
):
|
|
275
|
+
super().__init__(
|
|
276
|
+
connection,
|
|
277
|
+
(program,),
|
|
278
|
+
admission,
|
|
279
|
+
default_timeout=default_timeout,
|
|
280
|
+
reconnect=reconnect,
|
|
281
|
+
)
|
|
282
|
+
self.action, self.program, self.inputs, self.preview_mode = (
|
|
283
|
+
action,
|
|
284
|
+
program,
|
|
285
|
+
dict(inputs),
|
|
286
|
+
False,
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
def _state(
|
|
290
|
+
self, action: ActionRef[Any, Any], inputs: Mapping[str, object]
|
|
291
|
+
) -> _BulkState:
|
|
292
|
+
if self.quarantined:
|
|
293
|
+
raise ActionRefused("NATIVE_CONNECTION_QUARANTINED")
|
|
294
|
+
return _BulkState(self.action, self.program, self.inputs, self.preview_mode)
|
|
295
|
+
|
|
296
|
+
def preview(self, *, timeout: timedelta | None = None) -> BulkPreview:
|
|
297
|
+
self.preview_mode = True
|
|
298
|
+
try:
|
|
299
|
+
result = self._run(self.action, {}, timeout, True)
|
|
300
|
+
finally:
|
|
301
|
+
self.preview_mode = False
|
|
302
|
+
if isinstance(result, BulkPreview):
|
|
303
|
+
return result
|
|
304
|
+
if isinstance(result, Denied):
|
|
305
|
+
raise ActionDenied(result.code, result.original)
|
|
306
|
+
if isinstance(result, TimedOut):
|
|
307
|
+
raise ActionTimedOut("TIMED_OUT", result.original)
|
|
308
|
+
if isinstance(result, Indeterminate):
|
|
309
|
+
raise ActionIndeterminate("INDETERMINATE", result.original)
|
|
310
|
+
if isinstance(result, Conflict):
|
|
311
|
+
raise ActionConflict(result.code, result.retained, result.original)
|
|
312
|
+
raise ActionRefused("NATIVE_BULK_PREVIEW_FAILED")
|
|
313
|
+
|
|
314
|
+
def execute(self, *, timeout: timedelta | None = None) -> BulkOutcome:
|
|
315
|
+
result = self._run(self.action, {}, timeout, False)
|
|
316
|
+
if isinstance(result, Committed) and isinstance(result.output, BulkOutcome):
|
|
317
|
+
return result.output
|
|
318
|
+
if isinstance(result, Denied):
|
|
319
|
+
raise ActionDenied(result.code, result.original)
|
|
320
|
+
if isinstance(result, Conflict):
|
|
321
|
+
raise ActionConflict(result.code, result.retained, result.original)
|
|
322
|
+
if isinstance(result, TimedOut):
|
|
323
|
+
raise ActionTimedOut("TIMED_OUT", result.original)
|
|
324
|
+
if isinstance(result, Indeterminate):
|
|
325
|
+
raise ActionIndeterminate("INDETERMINATE", result.original)
|
|
326
|
+
raise ActionRefused("NATIVE_BULK_OUTCOME")
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def native_bulk_plan(
|
|
330
|
+
connection: Any,
|
|
331
|
+
program: NativeProgram,
|
|
332
|
+
action: ActionRef[Any, Any],
|
|
333
|
+
inputs: Mapping[str, object],
|
|
334
|
+
admission: NativeAdmission,
|
|
335
|
+
*,
|
|
336
|
+
default_timeout: timedelta,
|
|
337
|
+
reconnect: Any = None,
|
|
338
|
+
) -> AtomicBulkPlan:
|
|
339
|
+
return AtomicBulkPlan(
|
|
340
|
+
_Driver(
|
|
341
|
+
connection, program, action, inputs, admission, default_timeout, reconnect
|
|
342
|
+
)
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
class _AsyncDriver(AsyncPostgresActionExecutor):
|
|
347
|
+
def __init__(
|
|
348
|
+
self,
|
|
349
|
+
connection: Any,
|
|
350
|
+
program: NativeProgram,
|
|
351
|
+
action: ActionRef[Any, Any],
|
|
352
|
+
inputs: Mapping[str, object],
|
|
353
|
+
admission: AsyncNativeAdmission,
|
|
354
|
+
default_timeout: timedelta,
|
|
355
|
+
reconnect: Any,
|
|
356
|
+
):
|
|
357
|
+
super().__init__(
|
|
358
|
+
connection,
|
|
359
|
+
(program,),
|
|
360
|
+
admission,
|
|
361
|
+
default_timeout=default_timeout,
|
|
362
|
+
reconnect=reconnect,
|
|
363
|
+
)
|
|
364
|
+
self.action, self.program, self.inputs, self.preview_mode = (
|
|
365
|
+
action,
|
|
366
|
+
program,
|
|
367
|
+
dict(inputs),
|
|
368
|
+
False,
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
def _state(
|
|
372
|
+
self, action: ActionRef[Any, Any], inputs: Mapping[str, object]
|
|
373
|
+
) -> _BulkState:
|
|
374
|
+
if self.quarantined:
|
|
375
|
+
raise ActionRefused("NATIVE_CONNECTION_QUARANTINED")
|
|
376
|
+
return _BulkState(self.action, self.program, self.inputs, self.preview_mode)
|
|
377
|
+
|
|
378
|
+
async def preview(self, *, timeout: timedelta | None = None) -> BulkPreview:
|
|
379
|
+
self.preview_mode = True
|
|
380
|
+
try:
|
|
381
|
+
result = await self._run(self.action, {}, timeout, True)
|
|
382
|
+
finally:
|
|
383
|
+
self.preview_mode = False
|
|
384
|
+
if isinstance(result, BulkPreview):
|
|
385
|
+
return result
|
|
386
|
+
if isinstance(result, Denied):
|
|
387
|
+
raise ActionDenied(result.code, result.original)
|
|
388
|
+
if isinstance(result, TimedOut):
|
|
389
|
+
raise ActionTimedOut("TIMED_OUT", result.original)
|
|
390
|
+
if isinstance(result, Indeterminate):
|
|
391
|
+
raise ActionIndeterminate("INDETERMINATE", result.original)
|
|
392
|
+
if isinstance(result, Conflict):
|
|
393
|
+
raise ActionConflict(result.code, result.retained, result.original)
|
|
394
|
+
raise ActionRefused("NATIVE_BULK_PREVIEW_FAILED")
|
|
395
|
+
|
|
396
|
+
async def execute(self, *, timeout: timedelta | None = None) -> BulkOutcome:
|
|
397
|
+
result = await self._run(self.action, {}, timeout, False)
|
|
398
|
+
if isinstance(result, Committed) and isinstance(result.output, BulkOutcome):
|
|
399
|
+
return result.output
|
|
400
|
+
if isinstance(result, Denied):
|
|
401
|
+
raise ActionDenied(result.code, result.original)
|
|
402
|
+
if isinstance(result, Conflict):
|
|
403
|
+
raise ActionConflict(result.code, result.retained, result.original)
|
|
404
|
+
if isinstance(result, TimedOut):
|
|
405
|
+
raise ActionTimedOut("TIMED_OUT", result.original)
|
|
406
|
+
if isinstance(result, Indeterminate):
|
|
407
|
+
raise ActionIndeterminate("INDETERMINATE", result.original)
|
|
408
|
+
raise ActionRefused("NATIVE_BULK_OUTCOME")
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def native_bulk_plan_async(
|
|
412
|
+
connection: Any,
|
|
413
|
+
program: NativeProgram,
|
|
414
|
+
action: ActionRef[Any, Any],
|
|
415
|
+
inputs: Mapping[str, object],
|
|
416
|
+
admission: AsyncNativeAdmission,
|
|
417
|
+
*,
|
|
418
|
+
default_timeout: timedelta,
|
|
419
|
+
reconnect: Any = None,
|
|
420
|
+
) -> AsyncAtomicBulkPlan:
|
|
421
|
+
return AsyncAtomicBulkPlan(
|
|
422
|
+
_AsyncDriver(
|
|
423
|
+
connection, program, action, inputs, admission, default_timeout, reconnect
|
|
424
|
+
)
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
class PostgresBulkTarget(PostgresTarget):
|
|
429
|
+
def __init__(
|
|
430
|
+
self,
|
|
431
|
+
connection: Any,
|
|
432
|
+
configuration: PostgresConfiguration,
|
|
433
|
+
admission: NativeAdmission,
|
|
434
|
+
*,
|
|
435
|
+
reconnect: Any = None,
|
|
436
|
+
):
|
|
437
|
+
super().__init__(connection, configuration)
|
|
438
|
+
self.admission, self.reconnect = admission, reconnect
|
|
439
|
+
|
|
440
|
+
def plan(
|
|
441
|
+
self,
|
|
442
|
+
entry: CommandVariant,
|
|
443
|
+
params: Mapping[str, object],
|
|
444
|
+
*,
|
|
445
|
+
timeout: timedelta | None = None,
|
|
446
|
+
) -> AtomicBulkPlan:
|
|
447
|
+
from .targets import _milliseconds
|
|
448
|
+
|
|
449
|
+
_milliseconds(timeout, self.configuration.default_timeout)
|
|
450
|
+
if not entry.pin.matches(self.identity()):
|
|
451
|
+
raise ActionRefused("NOT_COMPILED_FOR_TARGET")
|
|
452
|
+
return native_bulk_plan(
|
|
453
|
+
self.connection,
|
|
454
|
+
entry.program,
|
|
455
|
+
entry.action,
|
|
456
|
+
params,
|
|
457
|
+
self.admission,
|
|
458
|
+
default_timeout=self.configuration.default_timeout
|
|
459
|
+
if timeout is None
|
|
460
|
+
else timeout,
|
|
461
|
+
reconnect=self.reconnect,
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
class AsyncPostgresBulkTarget(AsyncPostgresTarget):
|
|
466
|
+
def __init__(
|
|
467
|
+
self,
|
|
468
|
+
connection: Any,
|
|
469
|
+
configuration: PostgresConfiguration,
|
|
470
|
+
admission: AsyncNativeAdmission,
|
|
471
|
+
*,
|
|
472
|
+
reconnect: Any = None,
|
|
473
|
+
):
|
|
474
|
+
super().__init__(connection, configuration)
|
|
475
|
+
self.admission, self.reconnect = admission, reconnect
|
|
476
|
+
|
|
477
|
+
async def plan(
|
|
478
|
+
self,
|
|
479
|
+
entry: CommandVariant,
|
|
480
|
+
params: Mapping[str, object],
|
|
481
|
+
*,
|
|
482
|
+
timeout: timedelta | None = None,
|
|
483
|
+
) -> AsyncAtomicBulkPlan:
|
|
484
|
+
from .targets import _milliseconds
|
|
485
|
+
|
|
486
|
+
_milliseconds(timeout, self.configuration.default_timeout)
|
|
487
|
+
if not entry.pin.matches(self.identity()):
|
|
488
|
+
raise ActionRefused("NOT_COMPILED_FOR_TARGET")
|
|
489
|
+
return native_bulk_plan_async(
|
|
490
|
+
self.connection,
|
|
491
|
+
entry.program,
|
|
492
|
+
entry.action,
|
|
493
|
+
params,
|
|
494
|
+
self.admission,
|
|
495
|
+
default_timeout=self.configuration.default_timeout
|
|
496
|
+
if timeout is None
|
|
497
|
+
else timeout,
|
|
498
|
+
reconnect=self.reconnect,
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
class LakebaseBulkTarget(PostgresBulkTarget):
|
|
503
|
+
kind = "lakebase"
|
|
504
|
+
profile = "lakebase18"
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
class AsyncLakebaseBulkTarget(AsyncPostgresBulkTarget):
|
|
508
|
+
kind = "lakebase"
|
|
509
|
+
profile = "lakebase18"
|