-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathCatalogCacheRepository.cpp
More file actions
160 lines (132 loc) · 5.36 KB
/
CatalogCacheRepository.cpp
File metadata and controls
160 lines (132 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (C) 2019-2020 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
#include "CatalogCacheRepository.h"
#include <limits>
#include <string>
#include <vector>
#include <olp/core/cache/KeyValueCache.h>
#include <olp/core/logging/Log.h>
// clang-format off
#include "generated/parser/CatalogParser.h"
#include "generated/parser/VersionInfosParser.h"
#include "generated/parser/VersionResponseParser.h"
#include "generated/parser/JsonParserCacheValue.h"
#include <olp/core/generated/parser/JsonParser.h>
#include "generated/serializer/CatalogSerializer.h"
#include "generated/serializer/VersionResponseSerializer.h"
#include "generated/serializer/VersionInfosSerializer.h"
#include "generated/serializer/JsonSerializer.h"
// clang-format on
namespace {
constexpr auto kLogTag = "CatalogCacheRepository";
// Currently, we expire the catalog version after 5 minutes. Later we plan to
// give the user the control when to expire it.
constexpr auto kCatalogVersionExpiryTime = 5 * 60;
constexpr auto kChronoSecondsMax = std::chrono::seconds::max();
constexpr auto kTimetMax = std::numeric_limits<time_t>::max();
std::string CreateKey(const std::string& hrn) { return hrn + "::catalog"; }
std::string VersionKey(const std::string& hrn) {
return hrn + "::latestVersion";
}
std::string VersionInfosKey(const std::string& hrn, std::int64_t start,
std::int64_t end) {
return hrn + "::" + std::to_string(start) + "::" + std::to_string(end) +
"::versionInfos";
}
time_t ConvertTime(std::chrono::seconds time) {
return time == kChronoSecondsMax ? kTimetMax : time.count();
}
} // namespace
namespace olp {
namespace dataservice {
namespace read {
namespace repository {
CatalogCacheRepository::CatalogCacheRepository(
const client::HRN& hrn, std::shared_ptr<cache::KeyValueCache> cache,
std::chrono::seconds default_expiry)
: hrn_(hrn), cache_(cache), default_expiry_(ConvertTime(default_expiry)) {}
void CatalogCacheRepository::Put(const model::Catalog& catalog) {
std::string hrn(hrn_.ToCatalogHRNString());
auto key = CreateKey(hrn);
OLP_SDK_LOG_DEBUG_F(kLogTag, "Put -> '%s'", key.c_str());
cache_->Put(key, catalog,
[&]() { return olp::serializer::serialize(catalog); },
default_expiry_);
}
boost::optional<model::Catalog> CatalogCacheRepository::Get() {
std::string hrn(hrn_.ToCatalogHRNString());
auto key = CreateKey(hrn);
OLP_SDK_LOG_DEBUG_F(kLogTag, "Get -> '%s'", key.c_str());
auto cached_catalog = cache_->Get(key, [](const std::string& value) {
return parser::parse<model::Catalog>(value);
});
if (cached_catalog.empty()) {
return boost::none;
}
return boost::any_cast<model::Catalog>(cached_catalog);
}
void CatalogCacheRepository::PutVersion(const model::VersionResponse& version) {
std::string hrn(hrn_.ToCatalogHRNString());
OLP_SDK_LOG_DEBUG_F(kLogTag, "PutVersion -> '%s'", hrn.c_str());
cache_->Put(VersionKey(hrn), version,
[&]() { return olp::serializer::serialize(version); },
kCatalogVersionExpiryTime);
}
boost::optional<model::VersionResponse> CatalogCacheRepository::GetVersion() {
std::string hrn(hrn_.ToCatalogHRNString());
auto key = VersionKey(hrn);
OLP_SDK_LOG_DEBUG_F(kLogTag, "GetVersion -> '%s'", key.c_str());
auto cached_version = cache_->Get(key, [](const std::string& value) {
return parser::parse<model::VersionResponse>(value);
});
if (cached_version.empty()) {
return boost::none;
}
return boost::any_cast<model::VersionResponse>(cached_version);
}
void CatalogCacheRepository::PutVersionInfos(
std::int64_t start, std::int64_t end, const model::VersionInfos& versions) {
std::string hrn(hrn_.ToCatalogHRNString());
auto key = VersionInfosKey(hrn, start, end);
OLP_SDK_LOG_DEBUG_F(kLogTag, "PutVersionInfos -> '%s'", key.c_str());
auto list_versions = olp::serializer::serialize(versions);
auto versions_data = std::make_shared<cache::KeyValueCache::ValueType>(
list_versions.data(), list_versions.data() + list_versions.size());
cache_->Put(VersionInfosKey(hrn, start, end), versions_data, default_expiry_);
}
boost::optional<model::VersionInfos> CatalogCacheRepository::GetVersionInfos(
std::int64_t start, std::int64_t end) {
std::string hrn(hrn_.ToCatalogHRNString());
auto key = VersionInfosKey(hrn, start, end);
OLP_SDK_LOG_DEBUG_F(kLogTag, "GetVersionInfos -> '%s'", key.c_str());
auto value = cache_->Get(key);
if (!value) {
return boost::none;
}
return parser::parse<model::VersionInfos>(*value);
}
void CatalogCacheRepository::Clear() {
std::string hrn(hrn_.ToCatalogHRNString());
OLP_SDK_LOG_INFO_F(kLogTag, "Clear -> '%s'", CreateKey(hrn).c_str());
cache_->RemoveKeysWithPrefix(hrn);
}
} // namespace repository
} // namespace read
} // namespace dataservice
} // namespace olp