mirror of https://gitee.com/bigwinds/arangodb
updateByExample sharded
This commit is contained in:
parent
8d1ac573a6
commit
6b00e2ccc2
|
@ -373,21 +373,59 @@ ArangoCollection.prototype.replaceByExample = function (example,
|
||||||
newValue,
|
newValue,
|
||||||
waitForSync,
|
waitForSync,
|
||||||
limit) {
|
limit) {
|
||||||
var replaced = 0;
|
|
||||||
var documents;
|
|
||||||
|
|
||||||
if (limit === 0) {
|
if (limit === 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof newValue !== "object" || Array.isArray(newValue)) {
|
if (typeof newValue !== "object" || Array.isArray(newValue)) {
|
||||||
var err = new ArangoError();
|
var err1 = new ArangoError();
|
||||||
err.errorNum = internal.errors.ERROR_BAD_PARAMETER.code;
|
err1.errorNum = internal.errors.ERROR_BAD_PARAMETER.code;
|
||||||
err.errorMessage = "invalid value for parameter 'newValue'";
|
err1.errorMessage = "invalid value for parameter 'newValue'";
|
||||||
|
|
||||||
throw err;
|
throw err1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var replaced = 0;
|
||||||
|
var documents;
|
||||||
|
var cluster = require("org/arangodb/cluster");
|
||||||
|
|
||||||
|
if (cluster.isCoordinator()) {
|
||||||
|
if (limit > 0) {
|
||||||
|
var err2 = new ArangoError();
|
||||||
|
err2.errorNum = internal.errors.ERROR_NOT_IMPLEMENTED.code;
|
||||||
|
err2.errorMessage = "limit not supported in clustered operation";
|
||||||
|
|
||||||
|
throw err2;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dbName = require("internal").db._name();
|
||||||
|
var shards = cluster.shardList(dbName, this.name());
|
||||||
|
var coord = { coordTransactionID: ArangoClusterInfo.uniqid() };
|
||||||
|
var options = { coordTransactionID: coord.coordTransactionID, timeout: 360 };
|
||||||
|
|
||||||
|
shards.forEach(function (shard) {
|
||||||
|
ArangoClusterComm.asyncRequest("put",
|
||||||
|
"shard:" + shard,
|
||||||
|
dbName,
|
||||||
|
"/_api/simple/replace-by-example",
|
||||||
|
JSON.stringify({
|
||||||
|
collection: shard,
|
||||||
|
example: example,
|
||||||
|
newValue: newValue,
|
||||||
|
waitForSync: waitForSync
|
||||||
|
}),
|
||||||
|
{ },
|
||||||
|
options);
|
||||||
|
});
|
||||||
|
|
||||||
|
var results = cluster.wait(coord, shards), i;
|
||||||
|
for (i = 0; i < results.length; ++i) {
|
||||||
|
var body = JSON.parse(results[i].body);
|
||||||
|
|
||||||
|
replaced += (body.replaced || 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
documents = this.byExample(example);
|
documents = this.byExample(example);
|
||||||
if (limit > 0) {
|
if (limit > 0) {
|
||||||
documents = documents.limit(limit);
|
documents = documents.limit(limit);
|
||||||
|
@ -400,6 +438,7 @@ ArangoCollection.prototype.replaceByExample = function (example,
|
||||||
replaced++;
|
replaced++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return replaced;
|
return replaced;
|
||||||
};
|
};
|
||||||
|
@ -413,21 +452,61 @@ ArangoCollection.prototype.updateByExample = function (example,
|
||||||
keepNull,
|
keepNull,
|
||||||
waitForSync,
|
waitForSync,
|
||||||
limit) {
|
limit) {
|
||||||
var updated = 0;
|
|
||||||
var documents;
|
|
||||||
|
|
||||||
if (limit === 0) {
|
if (limit === 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof newValue !== "object" || Array.isArray(newValue)) {
|
if (typeof newValue !== "object" || Array.isArray(newValue)) {
|
||||||
var err = new ArangoError();
|
var err1 = new ArangoError();
|
||||||
err.errorNum = internal.errors.ERROR_BAD_PARAMETER.code;
|
err1.errorNum = internal.errors.ERROR_BAD_PARAMETER.code;
|
||||||
err.errorMessage = "invalid value for parameter 'newValue'";
|
err1.errorMessage = "invalid value for parameter 'newValue'";
|
||||||
|
|
||||||
throw err;
|
throw err1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var updated = 0;
|
||||||
|
var documents;
|
||||||
|
var cluster = require("org/arangodb/cluster");
|
||||||
|
|
||||||
|
if (cluster.isCoordinator()) {
|
||||||
|
if (limit > 0) {
|
||||||
|
var err2 = new ArangoError();
|
||||||
|
err2.errorNum = internal.errors.ERROR_NOT_IMPLEMENTED.code;
|
||||||
|
err2.errorMessage = "limit not supported in clustered operation";
|
||||||
|
|
||||||
|
throw err2;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dbName = require("internal").db._name();
|
||||||
|
var shards = cluster.shardList(dbName, this.name());
|
||||||
|
var coord = { coordTransactionID: ArangoClusterInfo.uniqid() };
|
||||||
|
var options = { coordTransactionID: coord.coordTransactionID, timeout: 360 };
|
||||||
|
|
||||||
|
shards.forEach(function (shard) {
|
||||||
|
ArangoClusterComm.asyncRequest("put",
|
||||||
|
"shard:" + shard,
|
||||||
|
dbName,
|
||||||
|
"/_api/simple/update-by-example",
|
||||||
|
JSON.stringify({
|
||||||
|
collection: shard,
|
||||||
|
example: example,
|
||||||
|
newValue: newValue,
|
||||||
|
waitForSync: waitForSync,
|
||||||
|
keepNull: keepNull
|
||||||
|
}),
|
||||||
|
{ },
|
||||||
|
options);
|
||||||
|
});
|
||||||
|
|
||||||
|
var results = cluster.wait(coord, shards), i;
|
||||||
|
for (i = 0; i < results.length; ++i) {
|
||||||
|
var body = JSON.parse(results[i].body);
|
||||||
|
|
||||||
|
updated += (body.updated || 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
documents = this.byExample(example);
|
documents = this.byExample(example);
|
||||||
if (limit > 0) {
|
if (limit > 0) {
|
||||||
documents = documents.limit(limit);
|
documents = documents.limit(limit);
|
||||||
|
@ -440,6 +519,7 @@ ArangoCollection.prototype.updateByExample = function (example,
|
||||||
updated++;
|
updated++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return updated;
|
return updated;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue