Repository navigation
Expand file tree
/
Copy pathInventoryWorker.hpp
More file actions
100 lines (92 loc) · 6.03 KB
/
Copy pathInventoryWorker.hpp
File metadata and controls
100 lines (92 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @brief Local application component: stock changes with explicit host audit refusal policy. */
#ifndef MDDLOG_EXAMPLES_INVENTORYWORKER_HPP
#define MDDLOG_EXAMPLES_INVENTORYWORKER_HPP
// Import std, mddlog.core.governedbinding and mddlog.core.auditbinding before this header.
namespace inventory {
using namespace mddlog::core;
/** @brief Mutation and audit admission are separate facts; an absent outcome means no action ran. */
struct [[nodiscard]] AdjustmentResult {
AuditWriteResult request;
std::optional<AuditWriteResult> outcome;
std::optional<WriteResult> diagnostic;
bool changed = false;
};
/**
* @brief Adjust a local stock count; a refused request blocks mutation, a refused outcome cannot undo it.
* @note The host prepares both bindings and keeps their rings alive through all worker copies and joins.
* One thread owns this mutable component. Consumers, retries and storage remain outside apply().
*/
template <std::size_t DiagnosticCapacity, std::size_t AuditCapacity>
class InventoryWorker {
public:
/** @brief Copy prepared bindings; their destinations remain borrowed from the host. */
InventoryWorker(GovernedBinding<DiagnosticCapacity> diagnosticBinding, AuditBinding<AuditCapacity> auditBinding) noexcept
: logger(diagnosticBinding), audit(auditBinding) {}
/** @brief Host supplies both observation times; invalid stock changes explicitly emit Failed. */
[[nodiscard]] AdjustmentResult apply(int delta, RawTime requestedAt, RawTime observedAt) noexcept {
const auto request = audit.record(AuditPhase::Requested, requestedAt);
if (!request.wasAdmitted())
return {.request = request};
const auto next = static_cast<std::int64_t>(stock) + delta;
const bool valid = next >= 0 && next <= std::numeric_limits<int>::max();
if (valid)
stock = static_cast<int>(next);
const auto outcome = audit.record(valid ? AuditPhase::Executed : AuditPhase::Failed, observedAt, {.sourceSequence = request.sequence()});
const auto diagnostic = logger.info(observedAt, valid ? "Stock adjusted" : "Stock change rejected");
return {.request = request, .outcome = outcome, .diagnostic = diagnostic, .changed = valid};
}
/** @brief Inspect the local stock count after a completed or refused adjustment. */
[[nodiscard]] int quantity() const noexcept {
return stock;
}
private:
GovernedBinding<DiagnosticCapacity> logger;
AuditBinding<AuditCapacity> audit;
int stock = 0;
};
/** @brief Equivalent low-level business function, for the same concrete stock-change policy. */
template <std::size_t Capacity>
[[nodiscard]] AdjustmentResult adjustBefore(RingLog<Capacity>& ring,
AuditRing<Capacity>& auditRing,
const DiagnosticContext& context,
const AuditDescription& description,
const AuditContext& auditContext,
int& stock,
int delta,
RawTime requestedAt,
RawTime observedAt) noexcept {
const auto request = auditRing.tryRecord({.category = description.category(),
.phase = AuditPhase::Requested,
.time = requestedAt,
.action = description.action(),
.actor = auditContext.actor(),
.target = auditContext.target(),
.requirementRef = description.requirementRef(),
.riskRef = description.riskRef(),
.correlationId = auditContext.correlationId()});
if (!request.wasAdmitted())
return {.request = request};
const auto next = static_cast<std::int64_t>(stock) + delta;
const bool valid = next >= 0 && next <= std::numeric_limits<int>::max();
if (valid)
stock = static_cast<int>(next);
const auto outcome = auditRing.tryRecord({.category = description.category(),
.phase = valid ? AuditPhase::Executed : AuditPhase::Failed,
.time = observedAt,
.action = description.action(),
.actor = auditContext.actor(),
.target = auditContext.target(),
.requirementRef = description.requirementRef(),
.riskRef = description.riskRef(),
.correlationId = auditContext.correlationId(),
.sourceSequence = request.sequence()});
const auto diagnostic = ring.tryWrite({.time = observedAt,
.location = std::source_location::current(),
.message = valid ? "Stock adjusted" : "Stock change rejected",
.component = context.component(),
.operationId = context.operationId(),
.correlationId = context.correlationId()});
return {.request = request, .outcome = outcome, .diagnostic = diagnostic, .changed = valid};
}
} // namespace inventory
#endif // MDDLOG_EXAMPLES_INVENTORYWORKER_HPP