From 8585eea4b773a8e5aa65e762e8200f3282390c49 Mon Sep 17 00:00:00 2001 From: George Powley Date: Mon, 2 Oct 2023 15:12:06 -0400 Subject: [PATCH] [c++] Add async query support --- libtiledbsoma/src/soma/managed_query.cc | 16 ++++++++++------ libtiledbsoma/src/soma/managed_query.h | 4 ++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libtiledbsoma/src/soma/managed_query.cc b/libtiledbsoma/src/soma/managed_query.cc index c3f9c5fe1e..4e1a0aea7a 100644 --- a/libtiledbsoma/src/soma/managed_query.cc +++ b/libtiledbsoma/src/soma/managed_query.cc @@ -158,7 +158,12 @@ void ManagedQuery::submit_read() { // Do not submit if the query contains only empty ranges if (!is_empty_query()) { - query_->submit(); + // Submit query in a separate thread, so we can return immediately + query_future_ = std::async(std::launch::async, [&]() { + LOG_DEBUG("[ManagedQuery] submit thread start"); + query_->submit(); + LOG_DEBUG("[ManagedQuery] submit thread done"); + }); } query_submitted_ = true; } @@ -179,11 +184,10 @@ std::shared_ptr ManagedQuery::results() { } query_submitted_ = false; - // Poll status until query is not INPROGRESS - Query::Status status; - do { - status = query_->query_status(); - } while (status == Query::Status::INPROGRESS); + // Wait for query to complete + LOG_DEBUG(fmt::format("[ManagedQuery] [{}] Waiting for query", name_)); + query_future_.wait(); + auto status = query_->query_status(); LOG_DEBUG(fmt::format( "[ManagedQuery] [{}] Query status = {}", name_, (int)status)); diff --git a/libtiledbsoma/src/soma/managed_query.h b/libtiledbsoma/src/soma/managed_query.h index f6a1d86a1f..6ba8e32d7f 100644 --- a/libtiledbsoma/src/soma/managed_query.h +++ b/libtiledbsoma/src/soma/managed_query.h @@ -33,6 +33,7 @@ #ifndef MANAGED_QUERY_H #define MANAGED_QUERY_H +#include #include // for windows: error C2039: 'runtime_error': is not a member of 'std' #include @@ -467,6 +468,9 @@ class ManagedQuery { // True if the query has been submitted and the results have not been read bool query_submitted_ = false; + + // Future for asyncronous query + std::future query_future_; }; }; // namespace tiledbsoma