@@ -187,10 +187,10 @@ void CppMetricsServiceHandler::getPagedCppAstNodeMetricsForPath(
187187 std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
188188 const std::string& path_,
189189 const std::int32_t pageSize_,
190- const std:: int32_t pageNumber_ )
190+ const core::AstNodeId& previousId_ )
191191{
192- std::vector<model::CppAstNodeId> paged_nodes = pageMetrics<model::CppAstNodeId, model::CppAstNodeMetricsDistinctView> (
193- path_, pageSize_, pageNumber_ );
192+ std::vector<model::CppAstNodeId> paged_nodes = pageAstNodeMetrics (
193+ path_, pageSize_, previousId_. empty () ? 0 : std::stoull (previousId_) );
194194
195195 queryCppAstNodeMetricsForPath (_return,
196196 CppNodeMetricsQuery::CppAstNodeMetrics::astNodeId.in_range (paged_nodes.begin (), paged_nodes.end ()));
@@ -241,10 +241,10 @@ void CppMetricsServiceHandler::getPagedCppAstNodeMetricsDetailedForPath(
241241 std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
242242 const std::string& path_,
243243 const std::int32_t pageSize_,
244- const std:: int32_t pageNumber_ )
244+ const core::AstNodeId& previousId_ )
245245{
246- std::vector<model::CppAstNodeId> paged_nodes = pageMetrics<model::CppAstNodeId, model::CppAstNodeMetricsDistinctView> (
247- path_, pageSize_, pageNumber_ );
246+ std::vector<model::CppAstNodeId> paged_nodes = pageAstNodeMetrics (
247+ path_, pageSize_, previousId_. empty () ? 0 : std::stoull (previousId_) );
248248
249249 queryCppAstNodeMetricsDetailedForPath (_return,
250250 CppNodeMetricsQuery::CppAstNodeMetrics::astNodeId.in_range (paged_nodes.begin (), paged_nodes.end ()));
@@ -290,16 +290,16 @@ void CppMetricsServiceHandler::getPagedCppFileMetricsForPath(
290290 std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
291291 const std::string& path_,
292292 const std::int32_t pageSize_,
293- const std:: int32_t pageNumber_ )
293+ const core::FileId& previousId_ )
294294{
295- std::vector<model::FileId> paged_files = pageMetrics<model::FileId, model::CppModuleMetricsDistinctView>(path_, pageSize_, pageNumber_);
295+ std::vector<model::FileId> paged_files = pageFileMetrics (
296+ path_, pageSize_, previousId_.empty () ? 0 : std::stoull (previousId_));
296297 queryCppFileMetricsForPath (_return,
297298 CppModuleMetricsQuery::CppFileMetrics::file.in_range (paged_files.begin (), paged_files.end ()));
298299}
299300
300- std::string CppMetricsServiceHandler::getPagingQuery (
301- const std::int32_t pageSize_,
302- const std::int32_t pageNumber_)
301+ std::string CppMetricsServiceHandler::getLimitQuery (
302+ const std::int32_t pageSize_)
303303{
304304 if (pageSize_ <= 0 )
305305 {
@@ -308,15 +308,65 @@ std::string CppMetricsServiceHandler::getPagingQuery(
308308 throw ex;
309309 }
310310
311- if (pageNumber_ <= 0 )
312- {
313- core::InvalidInput ex;
314- ex.__set_msg (" Invalid page number: " + std::to_string (pageNumber_));
315- throw ex;
316- }
311+ return " LIMIT " + std::to_string (pageSize_);
312+ }
313+
314+ std::vector<model::CppAstNodeId> CppMetricsServiceHandler::pageAstNodeMetrics (
315+ const std::string& path_,
316+ const std::int32_t pageSize_,
317+ const model::CppAstNodeId previousId_)
318+ {
319+ typedef odb::query<model::CppAstNodeMetricsDistinctView> MetricQuery;
320+ typedef odb::result<model::CppAstNodeMetricsDistinctView> MetricResult;
321+
322+ return _transaction ([&, this ](){
323+ MetricQuery condition = MetricQuery::File::path.like (path_ + ' %' );
324+ if (previousId_ != 0 ) {
325+ condition = condition && (MetricQuery::CppAstNodeMetrics::astNodeId > previousId_);
326+ }
327+
328+ MetricResult paged_nodes = _db->query <model::CppAstNodeMetricsDistinctView>(
329+ condition +
330+ (" ORDER BY" + odb::query<model::CppAstNodeMetrics>::astNodeId) +
331+ getLimitQuery (pageSize_));
317332
318- const std::int32_t offset = (pageNumber_ - 1 ) * pageSize_;
319- return " LIMIT " + std::to_string (pageSize_) + " OFFSET " + std::to_string (offset);
333+ std::vector<model::CppAstNodeId> paged_ids (paged_nodes.size ());
334+ std::transform (paged_nodes.begin (), paged_nodes.end (), paged_ids.begin (),
335+ [](const model::CppAstNodeMetricsDistinctView& e){
336+ return e.astNodeId ;
337+ });
338+
339+ return paged_ids;
340+ });
341+ }
342+
343+ std::vector<model::FileId> CppMetricsServiceHandler::pageFileMetrics (
344+ const std::string& path_,
345+ const std::int32_t pageSize_,
346+ const model::FileId previousId_)
347+ {
348+ typedef odb::query<model::CppModuleMetricsDistinctView> MetricQuery;
349+ typedef odb::result<model::CppModuleMetricsDistinctView> MetricResult;
350+
351+ return _transaction ([&, this ](){
352+ MetricQuery condition = MetricQuery::File::path.like (path_ + ' %' );
353+ if (previousId_ != 0 ) {
354+ condition = condition && (MetricQuery::CppFileMetrics::file > previousId_);
355+ }
356+
357+ MetricResult paged_nodes = _db->query <model::CppModuleMetricsDistinctView>(
358+ condition +
359+ (" ORDER BY" + odb::query<model::CppFileMetrics>::file) +
360+ getLimitQuery (pageSize_));
361+
362+ std::vector<model::FileId> paged_ids (paged_nodes.size ());
363+ std::transform (paged_nodes.begin (), paged_nodes.end (), paged_ids.begin (),
364+ [](const model::CppModuleMetricsDistinctView& e){
365+ return e.fileId ;
366+ });
367+
368+ return paged_ids;
369+ });
320370}
321371
322372} // cppmetrics
0 commit comments