1
0
Fork 0

putting RangeInfo stuff into new file.

This commit is contained in:
James 2014-08-22 10:34:03 +02:00
parent fcb3342729
commit 1afad665fa
5 changed files with 358 additions and 230 deletions

View File

@ -38,10 +38,11 @@
#include "Aql/Expression.h"
#include "Aql/Index.h"
#include "Aql/ModificationOptions.h"
#include "Aql/Variable.h"
#include "Aql/Types.h"
#include "Aql/WalkerWorker.h"
#include "Aql/Query.h"
#include "Aql/RangeInfo.h"
#include "Aql/Types.h"
#include "Aql/Variable.h"
#include "Aql/WalkerWorker.h"
#include "lib/Basics/json-utilities.h"
@ -837,233 +838,6 @@ namespace triagens {
};
// -----------------------------------------------------------------------------
// --SECTION-- class IndexRangeNode
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief struct to keep an upper or lower bound for the range info. Use
/// nullptr instead if you want to have no bound.
////////////////////////////////////////////////////////////////////////////////
struct RangeInfoBound {
RangeInfoBound(AstNode const* bound, bool include) : _include(include) {
_bound = Json(TRI_UNKNOWN_MEM_ZONE, bound->toJson(TRI_UNKNOWN_MEM_ZONE, true));
}
~RangeInfoBound(){}
RangeInfoBound ( RangeInfoBound const& copy ) = delete;
RangeInfoBound& operator= ( RangeInfoBound const& copy ) = delete;
Json toJson () const {
Json item(basics::Json::Array);
item("bound", Json(TRI_UNKNOWN_MEM_ZONE,
TRI_CopyJson(TRI_UNKNOWN_MEM_ZONE, _bound.json())))
("include", Json(_include));
return item;
}
Json _bound;
bool _include;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief struct to keep range info
////////////////////////////////////////////////////////////////////////////////
struct RangeInfo{
RangeInfo ( RangeInfoBound const* low,
RangeInfoBound const* high )
: _low(low), _high(high), _valid(true) {}
RangeInfo( const RangeInfo& copy ) = delete;
RangeInfo& operator= ( RangeInfo const& copy ) = delete;
~RangeInfo(){
if(_low != nullptr){
delete _low;
}
if(_high != nullptr){
delete _high;
}
}
Json toJson () {
Json item(basics::Json::Array);
if(_low != nullptr && _high != nullptr){
item("low", _low->toJson())
("high", _high->toJson())
("valid", Json(_valid));
}
else if(_low != nullptr){
item("low", _low->toJson())
("valid", Json(_valid));
}
else if(_high != nullptr){
item("high", _high->toJson())
("valid", Json(_valid));
}
return item;
}
std::string toString() {
return this->toJson().toString();
}
bool is1ValueRangeInfo () { //i.e. the range only covers single value
return _valid && _low != nullptr && _high != nullptr &&
TRI_CheckSameValueJson(_low->_bound.json(), _high->_bound.json())
&& _low->_include && _high->_include;
}
RangeInfoBound const* _low;
RangeInfoBound const* _high;
bool _valid;
};
// 3-way comparison: a return of -1 indicates that left is
// tighter than right, 0 that they are equal, 1 that right is tighter than
// left. For example, (x<1) is tighter than (x<=1) and (x>1) is tighter
// than (x>=1) . . .
// lowhigh should be -1 if the comparison is of lower bounds,
// and 1 if it is of upper bounds . . .
static int CompareRangeInfoBound (RangeInfoBound const* left,
RangeInfoBound const* right, int lowhigh) {
if (left == nullptr) {
return (right == nullptr ? 0 : 1);
}
if (right == nullptr) {
return -1;
}
int cmp = TRI_CompareValuesJson(left->_bound.json(), right->_bound.json());
if (cmp == 0 && (left->_include != right->_include)) {
return (left->_include?1:-1);
}
return cmp * lowhigh;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief class to keep a vector of RangeInfos . . .
////////////////////////////////////////////////////////////////////////////////
class RangesInfo{
public:
RangesInfo( const RangesInfo& copy ) = delete;
RangesInfo& operator= ( RangesInfo const& copy ) = delete;
RangesInfo () : _ranges(){}
~RangesInfo(){}
RangeInfo* find(std::string var, std::string name) const {
auto it1 = _ranges.find(var);
if (it1 == _ranges.end()) {
return nullptr;
}
auto it2 = it1->second.find(name);
if (it2 == it1->second.end()) {
return nullptr;
}
return (*it2).second;
}
std::unordered_map<std::string, RangeInfo*>* find(std::string var) {
auto it = _ranges.find(var);
if (it == _ranges.end()) {
return nullptr;
}
return &((*it).second);
}
void insert (std::string var, std::string name,
RangeInfoBound* low, RangeInfoBound* high) {
auto oldMap = find(var);
auto newRange = new RangeInfo(low, high);
if (oldMap == nullptr) {
// TODO add exception . . .
auto newMap = std::unordered_map<std::string, RangeInfo*>();
newMap.insert(make_pair(name, newRange));
_ranges.insert(std::make_pair(var, newMap));
return;
}
auto oldRange = find(var, name); //TODO improve, using oldMap
if (oldRange == nullptr) {
// TODO add exception . . .
oldMap->insert(make_pair(name, newRange));
return;
}
if (!oldRange->_valid) { // intersection of the empty set with any set is empty!
return;
}
//this case is not covered by those below . . .
if (oldRange->is1ValueRangeInfo() && newRange->is1ValueRangeInfo()) {
if (!TRI_CheckSameValueJson(oldRange->_low->_bound.json(),
newRange->_low->_bound.json())) {
oldRange->_valid = false;
return;
}
}
if (CompareRangeInfoBound(newRange->_low, oldRange->_low, -1) == -1) {
oldRange->_low = newRange->_low;
}
if (CompareRangeInfoBound(newRange->_high, oldRange->_high, 1) == -1) {
oldRange->_high = newRange->_high;
}
// check the new range bounds are valid
if (oldRange->_low != nullptr && oldRange->_high != nullptr) {
int cmp = TRI_CompareValuesJson(oldRange->_low->_bound.json(),
oldRange->_high->_bound.json());
if (cmp == 1 || (cmp == 0 &&
!(oldRange->_low->_include == true && oldRange->_high->_include == true ))) {
// range invalid
oldRange->_valid = false;
}
}
}
size_t size () const {
return _ranges.size();
}
Json toJson () const {
Json list(Json::List);
for (auto x : _ranges) {
for (auto y: x.second){
Json item(Json::Array);
item("var", Json(x.first))
("attribute name", Json(y.first))
("range info", y.second->toJson());
list(item);
}
}
return list;
}
std::string toString() const {
return this->toJson().toString();
}
private:
std::unordered_map<std::string, std::unordered_map<std::string, RangeInfo*>> _ranges;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief class IndexRangeNode

135
arangod/Aql/RangeInfo.cpp Normal file
View File

@ -0,0 +1,135 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Infrastructure for RangeInfo
///
/// @file arangod/Aql/RangeInfo.h
///
/// 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 not James
/// @author Copyright 2014, triagens GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include <Basics/Common.h>
#include "Aql/RangeInfo.h"
using namespace triagens::basics;
using namespace triagens::aql;
////////////////////////////////////////////////////////////////////////////////
/// @brief 3-way comparison of the tightness of upper or lower RangeInfoBounds.
/// Returns -1 if <left> is tighter than <right>, 1 if <right> is tighter than
/// <left>, and 0 if the bounds are the same. The argument <lowhigh> should be
/// -1 if we are comparing lower bounds and 1 if we are comparing upper bounds.
///
/// If <left> or <right> is a nullptr, this indicates no bound.
///
/// If ~ is the comparison and (x,y), (z,t) are RangeInfoBounds (i.e. x,z are
/// Json values, y,t are booleans) that we are comparing as lower bounds, then
/// the following holds:
///
/// -1 x>z or (x=z, y=false, z=true)
/// (x,y) ~ (z,t) = 0 x=z, y=t
/// 1 z>x or (x=z, y=true, t=false)
///
/// as upper bounds:
///
/// -1 x<z or (x=z, y=false, z=true)
/// (x,y) ~ (z,t) = 0 x=z, y=t
/// 1 z<x or (x=z, y=true, t=false)
///
/// For example (x<2) is tighter than (x<3) and (x<=2). The bound (x<2) is
/// represented as (2, false), and (x<=2) is (2, true), and so as upper bounds
/// (2, false) ~ (2, true) = -1 indicating that (2,false)=(x<2) is tighter than
/// (2, true)=(x<=2).
////////////////////////////////////////////////////////////////////////////////
static int CompareRangeInfoBound (RangeInfoBound const* left,
RangeInfoBound const* right, int lowhigh) {
if (left == nullptr) {
return (right == nullptr ? 0 : 1);
}
if (right == nullptr) {
return -1;
}
int cmp = TRI_CompareValuesJson(left->_bound.json(), right->_bound.json());
if (cmp == 0 && (left->_include != right->_include)) {
return (left->_include?1:-1);
}
return cmp * lowhigh;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief insert if there is no range corresponding to variable name <var>,
/// and attribute <name>, and otherwise intersection with existing range
////////////////////////////////////////////////////////////////////////////////
void RangesInfo::insert (std::string var, std::string name,
RangeInfoBound* low, RangeInfoBound* high) {
auto oldMap = find(var);
auto newRange = new RangeInfo(low, high);
if (oldMap == nullptr) {
// TODO add exception . . .
auto newMap = std::unordered_map<std::string, RangeInfo*>();
newMap.insert(make_pair(name, newRange));
_ranges.insert(std::make_pair(var, newMap));
return;
}
auto oldRange = find(var, name); //TODO improve, using oldMap
if (oldRange == nullptr) {
// TODO add exception . . .
oldMap->insert(make_pair(name, newRange));
return;
}
if (!oldRange->_valid) { // intersection of the empty set with any set is empty!
return;
}
//this case is not covered by those below . . .
if (oldRange->is1ValueRangeInfo() && newRange->is1ValueRangeInfo()) {
if (!TRI_CheckSameValueJson(oldRange->_low->_bound.json(),
newRange->_low->_bound.json())) {
oldRange->_valid = false;
return;
}
}
if (CompareRangeInfoBound(newRange->_low, oldRange->_low, -1) == -1) {
oldRange->_low = newRange->_low;
}
if (CompareRangeInfoBound(newRange->_high, oldRange->_high, 1) == -1) {
oldRange->_high = newRange->_high;
}
// check the new range bounds are valid
if (oldRange->_low != nullptr && oldRange->_high != nullptr) {
int cmp = TRI_CompareValuesJson(oldRange->_low->_bound.json(),
oldRange->_high->_bound.json());
if (cmp == 1 || (cmp == 0 &&
!(oldRange->_low->_include == true && oldRange->_high->_include == true ))) {
// range invalid
oldRange->_valid = false;
}
}
};

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

@ -0,0 +1,217 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Infrastructure for RangeInfo
///
/// @file arangod/Aql/RangeInfo.h
///
/// 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 not James
/// @author Copyright 2014, triagens GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_AQL_RANGE_INFO_H
#define ARANGODB_AQL_RANGE_INFO_H 1
#include <Basics/Common.h>
#include <Basics/JsonHelper.h>
#include "Aql/AstNode.h"
//#include <VocBase/voc-types.h>
//#include <VocBase/vocbase.h>
//#include "Aql/Expression.h"
//#include "Aql/Index.h"
//#include "Aql/ModificationOptions.h"
//#include "Aql/Variable.h"
//#include "Aql/Types.h"
//#include "Aql/WalkerWorker.h"
//#include "Aql/Query.h"
#include "lib/Basics/json-utilities.h"
using Json = triagens::basics::Json;
namespace triagens {
namespace aql {
// -----------------------------------------------------------------------------
// --SECTION-- class RangeInfoBound
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief struct to keep an upper or lower bound named _bound and a bool
/// _include which indicates if the _bound is included or not.
////////////////////////////////////////////////////////////////////////////////
struct RangeInfoBound {
RangeInfoBound(AstNode const* bound, bool include) : _include(include) {
_bound = Json(TRI_UNKNOWN_MEM_ZONE, bound->toJson(TRI_UNKNOWN_MEM_ZONE, true));
}
~RangeInfoBound(){}
RangeInfoBound ( RangeInfoBound const& copy ) = delete;
RangeInfoBound& operator= ( RangeInfoBound const& copy ) = delete;
Json toJson () const {
Json item(basics::Json::Array);
item("bound", Json(TRI_UNKNOWN_MEM_ZONE,
TRI_CopyJson(TRI_UNKNOWN_MEM_ZONE, _bound.json())))
("include", Json(_include));
return item;
}
Json _bound;
bool _include;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief struct to keep a pair of RangeInfoBounds _low and _high, and _valid
/// to indicate if the range is valid (i.e. not of the form x<2 and x>3).
////////////////////////////////////////////////////////////////////////////////
struct RangeInfo{
RangeInfo ( RangeInfoBound const* low,
RangeInfoBound const* high )
: _low(low), _high(high), _valid(true) {}
RangeInfo( const RangeInfo& copy ) = delete;
RangeInfo& operator= ( RangeInfo const& copy ) = delete;
~RangeInfo(){
if(_low != nullptr){
delete _low;
}
if(_high != nullptr){
delete _high;
}
}
//TODO improve
Json toJson () {
Json item(basics::Json::Array);
if(_low != nullptr && _high != nullptr){
item("low", _low->toJson())
("high", _high->toJson())
("valid", Json(_valid));
}
else if(_low != nullptr){
item("low", _low->toJson())
("valid", Json(_valid));
}
else if(_high != nullptr){
item("high", _high->toJson())
("valid", Json(_valid));
}
return item;
}
std::string toString() {
return this->toJson().toString();
}
// is the range a unique value (i.e. something like x<=1 and x>=1)
bool is1ValueRangeInfo () { //i.e. the range only covers single value
return _valid && _low != nullptr && _high != nullptr &&
TRI_CheckSameValueJson(_low->_bound.json(), _high->_bound.json())
&& _low->_include && _high->_include;
}
RangeInfoBound const* _low;
RangeInfoBound const* _high;
bool _valid;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief class to keep RangeInfos associated to variable and attribute names.
////////////////////////////////////////////////////////////////////////////////
class RangesInfo{
public:
RangesInfo( const RangesInfo& copy ) = delete;
RangesInfo& operator= ( RangesInfo const& copy ) = delete;
RangesInfo () : _ranges(){}
~RangesInfo(){}
// find the range info for variable <var> and attributes <name>
RangeInfo* find (std::string var, std::string name) const {
auto it1 = _ranges.find(var);
if (it1 == _ranges.end()) {
return nullptr;
}
auto it2 = it1->second.find(name);
if (it2 == it1->second.end()) {
return nullptr;
}
return (*it2).second;
}
// find the all the range infos for variable <var>
std::unordered_map<std::string, RangeInfo*>* find (std::string var) {
auto it = _ranges.find(var);
if (it == _ranges.end()) {
return nullptr;
}
return &((*it).second);
}
// insert if it's not already there and otherwise intersection with
// existing range
void insert (std::string var, std::string name,
RangeInfoBound* low, RangeInfoBound* high);
// the number of range infos stored
size_t size () const {
return _ranges.size();
}
std::string toString() const {
return this->toJson().toString();
}
Json toJson () const {
Json list(Json::List);
for (auto x : _ranges) {
for (auto y: x.second){
Json item(Json::Array);
item("var", Json(x.first))
("attribute name", Json(y.first))
("range info", y.second->toJson());
list(item);
}
}
return list;
}
private:
std::unordered_map<std::string, std::unordered_map<std::string, RangeInfo*>> _ranges;
};
}
}
#endif

View File

@ -74,6 +74,7 @@ add_executable(
Aql/OptimizerRules.cpp
Aql/Parser.cpp
Aql/Query.cpp
Aql/RangeInfo.cpp
Aql/Scopes.cpp
Aql/Types.cpp
Aql/tokens.cpp

View File

@ -55,6 +55,7 @@ arangod_libarangod_a_SOURCES = \
arangod/Aql/OptimizerRules.cpp \
arangod/Aql/Parser.cpp \
arangod/Aql/Query.cpp \
arangod/Aql/RangeInfo.cpp \
arangod/Aql/Scopes.cpp \
arangod/Aql/Types.cpp \
arangod/Aql/tokens.cpp \