ravendb
Version:
RavenDB client for Node.js
101 lines • 4.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionInfo = void 0;
const index_js_1 = require("../../Exceptions/index.js");
const StringUtil_js_1 = require("../../Utility/StringUtil.js");
const HashCalculator_js_1 = require("../Queries/HashCalculator.js");
const node_buffer_1 = require("node:buffer");
class SessionInfo {
static _clientSessionIdCounter = 0;
_sessionId;
_sessionIdUsed;
_loadBalancerContextSeed;
_canUseLoadBalanceBehavior;
_session;
lastClusterTransactionIndex;
noCaching;
constructor(session, options, documentStore) {
if (!documentStore) {
(0, index_js_1.throwError)("InvalidArgumentException", "DocumentStore cannot be null");
}
if (!session) {
(0, index_js_1.throwError)("InvalidArgumentException", "Session cannot be null");
}
this._session = session;
this._loadBalancerContextSeed = session.requestExecutor.conventions.loadBalancerContextSeed;
this._canUseLoadBalanceBehavior = session.conventions.loadBalanceBehavior === "UseSessionContext"
&& !!session.conventions.loadBalancerPerSessionContextSelector;
this.lastClusterTransactionIndex = documentStore.getLastTransactionIndex(session.databaseName);
this.noCaching = options.noCaching;
}
incrementRequestCount() {
this._session.incrementRequestCount();
}
setContext(sessionKey) {
if (StringUtil_js_1.StringUtil.isNullOrWhitespace(sessionKey)) {
(0, index_js_1.throwError)("InvalidArgumentException", "Session key cannot be null or whitespace.");
}
this._setContextInternal(sessionKey);
this._canUseLoadBalanceBehavior = this._canUseLoadBalanceBehavior
|| this._session.conventions.loadBalanceBehavior === "UseSessionContext";
}
_setContextInternal(sessionKey) {
if (this._sessionIdUsed) {
(0, index_js_1.throwError)("InvalidOperationException", "Unable to set the session context after it has already been used. " +
"The session context can only be modified before it is utilized.");
}
if (!sessionKey) {
this._sessionId = ++SessionInfo._clientSessionIdCounter;
}
else {
const hash = new HashCalculator_js_1.HashCalculator();
hash.write(sessionKey);
hash.write(this._loadBalancerContextSeed);
const buffer = node_buffer_1.Buffer.from(hash.getHash());
this._sessionId = (buffer[0] << 16) + (buffer[1] << 8) + buffer[2];
}
}
async getCurrentSessionNode(requestExecutor) {
let result;
if (requestExecutor.conventions.loadBalanceBehavior === "UseSessionContext") {
if (this._canUseLoadBalanceBehavior) {
result = await requestExecutor.getNodeBySessionId(this.getSessionId());
}
}
switch (requestExecutor.conventions.readBalanceBehavior) {
case "None": {
result = await requestExecutor.getPreferredNode();
break;
}
case "RoundRobin": {
result = await requestExecutor.getNodeBySessionId(this.getSessionId());
break;
}
case "FastestNode": {
result = await requestExecutor.getFastestNode();
break;
}
default: {
(0, index_js_1.throwError)("InvalidArgumentException", requestExecutor.conventions.readBalanceBehavior);
}
}
return result.currentNode;
}
getSessionId() {
if (!this._sessionId) {
let context;
const selector = this._session.conventions.loadBalancerPerSessionContextSelector;
if (selector) {
context = selector(this._session.databaseName);
}
this._setContextInternal(context);
}
this._sessionIdUsed = true;
return this._sessionId;
}
canUseLoadBalanceBehavior() {
return this._canUseLoadBalanceBehavior;
}
}
exports.SessionInfo = SessionInfo;
//# sourceMappingURL=IDocumentSession.js.map