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

Extension of KeyValueCache interface #1513

Merged
merged 1 commit into from
May 21, 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
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 @@
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 @@
* @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");

Check warning on line 202 in olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h#L200-L202

Added lines #L200 - L202 were not covered by tests
}

/**
* @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,

Check warning on line 214 in olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h#L214

Added line #L214 was not covered by tests
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");

Check warning on line 220 in olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h#L217-L220

Added lines #L217 - L220 were not covered by tests
}

/**
* @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");

Check warning on line 232 in olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h#L230-L232

Added lines #L230 - L232 were not covered by tests
}

/**
* @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");

Check warning on line 245 in olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h#L243-L245

Added lines #L243 - L245 were not covered by tests
}
};

} // 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 @@

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

OperationOutcome<KeyValueCache::ValueTypePtr> DefaultCache::Read(

Check warning on line 93 in olp-cpp-sdk-core/src/cache/DefaultCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCache.cpp#L93

Added line #L93 was not covered by tests
const std::string& key) {
return impl_->Read(key);

Check warning on line 95 in olp-cpp-sdk-core/src/cache/DefaultCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCache.cpp#L95

Added line #L95 was not covered by tests
}

OperationOutcomeEmpty DefaultCache::Write(

Check warning on line 98 in olp-cpp-sdk-core/src/cache/DefaultCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCache.cpp#L98

Added line #L98 was not covered by tests
const std::string& key, const KeyValueCache::ValueTypePtr& value,
time_t expiry) {
return impl_->Write(key, value, expiry);

Check warning on line 101 in olp-cpp-sdk-core/src/cache/DefaultCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCache.cpp#L101

Added line #L101 was not covered by tests
}

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

Check warning on line 105 in olp-cpp-sdk-core/src/cache/DefaultCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCache.cpp#L104-L105

Added lines #L104 - L105 were not covered by tests
}

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

Check warning on line 109 in olp-cpp-sdk-core/src/cache/DefaultCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCache.cpp#L108-L109

Added lines #L108 - L109 were not covered by tests
}

} // namespace cache
} // namespace olp
Loading
Loading