1
0
Fork 0

Added CXX implementation of POP

This commit is contained in:
Michael Hackstein 2015-11-02 17:03:33 +01:00 committed by Frank Celler
parent 496e1cbc0f
commit 5154e4de7a
4 changed files with 70 additions and 4 deletions

View File

@ -179,7 +179,7 @@ std::unordered_map<std::string, Function const> const Executor::FunctionNames{
{ "APPLY", Function("APPLY", "AQL_APPLY", "s|l", false, false, true, false, false) },
{ "PUSH", Function("PUSH", "AQL_PUSH", "l,.|b", true, true, false, true, false, &Functions::Push) },
{ "APPEND", Function("APPEND", "AQL_APPEND", "l,lz|b", true, true, false, true, true) },
{ "POP", Function("POP", "AQL_POP", "l", true, true, false, true, true) },
{ "POP", Function("POP", "AQL_POP", "l", true, true, false, true, true, &Functions::Pop) },
{ "SHIFT", Function("SHIFT", "AQL_SHIFT", "l", true, true, false, true, true) },
{ "UNSHIFT", Function("UNSHIFT", "AQL_UNSHIFT", "l,.|b", true, true, false, true, true) },
{ "REMOVE_VALUE", Function("REMOVE_VALUE", "AQL_REMOVE_VALUE", "l,.|n", true, true, false, true, true) },

View File

@ -3397,6 +3397,37 @@ AqlValue Functions::Push (triagens::aql::Query* query,
return AqlValue(new Json(Json::Null));
}
////////////////////////////////////////////////////////////////////////////////
/// @brief function POP
////////////////////////////////////////////////////////////////////////////////
AqlValue Functions::Pop (triagens::aql::Query* query,
triagens::arango::AqlTransaction* trx,
FunctionParameters const& parameters) {
size_t const n = parameters.size();
if (n != 1) {
THROW_ARANGO_EXCEPTION_PARAMS(TRI_ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH, "POP", (int) 1, (int) 1);
}
Json list = ExtractFunctionParameter(trx, parameters, 0, false);
if (list.isNull()) {
return AqlValue(new Json(Json::Null));
}
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);
}
}
return AqlValue(new Json(TRI_UNKNOWN_MEM_ZONE, list.copy().steal()));
}
RegisterWarning(query, "POP", TRI_ERROR_QUERY_FUNCTION_ARGUMENT_TYPE_MISMATCH);
return AqlValue(new Json(Json::Null));
}
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------

View File

@ -132,6 +132,7 @@ namespace triagens {
static AqlValue FirstDocument (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
static AqlValue FirstList (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
static AqlValue Push (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
static AqlValue Pop (triagens::aql::Query*, triagens::arango::AqlTransaction*, FunctionParameters const&);
};
}

View File

@ -298,6 +298,12 @@ function ahuacatlListTestSuite () {
data.forEach(function (d) {
var actual = getQueryResults("RETURN POP(" + JSON.stringify(d[1]) + ")");
assertEqual(d[0], actual[0], d);
actual = getQueryResults("RETURN NOOPT(POP(" + JSON.stringify(d[1]) + "))");
assertEqual(d[0], actual[0], d);
actual = getQueryResults("RETURN NOOPT(V8(POP(" + JSON.stringify(d[1]) + ")))");
assertEqual(d[0], actual[0], d);
});
},
@ -319,6 +325,26 @@ function ahuacatlListTestSuite () {
l.pop();
assertEqual(l, actual[0]);
assertEqual(998, actual[0].length);
actual = getQueryResults("RETURN NOOPT(POP(" + JSON.stringify(l) + "))");
l.pop();
assertEqual(l, actual[0]);
assertEqual(997, actual[0].length);
actual = getQueryResults("RETURN NOOPT(POP(" + JSON.stringify(l) + "))");
l.pop();
assertEqual(l, actual[0]);
assertEqual(996, actual[0].length);
actual = getQueryResults("RETURN NOOPT(V8(POP(" + JSON.stringify(l) + ")))");
l.pop();
assertEqual(l, actual[0]);
assertEqual(995, actual[0].length);
actual = getQueryResults("RETURN NOOPT(V8(POP(" + JSON.stringify(l) + ")))");
l.pop();
assertEqual(l, actual[0]);
assertEqual(994, actual[0].length);
},
////////////////////////////////////////////////////////////////////////////////
@ -326,9 +352,17 @@ function ahuacatlListTestSuite () {
////////////////////////////////////////////////////////////////////////////////
testPopInvalid : function () {
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN POP()");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN POP([ ], 1)");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN POP([ ], 1, 2)");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN POP()");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN POP([ ], 1)");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN POP([ ], 1, 2)");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN NOOPT(POP())");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN NOOPT(POP([ ], 1))");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN NOOPT(POP([ ], 1, 2))");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN NOOPT(V8(POP()))");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN NOOPT(V8(POP([ ], 1)))");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN NOOPT(V8(POP([ ], 1, 2)))");
},
////////////////////////////////////////////////////////////////////////////////