Skip to content

Commit

Permalink
[c++] Add async query support
Browse files Browse the repository at this point in the history
  • Loading branch information
gspowley committed Oct 2, 2023
1 parent 649bf14 commit 8585eea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 10 additions & 6 deletions libtiledbsoma/src/soma/managed_query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -179,11 +184,10 @@ std::shared_ptr<ArrayBuffers> 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));
Expand Down
4 changes: 4 additions & 0 deletions libtiledbsoma/src/soma/managed_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#ifndef MANAGED_QUERY_H
#define MANAGED_QUERY_H

#include <future>
#include <stdexcept> // for windows: error C2039: 'runtime_error': is not a member of 'std'
#include <unordered_set>

Expand Down Expand Up @@ -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<void> query_future_;
};

}; // namespace tiledbsoma
Expand Down

0 comments on commit 8585eea

Please sign in to comment.