mirror of https://gitee.com/bigwinds/arangodb
orphan may only be added when not used in edge def
This commit is contained in:
parent
121b667078
commit
f067be5a97
|
@ -216,6 +216,7 @@
|
|||
"ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST" : { "code" : 1926, "message" : "collection does not exist" },
|
||||
"ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX" : { "code" : 1927, "message" : "not a vertex collection" },
|
||||
"ERROR_GRAPH_NOT_IN_ORPHAN_COLLECTION" : { "code" : 1928, "message" : "not in orphan collection" },
|
||||
"ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF" : { "code" : 1929, "message" : "collection used in edge def" },
|
||||
"ERROR_SESSION_UNKNOWN" : { "code" : 1950, "message" : "unknown session" },
|
||||
"ERROR_SESSION_EXPIRED" : { "code" : 1951, "message" : "session expired" },
|
||||
"SIMPLE_CLIENT_UNKNOWN_ERROR" : { "code" : 2000, "message" : "unknown client error" },
|
||||
|
|
|
@ -120,6 +120,22 @@ var findOrCreateCollectionsByEdgeDefinitions = function (edgeDefinitions, noCrea
|
|||
];
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief find or create a collection by name
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
var findOrCreateOrphanCollections = function (graphName, orphanCollections, noCreate) {
|
||||
var returnVals = [];
|
||||
if (!orphanCollections) {
|
||||
orphanCollections = [];
|
||||
}
|
||||
orphanCollections.forEach(function (e) {
|
||||
findOrCreateCollectionByName(e, ArangoCollection.TYPE_DOCUMENT, noCreate);
|
||||
returnVals.push(db[e]);
|
||||
});
|
||||
return returnVals;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief internal function to get graphs collection
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -1425,8 +1441,11 @@ var _extendEdgeDefinitions = function (edgeDefinition) {
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
var _create = function (graphName, edgeDefinitions) {
|
||||
var _create = function (graphName, edgeDefinitions, orphanCollections) {
|
||||
|
||||
if (!orphanCollections) {
|
||||
orphanCollections = [];
|
||||
}
|
||||
var gdb = getGraphCollection(),
|
||||
err,
|
||||
graphAlreadyExists = true,
|
||||
|
@ -1497,13 +1516,18 @@ var _create = function (graphName, edgeDefinitions) {
|
|||
}
|
||||
|
||||
collections = findOrCreateCollectionsByEdgeDefinitions(edgeDefinitions, false);
|
||||
orphanCollections.forEach(
|
||||
function(oC) {
|
||||
findOrCreateCollectionByName(oC, ArangoCollection.TYPE_DOCUMENT);
|
||||
}
|
||||
);
|
||||
|
||||
gdb.save({
|
||||
'edgeDefinitions' : edgeDefinitions,
|
||||
'_key' : graphName
|
||||
});
|
||||
|
||||
return new Graph(graphName, edgeDefinitions, collections[0], collections[1]);
|
||||
return new Graph(graphName, edgeDefinitions, collections[0], collections[1], orphanCollections);
|
||||
|
||||
};
|
||||
|
||||
|
@ -1701,7 +1725,10 @@ var createHiddenProperty = function(obj, name, value) {
|
|||
/// @endDocuBlock
|
||||
///
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollections) {
|
||||
var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollections, orphanCollections) {
|
||||
if (!orphanCollections) {
|
||||
orphanCollections = [];
|
||||
}
|
||||
var self = this;
|
||||
// Create Hidden Properties
|
||||
createHiddenProperty(this, "__name", graphName);
|
||||
|
@ -1710,7 +1737,7 @@ var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollecti
|
|||
createHiddenProperty(this, "__edgeDefinitions", edgeDefinitions);
|
||||
createHiddenProperty(this, "__idsToRemove", []);
|
||||
createHiddenProperty(this, "__collectionsToLock", []);
|
||||
createHiddenProperty(this, "__orphanCollections", []);
|
||||
createHiddenProperty(this, "__orphanCollections", orphanCollections);
|
||||
|
||||
// fills this.__idsToRemove and this.__collectionsToLock
|
||||
var removeEdge = function (edgeId, options) {
|
||||
|
@ -1903,7 +1930,7 @@ var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollecti
|
|||
var _graph = function(graphName) {
|
||||
|
||||
var gdb = getGraphCollection(),
|
||||
g, collections;
|
||||
g, collections, orphanCollections;
|
||||
|
||||
try {
|
||||
g = gdb.document(graphName);
|
||||
|
@ -1919,8 +1946,12 @@ var _graph = function(graphName) {
|
|||
}
|
||||
|
||||
collections = findOrCreateCollectionsByEdgeDefinitions(g.edgeDefinitions, true);
|
||||
orphanCollections = g.orphanCollections;
|
||||
if (!orphanCollections) {
|
||||
orphanCollections = [];
|
||||
}
|
||||
|
||||
return new Graph(graphName, g.edgeDefinitions, collections[0], collections[1]);
|
||||
return new Graph(graphName, g.edgeDefinitions, collections[0], collections[1], orphanCollections);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -2483,6 +2514,12 @@ Graph.prototype._extendEdgeDefinitions = function(edgeDefinition) {
|
|||
this.__edgeCollections[edgeDefinition.collection] = db[edgeDefinition.collection];
|
||||
edgeDefinition.from.forEach(
|
||||
function(vc) {
|
||||
//remove from __orphanCollections
|
||||
var orphanIndex = self.__orphanCollections.indexOf(vc);
|
||||
if (orphanIndex !== -1) {
|
||||
self.__orphanCollections.splice(orphanIndex, 1);
|
||||
}
|
||||
//push into __vertexCollections
|
||||
if (self.__vertexCollections[vc] === undefined) {
|
||||
self.__vertexCollections[vc] = db[vc];
|
||||
}
|
||||
|
@ -2490,6 +2527,12 @@ Graph.prototype._extendEdgeDefinitions = function(edgeDefinition) {
|
|||
);
|
||||
edgeDefinition.to.forEach(
|
||||
function(vc) {
|
||||
//remove from __orphanCollections
|
||||
var orphanIndex = self.__orphanCollections.indexOf(vc);
|
||||
if (orphanIndex !== -1) {
|
||||
self.__orphanCollections.splice(orphanIndex, 1);
|
||||
}
|
||||
//push into __vertexCollections
|
||||
if (self.__vertexCollections[vc] === undefined) {
|
||||
self.__vertexCollections[vc] = db[vc];
|
||||
}
|
||||
|
@ -2529,6 +2572,8 @@ Graph.prototype._editEdgeDefinitions = function(edgeDefinition, dropCollections)
|
|||
var self = this;
|
||||
var dropCandidates;
|
||||
var currentEdgeDefinition = {};
|
||||
var exOrphanCandidates = [];
|
||||
var effectedGraphs = [];
|
||||
|
||||
|
||||
//check, if in graphs edge definition
|
||||
|
@ -2552,6 +2597,7 @@ Graph.prototype._editEdgeDefinitions = function(edgeDefinition, dropCollections)
|
|||
eDs[id].from = edgeDefinition.from;
|
||||
eDs[id].to = edgeDefinition.to;
|
||||
db._graphs.update(graph._key, {edgeDefinitions: eDs});
|
||||
effectedGraphs.push(graph._key);
|
||||
if (graph._key === self.__name) {
|
||||
self.__edgeDefinitions[id].from = edgeDefinition.from;
|
||||
self.__edgeDefinitions[id].to = edgeDefinition.to;
|
||||
|
@ -2584,11 +2630,11 @@ Graph.prototype._editEdgeDefinitions = function(edgeDefinition, dropCollections)
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
//push "new" collections into vertexCollections
|
||||
edgeDefinition.from.forEach(
|
||||
function(vc) {
|
||||
if (self.__vertexCollections[vc] === undefined) {
|
||||
exOrphanCandidates.push(vc);
|
||||
self.__vertexCollections[vc] = db[vc];
|
||||
}
|
||||
}
|
||||
|
@ -2596,6 +2642,7 @@ Graph.prototype._editEdgeDefinitions = function(edgeDefinition, dropCollections)
|
|||
edgeDefinition.to.forEach(
|
||||
function(vc) {
|
||||
if (self.__vertexCollections[vc] === undefined) {
|
||||
exOrphanCandidates.push(vc);
|
||||
self.__vertexCollections[vc] = db[vc];
|
||||
}
|
||||
}
|
||||
|
@ -2622,6 +2669,26 @@ Graph.prototype._editEdgeDefinitions = function(edgeDefinition, dropCollections)
|
|||
}
|
||||
);
|
||||
|
||||
//orphans treatment
|
||||
effectedGraphs.forEach(
|
||||
function(gN) {
|
||||
var g;
|
||||
if (gN === self.__name) {
|
||||
g = self;
|
||||
} else {
|
||||
g = _graph(gN);
|
||||
}
|
||||
var orphans = g._getOrphanCollections();
|
||||
exOrphanCandidates.forEach(
|
||||
function(eOC) {
|
||||
if (orphans.indexOf(eOC) !== -1) {
|
||||
g._removeOrphanCollection(eOC);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -2720,17 +2787,17 @@ Graph.prototype._deleteEdgeDefinition = function(edgeCollection, dropCollections
|
|||
///
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Graph.prototype._addOrphanCollection = function(vertexCollection, createCollection) {
|
||||
Graph.prototype._addOrphanCollection = function(orphanCollectionName, createCollection) {
|
||||
//check edgeCollection
|
||||
var ec = db._collection(vertexCollection);
|
||||
var ec = db._collection(orphanCollectionName);
|
||||
var err;
|
||||
if (ec === null) {
|
||||
if (createCollection !== false) {
|
||||
db._create(vertexCollection);
|
||||
db._create(orphanCollectionName);
|
||||
} else {
|
||||
err = new ArangoError();
|
||||
err.errorNum = arangodb.errors.ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST.code;
|
||||
err.errorMessage = vertexCollection + arangodb.errors.ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST.message;
|
||||
err.errorMessage = orphanCollectionName + arangodb.errors.ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST.message;
|
||||
throw err;
|
||||
}
|
||||
} else if (ec.type() !== 2) {
|
||||
|
@ -2739,8 +2806,14 @@ Graph.prototype._addOrphanCollection = function(vertexCollection, createCollecti
|
|||
err.errorMessage = arangodb.errors.ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX.message;
|
||||
throw err;
|
||||
}
|
||||
if (this.__vertexCollections[orphanCollectionName] !== undefined) {
|
||||
err = new ArangoError();
|
||||
err.errorNum = arangodb.errors.ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX.code;
|
||||
err.errorMessage = arangodb.errors.ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX.message;
|
||||
throw err;
|
||||
}
|
||||
|
||||
this.__orphanCollections.push(vertexCollection);
|
||||
this.__orphanCollections.push(orphanCollectionName);
|
||||
db._graphs.update(this.__name, {orphanCollections: this.__orphanCollections});
|
||||
|
||||
};
|
||||
|
@ -2813,6 +2886,7 @@ Graph.prototype._removeOrphanCollection = function(orphanCollectionName, dropCol
|
|||
throw err;
|
||||
}
|
||||
this.__orphanCollections.splice(index, 1);
|
||||
db._graphs.update(this.__name, {orphanCollections: this.__orphanCollections});
|
||||
|
||||
if (dropCollection !== false) {
|
||||
var graphs = getGraphCollection().toArray();
|
||||
|
|
|
@ -216,6 +216,7 @@
|
|||
"ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST" : { "code" : 1926, "message" : "collection does not exist" },
|
||||
"ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX" : { "code" : 1927, "message" : "not a vertex collection" },
|
||||
"ERROR_GRAPH_NOT_IN_ORPHAN_COLLECTION" : { "code" : 1928, "message" : "not in orphan collection" },
|
||||
"ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF" : { "code" : 1929, "message" : "collection used in edge def" },
|
||||
"ERROR_SESSION_UNKNOWN" : { "code" : 1950, "message" : "unknown session" },
|
||||
"ERROR_SESSION_EXPIRED" : { "code" : 1951, "message" : "session expired" },
|
||||
"SIMPLE_CLIENT_UNKNOWN_ERROR" : { "code" : 2000, "message" : "unknown client error" },
|
||||
|
|
|
@ -2786,17 +2786,17 @@ Graph.prototype._deleteEdgeDefinition = function(edgeCollection, dropCollections
|
|||
///
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Graph.prototype._addOrphanCollection = function(vertexCollection, createCollection) {
|
||||
Graph.prototype._addOrphanCollection = function(orphanCollectionName, createCollection) {
|
||||
//check edgeCollection
|
||||
var ec = db._collection(vertexCollection);
|
||||
var ec = db._collection(orphanCollectionName);
|
||||
var err;
|
||||
if (ec === null) {
|
||||
if (createCollection !== false) {
|
||||
db._create(vertexCollection);
|
||||
db._create(orphanCollectionName);
|
||||
} else {
|
||||
err = new ArangoError();
|
||||
err.errorNum = arangodb.errors.ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST.code;
|
||||
err.errorMessage = vertexCollection + arangodb.errors.ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST.message;
|
||||
err.errorMessage = orphanCollectionName + arangodb.errors.ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST.message;
|
||||
throw err;
|
||||
}
|
||||
} else if (ec.type() !== 2) {
|
||||
|
@ -2805,8 +2805,14 @@ Graph.prototype._addOrphanCollection = function(vertexCollection, createCollecti
|
|||
err.errorMessage = arangodb.errors.ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX.message;
|
||||
throw err;
|
||||
}
|
||||
if (this.__vertexCollections[orphanCollectionName] !== undefined) {
|
||||
err = new ArangoError();
|
||||
err.errorNum = arangodb.errors.ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF.code;
|
||||
err.errorMessage = arangodb.errors.ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF.message;
|
||||
throw err;
|
||||
}
|
||||
|
||||
this.__orphanCollections.push(vertexCollection);
|
||||
this.__orphanCollections.push(orphanCollectionName);
|
||||
db._graphs.update(this.__name, {orphanCollections: this.__orphanCollections});
|
||||
|
||||
};
|
||||
|
|
|
@ -2562,8 +2562,8 @@ function OrphanCollectionSuite() {
|
|||
},
|
||||
|
||||
test_addOrphanCollection1: function() {
|
||||
g1._addOrphanCollection(vC1, false);
|
||||
assertEqual(g1._getOrphanCollections(), [vC1]);
|
||||
g1._addOrphanCollection(vC5, false);
|
||||
assertEqual(g1._getOrphanCollections(), [vC5]);
|
||||
},
|
||||
|
||||
test_addOrphanCollection2: function() {
|
||||
|
@ -2588,6 +2588,15 @@ function OrphanCollectionSuite() {
|
|||
assertEqual(g1._getOrphanCollections(), []);
|
||||
},
|
||||
|
||||
test_addOrphanCollection4: function() {
|
||||
try {
|
||||
g1._addOrphanCollection(vC1);
|
||||
} catch (e) {
|
||||
assertEqual(e.errorNum, ERRORS.ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF.code);
|
||||
assertEqual(e.errorMessage, ERRORS.ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF.message);
|
||||
}
|
||||
},
|
||||
|
||||
test_removeOrphanCollection1: function() {
|
||||
var name = "completelyNonsenseNameForACollectionBLUBBBBB"
|
||||
try {
|
||||
|
|
|
@ -296,6 +296,7 @@ ERROR_GRAPH_DUPLICATE,1925,"graph already exists","a graph with this name alread
|
|||
ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST,1926,"collection does not exist"," does not exist.",
|
||||
ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX,1927,"not a vertex collection","the collection is not a vertex collection.",
|
||||
ERROR_GRAPH_NOT_IN_ORPHAN_COLLECTION,1928,"not in orphan collection","Vertex collection not in orphan collection of the graph.",
|
||||
ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF,1929,"collection used in edge def","The collection is already used in an edge definition of the graph.",
|
||||
|
||||
################################################################################
|
||||
## Session errors
|
||||
|
|
|
@ -212,6 +212,7 @@ void TRI_InitialiseErrorMessages (void) {
|
|||
REG_ERROR(ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST, "collection does not exist");
|
||||
REG_ERROR(ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX, "not a vertex collection");
|
||||
REG_ERROR(ERROR_GRAPH_NOT_IN_ORPHAN_COLLECTION, "not in orphan collection");
|
||||
REG_ERROR(ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF, "collection used in edge def");
|
||||
REG_ERROR(ERROR_SESSION_UNKNOWN, "unknown session");
|
||||
REG_ERROR(ERROR_SESSION_EXPIRED, "session expired");
|
||||
REG_ERROR(SIMPLE_CLIENT_UNKNOWN_ERROR, "unknown client error");
|
||||
|
|
|
@ -498,6 +498,8 @@ extern "C" {
|
|||
/// the collection is not a vertex collection.
|
||||
/// - 1928: @LIT{not in orphan collection}
|
||||
/// Vertex collection not in orphan collection of the graph.
|
||||
/// - 1929: @LIT{collection used in edge def}
|
||||
/// The collection is already used in an edge definition of the graph.
|
||||
/// - 1950: @LIT{unknown session}
|
||||
/// Will be raised when an invalid/unknown session id is passed to the server.
|
||||
/// - 1951: @LIT{session expired}
|
||||
|
@ -2638,6 +2640,16 @@ void TRI_InitialiseErrorMessages (void);
|
|||
|
||||
#define TRI_ERROR_GRAPH_NOT_IN_ORPHAN_COLLECTION (1928)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief 1929: ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF
|
||||
///
|
||||
/// collection used in edge def
|
||||
///
|
||||
/// The collection is already used in an edge definition of the graph.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define TRI_ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF (1929)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief 1950: ERROR_SESSION_UNKNOWN
|
||||
///
|
||||
|
|
Loading…
Reference in New Issue