ravendb
Version:
RavenDB client for Node.js
264 lines • 11.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClusterTransactionOperationsBase = exports.StoredCompareExchange = void 0;
const index_js_1 = require("../../Exceptions/index.js");
const TypeUtil_js_1 = require("../../Utility/TypeUtil.js");
const CaseInsensitiveKeysMap_js_1 = require("../../Primitives/CaseInsensitiveKeysMap.js");
const CompareExchangeSessionValue_js_1 = require("../Operations/CompareExchange/CompareExchangeSessionValue.js");
const CompareExchangeValueResultParser_js_1 = require("../Operations/CompareExchange/CompareExchangeValueResultParser.js");
const GetCompareExchangeValueOperation_js_1 = require("../Operations/CompareExchange/GetCompareExchangeValueOperation.js");
const GetCompareExchangeValuesOperation_js_1 = require("../Operations/CompareExchange/GetCompareExchangeValuesOperation.js");
const StringUtil_js_1 = require("../../Utility/StringUtil.js");
const Constants_js_1 = require("../../Constants.js");
class StoredCompareExchange {
entity;
index;
constructor(index, entity) {
this.entity = entity;
this.index = index;
}
}
exports.StoredCompareExchange = StoredCompareExchange;
class ClusterTransactionOperationsBase {
_session;
_state = CaseInsensitiveKeysMap_js_1.CaseInsensitiveKeysMap.create();
_compareExchangeIncludes = CaseInsensitiveKeysMap_js_1.CaseInsensitiveKeysMap.create();
_missingDocumentsTooAtomicGuardIndex;
tryGetMissingAtomicGuardFor(docId, changeVectorCallback) {
if (!this._missingDocumentsTooAtomicGuardIndex) {
changeVectorCallback(null);
return false;
}
const cv = this._missingDocumentsTooAtomicGuardIndex.get(docId);
changeVectorCallback(cv);
return cv != null;
}
get numberOfTrackedCompareExchangeValues() {
return this._state.size;
}
constructor(session) {
if (session.transactionMode !== "ClusterWide") {
(0, index_js_1.throwError)("InvalidOperationException", "This function is part of cluster transaction session, "
+ "in order to use it you have to open the Session with ClusterWide option.");
}
this._session = session;
}
get session() {
return this._session;
}
isTracked(key) {
return this._tryGetCompareExchangeValueFromSession(key, TypeUtil_js_1.TypeUtil.NOOP);
}
createCompareExchangeValue(key, item) {
if (!key) {
(0, index_js_1.throwError)("InvalidArgumentException", "Key cannot be null");
}
let sessionValue;
if (!this._tryGetCompareExchangeValueFromSession(key, x => sessionValue = x)) {
sessionValue = new CompareExchangeSessionValue_js_1.CompareExchangeSessionValue(key, 0, "None");
this._state.set(key, sessionValue);
}
return sessionValue.create(item);
}
deleteCompareExchangeValue(keyOrItem, index) {
if (!TypeUtil_js_1.TypeUtil.isString(keyOrItem)) {
return this.deleteCompareExchangeValue(keyOrItem.key, keyOrItem.index);
}
const key = keyOrItem;
let sessionValue;
if (!this._tryGetCompareExchangeValueFromSession(key, s => sessionValue = s)) {
sessionValue = new CompareExchangeSessionValue_js_1.CompareExchangeSessionValue(key, 0, "None");
this._state.set(key, sessionValue);
}
sessionValue.delete(index);
}
clear() {
this._state.clear();
this._compareExchangeIncludes.clear();
this._missingDocumentsTooAtomicGuardIndex?.clear();
}
async _getCompareExchangeValueInternal(key, clazz) {
let notTracked;
const v = this.getCompareExchangeValueFromSessionInternal(key, t => notTracked = t, clazz);
if (!notTracked) {
return v;
}
this.session.incrementRequestCount();
const value = await this.session.operations.send(new GetCompareExchangeValueOperation_js_1.GetCompareExchangeValueOperation(key, CompareExchangeValueResultParser_js_1.ObjectNodeMarker, false));
if (TypeUtil_js_1.TypeUtil.isNullOrUndefined(value)) {
this.registerMissingCompareExchangeValue(key);
return null;
}
const sessionValue = this.registerCompareExchangeValue(value);
if (sessionValue) {
return sessionValue.getValue(clazz, this.session.conventions);
}
return null;
}
async _getCompareExchangeValuesInternal(startsWithOrKeys, clazz, start, pageSize) {
if (TypeUtil_js_1.TypeUtil.isString(startsWithOrKeys)) {
this.session.incrementRequestCount();
const values = await this.session.operations.send(new GetCompareExchangeValuesOperation_js_1.GetCompareExchangeValuesOperation({
startWith: startsWithOrKeys,
start,
pageSize,
clazz
}), this.session.sessionInfo);
const results = {};
for (const [key, value] of Object.entries(values)) {
if (TypeUtil_js_1.TypeUtil.isNullOrUndefined(value)) {
this.registerMissingCompareExchangeValue(key);
results[key] = null;
continue;
}
const sessionValue = this.registerCompareExchangeValue(value);
results[key] = sessionValue.getValue(clazz, this.session.conventions);
}
return results;
}
else {
let notTrackedKeys;
const results = this.getCompareExchangeValuesFromSessionInternal(startsWithOrKeys, x => notTrackedKeys = x, clazz);
if (!notTrackedKeys || !notTrackedKeys.size) {
return results;
}
this._session.incrementRequestCount();
const keysArray = Array.from(notTrackedKeys);
const values = await this.session.operations.send(new GetCompareExchangeValuesOperation_js_1.GetCompareExchangeValuesOperation({
keys: keysArray,
clazz
}), this.session.sessionInfo);
for (const key of keysArray) {
const value = values[key];
if (!value) {
this.registerMissingCompareExchangeValue(key);
results[key] = null;
continue;
}
const sessionValue = this.registerCompareExchangeValue(value);
results[value.key] = sessionValue.getValue(clazz, this.session.conventions);
}
return results;
}
}
getCompareExchangeValueFromSessionInternal(key, notTracked, clazz) {
let sessionValue;
if (this._tryGetCompareExchangeValueFromSession(key, s => sessionValue = s)) {
notTracked(false);
return sessionValue.getValue(clazz, this.session.conventions);
}
notTracked(true);
return null;
}
getCompareExchangeValuesFromSessionInternal(keys, notTrackedKeysSetter, clazz) {
let noTrackedKeys;
const results = {};
if (!keys || !keys.length) {
notTrackedKeysSetter(null);
return {};
}
for (const key of keys) {
let sessionValue;
if (this._tryGetCompareExchangeValueFromSession(key, s => sessionValue = s)) {
results[key] = sessionValue.getValue(clazz, this.session.conventions);
continue;
}
if (!noTrackedKeys) {
noTrackedKeys = new Set();
}
noTrackedKeys.add(key);
}
notTrackedKeysSetter(noTrackedKeys);
return results;
}
registerMissingCompareExchangeValue(key) {
const value = new CompareExchangeSessionValue_js_1.CompareExchangeSessionValue(key, -1, "Missing");
if (this.session.noTracking) {
return value;
}
this._state.set(key, value);
return value;
}
registerCompareExchangeIncludes(values, includingMissingAtomicGuards) {
if (this.session.noTracking) {
return;
}
if (values) {
for (const [key, value] of Object.entries(values)) {
const val = CompareExchangeValueResultParser_js_1.CompareExchangeValueResultParser.getSingleValue(value, false, this.session.conventions, null);
if (includingMissingAtomicGuards
&& StringUtil_js_1.StringUtil.startsWithIgnoreCase(val.key, Constants_js_1.COMPARE_EXCHANGE.RVN_ATOMIC_PREFIX)
&& val.changeVector) {
if (!this._missingDocumentsTooAtomicGuardIndex) {
this._missingDocumentsTooAtomicGuardIndex = new Map();
}
this._missingDocumentsTooAtomicGuardIndex.set(val.key.substring(Constants_js_1.COMPARE_EXCHANGE.RVN_ATOMIC_PREFIX.length), val.changeVector);
}
else {
this._registerCompareExchangeInclude(val);
}
}
}
}
registerCompareExchangeValue(value) {
ClusterTransactionOperationsBase._assertNotAtomicGuard(value);
if (this.session.noTracking) {
return new CompareExchangeSessionValue_js_1.CompareExchangeSessionValue(value);
}
this._compareExchangeIncludes.delete(value.key);
let sessionValue = this._state.get(value.key);
if (!sessionValue) {
sessionValue = new CompareExchangeSessionValue_js_1.CompareExchangeSessionValue(value);
this._state.set(value.key, sessionValue);
return sessionValue;
}
sessionValue.updateValue(value, this.session.conventions.objectMapper);
return sessionValue;
}
_registerCompareExchangeInclude(value) {
ClusterTransactionOperationsBase._assertNotAtomicGuard(value);
if (this.session.noTracking) {
return;
}
this._compareExchangeIncludes.set(value.key, value);
}
static _assertNotAtomicGuard(value) {
if (StringUtil_js_1.StringUtil.startsWithIgnoreCase(value.key, Constants_js_1.COMPARE_EXCHANGE.RVN_ATOMIC_PREFIX)) {
(0, index_js_1.throwError)("InvalidOperationException", "'" + value.key + "' is an atomic guard and you cannot load it via the session");
}
}
_tryGetCompareExchangeValueFromSession(key, valueSetter) {
const value = this._state.get(key);
valueSetter(value);
if (!TypeUtil_js_1.TypeUtil.isNullOrUndefined(value)) {
return true;
}
const includeValue = this._compareExchangeIncludes.get(key);
if (includeValue) {
valueSetter(this.registerCompareExchangeValue(includeValue));
return true;
}
return false;
}
prepareCompareExchangeEntities(result) {
if (!this._state.size) {
return;
}
for (const [key, value] of this._state.entries()) {
const command = value.getCommand(this.session.conventions);
if (!command) {
continue;
}
result.sessionCommands.push(command);
}
}
updateState(key, index) {
let sessionValue;
if (!this._tryGetCompareExchangeValueFromSession(key, x => sessionValue = x)) {
return;
}
sessionValue.updateState(index);
}
}
exports.ClusterTransactionOperationsBase = ClusterTransactionOperationsBase;
//# sourceMappingURL=ClusterTransactionOperationsBase.js.map