1
0
Fork 0

rename variables in JSON

This commit is contained in:
Jan Steemann 2014-07-31 14:02:17 +02:00
parent 76a9887e25
commit 4b6ab03102
9 changed files with 160 additions and 36 deletions

View File

@ -117,7 +117,7 @@ int ExecutionBlock::staticAnalysisRecursion (
_depth++;
curVar = 0;
auto p = static_cast<EnumerateCollectionPlan const*>(_exePlan);
varTab.insert(make_pair(p->_outVarNumber, VarDefPlace(_depth, 0)));
varTab.insert(make_pair(p->_outVariable->id, VarDefPlace(_depth, 0)));
break;
}
case ExecutionPlan::ENUMERATE_LIST: {

View File

@ -184,8 +184,7 @@ void EnumerateCollectionPlan::toJsonHelper (std::map<ExecutionPlan*, int>& index
json("vocbase", Json(_vocbase->_name));
}
json("collection", Json(_collname))
("outVarNumber", Json(static_cast<double>(_outVarNumber)))
("outVarName", Json(_outVarName));
("outVariable", _outVariable->toJson());
// And add it:
int len = static_cast<int>(nodes.size());

View File

@ -336,10 +336,9 @@ namespace triagens {
EnumerateCollectionPlan (TRI_vocbase_t* vocbase,
std::string collname,
VariableId outVarNumber,
std::string outVarName)
Variable const* outVariable)
: ExecutionPlan(), _vocbase(vocbase), _collname(collname),
_outVarNumber(outVarNumber), _outVarName(outVarName) {
_outVariable(outVariable) {
}
////////////////////////////////////////////////////////////////////////////////
@ -371,8 +370,7 @@ namespace triagens {
////////////////////////////////////////////////////////////////////////////////
virtual ExecutionPlan* clone () const {
auto c = new EnumerateCollectionPlan(_vocbase, _collname,
_outVarNumber, _outVarName);
auto c = new EnumerateCollectionPlan(_vocbase, _collname, _outVariable);
cloneDependencies(c);
return static_cast<ExecutionPlan*>(c);
}
@ -396,16 +394,10 @@ namespace triagens {
std::string _collname;
////////////////////////////////////////////////////////////////////////////////
/// @brief _outVarNumber, output variable
/// @brief output variable
////////////////////////////////////////////////////////////////////////////////
VariableId _outVarNumber;
////////////////////////////////////////////////////////////////////////////////
/// @brief _outVarName, name of variable to write to
////////////////////////////////////////////////////////////////////////////////
std::string _outVarName;
Variable const* _outVariable;
};

View File

@ -147,7 +147,7 @@ ExecutionPlan* PlanGenerator::fromNodeFor (Ast const* ast,
if (expression->type == NODE_TYPE_COLLECTION) {
// second operand is a collection
char const* collectionName = expression->getStringValue();
plan = new EnumerateCollectionPlan(ast->query()->vocbase(), std::string(collectionName), v->id, v->name);
plan = new EnumerateCollectionPlan(ast->query()->vocbase(), std::string(collectionName), v);
}
else if (expression->type == NODE_TYPE_REFERENCE) {
// second operand is already a variable

81
arangod/Aql/Variable.cpp Normal file
View File

@ -0,0 +1,81 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Aql, AST variable
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-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 ArangoDB GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "Aql/Variable.h"
using namespace triagens::aql;
using Json = triagens::basics::Json;
// -----------------------------------------------------------------------------
// --SECTION-- constructors / destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief create the variable
////////////////////////////////////////////////////////////////////////////////
Variable::Variable (std::string const& name,
VariableId id)
: name(name),
value(nullptr),
id(id),
refCount(0) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the variable
////////////////////////////////////////////////////////////////////////////////
Variable::~Variable () {
}
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief return a JSON representation of the variable
////////////////////////////////////////////////////////////////////////////////
Json Variable::toJson () const {
Json json(triagens::basics::Json::Array, 2);
json("id", Json(static_cast<double>(id)))
("name", Json(name));
return json;
}
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

View File

@ -1,5 +1,5 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Aql, AST variables
/// @brief Aql, AST variable
///
/// @file
///
@ -31,6 +31,7 @@
#define ARANGODB_AQL_VARIABLE_H 1
#include "Basics/Common.h"
#include "Basics/JsonHelper.h"
namespace triagens {
namespace aql {
@ -42,18 +43,23 @@ namespace triagens {
// -----------------------------------------------------------------------------
struct Variable {
Variable (std::string const& name,
VariableId id,
bool isUserDefined)
: name(name),
value(nullptr),
id(id),
refCount(0),
isUserDefined(isUserDefined) {
}
~Variable () {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief create the variable
////////////////////////////////////////////////////////////////////////////////
Variable (std::string const&,
VariableId);
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the variable
////////////////////////////////////////////////////////////////////////////////
~Variable ();
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief registers a constant value for the variable
@ -97,15 +103,49 @@ namespace triagens {
--refCount;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the variable is user-defined
////////////////////////////////////////////////////////////////////////////////
inline bool isUserDefined () const {
char const c = name[0];
// variables starting with a number are user-defined
return (c >= '0' && c <= '9');
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return a JSON representation of the variable
////////////////////////////////////////////////////////////////////////////////
triagens::basics::Json toJson () const;
// -----------------------------------------------------------------------------
// --SECTION-- public variables
// -----------------------------------------------------------------------------
std::string const name;
void* value;
VariableId const id;
uint32_t refCount;
bool const isUserDefined;
////////////////////////////////////////////////////////////////////////////////
/// @brief variable name
////////////////////////////////////////////////////////////////////////////////
std::string const name;
////////////////////////////////////////////////////////////////////////////////
/// @brief constant variable value (points to another AstNode)
////////////////////////////////////////////////////////////////////////////////
void* value;
////////////////////////////////////////////////////////////////////////////////
/// @brief variable id
////////////////////////////////////////////////////////////////////////////////
VariableId const id;
////////////////////////////////////////////////////////////////////////////////
/// @brief number of times the variable is used in the AST
////////////////////////////////////////////////////////////////////////////////
uint32_t refCount;
};

View File

@ -65,7 +65,11 @@ VariableGenerator::~VariableGenerator () {
Variable* VariableGenerator::createVariable (char const* name,
bool isUserDefined) {
auto variable = new Variable(std::string(name), nextId(), isUserDefined);
auto variable = new Variable(std::string(name), nextId());
if (isUserDefined) {
TRI_ASSERT(variable->isUserDefined());
}
try {
_variables.insert(std::make_pair(variable->id, variable));
@ -85,7 +89,11 @@ Variable* VariableGenerator::createVariable (char const* name,
Variable* VariableGenerator::createVariable (std::string const& name,
bool isUserDefined) {
auto variable = new Variable(name, nextId(), isUserDefined);
auto variable = new Variable(name, nextId());
if (isUserDefined) {
TRI_ASSERT(variable->isUserDefined());
}
try {
_variables.insert(std::make_pair(variable->id, variable));
@ -126,6 +134,8 @@ Variable* VariableGenerator::getVariable (VariableId id) const {
////////////////////////////////////////////////////////////////////////////////
std::string VariableGenerator::nextName () const {
// note: if the naming scheme is adjusted, it may be necessary to adjust
// Variable::isUserDefined, too!
return std::to_string(_id); // to_string: c++11
}

View File

@ -69,6 +69,7 @@ add_executable(
Aql/Scopes.cpp
Aql/tokens.cpp
Aql/V8Executor.cpp
Aql/Variable.cpp
Aql/VariableGenerator.cpp
BitIndexes/bitarray.cpp
BitIndexes/bitarrayIndex.cpp

View File

@ -50,6 +50,7 @@ arangod_libarangod_a_SOURCES = \
arangod/Aql/Scopes.cpp \
arangod/Aql/tokens.cpp \
arangod/Aql/V8Executor.cpp \
arangod/Aql/Variable.cpp \
arangod/Aql/VariableGenerator.cpp \
arangod/BitIndexes/bitarray.cpp \
arangod/BitIndexes/bitarrayIndex.cpp \