|
| 1 | +/** @file dynamodb_source.h |
| 2 | + * @brief LaunchDarkly Server-side DynamoDB Source C Binding. |
| 3 | + */ |
| 4 | +// NOLINTBEGIN modernize-use-using |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <launchdarkly/server_side/bindings/c/integrations/dynamodb/dynamodb_client_options.h> |
| 8 | + |
| 9 | +#include <launchdarkly/bindings/c/export.h> |
| 10 | + |
| 11 | +#ifdef __cplusplus |
| 12 | +extern "C" { |
| 13 | +// only need to export C interface if |
| 14 | +// used by C++ source code |
| 15 | +#endif |
| 16 | + |
| 17 | +/** |
| 18 | + * @brief LDServerLazyLoadDynamoDBSource represents a data source for the |
| 19 | + * Server-Side SDK backed by Amazon DynamoDB. It is meant to be used in place |
| 20 | + * of the standard LaunchDarkly Streaming or Polling data sources. |
| 21 | + * |
| 22 | + * Call @ref LDServerLazyLoadDynamoDBSource_New to obtain a new instance. |
| 23 | + * This instance can be passed into the SDK's DataSystem configuration via |
| 24 | + * the LazyLoad builder. |
| 25 | + * |
| 26 | + * The DynamoDB table must already exist and follow the LaunchDarkly schema: |
| 27 | + * a String partition key named `namespace` and a String sort key named |
| 28 | + * `key`. The LaunchDarkly Relay Proxy populates the table with this schema; |
| 29 | + * this source only reads from it. |
| 30 | + * |
| 31 | + * Example: |
| 32 | + * @code |
| 33 | + * // Optional: configure the AWS DynamoDB client. Pass NULL for defaults. |
| 34 | + * LDServerDynamoDBClientOptionsBuilder options = |
| 35 | + * LDServerDynamoDBClientOptionsBuilder_New(); |
| 36 | + * LDServerDynamoDBClientOptionsBuilder_Region(options, "us-east-1"); |
| 37 | + * |
| 38 | + * // Stack allocate the result struct, which will hold the result pointer or |
| 39 | + * // an error message. |
| 40 | + * struct LDServerLazyLoadDynamoDBResult result; |
| 41 | + * |
| 42 | + * if (!LDServerLazyLoadDynamoDBSource_New("my-table", "testprefix", options, |
| 43 | + * &result)) { |
| 44 | + * // On failure, you may print the error message (result.error_message), |
| 45 | + * // then exit or return. |
| 46 | + * } |
| 47 | + * |
| 48 | + * LDServerLazyLoadBuilder lazy_builder = LDServerLazyLoadBuilder_New(); |
| 49 | + * LDServerLazyLoadBuilder_SourcePtr( |
| 50 | + * lazy_builder, (LDServerLazyLoadSourcePtr)result.source); |
| 51 | + * |
| 52 | + * LDServerConfigBuilder cfg_builder = LDServerConfigBuilder_New("sdk-123"); |
| 53 | + * LDServerConfigBuilder_DataSystem_LazyLoad(cfg_builder, lazy_builder); |
| 54 | + * @endcode |
| 55 | + * |
| 56 | + * This implementation is backed by the AWS SDK for C++. |
| 57 | + */ |
| 58 | +typedef struct _LDServerLazyLoadDynamoDBSource* LDServerLazyLoadDynamoDBSource; |
| 59 | + |
| 60 | +/* Defines the size of the error message buffer in |
| 61 | + * LDServerLazyLoadDynamoDBResult. |
| 62 | + */ |
| 63 | +#ifndef LDSERVER_LAZYLOAD_DYNAMODBSOURCE_ERROR_MESSAGE_SIZE |
| 64 | +#define LDSERVER_LAZYLOAD_DYNAMODBSOURCE_ERROR_MESSAGE_SIZE 256 |
| 65 | +#endif |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief Stores the result of calling @ref LDServerLazyLoadDynamoDBSource_New. |
| 69 | + * |
| 70 | + * On successful creation, source will contain a pointer which may be passed |
| 71 | + * into the LaunchDarkly SDK's configuration. |
| 72 | + * |
| 73 | + * On failure, error_message contains a NULL-terminated string describing the |
| 74 | + * error. |
| 75 | + * |
| 76 | + * The message may be truncated if it was originally longer than |
| 77 | + * error_message's buffer size. |
| 78 | + */ |
| 79 | +struct LDServerLazyLoadDynamoDBResult { |
| 80 | + LDServerLazyLoadDynamoDBSource source; |
| 81 | + char error_message[LDSERVER_LAZYLOAD_DYNAMODBSOURCE_ERROR_MESSAGE_SIZE]; |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief Creates a new DynamoDB data source suitable for usage in the SDK's |
| 86 | + * Lazy Load data system. |
| 87 | + * |
| 88 | + * In this system, the SDK will query DynamoDB for flag/segment data as |
| 89 | + * required, with an in-memory cache to reduce the number of queries. |
| 90 | + * |
| 91 | + * Data is never written back to DynamoDB by the SDK; the LaunchDarkly Relay |
| 92 | + * Proxy populates the table. |
| 93 | + * |
| 94 | + * @param table_name Name of the DynamoDB table to read from. The table must |
| 95 | + * already exist; this function does not create it. Must not be NULL. |
| 96 | + * |
| 97 | + * @param prefix Prefix to use when reading SDK data from DynamoDB. This |
| 98 | + * allows multiple SDK environments to coexist in the same table. Must not |
| 99 | + * be NULL. |
| 100 | + * |
| 101 | + * @param options Optional AWS DynamoDB client configuration. When non-NULL, |
| 102 | + * the builder is consumed and freed by this function; do not call |
| 103 | + * @ref LDServerDynamoDBClientOptionsBuilder_Free on it afterward. When NULL, |
| 104 | + * the AWS SDK's default provider chain is used for region, endpoint, and |
| 105 | + * credentials. |
| 106 | + * |
| 107 | + * @param out_result Pointer to struct where the source pointer or an error |
| 108 | + * message should be stored. Must not be NULL. |
| 109 | + * |
| 110 | + * @return True if the source was created successfully; out_result->source |
| 111 | + * will contain the pointer. The caller must either free the pointer with |
| 112 | + * @ref LDServerLazyLoadDynamoDBSource_Free, OR pass it into the SDK's |
| 113 | + * configuration methods which will take ownership (in which case do not |
| 114 | + * call @ref LDServerLazyLoadDynamoDBSource_Free.) |
| 115 | + */ |
| 116 | +LD_EXPORT(bool) |
| 117 | +LDServerLazyLoadDynamoDBSource_New( |
| 118 | + char const* table_name, |
| 119 | + char const* prefix, |
| 120 | + LDServerDynamoDBClientOptionsBuilder options, |
| 121 | + struct LDServerLazyLoadDynamoDBResult* out_result); |
| 122 | + |
| 123 | +/** |
| 124 | + * @brief Frees a DynamoDB data source pointer. Only necessary to call if not |
| 125 | + * passing ownership to SDK configuration. |
| 126 | + */ |
| 127 | +LD_EXPORT(void) |
| 128 | +LDServerLazyLoadDynamoDBSource_Free(LDServerLazyLoadDynamoDBSource source); |
| 129 | + |
| 130 | +#ifdef __cplusplus |
| 131 | +} |
| 132 | +#endif |
| 133 | + |
| 134 | +// NOLINTEND modernize-use-using |
0 commit comments