-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cc: add support for registering a platform KV store (#2430)
Description: Adds support for registering a platform KV store implementation via EngineBuilder. Risk Level: Low Testing: CI Signed-off-by: Mike Schore <[email protected]>
- Loading branch information
Showing
6 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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,46 @@ | ||
#include "library/cc/key_value_store.h" | ||
|
||
#include "library/common/data/utility.h" | ||
|
||
namespace Envoy { | ||
namespace Platform { | ||
|
||
namespace { | ||
|
||
envoy_data c_kv_store_read(envoy_data key, const void* context) { | ||
auto kv_store = *static_cast<KeyValueStoreSharedPtr*>(const_cast<void*>(context)); | ||
|
||
auto value = kv_store->read(Data::Utility::copyToString(key)); | ||
release_envoy_data(key); | ||
|
||
return value.has_value() ? Data::Utility::copyToBridgeData(value.value()) : envoy_nodata; | ||
} | ||
|
||
void c_kv_store_save(envoy_data key, envoy_data value, const void* context) { | ||
auto kv_store = *static_cast<KeyValueStoreSharedPtr*>(const_cast<void*>(context)); | ||
|
||
kv_store->save(Data::Utility::copyToString(key), Data::Utility::copyToString(value)); | ||
release_envoy_data(key); | ||
release_envoy_data(value); | ||
} | ||
|
||
void c_kv_store_remove(envoy_data key, const void* context) { | ||
auto kv_store = *static_cast<KeyValueStoreSharedPtr*>(const_cast<void*>(context)); | ||
|
||
kv_store->remove(Data::Utility::copyToString(key)); | ||
release_envoy_data(key); | ||
} | ||
|
||
} // namespace | ||
|
||
envoy_kv_store KeyValueStore::asEnvoyKeyValueStore() { | ||
return envoy_kv_store{ | ||
&c_kv_store_read, | ||
&c_kv_store_save, | ||
&c_kv_store_remove, | ||
new KeyValueStoreSharedPtr(shared_from_this()), | ||
}; | ||
} | ||
|
||
} // namespace Platform | ||
} // namespace Envoy |
This file contains 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,51 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#include "envoy/common/pure.h" | ||
|
||
#include "absl/types/optional.h" | ||
#include "library/common/extensions/key_value/platform/c_types.h" | ||
|
||
namespace Envoy { | ||
namespace Platform { | ||
|
||
/** | ||
* `KeyValueStore` is an interface that may be implemented to provide access to an arbitrary | ||
* key-value store implementation that may be made accessible to internal Envoy Mobile code. | ||
*/ | ||
struct KeyValueStore : public std::enable_shared_from_this<KeyValueStore> { | ||
public: | ||
virtual ~KeyValueStore() = default; | ||
|
||
/** | ||
* Returns the value associated with the provided key, if any. | ||
* @param key supplies a key to return the value of. | ||
* @return the value, if the key is in the store, absl::nullopt otherwise. | ||
*/ | ||
virtual absl::optional<std::string> read(const std::string& key) PURE; | ||
|
||
/** | ||
* Adds or updates a key:value pair in the store. | ||
* @param key supplies a key to add or update. | ||
* @param value supplies the value to set for that key. | ||
*/ | ||
virtual void save(std::string key, std::string value) PURE; | ||
|
||
/** | ||
* Removes a key:value pair from the store. This is a no-op if the key is not present. | ||
* @param key supplies a key to remove from the store. | ||
*/ | ||
virtual void remove(const std::string& key) PURE; | ||
|
||
/** | ||
* Maps an implementation to its internal representation. | ||
* @return portable internal type used to call an implementation. | ||
*/ | ||
virtual envoy_kv_store asEnvoyKeyValueStore() final; | ||
}; | ||
|
||
using KeyValueStoreSharedPtr = std::shared_ptr<KeyValueStore>; | ||
|
||
} // namespace Platform | ||
} // namespace Envoy |