Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
schema_registry.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstdint>
7#include <memory>
8#include <span>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
14namespace pulp::timeline {
15
19
28
39
42
50 public:
52 explicit BoundedJsonSink(std::size_t maximum, PersistenceErrorCode overflow_code =
54 : maximum_(maximum), overflow_code_(overflow_code) {}
55
60
62 bool append(std::string_view bytes);
64 bool failed() const noexcept {
65 return failed_;
66 }
67
68 std::size_t size() const noexcept {
69 return output_.size();
70 }
71
72 std::size_t maximum() const noexcept {
73 return maximum_;
74 }
75
76 std::size_t remaining() const noexcept {
77 return maximum_ - output_.size();
78 }
79
80 PersistenceError error(std::string path = {}) const;
81
82 private:
83 friend class SchemaRegistry;
84 const std::string& stored_output() const noexcept {
85 return output_;
86 }
87 const std::size_t maximum_ = 0;
89 std::string output_;
90 bool failed_ = false;
91 std::uint64_t attempted_ = 0;
92};
93
97 FieldSchema() = default;
99 FieldSchema(std::string field_name, SchemaValueKind value_kind, bool is_required = true,
100 std::string reference = {})
101 : name(std::move(field_name)), kind(value_kind), required(is_required),
102 referenced_type(std::move(reference)) {}
103
104 std::string name;
106 bool required = true;
107 std::string referenced_type;
108};
109
114 const JsonValue& data, const void* context) noexcept;
117 const std::shared_ptr<const void>& value, BoundedJsonSink& output,
118 const void* context) noexcept;
120using SchemaRetainedSizeFn = std::size_t (*)(const std::shared_ptr<const void>& value,
121 const void* context) noexcept;
124 std::string_view source_envelope, BoundedJsonSink& output, const void* context) noexcept;
125
131 std::shared_ptr<const void> context;
135};
136
142 std::uint32_t from_version = 0;
143 std::uint32_t to_version = 0;
144 std::shared_ptr<const void> context;
146};
147
154 std::string type_name;
156 std::uint32_t current_version = 0;
157 std::vector<FieldSchema> fields;
159 std::vector<MigrationStep> upgrades;
160 std::vector<MigrationStep> downgrades;
161};
162
172
179
182
190 public:
192 SchemaRegistry() = default;
193
197 const TypeSchema* find(SchemaDomain domain, std::string_view type_name) const noexcept;
199 std::span<const TypeSchema> types() const noexcept;
201 std::shared_ptr<const void> identity() const noexcept {
202 return impl_;
203 }
204
211 migrate(SchemaDomain domain, std::string_view type_name, std::uint32_t source_version,
212 std::uint32_t target_version, std::string_view source_envelope,
213 const DecodeLimits& limits = {}) const;
214
221 const std::shared_ptr<const void>& value, std::size_t maximum_bytes) const;
230 std::shared_ptr<const void> value,
231 std::size_t maximum_bytes) const;
232
233 private:
235 struct Impl;
236 explicit SchemaRegistry(std::shared_ptr<const Impl> impl) : impl_(std::move(impl)) {}
237 std::shared_ptr<const Impl> impl_;
238};
239
256
264
266
267} // namespace pulp::timeline
Result<T, E> — either holds a T or an E, never both.
Definition result.hpp:76
Append-only JSON output sink with an enforced byte ceiling.
Definition schema_registry.hpp:49
BoundedJsonSink(const BoundedJsonSink &)=delete
friend class SchemaRegistry
Definition schema_registry.hpp:83
bool failed() const noexcept
Reports whether an append has exceeded the ceiling.
Definition schema_registry.hpp:64
BoundedJsonSink(BoundedJsonSink &&)=delete
std::size_t size() const noexcept
Returns the number of bytes accepted so far.
Definition schema_registry.hpp:68
std::size_t maximum() const noexcept
Returns the configured byte ceiling.
Definition schema_registry.hpp:72
BoundedJsonSink & operator=(BoundedJsonSink &&)=delete
std::size_t remaining() const noexcept
Returns the number of bytes that can still be accepted.
Definition schema_registry.hpp:76
PersistenceError error(std::string path={}) const
Describes the recorded overflow and associates it with path.
BoundedJsonSink(std::size_t maximum, PersistenceErrorCode overflow_code=PersistenceErrorCode::OutputLimitExceeded)
Creates an empty sink with a maximum output size and overflow code.
Definition schema_registry.hpp:52
BoundedJsonSink & operator=(const BoundedJsonSink &)=delete
bool append(std::string_view bytes)
Appends all bytes or none of them when the ceiling would be exceeded.
Mutable, single-owner accumulator for constructing a SchemaRegistry.
Definition schema_registry.hpp:243
runtime::Result< SchemaRegistration, SchemaError > register_type(TypeSchema schema)
Validates and takes ownership of one schema registration.
runtime::Result< SchemaRegistry, SchemaError > build() &&
Sorts the accumulated registrations and consumes them into a registry.
friend class SchemaRegistryBuilder
Definition schema_registry.hpp:234
const TypeSchema * find(SchemaDomain domain, std::string_view type_name) const noexcept
Finds a type by exact domain and name.
runtime::Result< RegisteredContent, PersistenceError > create_registered_no_owned_ids(const SchemaIdentity &identity, std::shared_ptr< const void > value, std::size_t maximum_bytes) const
Creates registered Content after bounded encode and canonical validation.
runtime::Result< std::string, PersistenceError > migrate(SchemaDomain domain, std::string_view type_name, std::uint32_t source_version, std::uint32_t target_version, std::string_view source_envelope, const DecodeLimits &limits={}) const
Migrates an exact envelope along registered adjacent version edges.
std::shared_ptr< const void > identity() const noexcept
Opaque identity shared by copies of this immutable registry.
Definition schema_registry.hpp:201
SchemaRegistry()=default
Constructs an empty registry.
runtime::Result< std::string, PersistenceError > encode_registered(SchemaDomain domain, const SchemaIdentity &identity, const std::shared_ptr< const void > &value, std::size_t maximum_bytes) const
Encodes a current-version registered value under a byte ceiling.
std::span< const TypeSchema > types() const noexcept
Returns all types sorted by domain and type name.
std::variant< InsertClip, RemoveClip, InsertAutomationLane, RemoveAutomationLane, MoveClip, SetNoteVelocity, ReplaceNoteContent, SetClipPlaybackProperties, SetTempoMap, SetMeterMap, CreateAsset, RemoveAsset, InsertTakeLane, RemoveTakeLane, SetRecordArm, InsertTake, RemoveTake, SetActiveTakeLane, SetTakeComp, SetTrackFreeze, InsertMarker, RemoveMarker, InsertRegion, RemoveRegion, SetChordScaleLane, SetGroove, InsertScene, RemoveScene, InsertSlot, RemoveSlot, InsertSequence, CloneSequence, RemoveSequence, SetClipSequenceRef, SetTrackMixer, InsertTrack, RemoveTrack, SetTrackName, MoveTrack, SetNoteEvents, InsertNotes, RemoveNotes, InsertDevice, RemoveDevice, MoveDevice, RetargetDevice, SetDeviceState, SetDynamicsLane, InsertMidiExpressionLane, RemoveMidiExpressionLane, SetMidiExpressionLanePoints, SetProjectTuning, SetTrackTuning, SetRegion, InsertModulator, RemoveModulator, SetModulator, InsertMacro, RemoveMacro, SetMacro, SetMacroValue, InsertModulationRoute, RemoveModulationRoute, SetModulationRoute > Command
Exhaustive set of durable Timeline document mutations.
Definition command.hpp:800
runtime::Result< SchemaWriteSuccess, PersistenceError >(*)( const std::shared_ptr< const void > &value, BoundedJsonSink &output, const void *context) noexcept SchemaEncodeFn
Encodes a type-erased value into a quota-enforcing JSON sink.
Definition schema_registry.hpp:116
PersistenceErrorCode
Stable category for a JSON, schema, quota, migration, or model rejection.
Definition schema_json.hpp:23
SchemaErrorCode
Reason a TypeSchema cannot be registered.
Definition schema_registry.hpp:164
SchemaDomain
Namespace in which a schema type name is unique.
Definition schema_registry.hpp:21
runtime::Result< SchemaRegistry, SchemaError > make_builtin_timeline_registry()
Constructs a registry containing exactly the built-in Timeline schemas.
runtime::Result< std::shared_ptr< const void >, PersistenceError >(*)( const JsonValue &data, const void *context) noexcept SchemaDecodeFn
Decodes schema data into an immutable type-erased value.
Definition schema_registry.hpp:113
runtime::Result< SchemaWriteSuccess, PersistenceError >(*)( std::string_view source_envelope, BoundedJsonSink &output, const void *context) noexcept SchemaMigrationFn
Rewrites one exact schema envelope into a quota-enforcing sink.
Definition schema_registry.hpp:123
SchemaValueKind
JSON representation declared for a registered field.
Definition schema_registry.hpp:30
std::size_t(*)(const std::shared_ptr< const void > &value, const void *context) noexcept SchemaRetainedSizeFn
Estimates bytes retained by a decoded type-erased value.
Definition schema_registry.hpp:120
runtime::Result< SchemaRegistration, SchemaError > register_builtin_timeline_schemas(SchemaRegistryBuilder &builder)
Registers the built-in document, content, command, and diagnostic schemas.
@ OutputLimitExceeded
Generated JSON exceeded its output ceiling.
Definition schema_json.hpp:37
@ InvalidIdentity
Definition schema_registry.hpp:165
@ DuplicateType
Definition schema_registry.hpp:166
@ DuplicateField
Definition schema_registry.hpp:167
@ DuplicateMigration
Definition schema_registry.hpp:170
@ InvalidCodec
Definition schema_registry.hpp:168
@ InvalidMigration
Definition schema_registry.hpp:169
@ Document
Definition schema_registry.hpp:22
@ Diagnostic
Definition schema_registry.hpp:26
@ Content
Definition schema_registry.hpp:23
@ String
Definition schema_registry.hpp:35
@ Boolean
Definition schema_registry.hpp:31
@ Array
Definition schema_registry.hpp:37
@ Object
Definition schema_registry.hpp:36
@ I64String
Definition schema_registry.hpp:33
@ U64String
Definition schema_registry.hpp:34
@ U32
Definition schema_registry.hpp:32
Definition program_compiler.hpp:18
STL namespace.
Result<T, E> — success / failure union for fallible operations.
Alternate fidelity, analysis, or cache form of the same logical asset.
Definition assets.hpp:74
Parser, migration, opaque-payload, and Timeline structure quotas.
Definition schema_json.hpp:78
std::string referenced_type
Definition schema_registry.hpp:107
FieldSchema()=default
Constructs an empty, invalid field descriptor for aggregate assembly.
std::string name
Definition schema_registry.hpp:104
FieldSchema(std::string field_name, SchemaValueKind value_kind, bool is_required=true, std::string reference={})
Constructs field metadata and takes ownership of its names.
Definition schema_registry.hpp:99
bool required
Definition schema_registry.hpp:106
SchemaValueKind kind
Definition schema_registry.hpp:105
Owning JSON DOM node with source byte bounds.
Definition schema_json.hpp:160
One adjacent version transition for a schema type.
Definition schema_registry.hpp:141
std::uint32_t to_version
Definition schema_registry.hpp:143
std::shared_ptr< const void > context
Definition schema_registry.hpp:144
std::uint32_t from_version
Definition schema_registry.hpp:142
SchemaMigrationFn migrate
Definition schema_registry.hpp:145
Structured persistence failure.
Definition schema_json.hpp:46
Immutable callback bundle and shared context for a registered type.
Definition schema_registry.hpp:130
SchemaEncodeFn encode
Definition schema_registry.hpp:133
std::shared_ptr< const void > context
Definition schema_registry.hpp:131
SchemaDecodeFn decode
Definition schema_registry.hpp:132
SchemaRetainedSizeFn retained_size
Definition schema_registry.hpp:134
Schema-registration failure with the affected type and version.
Definition schema_registry.hpp:174
SchemaErrorCode code
Definition schema_registry.hpp:175
std::uint32_t version
Definition schema_registry.hpp:177
std::string type_name
Definition schema_registry.hpp:176
Versioned extension-schema identity.
Definition clip.hpp:102
Empty success value returned after registering a schema.
Definition schema_registry.hpp:181
Empty success value returned by bounded schema writers.
Definition schema_registry.hpp:41
Complete registration contract for one domain-qualified schema type.
Definition schema_registry.hpp:153
std::vector< MigrationStep > downgrades
Definition schema_registry.hpp:160
SchemaCodec codec
Definition schema_registry.hpp:158
std::vector< FieldSchema > fields
Definition schema_registry.hpp:157
SchemaDomain domain
Definition schema_registry.hpp:155
std::vector< MigrationStep > upgrades
Definition schema_registry.hpp:159
std::string type_name
Definition schema_registry.hpp:154
std::uint32_t current_version
Definition schema_registry.hpp:156