mirror of https://gitee.com/bigwinds/arangodb
Code Review: Common Graph Module
This commit is contained in:
parent
7d613259f1
commit
79a6772667
|
@ -1,4 +1,4 @@
|
||||||
/*jslint indent: 2, nomen: true, maxlen: 100, sloppy: true, vars: true, white: true, plusplus: true */
|
/*jslint indent: 2, nomen: true, maxlen: 100, sloppy: true, plusplus: true */
|
||||||
/*global require, exports */
|
/*global require, exports */
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -40,6 +40,10 @@ var arangodb = require("org/arangodb"),
|
||||||
// --SECTION-- module "org/arangodb/graph"
|
// --SECTION-- module "org/arangodb/graph"
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// @brief constructs an iterator for a given cursor, wrapping each result
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
Iterator = function (wrapper, cursor, stringRepresentation) {
|
Iterator = function (wrapper, cursor, stringRepresentation) {
|
||||||
this.next = function next() {
|
this.next = function next() {
|
||||||
if (cursor.hasNext()) {
|
if (cursor.hasNext()) {
|
||||||
|
@ -102,14 +106,15 @@ GraphArray.prototype = new Array(0);
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.map = function (fun, thisp) {
|
GraphArray.prototype.map = function (fun, thisp) {
|
||||||
var len = this.length;
|
var len = this.length,
|
||||||
var i;
|
i,
|
||||||
|
res;
|
||||||
|
|
||||||
if (typeof fun !== "function") {
|
if (typeof fun !== "function") {
|
||||||
throw new TypeError();
|
throw new TypeError();
|
||||||
}
|
}
|
||||||
|
|
||||||
var res = new GraphArray(len);
|
res = new GraphArray(len);
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (this.hasOwnProperty(i)) {
|
if (this.hasOwnProperty(i)) {
|
||||||
|
@ -125,7 +130,9 @@ GraphArray.prototype.map = function (fun, thisp) {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.getInVertex = function () {
|
GraphArray.prototype.getInVertex = function () {
|
||||||
return this.map(function(a) {return a.getInVertex();});
|
return this.map(function (a) {
|
||||||
|
return a.getInVertex();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -133,7 +140,9 @@ GraphArray.prototype.getInVertex = function () {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.getOutVertex = function () {
|
GraphArray.prototype.getOutVertex = function () {
|
||||||
return this.map(function(a) {return a.getOutVertex();});
|
return this.map(function (a) {
|
||||||
|
return a.getOutVertex();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -141,7 +150,9 @@ GraphArray.prototype.getOutVertex = function () {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.getPeerVertex = function (vertex) {
|
GraphArray.prototype.getPeerVertex = function (vertex) {
|
||||||
return this.map(function(a) {return a.getPeerVertex(vertex);});
|
return this.map(function (a) {
|
||||||
|
return a.getPeerVertex(vertex);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -149,7 +160,9 @@ GraphArray.prototype.getPeerVertex = function (vertex) {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.setProperty = function (name, value) {
|
GraphArray.prototype.setProperty = function (name, value) {
|
||||||
return this.map(function(a) {return a.setProperty(name, value);});
|
return this.map(function (a) {
|
||||||
|
return a.setProperty(name, value);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -157,7 +170,9 @@ GraphArray.prototype.setProperty = function (name, value) {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.edges = function () {
|
GraphArray.prototype.edges = function () {
|
||||||
return this.map(function(a) {return a.edges();});
|
return this.map(function (a) {
|
||||||
|
return a.edges();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -165,7 +180,9 @@ GraphArray.prototype.edges = function () {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.outbound = function () {
|
GraphArray.prototype.outbound = function () {
|
||||||
return this.map(function(a) {return a.outbound();});
|
return this.map(function (a) {
|
||||||
|
return a.outbound();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -173,7 +190,9 @@ GraphArray.prototype.outbound = function () {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.inbound = function () {
|
GraphArray.prototype.inbound = function () {
|
||||||
return this.map(function(a) {return a.inbound();});
|
return this.map(function (a) {
|
||||||
|
return a.inbound();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -182,7 +201,9 @@ GraphArray.prototype.inbound = function () {
|
||||||
|
|
||||||
GraphArray.prototype.getInEdges = function () {
|
GraphArray.prototype.getInEdges = function () {
|
||||||
var args = arguments;
|
var args = arguments;
|
||||||
return this.map(function(a) {return a.getInEdges.apply(a, args);});
|
return this.map(function (a) {
|
||||||
|
return a.getInEdges.apply(a, args);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -191,7 +212,9 @@ GraphArray.prototype.getInEdges = function () {
|
||||||
|
|
||||||
GraphArray.prototype.getOutEdges = function () {
|
GraphArray.prototype.getOutEdges = function () {
|
||||||
var args = arguments;
|
var args = arguments;
|
||||||
return this.map(function(a) {return a.getOutEdges.apply(a, args);});
|
return this.map(function (a) {
|
||||||
|
return a.getOutEdges.apply(a, args);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -200,7 +223,9 @@ GraphArray.prototype.getOutEdges = function () {
|
||||||
|
|
||||||
GraphArray.prototype.getEdges = function () {
|
GraphArray.prototype.getEdges = function () {
|
||||||
var args = arguments;
|
var args = arguments;
|
||||||
return this.map(function(a) {return a.getEdges.apply(a, args);});
|
return this.map(function (a) {
|
||||||
|
return a.getEdges.apply(a, args);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -208,7 +233,9 @@ GraphArray.prototype.getEdges = function () {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.degree = function () {
|
GraphArray.prototype.degree = function () {
|
||||||
return this.map(function(a) {return a.degree();});
|
return this.map(function (a) {
|
||||||
|
return a.degree();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -216,15 +243,19 @@ GraphArray.prototype.degree = function () {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.inDegree = function () {
|
GraphArray.prototype.inDegree = function () {
|
||||||
return this.map(function(a) {return a.inDegree();});
|
return this.map(function (a) {
|
||||||
|
return a.inDegree();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// @brief returns the number of out-edges
|
/// @brief returns the number of out-edges
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.inDegree = function () {
|
GraphArray.prototype.outDegree = function () {
|
||||||
return this.map(function(a) {return a.outDegree();});
|
return this.map(function (a) {
|
||||||
|
return a.outDegree();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -232,7 +263,9 @@ GraphArray.prototype.inDegree = function () {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
GraphArray.prototype.properties = function () {
|
GraphArray.prototype.properties = function () {
|
||||||
return this.map(function(a) {return a.properties();});
|
return this.map(function (a) {
|
||||||
|
return a.properties();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -426,16 +459,13 @@ Edge.prototype.getPeerVertex = function (vertex) {
|
||||||
Edge.prototype._PRINT = function (context) {
|
Edge.prototype._PRINT = function (context) {
|
||||||
if (!this._properties._id) {
|
if (!this._properties._id) {
|
||||||
context.output += "[deleted Edge]";
|
context.output += "[deleted Edge]";
|
||||||
}
|
} else if (this._properties._key !== undefined) {
|
||||||
else if (this._properties._key !== undefined) {
|
|
||||||
if (typeof this._properties._key === "string") {
|
if (typeof this._properties._key === "string") {
|
||||||
context.output += "Edge(\"" + this._properties._key + "\")";
|
context.output += "Edge(\"" + this._properties._key + "\")";
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
context.output += "Edge(" + this._properties._key + ")";
|
context.output += "Edge(" + this._properties._key + ")";
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
context.output += "Edge(<" + this._id + ">)";
|
context.output += "Edge(<" + this._id + ">)";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -608,16 +638,13 @@ Vertex.prototype.properties = function () {
|
||||||
Vertex.prototype._PRINT = function (context) {
|
Vertex.prototype._PRINT = function (context) {
|
||||||
if (! this._properties._id) {
|
if (! this._properties._id) {
|
||||||
context.output += "[deleted Vertex]";
|
context.output += "[deleted Vertex]";
|
||||||
}
|
} else if (this._properties._key !== undefined) {
|
||||||
else if (this._properties._key !== undefined) {
|
|
||||||
if (typeof this._properties._key === "string") {
|
if (typeof this._properties._key === "string") {
|
||||||
context.output += "Vertex(\"" + this._properties._key + "\")";
|
context.output += "Vertex(\"" + this._properties._key + "\")";
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
context.output += "Vertex(" + this._properties._key + ")";
|
context.output += "Vertex(" + this._properties._key + ")";
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
context.output += "Vertex(<" + this._id + ">)";
|
context.output += "Vertex(<" + this._id + ">)";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -650,7 +677,7 @@ Graph.prototype._prepareEdgeData = function (data, label) {
|
||||||
label = data.$label;
|
label = data.$label;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is.notExisty(data) || is.noObject(data)) {
|
if (is.object(data)) {
|
||||||
edgeData = {};
|
edgeData = {};
|
||||||
} else {
|
} else {
|
||||||
edgeData = data._shallowCopy || {};
|
edgeData = data._shallowCopy || {};
|
||||||
|
@ -664,7 +691,7 @@ Graph.prototype._prepareEdgeData = function (data, label) {
|
||||||
Graph.prototype._prepareVertexData = function (data) {
|
Graph.prototype._prepareVertexData = function (data) {
|
||||||
var vertexData;
|
var vertexData;
|
||||||
|
|
||||||
if (is.notExisty(data) || is.noObject(data)) {
|
if (is.object(data)) {
|
||||||
vertexData = {};
|
vertexData = {};
|
||||||
} else {
|
} else {
|
||||||
vertexData = data._shallowCopy || {};
|
vertexData = data._shallowCopy || {};
|
||||||
|
@ -690,7 +717,7 @@ Graph.prototype._prepareVertexData = function (data) {
|
||||||
Graph.prototype.getOrAddVertex = function (id) {
|
Graph.prototype.getOrAddVertex = function (id) {
|
||||||
var v = this.getVertex(id);
|
var v = this.getVertex(id);
|
||||||
|
|
||||||
if (v === null) {
|
if (is.notExisty(v)) {
|
||||||
v = this.addVertex(id);
|
v = this.addVertex(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue