I want to update my very large collection (close to 10G) with JavaScript SP for performance reasons. It's a very simple operations so that I decide to run it on server instead of reading all of it to my client workstation which will take long time.
And here's the problem: after a very short while the queryDocuments returns NULL instead of iterator. I understand this is because of the artificially imposed performance limits.
I tried very different page size, from 1000 down to 1, and even with 1 this does NOT work: after 70-90 queries I am kicked out with my query no longer accepted.
I tried different workarounds. But I could not find a way to pause execution: the standard setTimeout function is NOT implemented ... so I cannot pause for a while... I am forced to ask for the next portion of docs in callback immediately as there's no way to pause, and because of this I am kicked out.
var hexStrToIPv4 = function(hex_ip) {
var nAddr = parseInt("0x" + hex_ip);
return nAddr;
};
var debug = function(msg) {
if (console && console.debug)
console.debug(msg);
};
var options = { pageSize: 1000, };
if (continuationToken) {
options.continuation = continuationToken;
}
var getDocs = function() {
var accepted = collection.queryDocuments(collection.getSelfLink(), query, options,
function (err, documents, responseOptions) {
if (err)
throw new Error("Error " + err.message);
if (!documents || !documents.length)
{
__.response.setBody({ "message": "DONE" });
}
else
{
documents.forEach(function(xxx) {
if (!xxx.from || !xxx.to || !xxx.len) {
xxx.from = hexStrToIPv4(xxx.start);
xxx.to = hexStrToIPv4(xxx.end);
xxx.len = xxx.to - xxx.from;
collection.replaceDocument(xxx._self, xxx, function(err, replaced) {
if (err) throw "Error replacing document: " + err;
});
}
});
// fetch and process next portion:
options.continuation = responseOptions.continuation;
continuationToken = responseOptions.continuation;
getDocs();
}
});
if (!accepted) {
// If the execution limit reached; run the script again with the continuationToken as a script parameter.
__.response.setBody({
"message": "Execution limit reached.",
"continuationToken": continuationToken,
});
};
} // getDocs
// entry point:
getDocs();
}