Skip to content

Commit

Permalink
Add versions list implementation
Browse files Browse the repository at this point in the history
Add interface to get versions list to catalog client.
Implement get versions list.

Relates-To: OLPEDGE-1606

Signed-off-by: Liubov Didkivska <[email protected]>
  • Loading branch information
Liubov Didkivska committed Jun 9, 2020
1 parent f54b906 commit 0f27430
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 HERE Europe B.V.
* 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.
Expand Down Expand Up @@ -31,6 +31,7 @@
#include <olp/dataservice/read/DataServiceReadApi.h>
#include <olp/dataservice/read/PartitionsRequest.h>
#include <olp/dataservice/read/Types.h>
#include <olp/dataservice/read/VersionsRequest.h>

namespace olp {

Expand Down Expand Up @@ -137,6 +138,32 @@ class DATASERVICE_READ_API CatalogClient final {
client::CancellableFuture<CatalogVersionResponse> GetLatestVersion(
CatalogVersionRequest request);

/**
* @brief Gets the catalog versions list.
*
* @param request The `VersionsRequest` instance that contains
* a complete set of request parameters.
* @param callback The `VersionsResponseCallback` object that is invoked if
* the list of versions is available or an error is encountered.
*
* @return A token that can be used to cancel this request.
*/
client::CancellationToken ListVersions(VersionsRequest request,
VersionsResponseCallback callback);

/**
* @brief Gets the catalog versions list.
*
* @param request The `VersionsRequest` instance that contains
* a complete set of request parameters.
*
* @return CancellableFuture` that contains the `VersionsResponse`
* instance with the list of versions or an error. You can also
* use `CancellableFuture` to cancel this request.
*/
client::CancellableFuture<VersionsResponse> ListVersions(
VersionsRequest request);

private:
std::unique_ptr<CatalogClientImpl> impl_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,11 @@ class DATASERVICE_READ_API VersionsRequest final {
/**
* @brief Creates a readable format for the request.
*
* @param layer_id The ID of the layer that is used for the request.
*
* @return A string representation of the request.
*/
inline std::string CreateKey(const std::string& layer_id) const {
inline std::string CreateKey() const {
std::stringstream out;
out << layer_id << "[";
out << "[";
out << GetStartVersion() << ", " << GetEndVersion();
out << "]";
if (GetBillingTag()) {
Expand Down
12 changes: 11 additions & 1 deletion olp-cpp-sdk-dataservice-read/src/CatalogClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 HERE Europe B.V.
* 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.
Expand Down Expand Up @@ -61,6 +61,16 @@ client::CancellableFuture<CatalogVersionResponse>
CatalogClient::GetLatestVersion(CatalogVersionRequest request) {
return impl_->GetLatestVersion(std::move(request));
}

client::CancellationToken CatalogClient::ListVersions(
VersionsRequest request, VersionsResponseCallback callback) {
return impl_->ListVersions(std::move(request), std::move(callback));
}

client::CancellableFuture<VersionsResponse> CatalogClient::ListVersions(
VersionsRequest request) {
return impl_->ListVersions(std::move(request));
}
} // namespace read
} // namespace dataservice
} // namespace olp
61 changes: 52 additions & 9 deletions olp-cpp-sdk-dataservice-read/src/CatalogClientImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 HERE Europe B.V.
* 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.
Expand All @@ -19,6 +19,8 @@

#include "CatalogClientImpl.h"

#include <utility>

#include <olp/core/cache/DefaultCache.h>
#include <olp/core/client/OlpClientSettingsFactory.h>
#include <olp/core/client/PendingRequests.h>
Expand All @@ -31,14 +33,13 @@
namespace olp {
namespace dataservice {
namespace read {
using namespace repository;
using namespace olp::client;

namespace {
constexpr auto kLogTag = "CatalogClientImpl";
}

CatalogClientImpl::CatalogClientImpl(HRN catalog, OlpClientSettings settings)
CatalogClientImpl::CatalogClientImpl(client::HRN catalog,
client::OlpClientSettings settings)
: catalog_(std::move(catalog)), settings_(std::move(settings)) {
if (!settings_.cache) {
settings_.cache = client::OlpClientSettingsFactory::CreateDefaultCache({});
Expand All @@ -60,7 +61,7 @@ bool CatalogClientImpl::CancelPendingRequests() {
return pending_requests_->CancelAll();
}

CancellationToken CatalogClientImpl::GetCatalog(
client::CancellationToken CatalogClientImpl::GetCatalog(
CatalogRequest request, CatalogResponseCallback callback) {
auto schedule_get_catalog = [&](CatalogRequest request,
CatalogResponseCallback callback) {
Expand All @@ -81,7 +82,7 @@ CancellationToken CatalogClientImpl::GetCatalog(
std::move(callback));
}

CancellableFuture<CatalogResponse> CatalogClientImpl::GetCatalog(
client::CancellableFuture<CatalogResponse> CatalogClientImpl::GetCatalog(
CatalogRequest request) {
auto promise = std::make_shared<std::promise<CatalogResponse>>();
auto cancel_token =
Expand All @@ -92,7 +93,7 @@ CancellableFuture<CatalogResponse> CatalogClientImpl::GetCatalog(
std::move(promise));
}

CancellationToken CatalogClientImpl::GetLatestVersion(
client::CancellationToken CatalogClientImpl::GetLatestVersion(
CatalogVersionRequest request, CatalogVersionCallback callback) {
OLP_SDK_LOG_TRACE_F(kLogTag, "GetCatalog '%s'", request.CreateKey().c_str());
auto schedule_get_latest_version = [&](CatalogVersionRequest request,
Expand All @@ -114,8 +115,8 @@ CancellationToken CatalogClientImpl::GetLatestVersion(
std::move(request), std::move(callback));
}

CancellableFuture<CatalogVersionResponse> CatalogClientImpl::GetLatestVersion(
CatalogVersionRequest request) {
client::CancellableFuture<CatalogVersionResponse>
CatalogClientImpl::GetLatestVersion(CatalogVersionRequest request) {
auto promise = std::make_shared<std::promise<CatalogVersionResponse>>();
auto cancel_token = GetLatestVersion(
std::move(request), [promise](CatalogVersionResponse response) {
Expand All @@ -124,6 +125,48 @@ CancellableFuture<CatalogVersionResponse> CatalogClientImpl::GetLatestVersion(
return client::CancellableFuture<CatalogVersionResponse>(
std::move(cancel_token), std::move(promise));
}

client::CancellationToken CatalogClientImpl::ListVersions(
VersionsRequest request, VersionsResponseCallback callback) {
if (request.GetFetchOption() == CacheWithUpdate) {
auto task = [](client::CancellationContext) -> VersionsResponse {
return {{client::ErrorCode::InvalidArgument,
"CacheWithUpdate option can not be used for versioned catalog"}};
};
return AddTask(settings_.task_scheduler, pending_requests_, std::move(task),
std::move(callback));
}

auto schedule_get_versions_list = [&](VersionsRequest request,
VersionsResponseCallback callback) {
auto catalog = catalog_;
auto settings = settings_;
auto pending_requests = pending_requests_;

auto versions_list_task =
[=](client::CancellationContext context) -> VersionsResponse {
return repository::CatalogRepository::GetVersionsList(
std::move(catalog), context, std::move(request), std::move(settings));
};

return AddTask(settings.task_scheduler, pending_requests,
std::move(versions_list_task), std::move(callback));
};

return ScheduleFetch(std::move(schedule_get_versions_list),
std::move(request), std::move(callback));
}

client::CancellableFuture<VersionsResponse> CatalogClientImpl::ListVersions(
VersionsRequest request) {
auto promise = std::make_shared<std::promise<VersionsResponse>>();
auto cancel_token =
ListVersions(std::move(request), [promise](VersionsResponse response) {
promise->set_value(std::move(response));
});
return client::CancellableFuture<VersionsResponse>(std::move(cancel_token),
std::move(promise));
}
} // namespace read
} // namespace dataservice
} // namespace olp
9 changes: 8 additions & 1 deletion olp-cpp-sdk-dataservice-read/src/CatalogClientImpl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 HERE Europe B.V.
* 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.
Expand Down Expand Up @@ -28,6 +28,7 @@
#include <olp/dataservice/read/CatalogRequest.h>
#include <olp/dataservice/read/CatalogVersionRequest.h>
#include <olp/dataservice/read/Types.h>
#include <olp/dataservice/read/VersionsRequest.h>

namespace olp {
namespace client {
Expand Down Expand Up @@ -61,6 +62,12 @@ class CatalogClientImpl final {
client::CancellableFuture<CatalogVersionResponse> GetLatestVersion(
CatalogVersionRequest request);

client::CancellationToken ListVersions(VersionsRequest request,
VersionsResponseCallback callback);

client::CancellableFuture<VersionsResponse> ListVersions(
VersionsRequest request);

private:
client::HRN catalog_;
client::OlpClientSettings settings_;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 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 <rapidjson/document.h>

#include "VersionInfosSerializer.h"

#include <olp/core/generated/serializer/SerializerWrapper.h>

namespace olp {
namespace serializer {

void to_json(const dataservice::read::model::VersionDependency& x,
rapidjson::Value& value,
rapidjson::Document::AllocatorType& allocator) {
value.SetObject();
serialize("hrn", x.GetHrn(), value, allocator);
serialize("version", x.GetVersion(), value, allocator);
serialize("direct", x.GetDirect(), value, allocator);
}

void to_json(const dataservice::read::model::VersionInfo& x,
rapidjson::Value& value,
rapidjson::Document::AllocatorType& allocator) {
value.SetObject();
serialize("dependencies", x.GetDependencies(), value, allocator);
serialize("timestamp", x.GetTimestamp(), value, allocator);
serialize("version", x.GetVersion(), value, allocator);
serialize("partitionCounts", x.GetPartitionCounts(), value, allocator);
}

void to_json(const dataservice::read::model::VersionInfos& x,
rapidjson::Value& value,
rapidjson::Document::AllocatorType& allocator) {
value.SetObject();
serialize("versions", x.GetVersions(), value, allocator);
}
} // namespace serializer
} // namespace olp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 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
*/

#pragma once

#include <rapidjson/document.h>

#include "olp/dataservice/read/model/VersionInfos.h"

namespace olp {
namespace serializer {

void to_json(const dataservice::read::model::VersionDependency& x,
rapidjson::Value& value,
rapidjson::Document::AllocatorType& allocator);

void to_json(const dataservice::read::model::VersionInfo& x,
rapidjson::Value& value,
rapidjson::Document::AllocatorType& allocator);

void to_json(const dataservice::read::model::VersionInfos& x,
rapidjson::Value& value,
rapidjson::Document::AllocatorType& allocator);
} // namespace serializer
} // namespace olp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 HERE Europe B.V.
* 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.
Expand All @@ -26,10 +26,12 @@

// clang-format off
#include "generated/parser/CatalogParser.h"
#include "generated/parser/VersionInfosParser.h"
#include "generated/parser/VersionResponseParser.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

Expand All @@ -46,6 +48,11 @@ 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::int16_t start,
std::int16_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();
Expand Down Expand Up @@ -111,6 +118,31 @@ boost::optional<model::VersionResponse> CatalogCacheRepository::GetVersion() {
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());
OLP_SDK_LOG_DEBUG_F(kLogTag, "PutVersionInfos -> '%s'", hrn.c_str());

cache_->Put(VersionInfosKey(hrn, start, end), versions,
[&]() { return olp::serializer::serialize(versions); },
kCatalogVersionExpiryTime);
}
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 cached_versions = cache_->Get(key, [](const std::string& value) {
return parser::parse<model::VersionInfos>(value);
});

if (cached_versions.empty()) {
return boost::none;
}
return boost::any_cast<model::VersionInfos>(cached_versions);
}

void CatalogCacheRepository::Clear() {
std::string hrn(hrn_.ToCatalogHRNString());
OLP_SDK_LOG_INFO_F(kLogTag, "Clear -> '%s'", CreateKey(hrn).c_str());
Expand Down
Loading

0 comments on commit 0f27430

Please sign in to comment.