motorq-documentdb
Version:
Azure Cosmos DB Service Node.js SDK for SQL API
201 lines (177 loc) • 10.1 kB
JavaScript
/*
The MIT License (MIT)
Copyright (c) 2017 Microsoft Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
"use strict";
var Base = require("../base")
, QueryMetrics = require("../queryMetrics/queryMetrics.js")
, ClientSideMetrics = require("../queryMetrics/clientSideMetrics.js")
, Constants = require("../constants")
, HeaderUtils = require('./headerUtils');
//SCRIPT START
var DefaultQueryExecutionContext = Base.defineClass(
/**
* Provides the basic Query Execution Context. This wraps the internal logic query execution using provided fetch functions
* @constructor DefaultQueryExecutionContext
* @param {DocumentClient} documentclient - The service endpoint to use to create the client.
* @param {SqlQuerySpec | string} query - A SQL query.
* @param {FeedOptions} [options] - Represents the feed options.
* @param {callback | callback[]} fetchFunctions - A function to retrieve each page of data. An array of functions may be used to query more than one partition.
* @ignore
*/
function(documentclient, query, options, fetchFunctions){
this.documentclient = documentclient;
this.query = query;
this.resources = [];
this.currentIndex = 0;
this.currentPartitionIndex = 0;
this.fetchFunctions = (Array.isArray(fetchFunctions)) ? fetchFunctions : [fetchFunctions];
this.options = options || {};
this.continuation = this.options.continuation || null;
this.state = DefaultQueryExecutionContext.STATES.start;
},
{
/**
* Execute a provided callback on the next element in the execution context.
* @memberof DefaultQueryExecutionContext
* @instance
* @param {callback} callback - Function to execute for each element. the function takes two parameters error, element.
*/
nextItem: function (callback) {
var that = this;
this.current(function (err, resources, headers) {
++that.currentIndex;
callback(err, resources, headers);
});
},
/**
* Retrieve the current element on the execution context.
* @memberof DefaultQueryExecutionContext
* @instance
* @param {callback} callback - Function to execute for the current element. the function takes two parameters error, element.
*/
current: function(callback) {
var that = this;
if (this.currentIndex < this.resources.length) {
return callback(undefined, this.resources[this.currentIndex], HeaderUtils.getInitialHeader());
}
if (this._canFetchMore()) {
this.fetchMore(function (err, resources, headers) {
if (err) {
return callback(err, undefined, headers);
}
that.resources = resources;
if (that.resources.length === 0) {
if (!that.continuation && that.currentPartitionIndex >= that.fetchFunctions.length) {
that.state = DefaultQueryExecutionContext.STATES.ended;
callback(undefined, undefined, headers);
} else {
that.current(callback);
}
return undefined;
}
callback(undefined, that.resources[that.currentIndex], headers);
});
} else {
this.state = DefaultQueryExecutionContext.STATES.ended;
callback(undefined, undefined, HeaderUtils.getInitialHeader());
}
},
/**
* Determine if there are still remaining resources to processs based on the value of the continuation token or the elements remaining on the current batch in the execution context.
* @memberof DefaultQueryExecutionContext
* @instance
* @returns {Boolean} true if there is other elements to process in the DefaultQueryExecutionContext.
*/
hasMoreResults: function () {
return this.state === DefaultQueryExecutionContext.STATES.start || this.continuation !== undefined || this.currentIndex < this.resources.length || this.currentPartitionIndex < this.fetchFunctions.length;
},
/**
* Fetches the next batch of the feed and pass them as an array to a callback
* @memberof DefaultQueryExecutionContext
* @instance
* @param {callback} callback - Function execute on the feed response, takes two parameters error, resourcesList
*/
fetchMore: function (callback) {
if (this.currentPartitionIndex >= this.fetchFunctions.length) {
return callback(undefined, undefined, HeaderUtils.getInitialHeader());
}
var that = this;
// Keep to the original continuation and to restore the value after fetchFunction call
var originalContinuation = this.options.continuation;
this.options.continuation = this.continuation;
// Return undefined if there is no more results
if (this.currentPartitionIndex >= that.fetchFunctions.length) {
return callback(undefined, undefined, HeaderUtils.getInitialHeader());
}
var fetchFunction = this.fetchFunctions[this.currentPartitionIndex];
fetchFunction(this.options, function(err, resources, responseHeaders){
if(err) {
that.state = DefaultQueryExecutionContext.STATES.ended;
return callback(err, undefined, responseHeaders);
}
that.continuation = responseHeaders[Constants.HttpHeaders.Continuation];
if (!that.continuation) {
++that.currentPartitionIndex;
}
that.state = DefaultQueryExecutionContext.STATES.inProgress;
that.currentIndex = 0;
that.options.continuation = originalContinuation;
// deserializing query metrics so that we aren't working with delimited strings in the rest of the code base
if (Constants.HttpHeaders.QueryMetrics in responseHeaders) {
var delimitedString = responseHeaders[Constants.HttpHeaders.QueryMetrics];
var queryMetrics = QueryMetrics.createFromDelimitedString(delimitedString);
// Add the request charge to the query metrics so that we can have per partition request charge.
if (Constants.HttpHeaders.RequestCharge in responseHeaders) {
queryMetrics = new QueryMetrics(
queryMetrics._getRetrievedDocumentCount(),
queryMetrics._getRetrievedDocumentSize(),
queryMetrics._getOutputDocumentCount(),
queryMetrics._getOutputDocumentSize(),
queryMetrics._getIndexHitDocumentCount(),
queryMetrics._getTotalQueryExecutionTime(),
queryMetrics._getQueryPreparationTimes(),
queryMetrics._getIndexLookupTime(),
queryMetrics._getDocumentLoadTime(),
queryMetrics._getVMExecutionTime(),
queryMetrics._getRuntimeExecutionTimes(),
queryMetrics._getDocumentWriteTime(),
new ClientSideMetrics(responseHeaders[Constants.HttpHeaders.RequestCharge]));
}
// Wraping query metrics in a object where the key is '0' just so single partition and partition queries have the same response schema
responseHeaders[Constants.HttpHeaders.QueryMetrics] = {};
responseHeaders[Constants.HttpHeaders.QueryMetrics]["0"] = queryMetrics;
}
callback(undefined, resources, responseHeaders);
});
},
_canFetchMore: function () {
var res = (this.state === DefaultQueryExecutionContext.STATES.start
|| (this.continuation && this.state === DefaultQueryExecutionContext.STATES.inProgress)
|| (this.currentPartitionIndex < this.fetchFunctions.length
&& this.state === DefaultQueryExecutionContext.STATES.inProgress));
return res;
}
}, {
STATES: Object.freeze({ start: "start", inProgress: "inProgress", ended: "ended" })
}
);
//SCRIPT END
if (typeof exports !== "undefined") {
module.exports = DefaultQueryExecutionContext;
}