Skip to content
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

docs: add code example for setting up Lazy Load Redis source in C #431

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,47 @@ extern "C" {
// used by C++ source code
#endif

/**
* @brief LDServerLazyLoadRedisSource represents a data source for the
* Server-Side SDK backed by Redis. It is meant to be used in place of the
* standard LaunchDarkly Streaming or Polling data sources.
*
* Call @ref LDServerLazyLoadRedisSource_New to obtain a new instance. This
* instance can be passed into the SDK's DataSystem configuration via the
* LazyLoad builder.
*
* Example:
* @code
* // Stack allocate the result struct, which will hold the result pointer or
* // an error message.
* struct LDServerLazyLoadRedisResult result;
*
* // Create the Redis source, passing in arguments for the URI, prefix, and
* // pointer to the result.
* if (!LDServerLazyLoadRedisSource_New("redis://localhost:6379", "testprefix",
* &result)) {
* // On failure, you may print the error message (result.error_message),
* // then exit or return.
* }
*
* // Create a builder for the Lazy Load data system.
* LDServerLazyLoadBuilder lazy_builder = LDServerLazyLoadBuilder_New();
*
* // Pass the Redis source pointer into it.
* LDServerLazyLoadBuilder_SourcePtr(lazy_builder, result.source);
*
* // Create a standard server-side SDK configuration builder.
* LDServerConfigBuilder cfg_builder = LDServerConfigBuilder_New("sdk-123");
*
* // Tell the SDK config builder to use the Lazy Load system that was just
* // configured.
* LDServerConfigBuilder_DataSystem_LazyLoad(cfg_builder, lazy_builder);
* @endcode
*
* This implementation is backed by <a
* href="https://github.com/sewenew/redis-plus-plus">Redis++</a>, a C++ wrapper
* for the <a href="https://github.com/redis/hiredis">hiredis</a> library.
*/
typedef struct _LDServerLazyLoadRedisSource* LDServerLazyLoadRedisSource;

/* Defines the size of the error message buffer in LDServerLazyLoadResult.
Expand All @@ -21,7 +62,7 @@ typedef struct _LDServerLazyLoadRedisSource* LDServerLazyLoadRedisSource;
#endif

/**
* @brief Stores the result of calling LDDServerLazyLoadRedisSource_New.
* @brief Stores the result of calling @ref LDDServerLazyLoadRedisSource_New.
*
* On successful creation, source will contain a pointer which may be passed
* into the LaunchDarkly SDK's configuration.
Expand Down Expand Up @@ -57,9 +98,9 @@ struct LDServerLazyLoadRedisResult {
*
* @return True if the source was created successfully; out_result->source
* will contain the pointer. The caller must either free the
* pointer with LDServerLazyLoadRedisSource_Free, OR pass it into the SDK's
* pointer with @ref LDServerLazyLoadRedisSource_Free, OR pass it into the SDK's
* configuration methods which will take ownership (in which case do not call
* LDServerLazyLoadRedisSource_Free.)
* @ref LDServerLazyLoadRedisSource_Free.)
*/
LD_EXPORT(bool)
LDServerLazyLoadRedisSource_New(char const* uri,
Expand Down
4 changes: 3 additions & 1 deletion libs/server-sdk-redis-source/tests/redis_source_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gtest/gtest.h>

#include <launchdarkly/server_side/integrations/data_reader/kinds.hpp>
#include <launchdarkly/server_side/integrations/redis/redis_source.hpp>

Check failure on line 4 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:4:10 [clang-diagnostic-error]

'launchdarkly/server_side/integrations/redis/redis_source.hpp' file not found

#include <launchdarkly/context_builder.hpp>
#include <launchdarkly/server_side/client.hpp>
Expand All @@ -21,14 +21,16 @@

class RedisTests : public ::testing::Test {
public:
explicit RedisTests()

Check warning on line 24 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:24:5 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: source, client_
: uri_("tcp://localhost:6379"), prefix_("testprefix"), client_(uri_) {}
: uri_("redis://localhost:6379"),
prefix_("testprefix"),
client_(uri_) {}

void SetUp() override {
try {
client_.flushdb();
} catch (sw::redis::Error const& e) {
FAIL() << "couldn't clear Redis: " << e.what();

Check warning on line 33 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:33:23 [readability-implicit-bool-conversion]

implicit conversion 'const char *' -> bool
}

auto maybe_source = RedisDataSource::Create(uri_, prefix_);
Expand All @@ -36,32 +38,32 @@
source = std::move(*maybe_source);
}

void Init() {

Check warning on line 41 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:41:10 [readability-convert-member-functions-to-static]

method 'Init' can be made static
auto const client = PrefixedClient(client_, prefix_);
client.Init();
}

void PutFlag(Flag const& flag) {

Check warning on line 46 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:46:10 [readability-convert-member-functions-to-static]

method 'PutFlag' can be made static
auto const client = PrefixedClient(client_, prefix_);
client.PutFlag(flag);
}

void PutDeletedFlag(std::string const& key, std::string const& ts) {

Check warning on line 51 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:51:10 [readability-convert-member-functions-to-static]

method 'PutDeletedFlag' can be made static

Check warning on line 51 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:51:68 [readability-identifier-length]

parameter name 'ts' is too short, expected at least 3 characters
auto const client = PrefixedClient(client_, prefix_);
client.PutDeletedFlag(key, ts);
}

void PutDeletedSegment(std::string const& key, std::string const& ts) {

Check warning on line 56 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:56:10 [readability-convert-member-functions-to-static]

method 'PutDeletedSegment' can be made static

Check warning on line 56 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:56:71 [readability-identifier-length]

parameter name 'ts' is too short, expected at least 3 characters
auto const client = PrefixedClient(client_, prefix_);
client.PutDeletedSegment(key, ts);
}

void PutSegment(Segment const& segment) {

Check warning on line 61 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:61:10 [readability-convert-member-functions-to-static]

method 'PutSegment' can be made static
auto const client = PrefixedClient(client_, prefix_);
client.PutSegment(segment);
}

void WithPrefixedClient(

Check warning on line 66 in libs/server-sdk-redis-source/tests/redis_source_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk-redis-source/tests/redis_source_test.cpp:66:10 [readability-convert-member-functions-to-static]

method 'WithPrefixedClient' can be made static
std::string const& prefix,
std::function<void(PrefixedClient const&)> const& f) {
auto const client = PrefixedClient(client_, prefix);
Expand Down
Loading