Skip to content

Commit

Permalink
Extension of KeyValueCache interface (#1513)
Browse files Browse the repository at this point in the history
Added main operations like Read, Write, Remove.
The difference with the old one is that new API may
return actual error instead of cryptic `false`.
DefaultCache implementation was extended as well
to cover new methods.

Relates-To: OCMAM-3, OAM-2431

Signed-off-by: Andrey Kashcheev <[email protected]>
  • Loading branch information
andrey-kashcheev committed May 21, 2024
1 parent d16a78e commit 79d30c9
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 203 deletions.
41 changes: 41 additions & 0 deletions olp-cpp-sdk-core/include/olp/core/cache/DefaultCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,47 @@ class CORE_API DefaultCache : public KeyValueCache {
*/
void Promote(const std::string& key) override;

/**
* @brief Gets the binary data from the cache.
*
* @param key The key that is used to look for the binary data.
*
* @return The binary data or an error if the data could not be retrieved from
* the cache.
*/
OperationOutcome<ValueTypePtr> Read(const std::string& key) override;

/**
* @brief Stores the raw binary data as a value in the cache.
*
* @param key The key for this value.
* @param value The binary data that should be stored.
* @param expiry The expiry time (in seconds) of the key-value pair.
*
* @return An error if the data could not be written to the cache.
*/
OperationOutcomeEmpty Write(const std::string& key, const ValueTypePtr& value,
time_t expiry) override;

/**
* @brief Removes the key-value pair from the cache.
*
* @param key The key for the value.
*
* @return An error if the data could not be removed from the cache.
*/
OperationOutcomeEmpty Delete(const std::string& key) override;

/**
* @brief Removes the values with the keys that match the given
* prefix from the cache.
*
* @param prefix The prefix that matches the keys.
*
* @return An error if the data could not be removed from the cache.
*/
OperationOutcomeEmpty DeleteByPrefix(const std::string& prefix) override;

/**
* @brief Gets size of the corresponding cache.
*
Expand Down
63 changes: 63 additions & 0 deletions olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include <vector>

#include <olp/core/CoreApi.h>
#include <olp/core/client/ApiError.h>
#include <olp/core/client/ApiNoResult.h>
#include <olp/core/client/ApiResponse.h>
#include <olp/core/utils/WarningWorkarounds.h>
#include <boost/any.hpp>

Expand All @@ -36,6 +39,10 @@ namespace cache {
using Encoder = std::function<std::string()>;
using Decoder = std::function<boost::any(const std::string&)>;

template <typename Result>
using OperationOutcome = client::ApiResponse<Result, client::ApiError>;
using OperationOutcomeEmpty = OperationOutcome<client::ApiNoResult>;

/// An interface for a cache that expects a key-value pair.
class CORE_API KeyValueCache {
public:
Expand Down Expand Up @@ -181,6 +188,62 @@ class CORE_API KeyValueCache {
* @param key The key to promote in the cache LRU.
*/
virtual void Promote(const std::string& key) { OLP_SDK_CORE_UNUSED(key); }

/**
* @brief Gets the binary data from the cache.
*
* @param key The key that is used to look for the binary data.
*
* @return The binary data or an error if the data could not be retrieved from
* the cache.
*/
virtual OperationOutcome<ValueTypePtr> Read(const std::string& key) {
OLP_SDK_CORE_UNUSED(key);
return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
}

/**
* @brief Stores the raw binary data as a value in the cache.
*
* @param key The key for this value.
* @param value The binary data that should be stored.
* @param expiry The expiry time (in seconds) of the key-value pair.
*
* @return An error if the data could not be written to the cache.
*/
virtual OperationOutcomeEmpty Write(const std::string& key,
const ValueTypePtr& value,
time_t expiry = kDefaultExpiry) {
OLP_SDK_CORE_UNUSED(key);
OLP_SDK_CORE_UNUSED(value);
OLP_SDK_CORE_UNUSED(expiry);
return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
}

/**
* @brief Removes the key-value pair from the cache.
*
* @param key The key for the value.
*
* @return An error if the data could not be removed from the cache.
*/
virtual OperationOutcomeEmpty Delete(const std::string& key) {
OLP_SDK_CORE_UNUSED(key);
return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
}

/**
* @brief Removes the values with the keys that match the given
* prefix from the cache.
*
* @param prefix The prefix that matches the keys.
*
* @return An error if the data could not be removed from the cache.
*/
virtual OperationOutcomeEmpty DeleteByPrefix(const std::string& prefix) {
OLP_SDK_CORE_UNUSED(prefix);
return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
}
};

} // namespace cache
Expand Down
19 changes: 19 additions & 0 deletions olp-cpp-sdk-core/src/cache/DefaultCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,24 @@ uint64_t DefaultCache::Size(uint64_t new_size) { return impl_->Size(new_size); }

void DefaultCache::Promote(const std::string& key) { impl_->Promote(key); }

OperationOutcome<KeyValueCache::ValueTypePtr> DefaultCache::Read(
const std::string& key) {
return impl_->Read(key);
}

OperationOutcomeEmpty DefaultCache::Write(
const std::string& key, const KeyValueCache::ValueTypePtr& value,
time_t expiry) {
return impl_->Write(key, value, expiry);
}

OperationOutcomeEmpty DefaultCache::Delete(const std::string& key) {
return impl_->Delete(key);
}

OperationOutcomeEmpty DefaultCache::DeleteByPrefix(const std::string& prefix) {
return impl_->DeleteByPrefix(prefix);
}

} // namespace cache
} // namespace olp
Loading

0 comments on commit 79d30c9

Please sign in to comment.