1
0
Fork 0

Added CXX implementation for REMOVE_VALUE and REMOVE_NTH

This commit is contained in:
Michael Hackstein 2015-11-04 12:44:30 +01:00
parent 0f042e96ea
commit 3549be5088
4 changed files with 101 additions and 5 deletions

View File

@ -182,9 +182,9 @@ std::unordered_map<std::string, Function const> const Executor::FunctionNames{
{ "POP", Function("POP", "AQL_POP", "l", true, true, false, true, true, &Functions::Pop) },
{ "SHIFT", Function("SHIFT", "AQL_SHIFT", "l", true, true, false, true, true, &Functions::Shift) },
{ "UNSHIFT", Function("UNSHIFT", "AQL_UNSHIFT", "l,.|b", true, true, false, true, true, &Functions::Unshift) },
{ "REMOVE_VALUE", Function("REMOVE_VALUE", "AQL_REMOVE_VALUE", "l,.|n", true, true, false, true, true) },
{ "REMOVE_VALUE", Function("REMOVE_VALUE", "AQL_REMOVE_VALUE", "l,.|n", true, true, false, true, true, &Functions::RemoveValue) },
{ "REMOVE_VALUES", Function("REMOVE_VALUES", "AQL_REMOVE_VALUES", "l,lz", true, true, false, true, true) },
{ "REMOVE_NTH", Function("REMOVE_NTH", "AQL_REMOVE_NTH", "l,n", true, true, false, true, true) },
{ "REMOVE_NTH", Function("REMOVE_NTH", "AQL_REMOVE_NTH", "l,n", true, true, false, true, true, &Functions::RemoveNth) },
// document functions
{ "HAS", Function("HAS", "AQL_HAS", "az,s", true, true, false, true, true, &Functions::Has) },

View File

@ -3422,8 +3422,7 @@ AqlValue Functions::Pop (triagens::aql::Query* query,
if (list.isArray()) {
if (list.size() > 0) {
if (! TRI_DeleteArrayJson(TRI_UNKNOWN_MEM_ZONE, list.json(), list.size() - 1)) {
// This should never happen
TRI_ASSERT(false);
THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
}
}
return AqlValue(new Json(TRI_UNKNOWN_MEM_ZONE, list.copy().steal()));
@ -3551,7 +3550,7 @@ AqlValue Functions::Shift (triagens::aql::Query* query,
if (list.size() == 0) {
return AqlValue(new Json(Json::Array, 0));
}
if (!TRI_DeleteArrayJson(TRI_UNKNOWN_MEM_ZONE, list.json(), 0)) {
if (! TRI_DeleteArrayJson(TRI_UNKNOWN_MEM_ZONE, list.json(), 0)) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
}
return AqlValue(new Json(TRI_UNKNOWN_MEM_ZONE, list.copy().steal()));
@ -3561,6 +3560,97 @@ AqlValue Functions::Shift (triagens::aql::Query* query,
return AqlValue(new Json(Json::Null));
}
////////////////////////////////////////////////////////////////////////////////
/// @brief function REMOVE_VALUE
////////////////////////////////////////////////////////////////////////////////
AqlValue Functions::RemoveValue (triagens::aql::Query* query,
triagens::arango::AqlTransaction* trx,
FunctionParameters const& parameters) {
size_t const n = parameters.size();
if (n != 2 && n != 3) {
THROW_ARANGO_EXCEPTION_PARAMS(TRI_ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH, "REMOVE_VALUE", (int) 2, (int) 3);
}
Json list = ExtractFunctionParameter(trx, parameters, 0, false);
if (list.isNull()) {
return AqlValue(new Json(Json::Array, 0));
}
if (list.isArray()) {
bool useLimit = false;
Json result = list.copy();
size_t limit = result.size();
Json toRemove = ExtractFunctionParameter(trx, parameters, 1, false);
if (n == 3) {
Json limitJson = ExtractFunctionParameter(trx, parameters, 2, false);
if (! limitJson.isNull()) {
bool unused = false;
limit = static_cast<size_t>(ValueToNumber(limitJson.json(), unused));
useLimit = true;
}
}
for (size_t i = 0; i < result.size(); /* No increase */) {
if (useLimit && limit == 0) {
break;
}
if (TRI_CheckSameValueJson(toRemove.json(), result.at(i).json())) {
if (! TRI_DeleteArrayJson(TRI_UNKNOWN_MEM_ZONE, result.json(), i)) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
}
--limit;
}
else {
++i;
}
}
return AqlValue(new Json(TRI_UNKNOWN_MEM_ZONE, result.steal()));
}
RegisterInvalidArgumentWarning(query, "REMOVE_VALUE");
return AqlValue(new Json(Json::Null));
}
////////////////////////////////////////////////////////////////////////////////
/// @brief function REMOVE_NTH
////////////////////////////////////////////////////////////////////////////////
AqlValue Functions::RemoveNth (triagens::aql::Query* query,
triagens::arango::AqlTransaction* trx,
FunctionParameters const& parameters) {
size_t const n = parameters.size();
if (n != 2) {
THROW_ARANGO_EXCEPTION_PARAMS(TRI_ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH, "REMOVE_NTH", (int) 2, (int) 2);
}
Json list = ExtractFunctionParameter(trx, parameters, 0, false);
if (list.isNull()) {
return AqlValue(new Json(Json::Array, 0));
}
if (list.isArray()) {
double const count = static_cast<double>(list.size());
Json position = ExtractFunctionParameter(trx, parameters, 1, false);
bool unused = false;
double p = TRI_ToDoubleJson(position.json(), unused);
if (p >= count || p < - count) {
return AqlValue(new Json(TRI_UNKNOWN_MEM_ZONE, list.copy().steal()));
}
if (p < 0) {
p += count;
}
Json result = list.copy();
if (! TRI_DeleteArrayJson(TRI_UNKNOWN_MEM_ZONE, result.json(), static_cast<size_t>(p))) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
}
return AqlValue(new Json(TRI_UNKNOWN_MEM_ZONE, result.steal()));
}
RegisterInvalidArgumentWarning(query, "REMOVE_NTH");
return AqlValue(new Json(Json::Null));
}
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------

View File

@ -136,6 +136,8 @@ namespace triagens {
static AqlValue Append (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
static AqlValue Unshift (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
static AqlValue Shift (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
static AqlValue RemoveValue (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
static AqlValue RemoveNth (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
};
}

View File

@ -848,12 +848,16 @@ function ahuacatlListTestSuite () {
[ [ "1b", "1c", "2a", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 0 ] ],
[ [ "1a", "1c", "2a", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 1 ] ],
[ [ "1a", "1b", "2a", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 2 ] ],
[ [ "1a", "1b", "2a", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 2.2 ] ],
[ [ "1a", "1b", "2a", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 2.8 ] ],
[ [ "1a", "1b", "1c", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 3 ] ],
[ [ "1a", "1b", "1c", "2a", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 4 ] ],
[ [ "1a", "1b", "1c", "2a", "2b" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 5 ] ],
[ [ "1a", "1b", "1c", "2a", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], 6 ] ],
[ [ "1a", "1b", "1c", "2a", "2b" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], -1 ] ],
[ [ "1a", "1b", "1c", "2a", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], -2 ] ],
[ [ "1a", "1b", "1c", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], -2.2 ] ],
[ [ "1a", "1b", "1c", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], -2.8 ] ],
[ [ "1a", "1b", "1c", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], -3 ] ],
[ [ "1a", "1b", "2a", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], -4 ] ],
[ [ "1a", "1c", "2a", "2b", "3" ], [ [ "1a", "1b", "1c", "2a", "2b", "3" ], -5 ] ],