|
| 1 | +#ifndef SPACETIMEDB_HANDLER_CONTEXT_H |
| 2 | +#define SPACETIMEDB_HANDLER_CONTEXT_H |
| 3 | + |
| 4 | +#ifndef SPACETIMEDB_UNSTABLE_FEATURES |
| 5 | +#error "spacetimedb/handler_context.h requires SPACETIMEDB_UNSTABLE_FEATURES to be enabled" |
| 6 | +#endif |
| 7 | + |
| 8 | +#include <spacetimedb/abi/FFI.h> |
| 9 | +#include <spacetimedb/bsatn/timestamp.h> |
| 10 | +#include <spacetimedb/bsatn/uuid.h> |
| 11 | +#include <spacetimedb/http.h> |
| 12 | +#include <spacetimedb/internal/tx_execution.h> |
| 13 | +#include <spacetimedb/random.h> |
| 14 | +#include <spacetimedb/tx_context.h> |
| 15 | +#include <array> |
| 16 | +#include <cstdint> |
| 17 | +#include <functional> |
| 18 | +#include <memory> |
| 19 | +#include <optional> |
| 20 | +#include <type_traits> |
| 21 | + |
| 22 | +namespace SpacetimeDB { |
| 23 | + |
| 24 | +struct HandlerContext { |
| 25 | + Timestamp timestamp; |
| 26 | + HttpClient http; |
| 27 | + |
| 28 | +private: |
| 29 | + mutable std::shared_ptr<StdbRng> rng_instance; |
| 30 | + mutable uint32_t counter_uuid_ = 0; |
| 31 | + |
| 32 | +public: |
| 33 | + HandlerContext() = default; |
| 34 | + explicit HandlerContext(Timestamp t) : timestamp(t) {} |
| 35 | + |
| 36 | + Identity identity() const { |
| 37 | + std::array<uint8_t, 32> id_bytes; |
| 38 | + ::identity(id_bytes.data()); |
| 39 | + return Identity(id_bytes); |
| 40 | + } |
| 41 | + |
| 42 | + StdbRng& rng() const { |
| 43 | + if (!rng_instance) { |
| 44 | + rng_instance = std::make_shared<StdbRng>(timestamp); |
| 45 | + } |
| 46 | + return *rng_instance; |
| 47 | + } |
| 48 | + |
| 49 | + Uuid new_uuid_v4() const { |
| 50 | + std::array<uint8_t, 16> random_bytes; |
| 51 | + rng().fill_bytes(random_bytes.data(), random_bytes.size()); |
| 52 | + return Uuid::from_random_bytes_v4(random_bytes); |
| 53 | + } |
| 54 | + |
| 55 | + Uuid new_uuid_v7() const { |
| 56 | + std::array<uint8_t, 4> random_bytes; |
| 57 | + rng().fill_bytes(random_bytes.data(), random_bytes.size()); |
| 58 | + return Uuid::from_counter_v7(counter_uuid_, timestamp, random_bytes); |
| 59 | + } |
| 60 | + |
| 61 | +#ifdef SPACETIMEDB_UNSTABLE_FEATURES |
| 62 | + template<typename Func> |
| 63 | + auto with_tx(Func&& body) -> decltype(body(std::declval<TxContext&>())) { |
| 64 | + auto make_reducer_ctx = [](Timestamp tx_timestamp) { |
| 65 | + return ReducerContext( |
| 66 | + Identity{}, |
| 67 | + std::nullopt, |
| 68 | + tx_timestamp, |
| 69 | + AuthCtx::internal() |
| 70 | + ); |
| 71 | + }; |
| 72 | + return Internal::with_tx(make_reducer_ctx, body); |
| 73 | + } |
| 74 | + |
| 75 | + template<typename Func> |
| 76 | + auto try_with_tx(Func&& body) -> decltype(body(std::declval<TxContext&>())) { |
| 77 | + auto make_reducer_ctx = [](Timestamp tx_timestamp) { |
| 78 | + return ReducerContext( |
| 79 | + Identity{}, |
| 80 | + std::nullopt, |
| 81 | + tx_timestamp, |
| 82 | + AuthCtx::internal() |
| 83 | + ); |
| 84 | + }; |
| 85 | + return Internal::try_with_tx(make_reducer_ctx, body); |
| 86 | + } |
| 87 | +#endif |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace SpacetimeDB |
| 91 | + |
| 92 | +#endif // SPACETIMEDB_HANDLER_CONTEXT_H |
0 commit comments