1
0
Fork 0

Improve /_api/simple/remove-by-key.

Fix more document methods.
This commit is contained in:
Max Neunhoeffer 2016-03-17 18:49:52 +01:00
parent 8d35a2b6f9
commit 827f2f4f44
4 changed files with 51 additions and 13 deletions

View File

@ -181,6 +181,8 @@ void RestSimpleHandler::removeByKeys(VPackSlice const& slice) {
}
bool waitForSync = false;
bool silent = true;
bool returnOld = false;
{
VPackSlice const value = slice.get("options");
if (value.isObject()) {
@ -188,6 +190,14 @@ void RestSimpleHandler::removeByKeys(VPackSlice const& slice) {
if (wfs.isBool()) {
waitForSync = wfs.getBool();
}
wfs = value.get("silent");
if (wfs.isBool()) {
silent = wfs.getBool();
}
wfs = value.get("returnOld");
if (wfs.isBool()) {
returnOld = wfs.getBool();
}
}
}
@ -202,6 +212,13 @@ void RestSimpleHandler::removeByKeys(VPackSlice const& slice) {
"true, waitForSync: ");
aql.append(waitForSync ? "true" : "false");
aql.append(" }");
if (!silent) {
if (returnOld) {
aql.append(" RETURN OLD");
} else {
aql.append(" RETURN {_id: OLD._id, _key: OLD._key, _rev: OLD._rev}");
}
}
arangodb::aql::Query query(
_applicationV8, false, _vocbase, aql.c_str(), aql.size(),
@ -249,12 +266,16 @@ void RestSimpleHandler::removeByKeys(VPackSlice const& slice) {
result.add("ignored", VPackValue(ignored));
result.add("error", VPackValue(false));
result.add("code", VPackValue(_response->responseCode()));
if (!silent) {
result.add("old", queryResult.result->slice());
}
result.close();
VPackSlice s = result.slice();
auto transactionContext = std::make_shared<StandaloneTransactionContext>(_vocbase);
arangodb::basics::VPackStringBufferAdapter buffer(
_response->body().stringBuffer());
VPackDumper dumper(&buffer);
VPackDumper dumper(&buffer, transactionContext->getVPackOptions());
dumper.dump(s);
}
} catch (arangodb::basics::Exception const& ex) {

View File

@ -2082,7 +2082,7 @@ static void JS_SaveVocbase(v8::FunctionCallbackInfo<v8::Value> const& args) {
/// @brief inserts a document, using VPack
////////////////////////////////////////////////////////////////////////////////
static void JS_InsertVocbaseVPack(
static void JS_InsertVocbaseCol(
v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
@ -2099,22 +2099,32 @@ static void JS_InsertVocbaseVPack(
uint32_t const argLength = args.Length();
// Position of <data> and <options>
// They differ for edge and document.
// They differ for edge (old signature) and document.
uint32_t docIdx = 0;
uint32_t optsIdx = 1;
TRI_GET_GLOBALS();
if (isEdgeCollection) {
if (argLength < 3 || argLength > 4) {
if (argLength < 1) {
TRI_V8_THROW_EXCEPTION_USAGE("insert(<data>x, [, <options>i])");
}
bool oldEdgeSignature = false;
if (isEdgeCollection && argLength >= 3) {
oldEdgeSignature = true;
if (argLength > 4) {
TRI_V8_THROW_EXCEPTION_USAGE(
"insert(<from>, <to>, <data>, [<waitForSync>])");
"insert(<from>, <to>, <data> [, <options>])");
}
docIdx = 2;
optsIdx = 3;
if (args[2]->IsArray()) {
TRI_V8_THROW_EXCEPTION(TRI_ERROR_ARANGO_DOCUMENT_TYPE_INVALID);
}
} else {
if (argLength < 1 || argLength > 2) {
TRI_V8_THROW_EXCEPTION_USAGE("insert(<data>, [<waitForSync>])");
TRI_V8_THROW_EXCEPTION_USAGE("insert(<data> [, <options>])");
}
}
@ -2156,7 +2166,7 @@ static void JS_InsertVocbaseVPack(
THROW_ARANGO_EXCEPTION(res);
}
if (isEdgeCollection) {
if (isEdgeCollection && oldEdgeSignature) {
// Just insert from and to. Check is done later.
std::string tmpId(ExtractIdString(isolate, args[0]));
if (tmpId.empty()) {
@ -3027,7 +3037,7 @@ void TRI_InitV8collection(v8::Handle<v8::Context> context, TRI_server_t* server,
TRI_AddMethodVocbase(isolate, rt, TRI_V8_ASCII_STRING("figures"),
JS_FiguresVocbaseCol);
TRI_AddMethodVocbase(isolate, rt, TRI_V8_ASCII_STRING("insert"),
JS_InsertVocbaseVPack);
JS_InsertVocbaseCol);
TRI_AddMethodVocbase(isolate, rt, TRI_V8_ASCII_STRING("load"),
JS_LoadVocbaseCol);
TRI_AddMethodVocbase(isolate, rt, TRI_V8_ASCII_STRING("name"),
@ -3050,7 +3060,7 @@ void TRI_InitV8collection(v8::Handle<v8::Context> context, TRI_server_t* server,
JS_RotateVocbaseCol);
TRI_AddMethodVocbase(
isolate, rt, TRI_V8_ASCII_STRING("save"),
JS_InsertVocbaseVPack); // note: save is now an alias for insert
JS_InsertVocbaseCol); // note: save is now an alias for insert
TRI_AddMethodVocbase(isolate, rt, TRI_V8_ASCII_STRING("status"),
JS_StatusVocbaseCol);
TRI_AddMethodVocbase(isolate, rt, TRI_V8_ASCII_STRING("TRUNCATE"),

View File

@ -933,12 +933,18 @@ ArangoCollection.prototype.insert = function (from, to, data, options) {
type = ArangoCollection.TYPE_DOCUMENT;
}
if (type === ArangoCollection.TYPE_DOCUMENT) {
if (type === ArangoCollection.TYPE_DOCUMENT || data === undefined) {
data = from;
options = to;
url = "/_api/document?collection=" + encodeURIComponent(this.name());
url = "/_api/document/" + encodeURIComponent(this.name());
}
else if (type === ArangoCollection.TYPE_EDGE) {
if (typeof data === 'object' && Array.isArray(data)) {
throw new ArangoError({
errorNum : internal.errors.ERROR_ARANGO_DOCUMENT_TYPE_INVALID.code,
errorMessage : internal.errors.ERROR_ARANGO_DOCUMENT_TYPE_INVALID.message
});
}
if (typeof from === 'object' && from.hasOwnProperty("_id")) {
from = from._id;
}
@ -950,7 +956,7 @@ ArangoCollection.prototype.insert = function (from, to, data, options) {
data._from = from;
data._to = to;
url = "/_api/collection?collection=" + encodeURIComponent(this.name());
url = "/_api/collection/" + encodeURIComponent(this.name());
}
if (options === undefined) {

View File

@ -72,6 +72,7 @@ function CollectionDocumentSuiteBabies() {
testInsertRemoveMulti: function() {
var docs = collection.insert([{}, {}, {}]);
assertEqual(docs.length, 3);
require("internal").print("Foxxy:", docs);
collection.remove(docs);
assertEqual(collection.count(), 0);
},