1
0
Fork 0

moved index nodes to their own files

This commit is contained in:
Jan Steemann 2015-09-30 15:13:49 +02:00
parent b5cf8ce942
commit d99f7a498d
14 changed files with 1047 additions and 716 deletions

View File

@ -30,20 +30,21 @@
#include "Basics/Common.h"
#include "Aql/ClusterNodes.h"
#include "Aql/Collection.h"
#include "Aql/ExecutionBlock.h"
#include "Aql/ExecutionNode.h"
#include "Aql/ExecutionStats.h"
#include "Cluster/ClusterMethods.h"
#include "Utils/AqlTransaction.h"
struct TRI_json_t;
namespace triagens {
namespace arango {
class AqlTransaction;
struct ClusterCommResult;
}
namespace aql {
class AqlItemBlock;
struct Collection;
class ExecutionEngine;
// -----------------------------------------------------------------------------

View File

@ -30,16 +30,10 @@
#include "Basics/Common.h"
#include "Aql/Ast.h"
//#include "Aql/Collection.h"
//#include "Aql/Condition.h"
//#include "Aql/Expression.h"
#include "Aql/ExecutionNode.h"
#include "Aql/Query.h"
//#include "Aql/types.h"
#include "Aql/Variable.h"
//#include "Aql/WalkerWorker.h"
#include "Basics/JsonHelper.h"
//#include "lib/Basics/json-utilities.h"
#include "VocBase/voc-types.h"
#include "VocBase/vocbase.h"

View File

@ -27,6 +27,7 @@
#include "Aql/ConditionFinder.h"
#include "Aql/ExecutionPlan.h"
#include "Aql/IndexNode.h"
#include "Aql/SortCondition.h"
using namespace triagens::aql;

View File

@ -26,11 +26,13 @@
////////////////////////////////////////////////////////////////////////////////
#include "Aql/ExecutionNode.h"
#include "Aql/Ast.h"
#include "Aql/ClusterNodes.h"
#include "Aql/Collection.h"
#include "Aql/ExecutionPlan.h"
#include "Aql/IndexNode.h"
#include "Aql/IndexRangeNode.h"
#include "Aql/WalkerWorker.h"
#include "Aql/Ast.h"
#include "Basics/StringBuffer.h"
using namespace std;
@ -1612,342 +1614,6 @@ void IndexNode::getVariablesUsedHere (std::unordered_set<Variable const*>& vars)
// This is not yet clear how the variables will be stored
}
// -----------------------------------------------------------------------------
// --SECTION-- methods of IndexRangeNode
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief toJson, for IndexRangeNode
////////////////////////////////////////////////////////////////////////////////
void IndexRangeNode::toJsonHelper (triagens::basics::Json& nodes,
TRI_memory_zone_t* zone,
bool verbose) const {
triagens::basics::Json json(ExecutionNode::toJsonHelperGeneric(nodes, zone, verbose));
// call base class method
if (json.isEmpty()) {
return;
}
// put together the range info . . .
triagens::basics::Json ranges(triagens::basics::Json::Array, _ranges.size());
for (auto const& x : _ranges) {
triagens::basics::Json range(triagens::basics::Json::Array, x.size());
for(auto const& y : x) {
range.add(y.toJson());
}
ranges.add(range);
}
// Now put info about vocbase and cid in there
json("database", triagens::basics::Json(_vocbase->_name))
("collection", triagens::basics::Json(_collection->getName()))
("outVariable", _outVariable->toJson())
("ranges", ranges);
json("index", _index->toJson());
json("reverse", triagens::basics::Json(_reverse));
// And add it:
nodes(json);
}
ExecutionNode* IndexRangeNode::clone (ExecutionPlan* plan,
bool withDependencies,
bool withProperties) const {
std::vector<std::vector<RangeInfo>> ranges;
for (size_t i = 0; i < _ranges.size(); i++){
ranges.emplace_back(std::vector<RangeInfo>());
for (auto const& x : _ranges.at(i)) {
ranges.at(i).emplace_back(x);
}
}
auto outVariable = _outVariable;
if (withProperties) {
outVariable = plan->getAst()->variables()->createVariable(outVariable);
}
auto c = new IndexRangeNode(plan, _id, _vocbase, _collection,
outVariable, _index, ranges, _reverse);
cloneHelper(c, plan, withDependencies, withProperties);
return static_cast<ExecutionNode*>(c);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief constructor for IndexRangeNode from Json
////////////////////////////////////////////////////////////////////////////////
IndexRangeNode::IndexRangeNode (ExecutionPlan* plan,
triagens::basics::Json const& json)
: ExecutionNode(plan, json),
_vocbase(plan->getAst()->query()->vocbase()),
_collection(plan->getAst()->query()->collections()->get(JsonHelper::checkAndGetStringValue(json.json(), "collection"))),
_outVariable(varFromJson(plan->getAst(), json, "outVariable")),
_index(nullptr),
_ranges(),
_reverse(false) {
triagens::basics::Json rangeArrayJson(TRI_UNKNOWN_MEM_ZONE, JsonHelper::checkAndGetArrayValue(json.json(), "ranges"));
for (size_t i = 0; i < rangeArrayJson.size(); i++) { //loop over the ranges . . .
_ranges.emplace_back();
triagens::basics::Json rangeJson(rangeArrayJson.at(static_cast<int>(i)));
for (size_t j = 0; j < rangeJson.size(); j++) {
_ranges.at(i).emplace_back(rangeJson.at(static_cast<int>(j)));
}
}
// now the index . . .
// TODO the following could be a constructor method for
// an Index object when these are actually used
auto index = JsonHelper::checkAndGetObjectValue(json.json(), "index");
auto iid = JsonHelper::checkAndGetStringValue(index, "id");
_index = _collection->getIndex(iid);
_reverse = JsonHelper::checkAndGetBooleanValue(json.json(), "reverse");
if (_index == nullptr) {
THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, "index not found");
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief check whether the pattern matches this node's index
////////////////////////////////////////////////////////////////////////////////
ExecutionNode::IndexMatch IndexRangeNode::matchesIndex (IndexMatchVec const& pattern) const {
return CompareIndex(this, _index, pattern);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief the cost of an index range node is a multiple of the cost of
/// its unique dependency
////////////////////////////////////////////////////////////////////////////////
double IndexRangeNode::estimateCost (size_t& nrItems) const {
static double const EqualityReductionFactor = 100.0;
size_t incoming = 0;
double const dependencyCost = _dependencies.at(0)->getCost(incoming);
size_t docCount = _collection->count();
TRI_ASSERT(! _ranges.empty());
if (_index->type == triagens::arango::Index::TRI_IDX_TYPE_PRIMARY_INDEX) {
// always an equality lookup
// selectivity of primary index is always 1
nrItems = incoming * _ranges.size();
return dependencyCost + nrItems;
}
if (_index->type == triagens::arango::Index::TRI_IDX_TYPE_EDGE_INDEX) {
// always an equality lookup
// check if the index can provide a selectivity estimate
if (! estimateItemsWithIndexSelectivity(incoming, nrItems)) {
// use hard-coded heuristic
nrItems = incoming * _ranges.size() * docCount / static_cast<size_t>(EqualityReductionFactor);
}
nrItems = (std::max)(nrItems, static_cast<size_t>(1));
return dependencyCost + nrItems;
}
if (_index->type == triagens::arango::Index::TRI_IDX_TYPE_HASH_INDEX) {
// always an equality lookup
// check if the index can provide a selectivity estimate
if (! estimateItemsWithIndexSelectivity(incoming, nrItems)) {
// use hard-coded heuristic
if (_index->unique) {
nrItems = incoming * _ranges.size();
}
else {
double cost = static_cast<double>(docCount) * incoming * _ranges.size();
// the more attributes are contained in the index, the more specific the lookup will be
for (size_t i = 0; i < _ranges.at(0).size(); ++i) {
cost /= EqualityReductionFactor;
}
nrItems = static_cast<size_t>(cost);
}
}
nrItems = (std::max)(nrItems, static_cast<size_t>(1));
// the more attributes an index matches, the better it is
double matchLengthFactor = _ranges.at(0).size() * 0.01;
// this is to prefer the hash index over skiplists if everything else is equal
return dependencyCost + ((static_cast<double>(nrItems) - matchLengthFactor) * 0.9999995);
}
if (_index->type == triagens::arango::Index::TRI_IDX_TYPE_SKIPLIST_INDEX) {
auto const count = _ranges.at(0).size();
if (count == 0) {
// no ranges? so this is unlimited -> has to be more expensive
nrItems = incoming * docCount;
return dependencyCost + nrItems;
}
if (_index->unique) {
bool allEquality = true;
for (auto const& x : _ranges) {
// check if we are using all indexed attributes in the query
if (x.size() != _index->fields.size()) {
allEquality = false;
break;
}
// check if this is an equality comparison
if (x.empty() || ! x.back().is1ValueRangeInfo()) {
allEquality = false;
break;
}
}
if (allEquality) {
// unique index, all attributes compared using eq (==) operator
nrItems = incoming * _ranges.size();
return dependencyCost + nrItems;
}
}
// build a total cost for the index usage by peeking into all ranges
double totalCost = 0.0;
for (auto const& x : _ranges) {
double cost = static_cast<double>(docCount) * incoming;
for (auto const& y : x) { //only doing the 1-d case so far
if (y.is1ValueRangeInfo()) {
// equality lookup
cost /= EqualityReductionFactor;
continue;
}
bool hasLowerBound = false;
bool hasUpperBound = false;
if (y._lowConst.isDefined() || y._lows.size() > 0) {
hasLowerBound = true;
}
if (y._highConst.isDefined() || y._highs.size() > 0) {
hasUpperBound = true;
}
if (hasLowerBound && hasUpperBound) {
// both lower and upper bounds defined
cost /= 10.0;
}
else if (hasLowerBound || hasUpperBound) {
// either only low or high bound defined
cost /= 2.0;
}
// each bound (const and dynamic) counts!
size_t const numBounds = y._lows.size() +
y._highs.size() +
(y._lowConst.isDefined() ? 1 : 0) +
(y._highConst.isDefined() ? 1 : 0);
for (size_t j = 0; j < numBounds; ++j) {
// each dynamic bound again reduces the cost
cost *= 0.95;
}
}
totalCost += cost;
}
totalCost = static_cast<double>((std::max)(static_cast<size_t>(totalCost), static_cast<size_t>(1)));
nrItems = static_cast<size_t>(totalCost);
return dependencyCost + totalCost;
}
// no index
nrItems = incoming * docCount;
return dependencyCost + nrItems;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, returning a vector
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> IndexRangeNode::getVariablesUsedHere () const {
std::unordered_set<Variable const*> s;
// actual work is done by that method
getVariablesUsedHere(s);
// copy result into vector
std::vector<Variable const*> v;
v.reserve(s.size());
for (auto const& vv : s) {
v.emplace_back(const_cast<Variable*>(vv));
}
return v;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, modifying the set in-place
////////////////////////////////////////////////////////////////////////////////
void IndexRangeNode::getVariablesUsedHere (std::unordered_set<Variable const*>& vars) const {
for (auto const& x : _ranges) {
for (RangeInfo const& y : x) {
for (RangeInfoBound const& z : y._lows) {
AstNode const* a = z.getExpressionAst(_plan->getAst());
Ast::getReferencedVariables(a, vars);
}
for (RangeInfoBound const& z : y._highs) {
AstNode const* a = z.getExpressionAst(_plan->getAst());
Ast::getReferencedVariables(a, vars);
}
}
}
}
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief provide an estimate for the number of items, using the index
/// selectivity info (if present)
////////////////////////////////////////////////////////////////////////////////
bool IndexRangeNode::estimateItemsWithIndexSelectivity (size_t incoming,
size_t& nrItems) const {
// check if the index can provide a selectivity estimate
if (! _index->hasSelectivityEstimate()) {
return false;
}
// use index selectivity estimate
double estimate = _index->selectivityEstimate();
if (estimate <= 0.0) {
// avoid DIV0
return false;
}
nrItems = static_cast<size_t>(incoming * _ranges.size() * (1.0 / estimate));
return true;
}
// -----------------------------------------------------------------------------
// --SECTION-- methods of LimitNode
// -----------------------------------------------------------------------------

View File

@ -34,16 +34,12 @@
#include "Aql/Collection.h"
#include "Aql/Condition.h"
#include "Aql/Expression.h"
#include "Aql/Index.h"
#include "Aql/ModificationOptions.h"
#include "Aql/Query.h"
#include "Aql/RangeInfo.h"
#include "Aql/Range.h"
#include "Aql/types.h"
#include "Aql/Variable.h"
#include "Aql/WalkerWorker.h"
#include "Basics/JsonHelper.h"
#include "lib/Basics/json-utilities.h"
#include "VocBase/voc-types.h"
#include "VocBase/vocbase.h"
@ -53,10 +49,10 @@ namespace triagens {
}
namespace aql {
class Ast;
class ExecutionBlock;
class ExecutionPlan;
struct Index;
class RedundantCalculationsReplacer;
////////////////////////////////////////////////////////////////////////////////
@ -1179,368 +1175,6 @@ namespace triagens {
};
////////////////////////////////////////////////////////////////////////////////
/// @brief class IndexNode
////////////////////////////////////////////////////////////////////////////////
class IndexNode: public ExecutionNode {
friend class ExecutionBlock;
friend class IndexBlock;
public:
IndexNode (ExecutionPlan* plan,
size_t id,
TRI_vocbase_t* vocbase,
Collection const* collection,
Variable const* outVariable,
std::vector<Index const*> indexes,
Condition const* condition)
: ExecutionNode(plan, id),
_vocbase(vocbase),
_collection(collection),
_outVariable(outVariable),
_indexes(indexes),
_condition(condition) {
TRI_ASSERT(_vocbase != nullptr);
TRI_ASSERT(_collection != nullptr);
TRI_ASSERT(_outVariable != nullptr);
TRI_ASSERT(_condition != nullptr);
}
IndexNode (ExecutionPlan*, triagens::basics::Json const& base);
~IndexNode () {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the type of the node
////////////////////////////////////////////////////////////////////////////////
NodeType getType () const override final {
return INDEX;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the database
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* vocbase () const {
return _vocbase;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the collection
////////////////////////////////////////////////////////////////////////////////
Collection const* collection () const {
return _collection;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return out variable
////////////////////////////////////////////////////////////////////////////////
Variable const* outVariable () const {
return _outVariable;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief export to JSON
////////////////////////////////////////////////////////////////////////////////
void toJsonHelper (triagens::basics::Json&,
TRI_memory_zone_t*,
bool) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief clone ExecutionNode recursively
////////////////////////////////////////////////////////////////////////////////
ExecutionNode* clone (ExecutionPlan* plan,
bool withDependencies,
bool withProperties) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesSetHere
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> getVariablesSetHere () const override final {
return std::vector<Variable const*>{ _outVariable };
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, returning a vector
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> getVariablesUsedHere () const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, modifying the set in-place
////////////////////////////////////////////////////////////////////////////////
void getVariablesUsedHere (std::unordered_set<Variable const*>& vars) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief estimateCost
////////////////////////////////////////////////////////////////////////////////
double estimateCost (size_t&) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getIndexes, hand out the indexes used
////////////////////////////////////////////////////////////////////////////////
std::vector<Index const*> getIndexes () const {
return _indexes;
}
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief the database
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* _vocbase;
////////////////////////////////////////////////////////////////////////////////
/// @brief collection
////////////////////////////////////////////////////////////////////////////////
Collection const* _collection;
////////////////////////////////////////////////////////////////////////////////
/// @brief output variable
////////////////////////////////////////////////////////////////////////////////
Variable const* _outVariable;
////////////////////////////////////////////////////////////////////////////////
/// @brief the index
////////////////////////////////////////////////////////////////////////////////
std::vector<Index const*> _indexes;
////////////////////////////////////////////////////////////////////////////////
/// @brief the index(es) condition
////////////////////////////////////////////////////////////////////////////////
Condition const* _condition;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief class IndexRangeNode
////////////////////////////////////////////////////////////////////////////////
class IndexRangeNode: public ExecutionNode {
friend class ExecutionBlock;
friend class IndexRangeBlock;
////////////////////////////////////////////////////////////////////////////////
/// @brief constructor with a vocbase and a collection name
////////////////////////////////////////////////////////////////////////////////
// _ranges must correspond to a prefix of the fields of the index <index>, i.e.
// _ranges.at(i) is a range of values for idx->_fields._buffer[i].
public:
IndexRangeNode (ExecutionPlan* plan,
size_t id,
TRI_vocbase_t* vocbase,
Collection const* collection,
Variable const* outVariable,
Index const* index,
std::vector<std::vector<RangeInfo>> const& ranges,
bool reverse)
: ExecutionNode(plan, id),
_vocbase(vocbase),
_collection(collection),
_outVariable(outVariable),
_index(index),
_ranges(ranges),
_reverse(reverse) {
TRI_ASSERT(_vocbase != nullptr);
TRI_ASSERT(_collection != nullptr);
TRI_ASSERT(_outVariable != nullptr);
TRI_ASSERT(_index != nullptr);
}
IndexRangeNode (ExecutionPlan*, triagens::basics::Json const& base);
~IndexRangeNode () {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the type of the node
////////////////////////////////////////////////////////////////////////////////
NodeType getType () const override final {
return INDEX_RANGE;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the database
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* vocbase () const {
return _vocbase;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the collection
////////////////////////////////////////////////////////////////////////////////
Collection const* collection () const {
return _collection;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return out variable
////////////////////////////////////////////////////////////////////////////////
Variable const* outVariable () const {
return _outVariable;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the ranges
////////////////////////////////////////////////////////////////////////////////
std::vector<std::vector<RangeInfo>> const& ranges () const {
return _ranges;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief export to JSON
////////////////////////////////////////////////////////////////////////////////
void toJsonHelper (triagens::basics::Json&,
TRI_memory_zone_t*,
bool) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief clone ExecutionNode recursively
////////////////////////////////////////////////////////////////////////////////
ExecutionNode* clone (ExecutionPlan* plan,
bool withDependencies,
bool withProperties) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesSetHere
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> getVariablesSetHere () const override final {
return std::vector<Variable const*>{ _outVariable };
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, returning a vector
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> getVariablesUsedHere () const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, modifying the set in-place
////////////////////////////////////////////////////////////////////////////////
void getVariablesUsedHere (std::unordered_set<Variable const*>& vars) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief estimateCost
////////////////////////////////////////////////////////////////////////////////
double estimateCost (size_t&) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief check whether the pattern matches this node's index
////////////////////////////////////////////////////////////////////////////////
IndexMatch matchesIndex (IndexMatchVec const& pattern) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not a reverse index traversal is used
////////////////////////////////////////////////////////////////////////////////
void reverse (bool value) {
_reverse = value;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getIndex, hand out the index used
////////////////////////////////////////////////////////////////////////////////
Index const* getIndex () {
return _index;
}
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief provide an estimate for the number of items, using the index
/// selectivity info (if present)
////////////////////////////////////////////////////////////////////////////////
bool estimateItemsWithIndexSelectivity (size_t,
size_t&) const;
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief the database
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* _vocbase;
////////////////////////////////////////////////////////////////////////////////
/// @brief collection
////////////////////////////////////////////////////////////////////////////////
Collection const* _collection;
////////////////////////////////////////////////////////////////////////////////
/// @brief output variable
////////////////////////////////////////////////////////////////////////////////
Variable const* _outVariable;
////////////////////////////////////////////////////////////////////////////////
/// @brief the index
////////////////////////////////////////////////////////////////////////////////
Index const* _index;
////////////////////////////////////////////////////////////////////////////////
/// @brief the range info
////////////////////////////////////////////////////////////////////////////////
std::vector<std::vector<RangeInfo>> _ranges;
////////////////////////////////////////////////////////////////////////////////
/// @brief use a reverse index scan
////////////////////////////////////////////////////////////////////////////////
bool _reverse;
};
// -----------------------------------------------------------------------------
// --SECTION-- class LimitNode
// -----------------------------------------------------------------------------

View File

@ -32,6 +32,8 @@
#include "Aql/Collection.h"
#include "Aql/ExecutionBlock.h"
#include "Aql/ExecutionNode.h"
#include "Aql/IndexNode.h"
#include "Aql/RangeInfo.h"
#include "Utils/AqlTransaction.h"
#include "VocBase/shaped-json.h"

169
arangod/Aql/IndexNode.cpp Normal file
View File

@ -0,0 +1,169 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Infrastructure for ExecutionPlans
///
/// @file arangod/Aql/ExecutionNode.cpp
///
/// DISCLAIMER
///
/// Copyright 2010-2014 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Max Neunhoeffer
/// @author Copyright 2014, triagens GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "Aql/IndexNode.h"
#include "Aql/ClusterNodes.h"
#include "Aql/Collection.h"
#include "Aql/Condition.h"
#include "Aql/ExecutionPlan.h"
#include "Aql/Ast.h"
using namespace std;
using namespace triagens::basics;
using namespace triagens::aql;
// -----------------------------------------------------------------------------
// --SECTION-- methods of IndexNode
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief toJson, for IndexNode
////////////////////////////////////////////////////////////////////////////////
void IndexNode::toJsonHelper (triagens::basics::Json& nodes,
TRI_memory_zone_t* zone,
bool verbose) const {
triagens::basics::Json json(ExecutionNode::toJsonHelperGeneric(nodes, zone, verbose));
// call base class method
if (json.isEmpty()) {
return;
}
// Now put info about vocbase and cid in there
json("database", triagens::basics::Json(_vocbase->_name))
("collection", triagens::basics::Json(_collection->getName()))
("outVariable", _outVariable->toJson());
triagens::basics::Json indexes(triagens::basics::Json::Array, _indexes.size());
for (auto& index : _indexes) {
indexes.add(index->toJson());
}
json("indexes", indexes);
if (_condition != nullptr) {
json("condition", _condition->toJson(TRI_UNKNOWN_MEM_ZONE));
}
else {
json("condition", triagens::basics::Json(triagens::basics::Json::Object));
}
// And add it:
nodes(json);
}
ExecutionNode* IndexNode::clone (ExecutionPlan* plan,
bool withDependencies,
bool withProperties) const {
auto outVariable = _outVariable;
if (withProperties) {
outVariable = plan->getAst()->variables()->createVariable(outVariable);
}
auto c = new IndexNode(plan, _id, _vocbase, _collection,
outVariable, _indexes, _condition);
cloneHelper(c, plan, withDependencies, withProperties);
return static_cast<ExecutionNode*>(c);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief constructor for IndexNode from Json
////////////////////////////////////////////////////////////////////////////////
IndexNode::IndexNode (ExecutionPlan* plan,
triagens::basics::Json const& json)
: ExecutionNode(plan, json),
_vocbase(plan->getAst()->query()->vocbase()),
_collection(plan->getAst()->query()->collections()->get(JsonHelper::checkAndGetStringValue(json.json(), "collection"))),
_outVariable(varFromJson(plan->getAst(), json, "outVariable")),
_indexes(),
_condition(nullptr) {
auto indexes = JsonHelper::checkAndGetObjectValue(json.json(), "indexes");
TRI_ASSERT(TRI_IsArrayJson(indexes));
size_t length = TRI_LengthArrayJson(indexes);
_indexes.reserve(length);
for (size_t i = 0; i < length; ++i) {
auto iid = JsonHelper::checkAndGetStringValue(TRI_LookupArrayJson(indexes, i), "id");
auto index = _collection->getIndex(iid);
if (index == nullptr) {
THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, "index not found");
}
_indexes.emplace_back(index);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief the cost of an index range node is a multiple of the cost of
/// its unique dependency - TODO
////////////////////////////////////////////////////////////////////////////////
double IndexNode::estimateCost (size_t& nrItems) const {
nrItems = 1; // TODO FIXME
return 1.0;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, returning a vector
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> IndexNode::getVariablesUsedHere () const {
std::unordered_set<Variable const*> s;
// actual work is done by that method
getVariablesUsedHere(s);
// copy result into vector
std::vector<Variable const*> v;
v.reserve(s.size());
for (auto const& vv : s) {
v.emplace_back(const_cast<Variable*>(vv));
}
return v;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, modifying the set in-place - TODO
////////////////////////////////////////////////////////////////////////////////
void IndexNode::getVariablesUsedHere (std::unordered_set<Variable const*>& vars) const {
// This is not yet clear how the variables will be stored
}
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End:

217
arangod/Aql/IndexNode.h Normal file
View File

@ -0,0 +1,217 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief IndexNode
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2014 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Michael Hackstein
/// @author Jan Steemann
/// @author Copyright 2014, triagens GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_AQL_INDEX_NODE_H
#define ARANGODB_AQL_INDEX_NODE_H 1
#include "Basics/Common.h"
#include "Aql/Ast.h"
#include "Aql/ExecutionNode.h"
#include "Aql/RangeInfo.h"
#include "Aql/Range.h"
#include "Aql/types.h"
#include "Aql/Variable.h"
#include "Basics/JsonHelper.h"
#include "VocBase/voc-types.h"
#include "VocBase/vocbase.h"
namespace triagens {
namespace aql {
struct Collection;
class Condition;
class ExecutionBlock;
class ExecutionPlan;
struct Index;
////////////////////////////////////////////////////////////////////////////////
/// @brief class IndexNode
////////////////////////////////////////////////////////////////////////////////
class IndexNode : public ExecutionNode {
friend class ExecutionBlock;
friend class IndexBlock;
public:
IndexNode (ExecutionPlan* plan,
size_t id,
TRI_vocbase_t* vocbase,
Collection const* collection,
Variable const* outVariable,
std::vector<Index const*> indexes,
Condition const* condition)
: ExecutionNode(plan, id),
_vocbase(vocbase),
_collection(collection),
_outVariable(outVariable),
_indexes(indexes),
_condition(condition) {
TRI_ASSERT(_vocbase != nullptr);
TRI_ASSERT(_collection != nullptr);
TRI_ASSERT(_outVariable != nullptr);
TRI_ASSERT(_condition != nullptr);
}
IndexNode (ExecutionPlan*, triagens::basics::Json const& base);
~IndexNode () {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the type of the node
////////////////////////////////////////////////////////////////////////////////
NodeType getType () const override final {
return INDEX;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the database
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* vocbase () const {
return _vocbase;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the collection
////////////////////////////////////////////////////////////////////////////////
Collection const* collection () const {
return _collection;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return out variable
////////////////////////////////////////////////////////////////////////////////
Variable const* outVariable () const {
return _outVariable;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief export to JSON
////////////////////////////////////////////////////////////////////////////////
void toJsonHelper (triagens::basics::Json&,
TRI_memory_zone_t*,
bool) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief clone ExecutionNode recursively
////////////////////////////////////////////////////////////////////////////////
ExecutionNode* clone (ExecutionPlan* plan,
bool withDependencies,
bool withProperties) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesSetHere
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> getVariablesSetHere () const override final {
return std::vector<Variable const*>{ _outVariable };
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, returning a vector
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> getVariablesUsedHere () const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, modifying the set in-place
////////////////////////////////////////////////////////////////////////////////
void getVariablesUsedHere (std::unordered_set<Variable const*>& vars) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief estimateCost
////////////////////////////////////////////////////////////////////////////////
double estimateCost (size_t&) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getIndexes, hand out the indexes used
////////////////////////////////////////////////////////////////////////////////
std::vector<Index const*> getIndexes () const {
return _indexes;
}
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief the database
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* _vocbase;
////////////////////////////////////////////////////////////////////////////////
/// @brief collection
////////////////////////////////////////////////////////////////////////////////
Collection const* _collection;
////////////////////////////////////////////////////////////////////////////////
/// @brief output variable
////////////////////////////////////////////////////////////////////////////////
Variable const* _outVariable;
////////////////////////////////////////////////////////////////////////////////
/// @brief the index
////////////////////////////////////////////////////////////////////////////////
std::vector<Index const*> _indexes;
////////////////////////////////////////////////////////////////////////////////
/// @brief the index(es) condition
////////////////////////////////////////////////////////////////////////////////
Condition const* _condition;
};
} // namespace triagens::aql
} // namespace triagens
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End:

View File

@ -30,7 +30,7 @@
#include "Aql/Collection.h"
#include "Aql/ExecutionBlock.h"
#include "Aql/ExecutionNode.h"
#include "Aql/IndexRangeNode.h"
#include "Indexes/SkiplistIndex.h"
#include "Utils/AqlTransaction.h"
#include "VocBase/shaped-json.h"

View File

@ -0,0 +1,379 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief IndexRangeNode
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2014 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Max Neunhoeffer
/// @author Copyright 2014, triagens GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "Aql/IndexRangeNode.h"
#include "Aql/Collection.h"
#include "Aql/ExecutionPlan.h"
#include "Aql/WalkerWorker.h"
#include "Aql/Ast.h"
#include "Basics/StringBuffer.h"
using namespace std;
using namespace triagens::basics;
using namespace triagens::aql;
// -----------------------------------------------------------------------------
// --SECTION-- methods of IndexRangeNode
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief toJson, for IndexRangeNode
////////////////////////////////////////////////////////////////////////////////
void IndexRangeNode::toJsonHelper (triagens::basics::Json& nodes,
TRI_memory_zone_t* zone,
bool verbose) const {
triagens::basics::Json json(ExecutionNode::toJsonHelperGeneric(nodes, zone, verbose));
// call base class method
if (json.isEmpty()) {
return;
}
// put together the range info . . .
triagens::basics::Json ranges(triagens::basics::Json::Array, _ranges.size());
for (auto const& x : _ranges) {
triagens::basics::Json range(triagens::basics::Json::Array, x.size());
for(auto const& y : x) {
range.add(y.toJson());
}
ranges.add(range);
}
// Now put info about vocbase and cid in there
json("database", triagens::basics::Json(_vocbase->_name))
("collection", triagens::basics::Json(_collection->getName()))
("outVariable", _outVariable->toJson())
("ranges", ranges);
json("index", _index->toJson());
json("reverse", triagens::basics::Json(_reverse));
// And add it:
nodes(json);
}
ExecutionNode* IndexRangeNode::clone (ExecutionPlan* plan,
bool withDependencies,
bool withProperties) const {
std::vector<std::vector<RangeInfo>> ranges;
for (size_t i = 0; i < _ranges.size(); i++){
ranges.emplace_back(std::vector<RangeInfo>());
for (auto const& x : _ranges.at(i)) {
ranges.at(i).emplace_back(x);
}
}
auto outVariable = _outVariable;
if (withProperties) {
outVariable = plan->getAst()->variables()->createVariable(outVariable);
}
auto c = new IndexRangeNode(plan, _id, _vocbase, _collection,
outVariable, _index, ranges, _reverse);
cloneHelper(c, plan, withDependencies, withProperties);
return static_cast<ExecutionNode*>(c);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief constructor for IndexRangeNode from Json
////////////////////////////////////////////////////////////////////////////////
IndexRangeNode::IndexRangeNode (ExecutionPlan* plan,
triagens::basics::Json const& json)
: ExecutionNode(plan, json),
_vocbase(plan->getAst()->query()->vocbase()),
_collection(plan->getAst()->query()->collections()->get(JsonHelper::checkAndGetStringValue(json.json(), "collection"))),
_outVariable(varFromJson(plan->getAst(), json, "outVariable")),
_index(nullptr),
_ranges(),
_reverse(false) {
triagens::basics::Json rangeArrayJson(TRI_UNKNOWN_MEM_ZONE, JsonHelper::checkAndGetArrayValue(json.json(), "ranges"));
for (size_t i = 0; i < rangeArrayJson.size(); i++) { //loop over the ranges . . .
_ranges.emplace_back();
triagens::basics::Json rangeJson(rangeArrayJson.at(static_cast<int>(i)));
for (size_t j = 0; j < rangeJson.size(); j++) {
_ranges.at(i).emplace_back(rangeJson.at(static_cast<int>(j)));
}
}
// now the index . . .
// TODO the following could be a constructor method for
// an Index object when these are actually used
auto index = JsonHelper::checkAndGetObjectValue(json.json(), "index");
auto iid = JsonHelper::checkAndGetStringValue(index, "id");
_index = _collection->getIndex(iid);
_reverse = JsonHelper::checkAndGetBooleanValue(json.json(), "reverse");
if (_index == nullptr) {
THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, "index not found");
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief check whether the pattern matches this node's index
////////////////////////////////////////////////////////////////////////////////
ExecutionNode::IndexMatch IndexRangeNode::matchesIndex (IndexMatchVec const& pattern) const {
return CompareIndex(this, _index, pattern);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief the cost of an index range node is a multiple of the cost of
/// its unique dependency
////////////////////////////////////////////////////////////////////////////////
double IndexRangeNode::estimateCost (size_t& nrItems) const {
static double const EqualityReductionFactor = 100.0;
size_t incoming = 0;
double const dependencyCost = _dependencies.at(0)->getCost(incoming);
size_t docCount = _collection->count();
TRI_ASSERT(! _ranges.empty());
if (_index->type == triagens::arango::Index::TRI_IDX_TYPE_PRIMARY_INDEX) {
// always an equality lookup
// selectivity of primary index is always 1
nrItems = incoming * _ranges.size();
return dependencyCost + nrItems;
}
if (_index->type == triagens::arango::Index::TRI_IDX_TYPE_EDGE_INDEX) {
// always an equality lookup
// check if the index can provide a selectivity estimate
if (! estimateItemsWithIndexSelectivity(incoming, nrItems)) {
// use hard-coded heuristic
nrItems = incoming * _ranges.size() * docCount / static_cast<size_t>(EqualityReductionFactor);
}
nrItems = (std::max)(nrItems, static_cast<size_t>(1));
return dependencyCost + nrItems;
}
if (_index->type == triagens::arango::Index::TRI_IDX_TYPE_HASH_INDEX) {
// always an equality lookup
// check if the index can provide a selectivity estimate
if (! estimateItemsWithIndexSelectivity(incoming, nrItems)) {
// use hard-coded heuristic
if (_index->unique) {
nrItems = incoming * _ranges.size();
}
else {
double cost = static_cast<double>(docCount) * incoming * _ranges.size();
// the more attributes are contained in the index, the more specific the lookup will be
for (size_t i = 0; i < _ranges.at(0).size(); ++i) {
cost /= EqualityReductionFactor;
}
nrItems = static_cast<size_t>(cost);
}
}
nrItems = (std::max)(nrItems, static_cast<size_t>(1));
// the more attributes an index matches, the better it is
double matchLengthFactor = _ranges.at(0).size() * 0.01;
// this is to prefer the hash index over skiplists if everything else is equal
return dependencyCost + ((static_cast<double>(nrItems) - matchLengthFactor) * 0.9999995);
}
if (_index->type == triagens::arango::Index::TRI_IDX_TYPE_SKIPLIST_INDEX) {
auto const count = _ranges.at(0).size();
if (count == 0) {
// no ranges? so this is unlimited -> has to be more expensive
nrItems = incoming * docCount;
return dependencyCost + nrItems;
}
if (_index->unique) {
bool allEquality = true;
for (auto const& x : _ranges) {
// check if we are using all indexed attributes in the query
if (x.size() != _index->fields.size()) {
allEquality = false;
break;
}
// check if this is an equality comparison
if (x.empty() || ! x.back().is1ValueRangeInfo()) {
allEquality = false;
break;
}
}
if (allEquality) {
// unique index, all attributes compared using eq (==) operator
nrItems = incoming * _ranges.size();
return dependencyCost + nrItems;
}
}
// build a total cost for the index usage by peeking into all ranges
double totalCost = 0.0;
for (auto const& x : _ranges) {
double cost = static_cast<double>(docCount) * incoming;
for (auto const& y : x) { //only doing the 1-d case so far
if (y.is1ValueRangeInfo()) {
// equality lookup
cost /= EqualityReductionFactor;
continue;
}
bool hasLowerBound = false;
bool hasUpperBound = false;
if (y._lowConst.isDefined() || y._lows.size() > 0) {
hasLowerBound = true;
}
if (y._highConst.isDefined() || y._highs.size() > 0) {
hasUpperBound = true;
}
if (hasLowerBound && hasUpperBound) {
// both lower and upper bounds defined
cost /= 10.0;
}
else if (hasLowerBound || hasUpperBound) {
// either only low or high bound defined
cost /= 2.0;
}
// each bound (const and dynamic) counts!
size_t const numBounds = y._lows.size() +
y._highs.size() +
(y._lowConst.isDefined() ? 1 : 0) +
(y._highConst.isDefined() ? 1 : 0);
for (size_t j = 0; j < numBounds; ++j) {
// each dynamic bound again reduces the cost
cost *= 0.95;
}
}
totalCost += cost;
}
totalCost = static_cast<double>((std::max)(static_cast<size_t>(totalCost), static_cast<size_t>(1)));
nrItems = static_cast<size_t>(totalCost);
return dependencyCost + totalCost;
}
// no index
nrItems = incoming * docCount;
return dependencyCost + nrItems;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, returning a vector
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> IndexRangeNode::getVariablesUsedHere () const {
std::unordered_set<Variable const*> s;
// actual work is done by that method
getVariablesUsedHere(s);
// copy result into vector
std::vector<Variable const*> v;
v.reserve(s.size());
for (auto const& vv : s) {
v.emplace_back(const_cast<Variable*>(vv));
}
return v;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, modifying the set in-place
////////////////////////////////////////////////////////////////////////////////
void IndexRangeNode::getVariablesUsedHere (std::unordered_set<Variable const*>& vars) const {
for (auto const& x : _ranges) {
for (RangeInfo const& y : x) {
for (RangeInfoBound const& z : y._lows) {
AstNode const* a = z.getExpressionAst(_plan->getAst());
Ast::getReferencedVariables(a, vars);
}
for (RangeInfoBound const& z : y._highs) {
AstNode const* a = z.getExpressionAst(_plan->getAst());
Ast::getReferencedVariables(a, vars);
}
}
}
}
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief provide an estimate for the number of items, using the index
/// selectivity info (if present)
////////////////////////////////////////////////////////////////////////////////
bool IndexRangeNode::estimateItemsWithIndexSelectivity (size_t incoming,
size_t& nrItems) const {
// check if the index can provide a selectivity estimate
if (! _index->hasSelectivityEstimate()) {
return false;
}
// use index selectivity estimate
double estimate = _index->selectivityEstimate();
if (estimate <= 0.0) {
// avoid DIV0
return false;
}
nrItems = static_cast<size_t>(incoming * _ranges.size() * (1.0 / estimate));
return true;
}
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End:

View File

@ -0,0 +1,263 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief IndexRangeNode
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2014 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Max Neunhoeffer
/// @author Copyright 2014, triagens GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_AQL_INDEX_RANGE_NODE_H
#define ARANGODB_AQL_INDEX_RANGE_NODE_H 1
#include "Basics/Common.h"
#include "Aql/Ast.h"
#include "Aql/ExecutionNode.h"
#include "Aql/RangeInfo.h"
#include "Aql/types.h"
#include "Aql/Variable.h"
#include "Basics/JsonHelper.h"
#include "VocBase/voc-types.h"
#include "VocBase/vocbase.h"
namespace triagens {
namespace aql {
struct Collection;
class ExecutionBlock;
class ExecutionPlan;
struct Index;
////////////////////////////////////////////////////////////////////////////////
/// @brief class IndexRangeNode
////////////////////////////////////////////////////////////////////////////////
class IndexRangeNode : public ExecutionNode {
friend class ExecutionBlock;
friend class IndexRangeBlock;
////////////////////////////////////////////////////////////////////////////////
/// @brief constructor with a vocbase and a collection name
////////////////////////////////////////////////////////////////////////////////
// _ranges must correspond to a prefix of the fields of the index <index>, i.e.
// _ranges.at(i) is a range of values for idx->_fields._buffer[i].
public:
IndexRangeNode (ExecutionPlan* plan,
size_t id,
TRI_vocbase_t* vocbase,
Collection const* collection,
Variable const* outVariable,
Index const* index,
std::vector<std::vector<RangeInfo>> const& ranges,
bool reverse)
: ExecutionNode(plan, id),
_vocbase(vocbase),
_collection(collection),
_outVariable(outVariable),
_index(index),
_ranges(ranges),
_reverse(reverse) {
TRI_ASSERT(_vocbase != nullptr);
TRI_ASSERT(_collection != nullptr);
TRI_ASSERT(_outVariable != nullptr);
TRI_ASSERT(_index != nullptr);
}
IndexRangeNode (ExecutionPlan*, triagens::basics::Json const& base);
~IndexRangeNode () {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the type of the node
////////////////////////////////////////////////////////////////////////////////
NodeType getType () const override final {
return INDEX_RANGE;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the database
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* vocbase () const {
return _vocbase;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the collection
////////////////////////////////////////////////////////////////////////////////
Collection const* collection () const {
return _collection;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return out variable
////////////////////////////////////////////////////////////////////////////////
Variable const* outVariable () const {
return _outVariable;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the ranges
////////////////////////////////////////////////////////////////////////////////
std::vector<std::vector<RangeInfo>> const& ranges () const {
return _ranges;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief export to JSON
////////////////////////////////////////////////////////////////////////////////
void toJsonHelper (triagens::basics::Json&,
TRI_memory_zone_t*,
bool) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief clone ExecutionNode recursively
////////////////////////////////////////////////////////////////////////////////
ExecutionNode* clone (ExecutionPlan* plan,
bool withDependencies,
bool withProperties) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesSetHere
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> getVariablesSetHere () const override final {
return std::vector<Variable const*>{ _outVariable };
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, returning a vector
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> getVariablesUsedHere () const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief getVariablesUsedHere, modifying the set in-place
////////////////////////////////////////////////////////////////////////////////
void getVariablesUsedHere (std::unordered_set<Variable const*>& vars) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief estimateCost
////////////////////////////////////////////////////////////////////////////////
double estimateCost (size_t&) const override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief check whether the pattern matches this node's index
////////////////////////////////////////////////////////////////////////////////
IndexMatch matchesIndex (IndexMatchVec const& pattern) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not a reverse index traversal is used
////////////////////////////////////////////////////////////////////////////////
void reverse (bool value) {
_reverse = value;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief getIndex, hand out the index used
////////////////////////////////////////////////////////////////////////////////
Index const* getIndex () {
return _index;
}
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief provide an estimate for the number of items, using the index
/// selectivity info (if present)
////////////////////////////////////////////////////////////////////////////////
bool estimateItemsWithIndexSelectivity (size_t,
size_t&) const;
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief the database
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* _vocbase;
////////////////////////////////////////////////////////////////////////////////
/// @brief collection
////////////////////////////////////////////////////////////////////////////////
Collection const* _collection;
////////////////////////////////////////////////////////////////////////////////
/// @brief output variable
////////////////////////////////////////////////////////////////////////////////
Variable const* _outVariable;
////////////////////////////////////////////////////////////////////////////////
/// @brief the index
////////////////////////////////////////////////////////////////////////////////
Index const* _index;
////////////////////////////////////////////////////////////////////////////////
/// @brief the range info
////////////////////////////////////////////////////////////////////////////////
std::vector<std::vector<RangeInfo>> _ranges;
////////////////////////////////////////////////////////////////////////////////
/// @brief use a reverse index scan
////////////////////////////////////////////////////////////////////////////////
bool _reverse;
};
} // namespace triagens::aql
} // namespace triagens
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End:

View File

@ -32,6 +32,7 @@
#include "Aql/ExecutionEngine.h"
#include "Aql/ExecutionNode.h"
#include "Aql/Function.h"
#include "Aql/IndexRangeNode.h"
#include "Aql/Variable.h"
#include "Aql/types.h"

View File

@ -89,7 +89,9 @@ add_executable(
Aql/Functions.cpp
Aql/grammar.cpp
Aql/IndexBlock.cpp
Aql/IndexNode.cpp
Aql/IndexRangeBlock.cpp
Aql/IndexRangeNode.cpp
Aql/ModificationBlock.cpp
Aql/NodeFinder.cpp
Aql/Optimizer.cpp

View File

@ -48,7 +48,9 @@ arangod_libarangod_a_SOURCES = \
arangod/Aql/Functions.cpp \
arangod/Aql/grammar.cpp \
arangod/Aql/IndexBlock.cpp \
arangod/Aql/IndexNode.cpp \
arangod/Aql/IndexRangeBlock.cpp \
arangod/Aql/IndexRangeNode.cpp \
arangod/Aql/ModificationBlock.cpp \
arangod/Aql/NodeFinder.cpp \
arangod/Aql/Optimizer.cpp \