Skip to content

Commit 5a9c5ee

Browse files
author
Liubov Didkivska
committed
Add caching versions infos
Add implementation to put/get versions list to catalog client cache. Implement versions infos cache tests. Relates-To: OLPEDGE-1606 Signed-off-by: Liubov Didkivska <ext-liubov.didkivska@here.com>
1 parent e5e476d commit 5a9c5ee

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <olp/core/cache/KeyValueCache.h>
23+
#include <rapidjson/document.h>
24+
25+
namespace olp {
26+
namespace parser {
27+
template <typename T>
28+
inline T parse(const cache::KeyValueCache::ValueType& cached_json) {
29+
rapidjson::Document doc;
30+
T result{};
31+
if (!cached_json.empty() && cached_json.back() == '\0') {
32+
doc.Parse(reinterpret_cast<const char*>(cached_json.data()));
33+
if (doc.IsObject() || doc.IsArray()) {
34+
from_json(doc, result);
35+
}
36+
}
37+
return result;
38+
}
39+
40+
} // namespace parser
41+
} // namespace olp

olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "generated/parser/CatalogParser.h"
3131
#include "generated/parser/VersionInfosParser.h"
3232
#include "generated/parser/VersionResponseParser.h"
33+
#include "generated/parser/JsonParserCacheValue.h"
3334
#include <olp/core/generated/parser/JsonParser.h>
3435
#include "generated/serializer/CatalogSerializer.h"
3536
#include "generated/serializer/VersionResponseSerializer.h"
@@ -127,9 +128,9 @@ void CatalogCacheRepository::PutVersionInfos(
127128
OLP_SDK_LOG_DEBUG_F(kLogTag, "PutVersionInfos -> '%s'", key.c_str());
128129
auto list_versions = olp::serializer::serialize(versions);
129130

130-
auto versions_data = std::make_shared<std::vector<unsigned char>>(
131-
std::begin(list_versions), std::end(list_versions));
132-
131+
// include null terminated symbol
132+
auto versions_data = std::make_shared<cache::KeyValueCache::ValueType>(
133+
list_versions.data(), list_versions.data() + list_versions.size() + 1);
133134
cache_->Put(VersionInfosKey(hrn, start, end), versions_data, default_expiry_);
134135
}
135136

@@ -144,8 +145,7 @@ boost::optional<model::VersionInfos> CatalogCacheRepository::GetVersionInfos(
144145
return boost::none;
145146
}
146147

147-
auto list_versions = std::string(value->begin(), value->end());
148-
return parser::parse<model::VersionInfos>(list_versions);
148+
return parser::parse<model::VersionInfos>(*value);
149149
}
150150

151151
void CatalogCacheRepository::Clear() {

0 commit comments

Comments
 (0)