1
0
Fork 0
This commit is contained in:
Jan Steemann 2015-12-15 15:41:50 +01:00
parent fce17892c1
commit 00650d0330
5 changed files with 233 additions and 6 deletions

View File

@ -2449,6 +2449,31 @@ static void JS_RemoveVocbaseCol (const v8::FunctionCallbackInfo<v8::Value>& args
TRI_V8_TRY_CATCH_END
}
////////////////////////////////////////////////////////////////////////////////
/// @brief helper function to rename collections in _graphs as well
////////////////////////////////////////////////////////////////////////////////
static int RenameGraphCollections (v8::Isolate* isolate,
std::string const& oldName,
std::string const& newName) {
v8::HandleScope scope(isolate);
StringBuffer buffer(TRI_UNKNOWN_MEM_ZONE);
buffer.appendText("require('org/arangodb/general-graph')._renameCollection(\"");
buffer.appendJsonEncoded(oldName.c_str(), oldName.size());
buffer.appendText("\", \"");
buffer.appendJsonEncoded(newName.c_str(), newName.size());
buffer.appendText("\");");
TRI_ExecuteJavaScriptString(isolate,
isolate->GetCurrentContext(),
TRI_V8_ASCII_PAIR_STRING(buffer.c_str(), buffer.length()),
TRI_V8_ASCII_STRING("collection rename"),
false);
return TRI_ERROR_NO_ERROR;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief renames a collection
/// @startDocuBlock collectionRename
@ -2489,7 +2514,7 @@ static void JS_RenameVocbaseCol (const v8::FunctionCallbackInfo<v8::Value>& args
TRI_V8_THROW_EXCEPTION(TRI_ERROR_CLUSTER_UNSUPPORTED);
}
string const name = TRI_ObjectToString(args[0]);
std::string const name = TRI_ObjectToString(args[0]);
// second parameter "override" is to override renaming restrictions, e.g.
// renaming from a system collection name to a non-system collection name and
@ -2516,6 +2541,8 @@ static void JS_RenameVocbaseCol (const v8::FunctionCallbackInfo<v8::Value>& args
TRI_V8_THROW_EXCEPTION(TRI_ERROR_CLUSTER_UNSUPPORTED);
}
std::string const oldName(collection->_name);
int res = TRI_RenameCollectionVocBase(collection->_vocbase,
collection,
name.c_str(),
@ -2526,6 +2553,9 @@ static void JS_RenameVocbaseCol (const v8::FunctionCallbackInfo<v8::Value>& args
TRI_V8_THROW_EXCEPTION_MESSAGE(res, "cannot rename collection");
}
// rename collection inside _graphs as well
RenameGraphCollections(isolate, oldName, name);
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}

View File

@ -1339,7 +1339,6 @@ static void JS_ExecuteAqlJson (const v8::FunctionCallbackInfo<v8::Value>& args)
static void JS_ExecuteAql (const v8::FunctionCallbackInfo<v8::Value>& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
TRI_vocbase_t* vocbase = GetContextVocBase(isolate);

View File

@ -29,7 +29,6 @@ module.define("org/arangodb/general-graph", function(exports, module) {
/// @author Copyright 2011-2014, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var arangodb = require("org/arangodb"),
internal = require("internal"),
ArangoCollection = arangodb.ArangoCollection,
@ -2442,7 +2441,7 @@ var _graph = function(graphName) {
}
return new Graph(graphName, g.edgeDefinitions, collections[0], collections[1], orphanCollections,
g._rev , g._id);
g._rev, g._id);
};
////////////////////////////////////////////////////////////////////////////////
@ -2454,6 +2453,62 @@ var _exists = function(graphId) {
return gCol.exists(graphId);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief rename a collection inside the _graphs collections
////////////////////////////////////////////////////////////////////////////////
var _renameCollection = function(oldName, newName) {
db._executeTransaction({
collections: {
write: "_graphs"
},
action: function(params) {
var gdb = getGraphCollection();
if (! gdb) {
return;
}
gdb.toArray().forEach(function(doc) {
var c = _.clone(doc), i, j, changed = false;
if (c.edgeDefinitions) {
for (i = 0; i < c.edgeDefinitions.length; ++i) {
var def = c.edgeDefinitions[i];
if (def.collection === params.oldName) {
c.edgeDefinitions[i].collection = params.newName;
changed = true;
}
for (j = 0; j < def.from.length; ++j) {
if (def.from[j] === params.oldName) {
c.edgeDefinitions[i].from[j] = params.newName;
changed = true;
}
}
for (j = 0; j < def.to.length; ++j) {
if (def.to[j] === params.oldName) {
c.edgeDefinitions[i].to[j] = params.newName;
changed = true;
}
}
}
}
for (i = 0; i < c.orphanCollections.length; ++i) {
if (c.orphanCollections[i] === params.oldName) {
c.orphanCollections[i] = params.newName;
changed = true;
}
}
if (changed) {
gdb.update(doc._key, c);
}
});
},
params: {
oldName: oldName,
newName: newName
}
});
};
////////////////////////////////////////////////////////////////////////////////
/// @brief Helper for dropping collections of a graph.
////////////////////////////////////////////////////////////////////////////////
@ -4728,6 +4783,7 @@ exports._extendEdgeDefinitions = _extendEdgeDefinitions;
exports._create = _create;
exports._drop = _drop;
exports._exists = _exists;
exports._renameCollection = _renameCollection;
exports._list = _list;
exports._listObjects = _listObjects;

View File

@ -28,7 +28,6 @@
/// @author Copyright 2011-2014, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var arangodb = require("org/arangodb"),
internal = require("internal"),
ArangoCollection = arangodb.ArangoCollection,
@ -2441,7 +2440,7 @@ var _graph = function(graphName) {
}
return new Graph(graphName, g.edgeDefinitions, collections[0], collections[1], orphanCollections,
g._rev , g._id);
g._rev, g._id);
};
////////////////////////////////////////////////////////////////////////////////
@ -2453,6 +2452,62 @@ var _exists = function(graphId) {
return gCol.exists(graphId);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief rename a collection inside the _graphs collections
////////////////////////////////////////////////////////////////////////////////
var _renameCollection = function(oldName, newName) {
db._executeTransaction({
collections: {
write: "_graphs"
},
action: function(params) {
var gdb = getGraphCollection();
if (! gdb) {
return;
}
gdb.toArray().forEach(function(doc) {
var c = _.clone(doc), i, j, changed = false;
if (c.edgeDefinitions) {
for (i = 0; i < c.edgeDefinitions.length; ++i) {
var def = c.edgeDefinitions[i];
if (def.collection === params.oldName) {
c.edgeDefinitions[i].collection = params.newName;
changed = true;
}
for (j = 0; j < def.from.length; ++j) {
if (def.from[j] === params.oldName) {
c.edgeDefinitions[i].from[j] = params.newName;
changed = true;
}
}
for (j = 0; j < def.to.length; ++j) {
if (def.to[j] === params.oldName) {
c.edgeDefinitions[i].to[j] = params.newName;
changed = true;
}
}
}
}
for (i = 0; i < c.orphanCollections.length; ++i) {
if (c.orphanCollections[i] === params.oldName) {
c.orphanCollections[i] = params.newName;
changed = true;
}
}
if (changed) {
gdb.update(doc._key, c);
}
});
},
params: {
oldName: oldName,
newName: newName
}
});
};
////////////////////////////////////////////////////////////////////////////////
/// @brief Helper for dropping collections of a graph.
////////////////////////////////////////////////////////////////////////////////
@ -4727,6 +4782,7 @@ exports._extendEdgeDefinitions = _extendEdgeDefinitions;
exports._create = _create;
exports._drop = _drop;
exports._exists = _exists;
exports._renameCollection = _renameCollection;
exports._list = _list;
exports._listObjects = _listObjects;

View File

@ -87,6 +87,9 @@ function GeneralGraphCreationSuite() {
},
tearDown: function() {
db._drop("UnitTestsGraphRenamed1");
db._drop("UnitTestsGraphRenamed2");
db._drop("UnitTestsGraphRenamed3");
db._drop(ec1);
db._drop(ec2);
db._drop(ec3);
@ -106,6 +109,89 @@ function GeneralGraphCreationSuite() {
}
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: rename
////////////////////////////////////////////////////////////////////////////////
test_collectionRenameEdge: function() {
var g = graph._create(
gN1,
graph._edgeDefinitions(
graph._relation(rn1, [vn2, vn1], [vn4, vn3])
)
);
var doc = db._graphs.document(gN1);
assertEqual(1, doc.edgeDefinitions.length);
assertEqual(rn1, doc.edgeDefinitions[0].collection);
assertEqual(2, doc.edgeDefinitions[0].from.length);
assertEqual([ vn1, vn2 ], doc.edgeDefinitions[0].from.sort());
assertEqual(2, doc.edgeDefinitions[0].to.length);
assertEqual([ vn3, vn4 ], doc.edgeDefinitions[0].to.sort());
assertEqual([ ], doc.orphanCollections);
db._collection(rn1).rename("UnitTestsGraphRenamed1");
doc = db._graphs.document(gN1);
assertEqual(1, doc.edgeDefinitions.length);
assertEqual("UnitTestsGraphRenamed1", doc.edgeDefinitions[0].collection);
assertEqual(2, doc.edgeDefinitions[0].from.length);
assertEqual([ vn1, vn2 ], doc.edgeDefinitions[0].from.sort());
assertEqual(2, doc.edgeDefinitions[0].to.length);
assertEqual([ vn3, vn4 ], doc.edgeDefinitions[0].to.sort());
assertEqual([ ], doc.orphanCollections);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: rename
////////////////////////////////////////////////////////////////////////////////
test_collectionRenameVertex: function() {
var g = graph._create(
gN1,
graph._edgeDefinitions(
graph._relation(rn1, [vn2, vn1], [vn4, vn3])
)
);
db._collection(vn1).rename("UnitTestsGraphRenamed1");
var doc = db._graphs.document(gN1);
assertEqual(1, doc.edgeDefinitions.length);
assertEqual(rn1, doc.edgeDefinitions[0].collection);
assertEqual(2, doc.edgeDefinitions[0].from.length);
assertEqual([ "UnitTestsGraphRenamed1", vn2 ].sort(), doc.edgeDefinitions[0].from.sort());
assertEqual(2, doc.edgeDefinitions[0].to.length);
assertEqual([ vn3, vn4 ], doc.edgeDefinitions[0].to.sort());
assertEqual([ ], doc.orphanCollections);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: rename
////////////////////////////////////////////////////////////////////////////////
test_collectionRenameVertices: function() {
var g = graph._create(
gN1,
graph._edgeDefinitions(
graph._relation(rn1, [vn2, vn1], [vn4, vn3])
)
);
db._collection(rn1).rename("UnitTestsGraphRenamed1");
db._collection(vn1).rename("UnitTestsGraphRenamed2");
db._collection(vn4).rename("UnitTestsGraphRenamed3");
var doc = db._graphs.document(gN1);
assertEqual(1, doc.edgeDefinitions.length);
assertEqual("UnitTestsGraphRenamed1", doc.edgeDefinitions[0].collection);
assertEqual(2, doc.edgeDefinitions[0].from.length);
assertEqual([ "UnitTestsGraphRenamed2", vn2 ].sort(), doc.edgeDefinitions[0].from.sort());
assertEqual(2, doc.edgeDefinitions[0].to.length);
assertEqual([ vn3, "UnitTestsGraphRenamed3" ].sort(), doc.edgeDefinitions[0].to.sort());
assertEqual([ ], doc.orphanCollections);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: Graph Creation
////////////////////////////////////////////////////////////////////////////////