-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement typed publish-only database environments #5887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cloutiertyler
wants to merge
18
commits into
master
Choose a base branch
from
tyler/environment-variables
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+9,289
−436
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9222259
Add database environment storage, SQL, and module bindings
cloutiertyler 4613001
Implement typed publish-only database environments
cloutiertyler 05a8053
Clarify module library terminology in environment docs
cloutiertyler 648ae25
Address environment API and CLI review feedback
cloutiertyler 7d26b8d
Fix ENV CI coordination and restore C++ logging names
cloutiertyler 60c17d0
Fix environment CI isolation and update handler diagnostics
cloutiertyler 0293b62
Document direct HTTP environment publishing and retain query diagnostics
cloutiertyler 03cb28c
Add typed Rust environment enums and show values in env list
cloutiertyler 2bd321b
Fix ENV test CLI inspection and isolated root discovery
cloutiertyler 93f89e9
Add Rust module-test environment example and runtime coverage
cloutiertyler 60992ad
Apply ENV system table and test naming review cleanup
cloutiertyler b491ae4
Simplify ENV schema declaration state and example naming
cloutiertyler b0c498b
Clarify standalone publication locking and ENV recovery invariants
cloutiertyler 0b1abdb
Regenerate canonical C# environment metadata bindings
cloutiertyler 87169bd
Clarify environment usage and lower rejected SQL logging to debug
cloutiertyler 291e02d
Avoid inherited member collisions in C# environment accessors
cloutiertyler 81a4190
Separate generic view handling from ENV changes
cloutiertyler a7c7799
Add to C++ HandlerContext
JasonAtClockwork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| #ifndef SPACETIMEDB_ENVIRONMENT_H | ||
| #define SPACETIMEDB_ENVIRONMENT_H | ||
| #include <spacetimedb/abi/FFI.h> | ||
| #include <array> | ||
| #include <optional> | ||
| #include <spacetimedb/logger.h> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <type_traits> | ||
| #include <unordered_set> | ||
| #include <spacetimedb/internal/autogen/EnvironmentDeclaration.g.h> | ||
|
|
||
| namespace SpacetimeDB { | ||
| /// Read-only database environment. Reads use the current transaction, or a | ||
| /// short snapshot in a procedure outside a transaction. Values are not cached. | ||
| class EnvironmentBase { | ||
| public: | ||
| std::optional<std::string> get(std::string_view key) const { | ||
| if (key.empty() || key.size() > 256) LOG_PANIC("invalid environment variable name"); | ||
| BytesSource source{0}; | ||
| if (FFI::env_get(reinterpret_cast<const uint8_t*>(key.data()), static_cast<uint32_t>(key.size()), &source) != Status(0)) | ||
| LOG_PANIC("environment read failed"); | ||
| if (source == BytesSource{0}) return std::nullopt; | ||
| std::array<uint8_t, 1024> buffer; | ||
| std::string value; | ||
| for (;;) { | ||
| size_t len = buffer.size(); | ||
| const auto status = FFI::bytes_source_read(source, buffer.data(), &len); | ||
| if ((status != 0 && status != -1) || len > buffer.size()) LOG_PANIC("environment source read failed"); | ||
| value.append(reinterpret_cast<const char*>(buffer.data()), len); | ||
| if (status == -1) return value; | ||
| if (len == 0) LOG_PANIC("environment source made no progress"); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| namespace Internal { | ||
| inline std::vector<EnvironmentDeclaration>& environment_declarations() { | ||
| static std::vector<EnvironmentDeclaration> declarations; | ||
| return declarations; | ||
| } | ||
|
|
||
| template<typename T> | ||
| inline constexpr bool environment_string = std::is_same_v<T, std::string> || std::is_same_v<T, std::optional<std::string>>; | ||
|
|
||
| template<typename T> | ||
| T read_environment(std::string_view key) { | ||
| static_assert(environment_string<T>, "Environment declarations require string or optional<string>"); | ||
| auto value = EnvironmentBase{}.get(key); | ||
| if constexpr (std::is_same_v<T, std::string>) { | ||
| if (!value) LOG_PANIC("required environment value is absent"); | ||
| return std::move(*value); | ||
| } else { return value; } | ||
| } | ||
|
|
||
| template<typename T> | ||
| EnvironmentDeclaration declare_environment(std::string name) { | ||
| static_assert(environment_string<T>, "Environment declarations require string or optional<string>"); | ||
| EnvironmentConstraint constraint; | ||
| constraint.set<0>(std::monostate{}); | ||
| return {std::move(name), std::move(constraint), std::is_same_v<T, std::optional<std::string>>}; | ||
| } | ||
|
|
||
| // Preserve the complete source literal, including embedded NUL bytes. Implicit | ||
| // conversion through std::string(const char*) would truncate those constraints. | ||
| struct EnvironmentLiteral { | ||
| std::string value; | ||
| template<size_t N> | ||
| EnvironmentLiteral(const char (&text)[N]) : value(text, N - 1) {} | ||
| EnvironmentLiteral(std::string text) : value(std::move(text)) {} | ||
| }; | ||
|
|
||
| template<typename T> | ||
| EnvironmentDeclaration declare_environment(std::string name, std::initializer_list<EnvironmentLiteral> allowed) { | ||
| auto declaration = declare_environment<T>(std::move(name)); | ||
| if (allowed.size() == 0) LOG_PANIC("environment literal union cannot be empty"); | ||
| std::unordered_set<std::string> unique; | ||
| std::vector<std::string> values; | ||
| values.reserve(allowed.size()); | ||
| for (const auto& literal : allowed) { | ||
| const auto& value = literal.value; | ||
| if (value.size() > 8192 || !unique.insert(value).second) LOG_PANIC("invalid environment literal union"); | ||
| values.push_back(value); | ||
| } | ||
| if (values.size() == 1) declaration.constraint.template set<1>(std::move(values.front())); | ||
| else declaration.constraint.template set<2>(std::move(values)); | ||
| return declaration; | ||
| } | ||
|
|
||
| inline void validate_environment_declarations() { | ||
| const auto& declarations = environment_declarations(); | ||
| if (declarations.size() > 256) LOG_PANIC("too many environment declarations"); | ||
| std::unordered_set<std::string> keys; | ||
| const auto initial = [](char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_'; }; | ||
| for (const auto& declaration : declarations) { | ||
| const auto& key = declaration.name; | ||
| if (key.empty() || key.size() > 256 || !initial(key[0]) || !keys.insert(key).second) LOG_PANIC("invalid environment declaration name"); | ||
| for (char c : key) if (!initial(c) && !(c >= '0' && c <= '9')) LOG_PANIC("invalid environment declaration name"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #ifndef SPACETIMEDB_ENV_DECLARATION | ||
| class Environment : public EnvironmentBase {}; | ||
| #endif | ||
| } | ||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the tags are being reordered here, was this a bug with the old code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. The old C++ reader had these reversed: canonical BSATN uses tag 0 for Some and tag 1 for None. This fixes the reader without changing the wire format. The regression covers missing, present-empty, and embedded-NUL values, and checks that the following field is still decoded correctly.