-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Add C bindings for DynamoDB Big Segments store #579
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /** @file dynamodb_big_segment_store.h | ||
| * @brief LaunchDarkly Server-side DynamoDB Big Segments Store C Binding. | ||
| */ | ||
| // NOLINTBEGIN modernize-use-using | ||
| #pragma once | ||
|
|
||
| #include <launchdarkly/server_side/bindings/c/integrations/dynamodb/dynamodb_client_options.h> | ||
|
Check failure on line 7 in libs/server-sdk-dynamodb-source/include/launchdarkly/server_side/bindings/c/integrations/dynamodb/dynamodb_big_segment_store.h
|
||
|
|
||
| #include <launchdarkly/bindings/c/export.h> | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| // only need to export C interface if | ||
| // used by C++ source code | ||
| #endif | ||
|
|
||
| /** | ||
| * @brief LDServerBigSegmentsDynamoDBStore is a Big Segments persistent store | ||
| * for the Server-Side SDK backed by Amazon DynamoDB. It is meant to be passed | ||
| * to the SDK via the Big Segments config builder. | ||
| * | ||
| * Call @ref LDServerBigSegmentsDynamoDBStore_New to obtain a new instance. | ||
| * This instance is passed into the SDK's Big Segments configuration. | ||
| * | ||
| * The DynamoDB table must already exist and follow the LaunchDarkly schema: | ||
| * a String partition key named `namespace` and a String sort key named | ||
| * `key`. The same table can be shared with @ref LDServerLazyLoadDynamoDBSource | ||
| * -- Big Segments rows occupy their own partition-key values and do not | ||
| * conflict with flag/segment rows. The LaunchDarkly Relay Proxy populates | ||
| * Big Segments data in the table; this store only reads from it. | ||
| * | ||
| * Example: | ||
| * @code | ||
| * // Optional: configure the AWS DynamoDB client. Pass NULL for defaults. | ||
| * LDServerDynamoDBClientOptionsBuilder options = | ||
| * LDServerDynamoDBClientOptionsBuilder_New(); | ||
| * LDServerDynamoDBClientOptionsBuilder_Region(options, "us-east-1"); | ||
| * | ||
| * // Stack allocate the result struct, which will hold the result pointer or | ||
| * // an error message. | ||
| * struct LDServerBigSegmentsDynamoDBResult result; | ||
| * | ||
| * if (!LDServerBigSegmentsDynamoDBStore_New("my-table", "testprefix", options, | ||
| * &result)) { | ||
| * // On failure, you may print the error message (result.error_message), | ||
| * // then exit or return. | ||
| * } | ||
| * | ||
| * // Create the Big Segments builder, taking ownership of the store pointer. | ||
| * LDServerBigSegmentsBuilder bs_builder = LDServerBigSegmentsBuilder_New( | ||
| * (LDServerBigSegmentStorePtr)result.store); | ||
| * | ||
| * // Attach the Big Segments builder to the SDK config. | ||
| * LDServerConfigBuilder cfg_builder = LDServerConfigBuilder_New("sdk-123"); | ||
| * LDServerConfigBuilder_BigSegments(cfg_builder, bs_builder); | ||
| * @endcode | ||
| * | ||
| * This implementation is backed by the AWS SDK for C++. | ||
| */ | ||
| typedef struct _LDServerBigSegmentsDynamoDBStore* | ||
| LDServerBigSegmentsDynamoDBStore; | ||
|
|
||
| /* Defines the size of the error message buffer in | ||
| * LDServerBigSegmentsDynamoDBResult. | ||
| */ | ||
| #ifndef LDSERVER_BIGSEGMENTS_DYNAMODBSTORE_ERROR_MESSAGE_SIZE | ||
| #define LDSERVER_BIGSEGMENTS_DYNAMODBSTORE_ERROR_MESSAGE_SIZE 256 | ||
| #endif | ||
|
|
||
| /** | ||
| * @brief Stores the result of calling @ref LDServerBigSegmentsDynamoDBStore_New. | ||
| * | ||
| * On successful creation, store will contain a pointer which may be passed | ||
| * into the LaunchDarkly SDK's Big Segments configuration. | ||
| * | ||
| * On failure, error_message contains a NULL-terminated string describing the | ||
| * error. | ||
| * | ||
| * The message may be truncated if it was originally longer than | ||
| * error_message's buffer size. | ||
| * | ||
| * The message originates from the underlying AWS SDK and may echo back | ||
| * portions of the client configuration, including endpoint and region. | ||
| * Callers that surface this message (logs, telemetry, user-facing errors) | ||
| * may want to sanitize it accordingly. | ||
| */ | ||
| struct LDServerBigSegmentsDynamoDBResult { | ||
| LDServerBigSegmentsDynamoDBStore store; | ||
| char error_message[LDSERVER_BIGSEGMENTS_DYNAMODBSTORE_ERROR_MESSAGE_SIZE]; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Creates a new DynamoDB Big Segment store suitable for usage in the | ||
| * SDK's Big Segments configuration. | ||
| * | ||
| * @param table_name Name of the DynamoDB table to read from. The table must | ||
| * already exist; this function does not create it. Must not be NULL. | ||
| * | ||
| * @param prefix Prefix to use when reading Big Segments data from DynamoDB. | ||
| * This allows multiple SDK environments to coexist in the same table. Must | ||
| * not be NULL. | ||
| * | ||
| * @param options Optional AWS DynamoDB client configuration. When non-NULL, | ||
| * the builder is consumed and freed by this function; do not call | ||
| * @ref LDServerDynamoDBClientOptionsBuilder_Free on it afterward. When NULL, | ||
| * the AWS SDK's default provider chain is used for region, endpoint, and | ||
| * credentials. | ||
| * | ||
| * @param out_result Pointer to struct where the store pointer or an error | ||
| * message should be stored. Must not be NULL. | ||
| * | ||
| * @return True if the store was created successfully; out_result->store | ||
| * will contain the pointer. The caller must either free the pointer with | ||
| * @ref LDServerBigSegmentsDynamoDBStore_Free, OR pass it into the SDK's Big | ||
| * Segments configuration which will take ownership (in which case do not | ||
| * call @ref LDServerBigSegmentsDynamoDBStore_Free.) | ||
| */ | ||
| LD_EXPORT(bool) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we will need stdbool. Which also means we are missing some test case. Or maybe we transiently get stdbool? But it makes me nervous.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not missing test cases, but all of our tests are written in C++, not C. The only coverage we have of this is the |
||
| LDServerBigSegmentsDynamoDBStore_New( | ||
| char const* table_name, | ||
| char const* prefix, | ||
| LDServerDynamoDBClientOptionsBuilder options, | ||
| struct LDServerBigSegmentsDynamoDBResult* out_result); | ||
|
|
||
| /** | ||
| * @brief Frees a DynamoDB Big Segment store pointer. Only necessary to call | ||
| * if not passing ownership to the SDK's Big Segments configuration. | ||
| */ | ||
| LD_EXPORT(void) | ||
| LDServerBigSegmentsDynamoDBStore_Free(LDServerBigSegmentsDynamoDBStore store); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| // NOLINTEND modernize-use-using | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #include <launchdarkly/server_side/bindings/c/integrations/dynamodb/dynamodb_big_segment_store.h> | ||
|
Check failure on line 1 in libs/server-sdk-dynamodb-source/src/bindings/dynamodb/dynamodb_big_segment_store.cpp
|
||
|
|
||
| #include <launchdarkly/server_side/integrations/dynamodb/dynamodb_big_segment_store.hpp> | ||
| #include <launchdarkly/server_side/integrations/dynamodb/options.hpp> | ||
|
|
||
| #include <launchdarkly/detail/c_binding_helpers.hpp> | ||
|
|
||
| #include <cstring> | ||
| #include <utility> | ||
|
|
||
| using namespace launchdarkly::server_side::integrations; | ||
|
|
||
| LD_EXPORT(bool) | ||
| LDServerBigSegmentsDynamoDBStore_New( | ||
| char const* table_name, | ||
|
Check warning on line 15 in libs/server-sdk-dynamodb-source/src/bindings/dynamodb/dynamodb_big_segment_store.cpp
|
||
| char const* prefix, | ||
| LDServerDynamoDBClientOptionsBuilder options, | ||
| LDServerBigSegmentsDynamoDBResult* out_result) { | ||
| LD_ASSERT_NOT_NULL(table_name); | ||
| LD_ASSERT_NOT_NULL(prefix); | ||
| LD_ASSERT_NOT_NULL(out_result); | ||
|
|
||
| // Explicitly zero out the error_message buffer in case the error is | ||
| // shorter than the buffer. | ||
| memset(out_result->error_message, 0, | ||
| sizeof(LDServerBigSegmentsDynamoDBResult::error_message)); | ||
|
|
||
| // Ensure the store pointer isn't garbage. | ||
| out_result->store = nullptr; | ||
|
|
||
| DynamoDBClientOptions opts{}; | ||
|
Check warning on line 31 in libs/server-sdk-dynamodb-source/src/bindings/dynamodb/dynamodb_big_segment_store.cpp
|
||
| if (options != nullptr) { | ||
| auto* opts_ptr = reinterpret_cast<DynamoDBClientOptions*>(options); | ||
| opts = *opts_ptr; | ||
| LDServerDynamoDBClientOptionsBuilder_Free(options); | ||
| } | ||
|
|
||
| auto maybe_store = | ||
| DynamoDBBigSegmentStore::Create(table_name, prefix, std::move(opts)); | ||
| if (!maybe_store) { | ||
| // Avoid heap allocating another string to pass back to the caller; | ||
| // instead, we copy into the buffer and ensure a terminator is present. | ||
| // This does mean the message may be truncated. | ||
| std::size_t const len = maybe_store.error().copy( | ||
| out_result->error_message, sizeof(out_result->error_message) - 1); | ||
| out_result->error_message[len] = '\0'; | ||
| return false; | ||
| } | ||
|
|
||
| // The pointer is no longer managed and must either be freed by the caller, | ||
| // or passed into the SDK which will take ownership. | ||
| out_result->store = reinterpret_cast<LDServerBigSegmentsDynamoDBStore>( | ||
| maybe_store->release()); | ||
| return true; | ||
| } | ||
|
|
||
| LD_EXPORT(void) | ||
| LDServerBigSegmentsDynamoDBStore_Free(LDServerBigSegmentsDynamoDBStore store) { | ||
| delete reinterpret_cast<DynamoDBBigSegmentStore*>(store); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.