Rewrite code to have IndexStats in methods instead of having it separated

This commit is contained in:
Josip Mrden
2023-06-20 11:35:39 +02:00
parent 58867362d6
commit 48138bb52f
10 changed files with 59 additions and 54 deletions

View File

@@ -430,12 +430,12 @@ class DbAccessor final {
return accessor_->LabelPropertyIndexExists(label, prop);
}
std::optional<storage::LabelIndexStats> GetLabelIndexStats(const storage::LabelId &label) const {
return accessor_->GetLabelIndexStats(label);
std::optional<storage::LabelIndexStats> GetIndexStats(const storage::LabelId &label) const {
return accessor_->GetIndexStats(label);
}
std::optional<storage::IndexStats> GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const {
std::optional<storage::LabelPropertyIndexStats> GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const {
return accessor_->GetIndexStats(label, property);
}
@@ -451,12 +451,12 @@ class DbAccessor final {
return accessor_->DeleteIndexStatsForLabels(labels);
}
void SetLabelIndexStats(const storage::LabelId &label, const storage::LabelIndexStats &stats) {
accessor_->SetLabelIndexStats(label, stats);
void SetIndexStats(const storage::LabelId &label, const storage::LabelIndexStats &stats) {
accessor_->SetIndexStats(label, stats);
}
void SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property,
const storage::IndexStats &stats) {
const storage::LabelPropertyIndexStats &stats) {
accessor_->SetIndexStats(label, property, stats);
}

View File

@@ -1558,7 +1558,7 @@ std::vector<std::vector<TypedValue>> AnalyzeGraphQueryHandler::AnalyzeGraphCreat
});
auto average_degree = (double)total_degree / no_vertices;
execution_db_accessor->SetLabelIndexStats(
execution_db_accessor->SetIndexStats(
index_info, storage::LabelIndexStats{.count = no_vertices, .avg_degree = average_degree});
});
@@ -1594,10 +1594,10 @@ std::vector<std::vector<TypedValue>> AnalyzeGraphQueryHandler::AnalyzeGraphCreat
});
double average_degree = (double)vertex_degree_counter[label_property] / count_property_value;
execution_db_accessor->SetIndexStats(label_property.first, label_property.second,
storage::IndexStats{.count = count_property_value,
.statistic = chi_squared_stat,
.avg_group_size = avg_group_size,
.avg_degree = average_degree});
storage::LabelPropertyIndexStats{.count = count_property_value,
.statistic = chi_squared_stat,
.avg_group_size = avg_group_size,
.avg_degree = average_degree});
// Save result
result.emplace_back(execution_db_accessor->LabelToName(label_property.first));
result.emplace_back(execution_db_accessor->PropertyToName(label_property.second));

View File

@@ -106,7 +106,7 @@ class CostEstimator : public HierarchicalLogicalOperatorVisitor {
}
bool PostVisit(ScanAllByLabel &scan_all_by_label) override {
auto index_stats = db_accessor_->GetLabelIndexStats(scan_all_by_label.label_);
auto index_stats = db_accessor_->GetIndexStats(scan_all_by_label.label_);
if (index_stats) {
scope_.symbol_stats[scan_all_by_label.output_symbol_.name()] = SymbolStatistics{
.name = scan_all_by_label.output_symbol_.name(),

View File

@@ -525,7 +525,7 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor {
// FilterInfo with PropertyFilter.
FilterInfo filter;
int64_t vertex_count;
std::optional<storage::IndexStats> index_stats;
std::optional<storage::LabelPropertyIndexStats> index_stats;
};
bool DefaultPreVisit() override { throw utils::NotYetImplemented("optimizing index lookup"); }
@@ -592,8 +592,8 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor {
* @param vertex_count: New index's number of vertices.
* @return -1 if the new index is better, 0 if they are equal and 1 if the existing one is better.
*/
auto compare_indices = [](std::optional<LabelPropertyIndex> &found, std::optional<storage::IndexStats> &new_stats,
int vertex_count) {
auto compare_indices = [](std::optional<LabelPropertyIndex> &found,
std::optional<storage::LabelPropertyIndexStats> &new_stats, int vertex_count) {
if (!new_stats.has_value()) {
return 0;
}
@@ -630,7 +630,8 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor {
};
int64_t vertex_count = db_->VerticesCount(GetLabel(label), GetProperty(property));
std::optional<storage::IndexStats> new_stats = db_->GetIndexStats(GetLabel(label), GetProperty(property));
std::optional<storage::LabelPropertyIndexStats> new_stats =
db_->GetIndexStats(GetLabel(label), GetProperty(property));
// Conditions, from more to less important:
// the index with 10x less vertices is better.
@@ -769,7 +770,7 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor {
std::vector<Expression *> removed_expressions;
filters_.EraseLabelFilter(node_symbol, label, &removed_expressions);
filter_exprs_for_removal_.insert(removed_expressions.begin(), removed_expressions.end());
auto index_stats = db_->GetLabelIndexStats(GetLabel(label));
auto index_stats = db_->GetIndexStats(GetLabel(label));
if (index_stats.has_value()) {
scope_.symbol_stats[node_symbol.name()] = SymbolStatistics{.name = node_symbol.name(),
.cardinality = index_stats.value().count,

View File

@@ -78,12 +78,12 @@ class VertexCountCache {
return db_->LabelPropertyIndexExists(label, property);
}
std::optional<storage::LabelIndexStats> GetLabelIndexStats(const storage::LabelId &label) const {
return db_->GetLabelIndexStats(label);
std::optional<storage::LabelIndexStats> GetIndexStats(const storage::LabelId &label) const {
return db_->GetIndexStats(label);
}
std::optional<storage::IndexStats> GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const {
std::optional<storage::LabelPropertyIndexStats> GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const {
return db_->GetIndexStats(label, property);
}

View File

@@ -478,7 +478,9 @@ void LabelIndex::RunGC() {
}
}
void LabelIndex::SetIndexStats(const storage::LabelId &label, const LabelIndexStats &stats) { stats_[label] = stats; }
void LabelIndex::SetIndexStats(const storage::LabelId &label, const storage::LabelIndexStats &stats) {
stats_[label] = stats;
}
std::optional<LabelIndexStats> LabelIndex::GetIndexStats(const storage::LabelId &label) const {
if (auto it = stats_.find(label); it != stats_.end()) {
@@ -487,6 +489,15 @@ std::optional<LabelIndexStats> LabelIndex::GetIndexStats(const storage::LabelId
return {};
}
std::vector<LabelId> LabelIndex::ClearIndexStats() {
std::vector<LabelId> deleted_indexes;
deleted_indexes.reserve(stats_.size());
std::transform(stats_.begin(), stats_.end(), std::back_inserter(deleted_indexes),
[](const auto &elem) { return elem.first; });
stats_.clear();
return deleted_indexes;
}
bool LabelPropertyIndex::Entry::operator<(const Entry &rhs) {
if (value < rhs.value) {
return true;
@@ -837,15 +848,6 @@ std::vector<std::pair<LabelId, PropertyId>> LabelPropertyIndex::DeleteIndexStats
return deleted_indexes;
}
std::vector<LabelId> LabelIndex::ClearIndexStats() {
std::vector<LabelId> deleted_indexes;
deleted_indexes.reserve(stats_.size());
std::transform(stats_.begin(), stats_.end(), std::back_inserter(deleted_indexes),
[](const auto &elem) { return elem.first; });
stats_.clear();
return deleted_indexes;
}
std::vector<std::pair<LabelId, PropertyId>> LabelPropertyIndex::ClearIndexStats() {
std::vector<std::pair<LabelId, PropertyId>> deleted_indexes;
deleted_indexes.reserve(stats_.size());
@@ -856,12 +858,12 @@ std::vector<std::pair<LabelId, PropertyId>> LabelPropertyIndex::ClearIndexStats(
}
void LabelPropertyIndex::SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property,
const IndexStats &stats) {
const storage::LabelPropertyIndexStats &stats) {
stats_[{label, property}] = stats;
}
std::optional<IndexStats> LabelPropertyIndex::GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const {
std::optional<storage::LabelPropertyIndexStats> LabelPropertyIndex::GetIndexStats(
const storage::LabelId &label, const storage::PropertyId &property) const {
if (auto it = stats_.find({label, property}); it != stats_.end()) {
return it->second;
}

View File

@@ -147,7 +147,7 @@ class LabelIndex {
Config::Items config_;
};
struct IndexStats {
struct LabelPropertyIndexStats {
int64_t count;
double statistic, avg_group_size, avg_degree;
};
@@ -264,10 +264,10 @@ class LabelPropertyIndex {
std::vector<std::pair<LabelId, PropertyId>> DeleteIndexStatsForLabel(const storage::LabelId &label);
void SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property,
const storage::IndexStats &stats);
const storage::LabelPropertyIndexStats &stats);
std::optional<storage::IndexStats> GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const;
std::optional<storage::LabelPropertyIndexStats> GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const;
void Clear() { index_.clear(); }
@@ -275,7 +275,7 @@ class LabelPropertyIndex {
private:
std::map<std::pair<LabelId, PropertyId>, utils::SkipList<Entry>> index_;
std::map<std::pair<LabelId, PropertyId>, storage::IndexStats> stats_;
std::map<std::pair<LabelId, PropertyId>, storage::LabelPropertyIndexStats> stats_;
Indices *indices_;
Constraints *constraints_;
Config::Items config_;

View File

@@ -268,12 +268,12 @@ class Storage final {
return storage_->indices_.label_property_index.ApproximateVertexCount(label, property, lower, upper);
}
std::optional<storage::LabelIndexStats> GetLabelIndexStats(const storage::LabelId &label) const {
std::optional<storage::LabelIndexStats> GetIndexStats(const storage::LabelId &label) const {
return storage_->indices_.label_index.GetIndexStats(label);
}
std::optional<storage::IndexStats> GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const {
std::optional<storage::LabelPropertyIndexStats> GetIndexStats(const storage::LabelId &label,
const storage::PropertyId &property) const {
return storage_->indices_.label_property_index.GetIndexStats(label, property);
}
@@ -294,11 +294,12 @@ class Storage final {
return deleted_indexes;
}
void SetLabelIndexStats(const storage::LabelId &label, const LabelIndexStats &stats) {
void SetIndexStats(const storage::LabelId &label, const LabelIndexStats &stats) {
storage_->indices_.label_index.SetIndexStats(label, stats);
}
void SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property, const IndexStats &stats) {
void SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property,
const LabelPropertyIndexStats &stats) {
storage_->indices_.label_property_index.SetIndexStats(label, property, stats);
}

View File

@@ -27,6 +27,7 @@
#include "query/plan/planner.hpp"
#include "query/plan/pretty_print.hpp"
#include "query/typed_value.hpp"
#include "storage/v2/indices.hpp"
#include "storage/v2/property_value.hpp"
#include "utils/string.hpp"
@@ -213,13 +214,13 @@ class InteractiveDbAccessor {
return label_property_index_.at(key);
}
std::optional<memgraph::storage::IndexStats> GetIndexStats(memgraph::storage::LabelId label,
memgraph::storage::PropertyId property) const {
return dba_->GetIndexStats(label, property);
std::optional<memgraph::storage::LabelIndexStats> GetIndexStats(const memgraph::storage::LabelId label) const {
return dba_->GetIndexStats(label);
}
std::optional<memgraph::storage::LabelIndexStats> GetLabelIndexStats(memgraph::storage::LabelId label) const {
return dba_->GetLabelIndexStats(label);
std::optional<memgraph::storage::LabelPropertyIndexStats> GetIndexStats(
const memgraph::storage::LabelId label, const memgraph::storage::PropertyId property) const {
return dba_->GetIndexStats(label, property);
}
// Save the cached vertex counts to a stream.

View File

@@ -500,12 +500,12 @@ class FakeDbAccessor {
return false;
}
std::optional<memgraph::storage::IndexStats> GetIndexStats(memgraph::storage::LabelId label,
memgraph::storage::PropertyId property) const {
return memgraph::storage::IndexStats{.statistic = 0, .avg_group_size = 1}; // unique id
std::optional<memgraph::storage::LabelPropertyIndexStats> GetIndexStats(
const memgraph::storage::LabelId label, const memgraph::storage::PropertyId property) const {
return memgraph::storage::LabelPropertyIndexStats{.statistic = 0, .avg_group_size = 1}; // unique id
}
std::optional<memgraph::storage::LabelIndexStats> GetLabelIndexStats(memgraph::storage::LabelId label) const {
std::optional<memgraph::storage::LabelIndexStats> GetIndexStats(const memgraph::storage::LabelId label) const {
return memgraph::storage::LabelIndexStats{.count = 0, .avg_degree = 0}; // unique id
}