载入中...
搜索中...
未找到
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
67 bool success = false;
68 int changedCount = 0;
69 uint64_t revisionBefore = 0;
70 uint64_t revisionAfter = 0;
71 std::vector<PatchError> errors;
72};
73
76 uint64_t sequence = 0;
77 uint64_t revision = 0;
78 std::string subject;
79 std::string key;
80 std::string oldJson;
81 std::string newJson;
82 bool removed = false;
83};
84
87public:
89 bool set(const std::string& subject, const std::string& key, const std::string& jsonValue);
91 bool setExpected(const std::string& subject, const std::string& key, const std::string& jsonValue,
92 const std::string& expectedJson);
94 bool remove(const std::string& subject, const std::string& key);
96 bool removeExpected(const std::string& subject, const std::string& key, const std::string& expectedJson);
98 void clear();
100 int size() const;
102 const PatchResult& result() const;
103
104private:
105 friend class Store;
106 struct Operation {
107 bool remove = false;
108 std::string subject;
109 std::string key;
110 std::string value;
111 std::optional<std::string> expected;
112 std::string inputError;
113 };
114 std::vector<Operation> operations_;
115 PatchResult result_;
116};
117
120public:
122 explicit Store(eve::PersistentId instanceId = {});
129 [[nodiscard]] eve::Result<PatchBatchHandleRef> newBatch();
131 [[nodiscard]] eve::script::Borrowed<PatchBatch> resolveBatch(PatchBatchHandleRef reference) noexcept;
133 [[nodiscard]] eve::Result<void> releaseBatch(PatchBatchHandleRef reference);
135 [[nodiscard]] bool isBatchStale(PatchBatchHandleRef reference) const noexcept;
137 bool commit(PatchBatch* batch);
139 bool has(const std::string& subject, const std::string& key) const;
141 std::string get(const std::string& subject, const std::string& key) const;
143 uint64_t valueRevision(const std::string& subject, const std::string& key) const;
145 uint64_t revision() const;
147 int querySubjects();
149 int queryKeys(const std::string& subject);
151 std::string queryAt(int index) const;
153 int queryDirty();
155 std::string dirtySubjectAt(int index) const;
157 std::string dirtyKeyAt(int index) const;
159 void clearDirty();
161 int eventCount() const;
163 const ChangeEvent* eventAt(int index) const;
165 void clearEvents();
167 std::string snapshotJson() const;
174 [[nodiscard]] eve::Result<void> restoreJson(const std::string& json);
175
177 [[nodiscard]] eve::Result<eve::SnapshotEnvelope> snapshot(const eve::SnapshotHashProvider& hashProvider) const;
184 [[nodiscard]] eve::Result<void> restoreSnapshot(const eve::SnapshotEnvelope& snapshot,
185 const eve::SnapshotHashProvider& hashProvider);
187 [[nodiscard]] eve::Result<std::string> snapshotEnvelopeJson(const eve::SnapshotHashProvider& hashProvider) const;
189 [[nodiscard]] eve::Result<void> restoreSnapshotJson(std::string_view json,
190 const eve::SnapshotHashProvider& hashProvider);
191
192private:
194
195 struct Value {
196 std::string json;
197 uint64_t revision = 0;
198 };
199 using Values = std::map<std::string, std::map<std::string, Value>>;
200
201 Values values_;
202 uint64_t revision_ = 0;
203 uint64_t nextSequence_ = 1;
204 eve::PersistentId instanceId_;
206 std::set<std::pair<std::string, std::string>> dirty_;
207 std::vector<ChangeEvent> events_;
208 std::vector<std::string> query_;
209 std::vector<std::pair<std::string, std::string>> dirtyQuery_;
211 void copyTransactionStateFrom(const Store& source);
212 void swapTransactionState(Store& other) noexcept;
213 bool transactionStateEquals(const Store& other) const;
214};
215
231public:
237 StoreTransactionParticipant(Store& store, PatchBatch& batch) : store_(store), batch_(batch) {}
239 ~StoreTransactionParticipant() override = default;
240
242 std::string_view name() const noexcept override { return "statepatch.store"; }
243
245 [[nodiscard]] eve::Result<void> prepare(const transaction::TransactionContext& context) override;
247 [[nodiscard]] eve::Result<void> commit(const transaction::TransactionContext& context) override;
249 [[nodiscard]] eve::Result<void> rollback(const transaction::TransactionContext& context) override;
251 [[nodiscard]] eve::Result<void> compensate(const transaction::TransactionContext& context) override;
252
253private:
254 enum class Phase { Idle, Prepared, Committed, RolledBack, Compensated, Failed };
255
256 [[nodiscard]] eve::Result<void> lifecycleFailure(std::string_view operation) const;
257 [[nodiscard]] eve::Result<void> contextFailure(const transaction::TransactionContext& context) const;
258 bool contextMatches(const transaction::TransactionContext& context) const noexcept;
259
260 Store& store_;
261 PatchBatch& batch_;
262 std::unique_ptr<Store> before_;
263 std::unique_ptr<Store> prepared_;
264 std::unique_ptr<Store> expectedAfter_;
265 std::string transactionId_;
266 std::string correlationId_;
267 std::string causationId_;
268 Phase phase_ = Phase::Idle;
269};
270
272class StatePatch : public Module {
273public:
276 StatePatch() = default;
278 ~StatePatch() override;
279
287 [[nodiscard]] static eve::Result<StateStoreHandleRef> newStore();
293 [[nodiscard]] static bool isStale(StateStoreHandleRef reference) noexcept;
294
300 [[nodiscard]] static bool isBatchStale(StateBatchHandleRef reference) noexcept;
301
302private:
303 void attachWorldAdapter(Store& store);
304 void detachWorldAdapter(Store& store);
305
307 std::unordered_map<Store*, std::unique_ptr<StatePatchStateAdapter>> worldAdapters_;
308};
309
310} // namespace eve::statepatch
ActionParameterOperation operation
double value
int subject
Definition AnimSmr.cpp:163
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::uint32_t key
std::uint64_t revision
std::function< eve::Result< void >()> release
Definition Procgen.cpp:86
Move-only, checked operation results for the common layer.
Versioned, integrity-checked persistence envelopes.
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
float size
Definition TreeMesh.cpp:156
uint32_t index
const UnitySourceAsset & source
const VegetationPresetContext & context
EVENGINE_API_FOUNDATION public API.
Definition Module.h:46
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Id128 public API.
Definition Identity.h:112
Non-owning C++ observation with no implicit lifetime extension.
Slot/generation registry whose objects are exclusively unique-owned.
Ordered collection of set and remove operations committed atomically.
Definition StatePatch.h:86
Script module factory for generic state patch stores.
Definition StatePatch.h:272
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< StateStoreHandleRef > newStore()
Allocates a state store and returns its ownership reference.
Transaction participant that atomically applies one StatePatch batch.
Definition StatePatch.h:230
std::string_view name() const noexcept override
Stable participant name used in transaction diagnostics.
Definition StatePatch.h:242
StoreTransactionParticipant(Store &store, PatchBatch &batch)
Bind one patch batch to a Store transaction.
Definition StatePatch.h:237
~StoreTransactionParticipant() override=default
Store transaction participant.
Deterministic subject-and-key JSON value store with atomic patching.
Definition StatePatch.h:119
Synchronous participant contract for one coordinator transaction.
Immutable context shared by every participant of one transaction.
Definition Transaction.h:30
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:75
Handle domain for store-owned patch batches.
Definition StatePatch.h:30
One validation error produced while applying a patch batch.
Definition StatePatch.h:57
Summary of the most recent batch commit attempt.
Definition StatePatch.h:66
std::vector< PatchError > errors
Definition StatePatch.h:71
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