Skip to content

Commit bb6508c

Browse files
Bohdan Kurylovychborzun
authored andcommitted
Added VersionedLayerClient::PrefetchTiles method
Implemented the VersionedLayerClient::PrefetchTiles method in dataservice-read component, which is based on the CatalogClient::PrefetchTiles implementation. Also, moved the integration and functional tests regarding the pre-fetch functionality from CatalogClientTest with following changes: - added DataserviceReadVersionedLayerClientTest.cpp file to integration tests target; - removed the PrefetchEmbedded integration test from tests in DataserviceReadVersionedLayerClientTest test suite (see OLPEDGE-906); - re-enabled the PrefetchBusy test in both CatalogClientTest and DataserviceReadVersionedLayerClientTest. Relates to: OLPEDGE-795 Signed-off-by: Bohdan Kurylovych <[email protected]>
1 parent 3fd8e5d commit bb6508c

File tree

10 files changed

+551
-56
lines changed

10 files changed

+551
-56
lines changed

olp-cpp-sdk-dataservice-read/include/olp/dataservice/read/CatalogClient.h

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <olp/core/client/ApiResponse.h>
2929
#include <olp/core/client/OlpClientSettings.h>
3030
#include <olp/core/geo/tiling/TileKey.h>
31+
#include "olp/dataservice/read/PrefetchTileResult.h"
3132
#include "olp/dataservice/read/model/Catalog.h"
3233
#include "olp/dataservice/read/model/Data.h"
3334
#include "olp/dataservice/read/model/Partitions.h"
@@ -42,6 +43,8 @@ class HRN;
4243
namespace dataservice {
4344
namespace read {
4445

46+
class PrefetchTilesRequest;
47+
4548
namespace model {
4649
class Partition;
4750
} // namespace model
@@ -79,52 +82,6 @@ using DataResult = model::Data;
7982
using DataResponse = client::ApiResponse<DataResult, client::ApiError>;
8083
using DataResponseCallback = std::function<void(DataResponse response)>;
8184

82-
class PrefetchTilesRequest;
83-
class DATASERVICE_READ_API PrefetchTileNoError {
84-
public:
85-
PrefetchTileNoError() = default;
86-
PrefetchTileNoError(const PrefetchTileNoError& other) = default;
87-
~PrefetchTileNoError() = default;
88-
};
89-
90-
class DATASERVICE_READ_API PrefetchTileResult
91-
: public client::ApiResponse<PrefetchTileNoError, client::ApiError> {
92-
public:
93-
PrefetchTileResult()
94-
: client::ApiResponse<PrefetchTileNoError, client::ApiError>() {}
95-
96-
/**
97-
* @brief PrefetchTileResult Constructor for a successfully executed request.
98-
*/
99-
PrefetchTileResult(const geo::TileKey& tile,
100-
const PrefetchTileNoError& result)
101-
: client::ApiResponse<PrefetchTileNoError, client::ApiError>(result),
102-
tile_key_(tile) {}
103-
104-
/**
105-
* @brief PrefetchTileResult Constructor if request unsuccessfully executed
106-
*/
107-
PrefetchTileResult(const geo::TileKey& tile, const client::ApiError& error)
108-
: client::ApiResponse<PrefetchTileNoError, client::ApiError>(error),
109-
tile_key_(tile) {}
110-
111-
/**
112-
* @brief PrefetchTileResult Copy constructor.
113-
*/
114-
PrefetchTileResult(const PrefetchTileResult& r)
115-
: client::ApiResponse<PrefetchTileNoError, client::ApiError>(r),
116-
tile_key_(r.tile_key_) {}
117-
118-
/**
119-
* @brief ApiResponse Constructor if request unsuccessfully executed
120-
*/
121-
PrefetchTileResult(const client::ApiError& error)
122-
: client::ApiResponse<PrefetchTileNoError, client::ApiError>(error) {}
123-
124-
public:
125-
geo::TileKey tile_key_;
126-
};
127-
12885
/**
12986
* @brief The PrefetchTilesResponse class encapsulates the result of a prefetch
13087
* operation
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (C) 2019 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/client/ApiError.h>
23+
#include <olp/core/client/ApiResponse.h>
24+
#include <olp/core/geo/tiling/TileKey.h>
25+
#include <olp/dataservice/read/DataServiceReadApi.h>
26+
27+
namespace olp {
28+
namespace dataservice {
29+
namespace read {
30+
31+
class DATASERVICE_READ_API PrefetchTileNoError {
32+
public:
33+
PrefetchTileNoError() = default;
34+
PrefetchTileNoError(const PrefetchTileNoError& other) = default;
35+
~PrefetchTileNoError() = default;
36+
};
37+
38+
/**
39+
* @brif Class representing the result of pre-fetch operation.
40+
* It will contain either a successful result represented as \c geo::TileKey
41+
* object or will contain the failure error.
42+
*/
43+
class DATASERVICE_READ_API PrefetchTileResult
44+
: public client::ApiResponse<PrefetchTileNoError, client::ApiError> {
45+
public:
46+
PrefetchTileResult()
47+
: client::ApiResponse<PrefetchTileNoError, client::ApiError>() {}
48+
49+
/**
50+
* @brief PrefetchTileResult Constructor for a successfully executed request.
51+
*/
52+
PrefetchTileResult(const geo::TileKey& tile,
53+
const PrefetchTileNoError& result)
54+
: client::ApiResponse<PrefetchTileNoError, client::ApiError>(result),
55+
tile_key_(tile) {}
56+
57+
/**
58+
* @brief PrefetchTileResult Constructor if request unsuccessfully executed
59+
*/
60+
PrefetchTileResult(const geo::TileKey& tile, const client::ApiError& error)
61+
: client::ApiResponse<PrefetchTileNoError, client::ApiError>(error),
62+
tile_key_(tile) {}
63+
64+
/**
65+
* @brief PrefetchTileResult Copy constructor.
66+
*/
67+
PrefetchTileResult(const PrefetchTileResult& r)
68+
: client::ApiResponse<PrefetchTileNoError, client::ApiError>(r),
69+
tile_key_(r.tile_key_) {}
70+
71+
/**
72+
* @brief ApiResponse Constructor if request unsuccessfully executed
73+
*/
74+
PrefetchTileResult(const client::ApiError& error)
75+
: client::ApiResponse<PrefetchTileNoError, client::ApiError>(error) {}
76+
77+
public:
78+
/// The pre-fetched tile key.
79+
geo::TileKey tile_key_;
80+
};
81+
82+
} // namespace read
83+
} // namespace dataservice
84+
} // namespace olp

olp-cpp-sdk-dataservice-read/include/olp/dataservice/read/VersionedLayerClient.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#pragma once
2121

2222
#include <memory>
23+
#include <vector>
2324

2425
#include <olp/core/client/ApiError.h>
2526
#include <olp/core/client/ApiResponse.h>
@@ -29,6 +30,8 @@
2930
#include <olp/dataservice/read/DataRequest.h>
3031
#include <olp/dataservice/read/DataServiceReadApi.h>
3132
#include <olp/dataservice/read/PartitionsRequest.h>
33+
#include <olp/dataservice/read/PrefetchTileResult.h>
34+
#include <olp/dataservice/read/PrefetchTilesRequest.h>
3235
#include <olp/dataservice/read/model/Data.h>
3336
#include <olp/dataservice/read/model/Partitions.h>
3437

@@ -53,6 +56,16 @@ class DATASERVICE_READ_API VersionedLayerClient final {
5356
using PartitionsResponse =
5457
client::ApiResponse<PartitionsResult, client::ApiError>;
5558
using PartitionsCallback = std::function<void(PartitionsResponse response)>;
59+
60+
/// PrefetchTileResult alias
61+
using PrefetchTilesResult = std::vector<std::shared_ptr<PrefetchTileResult>>;
62+
/// Alias for the response to the \c PrefetchTileRequest
63+
using PrefetchTilesResponse =
64+
client::ApiResponse<PrefetchTilesResult, client::ApiError>;
65+
/// Alias for the callback to the results of PrefetchTilesResponse
66+
using PrefetchTilesResponseCallback =
67+
std::function<void(const PrefetchTilesResponse& response)>;
68+
5669
/**
5770
* @brief VersionedLayerClient constructor
5871
* @param catalog this versioned layer client uses during requests.
@@ -93,6 +106,26 @@ class DATASERVICE_READ_API VersionedLayerClient final {
93106
client::CancellationToken GetPartitions(PartitionsRequest partitions_request,
94107
PartitionsCallback callback) const;
95108

109+
/**
110+
* @brief Pre-fetches a set of tiles asychronously.
111+
*
112+
* This method recursively downloads all tilekeys from minLevel to maxLevel
113+
* specified in the \c PrefetchTilesRequest's properties. This will help to
114+
* reduce the network load by using the pre-fetched tiles' data from cache.
115+
*
116+
* \note - this does not guarantee that all tiles are available offline, as
117+
* the cache might overflow and data might be evicted at any point.
118+
*
119+
* @param request contains the complete set of request parameters.
120+
* \note The \c PrefetchTilesRequest's GetLayerId value will be ignored and
121+
* the parameter from constructore will be used instead.
122+
* @param callback will be invoked once the \c PrefetchTilesResult is
123+
* available, or an error is encountered.
124+
* @return A token that can be used to cancel this request.
125+
*/
126+
olp::client::CancellationToken PrefetchTiles(
127+
PrefetchTilesRequest request, PrefetchTilesResponseCallback callback);
128+
96129
private:
97130
std::unique_ptr<VersionedLayerClientImpl> impl_;
98131
};

olp-cpp-sdk-dataservice-read/src/CatalogClientImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ client::CancellationToken CatalogClientImpl::PrefetchTiles(
249249
callback(response);
250250
};
251251

252-
auto token = prefetch_provider_->PrefetchTiles(request, callback);
252+
auto token = prefetch_provider_->PrefetchTiles(request, request_callback);
253253

254254
pending_requests_->Insert(token, request_key);
255255
return token;

olp-cpp-sdk-dataservice-read/src/VersionedLayerClient.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ client::CancellationToken VersionedLayerClient::GetPartitions(
4646
std::move(callback));
4747
}
4848

49+
olp::client::CancellationToken VersionedLayerClient::PrefetchTiles(
50+
PrefetchTilesRequest request, PrefetchTilesResponseCallback callback) {
51+
return impl_->PrefetchTiles(std::move(request), std::move(callback));
52+
}
53+
4954
} // namespace read
5055
} // namespace dataservice
5156
} // namespace olp

olp-cpp-sdk-dataservice-read/src/VersionedLayerClientImpl.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
#include <olp/core/client/OlpClientSettingsFactory.h>
2424
#include <olp/core/context/Context.h>
2525
#include <olp/core/thread/TaskScheduler.h>
26+
#include "PrefetchTilesProvider.h"
2627
#include "TaskContext.h"
2728
#include "repositories/ApiRepository.h"
2829
#include "repositories/CatalogRepository.h"
2930
#include "repositories/DataRepository.h"
3031
#include "repositories/ExecuteOrSchedule.inl"
3132
#include "repositories/PartitionsRepository.h"
33+
#include "repositories/PrefetchTilesRepository.h"
3234

3335
namespace olp {
3436
namespace dataservice {
@@ -58,6 +60,16 @@ VersionedLayerClientImpl::VersionedLayerClientImpl(
5860

5961
partition_repo_ = std::make_shared<repository::PartitionsRepository>(
6062
catalog_, api_repo, catalog_repo, settings_->cache);
63+
64+
auto data_repo = std::make_shared<repository::DataRepository>(
65+
catalog_, api_repo, catalog_repo, partition_repo_, settings_->cache);
66+
67+
auto prefetch_repo = std::make_shared<repository::PrefetchTilesRepository>(
68+
catalog_, api_repo, partition_repo_->GetPartitionsCacheRepository(),
69+
settings_);
70+
71+
prefetch_provider_ = std::make_shared<PrefetchTilesProvider>(
72+
catalog_, api_repo, catalog_repo, data_repo, prefetch_repo, settings_);
6173
}
6274

6375
VersionedLayerClientImpl::~VersionedLayerClientImpl() {
@@ -136,6 +148,23 @@ client::CancellationToken VersionedLayerClientImpl::AddGetDataTask(
136148
return context.CancelToken();
137149
}
138150

151+
olp::client::CancellationToken VersionedLayerClientImpl::PrefetchTiles(
152+
PrefetchTilesRequest request, PrefetchTilesResponseCallback callback) {
153+
const int64_t request_key = pending_requests_->GenerateRequestPlaceholder();
154+
auto pending_requests = pending_requests_;
155+
auto request_callback = [=](const PrefetchTilesResponse& response) {
156+
pending_requests->Remove(request_key);
157+
if (callback) {
158+
callback(response);
159+
}
160+
};
161+
162+
request.WithLayerId(layer_id_);
163+
auto token = prefetch_provider_->PrefetchTiles(request, request_callback);
164+
pending_requests->Insert(token, request_key);
165+
return token;
166+
}
167+
139168
} // namespace read
140169
} // namespace dataservice
141170
} // namespace olp

olp-cpp-sdk-dataservice-read/src/VersionedLayerClientImpl.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <olp/core/client/OlpClientSettings.h>
3131
#include <olp/dataservice/read/DataRequest.h>
3232
#include <olp/dataservice/read/PartitionsRequest.h>
33+
#include <olp/dataservice/read/PrefetchTileResult.h>
34+
#include <olp/dataservice/read/PrefetchTilesRequest.h>
3335
#include <olp/dataservice/read/model/Data.h>
3436
#include <olp/dataservice/read/model/Partitions.h>
3537

@@ -43,6 +45,8 @@ namespace repository {
4345
class PartitionsRepository;
4446
}
4547

48+
class PrefetchTilesProvider;
49+
4650
class VersionedLayerClientImpl {
4751
public:
4852
/// DataResult alias
@@ -57,6 +61,15 @@ class VersionedLayerClientImpl {
5761
client::ApiResponse<PartitionsResult, client::ApiError>;
5862
using PartitionsCallback = std::function<void(PartitionsResponse response)>;
5963

64+
/// PrefetchTileResult alias
65+
using PrefetchTilesResult = std::vector<std::shared_ptr<PrefetchTileResult>>;
66+
/// Alias for the response to the \c PrefetchTileRequest
67+
using PrefetchTilesResponse =
68+
client::ApiResponse<PrefetchTilesResult, client::ApiError>;
69+
/// Alias for the callback to the results of PrefetchTilesResponse
70+
using PrefetchTilesResponseCallback =
71+
std::function<void(const PrefetchTilesResponse& response)>;
72+
6073
VersionedLayerClientImpl(olp::client::HRN catalog, std::string layer_id,
6174
olp::client::OlpClientSettings client_settings);
6275

@@ -68,6 +81,9 @@ class VersionedLayerClientImpl {
6881
virtual client::CancellationToken GetPartitions(
6982
PartitionsRequest partitions_request, PartitionsCallback callback) const;
7083

84+
virtual olp::client::CancellationToken PrefetchTiles(
85+
PrefetchTilesRequest request, PrefetchTilesResponseCallback callback);
86+
7187
private:
7288
olp::client::CancellationToken AddGetDataTask(DataRequest data_request,
7389
Callback callback) const;
@@ -79,6 +95,7 @@ class VersionedLayerClientImpl {
7995
std::shared_ptr<thread::TaskScheduler> task_scheduler_;
8096
std::shared_ptr<PendingRequests> pending_requests_;
8197
std::shared_ptr<repository::PartitionsRepository> partition_repo_;
98+
std::shared_ptr<PrefetchTilesProvider> prefetch_provider_;
8299
};
83100

84101
} // namespace read

0 commit comments

Comments
 (0)