1
0
Fork 0

byExample() sharded

This commit is contained in:
Jan Steemann 2014-01-29 17:55:33 +01:00
parent 57aa26bf4a
commit ae05c0c5d3
2 changed files with 72 additions and 3 deletions

View File

@ -80,6 +80,12 @@ var ArangoDatabase = require("org/arangodb/arango-database").ArangoDatabase;
////////////////////////////////////////////////////////////////////////////////
ArangoCollection.prototype.toArray = function () {
var cluster = require("org/arangodb/cluster");
if (cluster.isCoordinator()) {
return this.all().toArray();
}
return this.ALL(null, null).documents;
};

View File

@ -76,7 +76,8 @@ SimpleQueryAll.prototype.execute = function () {
var shards = cluster.shardList(dbName, this._collection.name());
var coord = { coordTransactionID: ArangoClusterInfo.uniqid() };
var options = { coordTransactionID: coord.coordTransactionID, timeout: 360 };
var limit = this._skip + this._limit;
shards.forEach(function (shard) {
ArangoClusterComm.asyncRequest("put",
"shard:" + shard,
@ -85,7 +86,7 @@ SimpleQueryAll.prototype.execute = function () {
JSON.stringify({
collection: shard,
skip: 0,
limit: this._skip + this._limit
limit: limit || undefined
}),
{ },
options);
@ -304,8 +305,70 @@ SimpleQueryByExample.prototype.execute = function () {
if (this._skip === null || this._skip <= 0) {
this._skip = 0;
}
var cluster = require("org/arangodb/cluster");
if (cluster.isCoordinator()) {
var dbName = require("internal").db._name();
var shards = cluster.shardList(dbName, this._collection.name());
var coord = { coordTransactionID: ArangoClusterInfo.uniqid() };
var options = { coordTransactionID: coord.coordTransactionID, timeout: 360 };
var limit = this._skip + this._limit;
var example = this._example;
shards.forEach(function (shard) {
ArangoClusterComm.asyncRequest("put",
"shard:" + shard,
dbName,
"/_api/simple/by-example",
JSON.stringify({
example: example,
collection: shard,
skip: 0,
limit: limit || undefined
}),
{ },
options);
});
documents = byExample(this._collection, this._example, this._skip, this._limit);
var _documents = [ ];
var result = cluster.wait(coord, shards);
var toSkip = this._skip, toLimit = this._limit;
result.forEach(function(part) {
var body = JSON.parse(part.body);
if (toSkip > 0) {
if (toSkip >= body.result.length) {
toSkip -= body.result.length;
return;
}
body.result = body.result.slice(toSkip);
toSkip = 0;
}
if (toLimit !== null && toLimit !== undefined) {
if (body.result.length >= toLimit) {
body.result = body.result.slice(0, toLimit);
toLimit = 0;
}
else {
toLimit -= body.result.length;
}
}
_documents = _documents.concat(body.result);
});
documents = {
documents: _documents,
count: _documents.length
};
}
else {
documents = byExample(this._collection, this._example, this._skip, this._limit);
}
this._execution = new GeneralArrayCursor(documents.documents);
this._countQuery = documents.count;