载入中...
搜索中...
未找到
StatePatch.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/Module.h"
6#include "common/Result.h"
7#include "common/Snapshot.h"
10
11#include <cstdint>
12#include <map>
13#include <memory>
14#include <optional>
15#include <set>
16#include <string>
17#include <string_view>
18#include <unordered_map>
19#include <vector>
20
21namespace eve::statepatch {
22
23class StatePatchStateAdapter;
24
33
44 std::uint64_t ownerEpoch = 0;
45
47 [[nodiscard]] constexpr std::uint64_t packed() const noexcept { return batch.packed(); }
49 [[nodiscard]] constexpr bool isValid() const noexcept {
50 return store.isValid() && batch.isValid() && ownerEpoch != 0;
51 }
52};
53
54class StoreTransactionParticipant;
55
57struct PatchError {
59 std::string subject;
60 std::string key;
61 std::string code;
62 std::string message;
63};
64
71 int changedCount = 0;
72 uint64_t revisionBefore = 0;
73 uint64_t revisionAfter = 0;
74};
75
88 bool success = false;
89 int changedCount = 0;
90 uint64_t revisionBefore = 0;
91 uint64_t revisionAfter = 0;
92 std::vector<PatchError> errors;
93};
94
97 uint64_t sequence = 0;
98 uint64_t revision = 0;
99 std::string subject;
100 std::string key;
101 std::string oldJson;
102 std::string newJson;
103 bool removed = false;
104};
105
108public:
110 bool set(const std::string& subject, const std::string& key, const std::string& jsonValue);
112 bool setExpected(const std::string& subject, const std::string& key, const std::string& jsonValue,
113 const std::string& expectedJson);
115 bool remove(const std::string& subject, const std::string& key);
117 bool removeExpected(const std::string& subject, const std::string& key, const std::string& expectedJson);
119 void clear();
121 int size() const;
123 const PatchResult& result() const;
124
125private:
126 friend class Store;
127 struct Operation {
128 bool remove = false;
129 std::string subject;
130 std::string key;
131 std::string value;
132 std::optional<std::string> expected;
133 std::string inputError;
134 };
135 std::vector<Operation> operations_;
136 PatchResult result_;
137};
138
141public:
143 explicit Store(eve::PersistentId instanceId = {});
150 [[nodiscard]] eve::Result<PatchBatchHandleRef> newBatch();
152 [[nodiscard]] eve::script::Borrowed<PatchBatch> resolveBatch(PatchBatchHandleRef reference) noexcept;
154 [[nodiscard]] eve::Result<void> releaseBatch(PatchBatchHandleRef reference);
156 [[nodiscard]] bool isBatchStale(PatchBatchHandleRef reference) const noexcept;
166 [[nodiscard]] eve::Result<PatchCommitInfo> commit(PatchBatch* batch);
168 bool has(const std::string& subject, const std::string& key) const;
170 std::string get(const std::string& subject, const std::string& key) const;
172 uint64_t valueRevision(const std::string& subject, const std::string& key) const;
174 uint64_t revision() const;
176 int querySubjects();
178 int queryKeys(const std::string& subject);
180 std::string queryAt(int index) const;
182 int queryDirty();
184 std::string dirtySubjectAt(int index) const;
186 std::string dirtyKeyAt(int index) const;
188 void clearDirty();
190 int eventCount() const;
192 const ChangeEvent* eventAt(int index) const;
194 void clearEvents();
196 std::string snapshotJson() const;
203 [[nodiscard]] eve::Result<void> restoreJson(const std::string& json);
204
206 [[nodiscard]] eve::Result<eve::SnapshotEnvelope> snapshot(const eve::SnapshotHashProvider& hashProvider) const;
213 [[nodiscard]] eve::Result<void> restoreSnapshot(const eve::SnapshotEnvelope& snapshot,
214 const eve::SnapshotHashProvider& hashProvider);
216 [[nodiscard]] eve::Result<std::string> snapshotEnvelopeJson(const eve::SnapshotHashProvider& hashProvider) const;
218 [[nodiscard]] eve::Result<void> restoreSnapshotJson(std::string_view json,
219 const eve::SnapshotHashProvider& hashProvider);
220
221private:
223
224 struct Value {
225 std::string json;
226 uint64_t revision = 0;
227 };
228 using Values = std::map<std::string, std::map<std::string, Value>>;
229
230 Values values_;
231 uint64_t revision_ = 0;
232 uint64_t nextSequence_ = 1;
233 eve::PersistentId instanceId_;
235 std::set<std::pair<std::string, std::string>> dirty_;
236 std::vector<ChangeEvent> events_;
237 std::vector<std::string> query_;
238 std::vector<std::pair<std::string, std::string>> dirtyQuery_;
240 void copyTransactionStateFrom(const Store& source);
241 void swapTransactionState(Store& other) noexcept;
242 bool transactionStateEquals(const Store& other) const;
243};
244
260public:
266 StoreTransactionParticipant(Store& store, PatchBatch& batch) : store_(store), batch_(batch) {}
268 ~StoreTransactionParticipant() override = default;
269
271 std::string_view name() const noexcept override { return "statepatch.store"; }
272
274 [[nodiscard]] eve::Result<void> prepare(const transaction::TransactionContext& context) override;
276 [[nodiscard]] eve::Result<void> commit(const transaction::TransactionContext& context) override;
278 [[nodiscard]] eve::Result<void> rollback(const transaction::TransactionContext& context) override;
280 [[nodiscard]] eve::Result<void> compensate(const transaction::TransactionContext& context) override;
281
282private:
283 enum class Phase { Idle, Prepared, Committed, RolledBack, Compensated, Failed };
284
285 [[nodiscard]] eve::Result<void> lifecycleFailure(std::string_view operation) const;
286 [[nodiscard]] eve::Result<void> contextFailure(const transaction::TransactionContext& context) const;
287 bool contextMatches(const transaction::TransactionContext& context) const noexcept;
288
289 Store& store_;
290 PatchBatch& batch_;
291 std::unique_ptr<Store> before_;
292 std::unique_ptr<Store> prepared_;
293 std::unique_ptr<Store> expectedAfter_;
294 std::string transactionId_;
295 std::string correlationId_;
296 std::string causationId_;
297 Phase phase_ = Phase::Idle;
298};
299
301class StatePatch : public Module {
302public:
305 StatePatch() = default;
307 ~StatePatch() override;
308
316 [[nodiscard]] static eve::Result<StateStoreHandleRef> newStore();
322 [[nodiscard]] static bool isStale(StateStoreHandleRef reference) noexcept;
323
329 [[nodiscard]] static bool isBatchStale(StateBatchHandleRef reference) noexcept;
330
331private:
332 void attachWorldAdapter(Store& store);
333 void detachWorldAdapter(Store& store);
334
336 std::unordered_map<Store*, std::unique_ptr<StatePatchStateAdapter>> worldAdapters_;
337};
338
339} // namespace eve::statepatch
ActionParameterOperation operation
int subject
Definition AnimSmr.cpp:163
std::uint64_t revision
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
SQObjectPtr key
Definition ECS.cpp:155
int instanceId
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
Poco::JSON::Object::Ptr result
Snapshot snapshot
Object::Ptr value
Move-only, checked operation results for the common layer.
std::string_view source
Versioned, integrity-checked persistence envelopes.
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
float size
Definition TreeMesh.cpp:158
uint32_t index
const VegetationPresetContext & context
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Id128 public API.
Definition Identity.h:111
Non-owning observation of a registry-owned object.
Slot/generation registry whose objects are exclusively unique-owned.
Ordered collection of set and remove operations committed atomically.
Definition StatePatch.h:107
Script module factory for generic state patch stores.
Definition StatePatch.h:301
static bool isStale(StateStoreHandleRef reference) noexcept
Reports whether a store reference is stale for the current module.
static eve::Result< void > releaseBatch(StateBatchHandleRef reference)
Releases a store-qualified patch batch.
static eve::script::Borrowed< PatchBatch > resolveBatch(StateBatchHandleRef reference) noexcept
Resolves a store-qualified batch as a non-owning observation.
static eve::script::Borrowed< Store > resolve(StateStoreHandleRef reference) noexcept
Resolves a live store as a non-owning observation.
static bool isBatchStale(StateBatchHandleRef reference) noexcept
Reports whether a store-qualified batch reference is stale.
StatePatch()=default
Constructs a StatePatch.
~StatePatch() override
Withdraw world-state capability listeners before owned stores are destroyed.
static eve::Result< void > release(StateStoreHandleRef reference)
Releases a module-owned state store.
static eve::Result< StateStoreHandleRef > newStore()
Allocates a state store and returns its ownership reference.
Transaction participant that atomically applies one StatePatch batch.
Definition StatePatch.h:259
std::string_view name() const noexcept override
Stable participant name used in transaction diagnostics.
Definition StatePatch.h:271
StoreTransactionParticipant(Store &store, PatchBatch &batch)
Bind one patch batch to a Store transaction.
Definition StatePatch.h:266
~StoreTransactionParticipant() override=default
Store transaction participant.
Deterministic subject-and-key JSON value store with atomic patching.
Definition StatePatch.h:140
Synchronous participant contract for one coordinator transaction.
Immutable context shared by every participant of one transaction.
Definition Transaction.h:31
std::function< Result< ContentId >(std::string_view canonicalInput)> SnapshotHashProvider
Injected content-digest implementation used by snapshots.
Definition Snapshot.h:36
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
constexpr bool isValid() const noexcept
Returns whether both the handle and its registry epoch are set.
constexpr std::uint64_t packed() const noexcept
Returns the packed slot/generation projection.
Immutable description of one committed value change.
Definition StatePatch.h:96
Handle domain for store-owned patch batches.
Definition StatePatch.h:30
Successful payload of Store::commit.
Definition StatePatch.h:70
One validation error produced while applying a patch batch.
Definition StatePatch.h:57
Compatibility summary of the most recent batch commit attempt. @compatibility Prefer Store::commit → ...
Definition StatePatch.h:87
std::vector< PatchError > errors
Definition StatePatch.h:92
A patch batch reference qualified by both its owning Store and batch slot.
Definition StatePatch.h:41
constexpr bool isValid() const noexcept
Returns whether both nested references are syntactically valid.
Definition StatePatch.h:49
constexpr std::uint64_t packed() const noexcept
Returns the batch slot/generation projection.
Definition StatePatch.h:47
Handle domain for module-owned state-patch stores.
Definition StatePatch.h:26