From dddc5cbd9e9aa0dc971d547980fad4bae970237b Mon Sep 17 00:00:00 2001 From: Michael Hackstein Date: Tue, 20 May 2014 16:22:44 +0200 Subject: [PATCH] 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 --- .../modules/org/arangodb/general-graph.js | 42 ++++++++++++------- js/common/tests/shell-general-graph.js | 14 +------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/js/common/modules/org/arangodb/general-graph.js b/js/common/modules/org/arangodb/general-graph.js index fa5c3dff60..1c0eaf88fc 100644 --- a/js/common/modules/org/arangodb/general-graph.js +++ b/js/common/modules/org/arangodb/general-graph.js @@ -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 diff --git a/js/common/tests/shell-general-graph.js b/js/common/tests/shell-general-graph.js index 23dc99faa4..3fb5e6ce46 100644 --- a/js/common/tests/shell-general-graph.js +++ b/js/common/tests/shell-general-graph.js @@ -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;