Skip to content

Commit

Permalink
Create the optimizer framework
Browse files Browse the repository at this point in the history
  • Loading branch information
msm-code committed Oct 1, 2024
1 parent 9f2a149 commit ffbb4b1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions libursa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ add_library(
QueryParser.h
QueryResult.cpp
QueryResult.h
QueryOptimizer.cpp
QueryOptimizer.h
RawFile.cpp
RawFile.h
Responses.cpp
Expand Down
4 changes: 3 additions & 1 deletion libursa/OnDiskDataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "DatabaseName.h"
#include "Json.h"
#include "Query.h"
#include "QueryOptimizer.h"
#include "spdlog/fmt/ostr.h"
#include "spdlog/spdlog.h"

Expand Down Expand Up @@ -91,7 +92,8 @@ void OnDiskDataset::execute(const Query &query, ResultWriter *out,
for (const auto &ndx : get_indexes()) {
types_to_query.emplace(ndx.index_type());
}
const Query plan = query.plan(types_to_query);
Query plan = query.plan(types_to_query);
plan = q_optimize(std::move(plan));
spdlog::debug("PLAN: {}", plan);

QueryResult result = this->query(plan, counters);
Expand Down
4 changes: 2 additions & 2 deletions libursa/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class PrimitiveQuery {
PrimitiveQuery(IndexType itype, TriGram trigram)
: itype(itype), trigram(trigram) {}

const IndexType itype;
const TriGram trigram;
IndexType itype;
TriGram trigram;

// We want to use PrimitiveQuery in STL containers, and this means they
// must be comparable using <. Specific order doesn't matter.
Expand Down
14 changes: 14 additions & 0 deletions libursa/QueryOptimizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "QueryOptimizer.h"

#include <vector>

Query q_optimize(Query &&q) {
if (q.get_type() == QueryType::PRIMITIVE) {
// Nothing to improve here.
return std::move(q);
}

// Optimization passes will be added here later.

return std::move(q);
}
8 changes: 8 additions & 0 deletions libursa/QueryOptimizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "Query.h"

// Optimizes a query, and returns the optimized version.
// Optimizations try to simplify the expression in various ways to make the
// execution faster - for example by enabling short-circuiting in some places.
Query q_optimize(Query &&query);
5 changes: 3 additions & 2 deletions libursa/Version.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ constexpr std::string_view ursadb_format_version = "1.5.0";

// Project version.
// Consider updating the version tag when doing PRs.
constexpr std::string_view ursadb_version_string =
"@PROJECT_VERSION@+debuglogs";
// clang-format off
constexpr std::string_view ursadb_version_string = "@PROJECT_VERSION@+opt0";
// clang-format on

0 comments on commit ffbb4b1

Please sign in to comment.