1#include "duckdb/storage/index.hpp"
2#include "duckdb/execution/expression_executor.hpp"
3#include "duckdb/planner/expression_iterator.hpp"
4#include "duckdb/planner/expression/bound_columnref_expression.hpp"
5#include "duckdb/planner/expression/bound_reference_expression.hpp"
6#include "duckdb/storage/table/append_state.hpp"
7
8using namespace duckdb;
9using namespace std;
10
11Index::Index(IndexType type, vector<column_t> column_ids,
12 vector<unique_ptr<Expression>> unbound_expressions)
13 : type(type), column_ids(column_ids), unbound_expressions(move(unbound_expressions)) {
14 for (auto &expr : this->unbound_expressions) {
15 types.push_back(expr->return_type);
16 bound_expressions.push_back(BindExpression(expr->Copy()));
17 }
18 for (auto &bound_expr : bound_expressions) {
19 executor.AddExpression(*bound_expr);
20 }
21 for (auto column_id : column_ids) {
22 column_id_set.insert(column_id);
23 }
24}
25
26void Index::InitializeLock(IndexLock &state) {
27 state.index_lock = unique_lock<mutex>(lock);
28}
29
30bool Index::Append(DataChunk &entries, Vector &row_identifiers) {
31 IndexLock state;
32 InitializeLock(state);
33 return Append(state, entries, row_identifiers);
34}
35
36void Index::Delete(DataChunk &entries, Vector &row_identifiers) {
37 IndexLock state;
38 InitializeLock(state);
39 Delete(state, entries, row_identifiers);
40}
41
42void Index::ExecuteExpressions(DataChunk &input, DataChunk &result) {
43 executor.Execute(input, result);
44}
45
46unique_ptr<Expression> Index::BindExpression(unique_ptr<Expression> expr) {
47 if (expr->type == ExpressionType::BOUND_COLUMN_REF) {
48 auto &bound_colref = (BoundColumnRefExpression &)*expr;
49 return make_unique<BoundReferenceExpression>(expr->return_type, column_ids[bound_colref.binding.column_index]);
50 }
51 ExpressionIterator::EnumerateChildren(*expr,
52 [&](unique_ptr<Expression> expr) { return BindExpression(move(expr)); });
53 return expr;
54}
55
56bool Index::IndexIsUpdated(vector<column_t> &column_ids) {
57 for (auto &column : column_ids) {
58 if (column_id_set.find(column) != column_id_set.end()) {
59 return true;
60 }
61 }
62 return false;
63}
64