1
0
Fork 0

Added an _exists function to the general-graph module, which checks if there is a graph with given name to avoid error throwing within apps

This commit is contained in:
Michael Hackstein 2014-05-20 16:22:44 +02:00
parent eb7150d815
commit dddc5cbd9e
2 changed files with 28 additions and 28 deletions

View File

@ -119,6 +119,19 @@ var findOrCreateCollectionsByEdgeDefinitions = function (edgeDefinitions, noCrea
];
};
////////////////////////////////////////////////////////////////////////////////
/// @brief internal function to get graphs collection
////////////////////////////////////////////////////////////////////////////////
var _getGraphCollection = function() {
var gCol = db._graphs;
if (gCol === null || gCol === undefined) {
throw "_graphs collection does not exist.";
}
return gCol;
};
// -----------------------------------------------------------------------------
// --SECTION-- module "org/arangodb/general-graph"
@ -336,14 +349,11 @@ var edgeDefinitions = function () {
var _create = function (graphName, edgeDefinitions) {
var gdb = db._graphs,
var gdb = _getGraphCollection(),
g,
graphAlreadyExists = true,
collections;
if (gdb === null || gdb === undefined) {
throw "_graphs collection does not exist.";
}
if (!graphName) {
throw "a graph name is required to create a graph.";
}
@ -435,13 +445,9 @@ var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollecti
var _graph = function(graphName) {
var gdb = db._graphs,
var gdb = _getGraphCollection(),
g, collections;
if (gdb === null || gdb === undefined) {
throw "_graphs collection does not exist.";
}
try {
g = gdb.document(graphName);
}
@ -457,18 +463,22 @@ var _graph = function(graphName) {
return new Graph(graphName, g.edgeDefinitions, collections[0], collections[1]);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief check if a graph exists.
////////////////////////////////////////////////////////////////////////////////
var _exists = function(graphId) {
var gCol = _getGraphCollection();
return gCol.exists(graphId);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief drop a graph.
////////////////////////////////////////////////////////////////////////////////
var _drop = function(graphId, dropCollections) {
var gdb = db._graphs;
if (gdb === null || gdb === undefined) {
throw "_graphs collection does not exist.";
}
var gdb = _getGraphCollection();
if (!gdb.exists(graphId)) {
throw "Graph " + graphId + " does not exist.";
@ -477,7 +487,6 @@ var _drop = function(graphId, dropCollections) {
if (dropCollections !== false) {
var graph = gdb.document(graphId);
var edgeDefinitions = graph.edgeDefinitions;
require("internal").print(edgeDefinitions);
edgeDefinitions.forEach(
function(edgeDefinition) {
var from = edgeDefinition.from;
@ -660,6 +669,7 @@ exports._graph = _graph;
exports.edgeDefinitions = edgeDefinitions;
exports._create = _create;
exports._drop = _drop;
exports._exists = _exists;
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE

View File

@ -424,19 +424,9 @@ function GeneralGraphAQLQueriesSuite() {
var v3 = "UnitTestV3";
var dropInclExcl = function() {
var col = db._collection("_graphs");
try {
col.remove(graphName);
} catch (e) {
return;
if (graph._exists(graphName)) {
graph._drop(graphName);
}
var colList = [v1, v2, v3, included, excluded];
_.each(colList, function(c) {
var colToClear = db._collection(c);
if (col) {
colToClear.truncate();
}
});
};
var e1, e2, e3;