@itwin/core-backend
Version:
iTwin.js backend components
261 lines • 14.7 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
if (value !== null && value !== void 0) {
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
var dispose, inner;
if (async) {
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
dispose = value[Symbol.asyncDispose];
}
if (dispose === void 0) {
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
dispose = value[Symbol.dispose];
if (async) inner = dispose;
}
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
env.stack.push({ value: value, dispose: dispose, async: async });
}
else if (async) {
env.stack.push({ async: true });
}
return value;
};
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
return function (env) {
function fail(e) {
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (r = env.stack.pop()) {
try {
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
};
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
});
import { assert, expect } from "chai";
import * as sinon from "sinon";
import { QueryBinder, QueryOptionsBuilder, QueryRowFormat } from "@itwin/core-common";
import { SnapshotDb } from "../../IModelDb";
import { IModelTestUtils } from "../IModelTestUtils";
import { Id64 } from "@itwin/core-bentley";
import { ECSqlStatement } from "../../ECSqlStatement";
import { ECDbTestHelper } from "./ECDbTestHelper";
import { KnownTestLocations } from "../KnownTestLocations";
describe("WithQueryReaderTests", () => {
let iModel;
before(async () => {
iModel = SnapshotDb.openFile(IModelTestUtils.resolveAssetFile("test.bim"));
});
after(async () => {
iModel.close();
});
it("check behvaiour if we call clearCaches in between", () => {
let actualRowCount = 0;
const expectedInstanceIds = ["0x1", "0xe", "0x10", "0x11", "0x12",
"0x13", "0x14", "0x15", "0x16", "0x17"];
expect(() => iModel.withQueryReader("SELECT * FROM bis.Element", (reader) => {
let loopCount = 0;
// First loop - read first 10 rows
while (loopCount < 10) {
reader.step();
actualRowCount++;
loopCount++;
assert.isDefined(reader.current[0]);
assert.equal(reader.current[0], expectedInstanceIds[actualRowCount - 1]);
}
assert.equal(loopCount, 10);
iModel.clearCaches();
reader.step(); // step should fail after clearCaches
})).to.throw("Step failed");
});
it("should throw error if we try to step on a closed iModelDb object", () => {
const imodelPath = iModel.pathName;
expect(() => iModel.withQueryReader("SELECT * FROM bis.Element", (reader) => {
let loopCount = 0;
while (loopCount < 10) {
reader.step();
assert.isDefined(reader.current[0]);
loopCount++;
}
assert.equal(loopCount, 10);
iModel.close();
iModel = SnapshotDb.openFile(imodelPath);
assert.isDefined(reader.current[0]);
reader.step(); // step should fail after iModelDb is closed
})).to.throw("Statement is not prepared");
});
it("returning reader from withQueryReader callback should throw error if we try to step on it", () => {
const readerObj = iModel.withQueryReader("SELECT * FROM bis.Element", (reader) => {
reader.step();
return reader;
});
expect(readerObj.current[0]).to.equal("0x1"); // will not throw error as we are just accessing current row
expect(() => readerObj.step()).to.throw("Statement is not prepared");
});
it("checking rowFormat unspecified case - values accessed by index", () => {
// Default rowFormat is UseECSqlPropertyIndexes: columns are accessed by their SELECT-order index.
iModel.withQueryReader("SELECT ECInstanceId, ECClassId FROM bis.Element", (reader) => {
assert.isTrue(reader.step());
const id = reader.current[0];
const classId = reader.current[1];
assert.isTrue(Id64.isValid(classId));
assert.isTrue(Id64.isValid(id));
assert.equal(id, "0x19");
});
});
it("checking rowFormat UseECSqlPropertyIndexes - values accessed by index", () => {
const config = new QueryOptionsBuilder().setRowFormat(QueryRowFormat.UseECSqlPropertyIndexes).getOptions();
iModel.withQueryReader("SELECT ECInstanceId, ECClassId FROM bis.Element", (reader) => {
assert.isTrue(reader.step());
// Index 0 → ECInstanceId, index 1 → ECClassId
const id = reader.current[0];
const classId = reader.current[1];
assert.equal(id, "0x19");
assert.isTrue(Id64.isValid(classId));
// Swapping column order changes index, not value
}, undefined, config);
});
it("checking rowFormat UseECSqlPropertyNames - values accessed by ECSQL property name", () => {
const config = new QueryOptionsBuilder().setRowFormat(QueryRowFormat.UseECSqlPropertyNames).getOptions();
iModel.withQueryReader("SELECT ECInstanceId, ECClassId FROM bis.Element", (reader) => {
assert.isTrue(reader.step());
const id = reader.current.ECInstanceId;
const classId = reader.current.ECClassId;
assert.equal(id, "0x19");
assert.isTrue(Id64.isValid(classId));
}, undefined, config);
});
it("checking rowFormat UseJsPropertyNames - values accessed by JavaScript property name", () => {
// eslint-disable-next-line @typescript-eslint/no-deprecated
const config = new QueryOptionsBuilder().setRowFormat(QueryRowFormat.UseJsPropertyNames).getOptions();
iModel.withQueryReader("SELECT ECInstanceId, ECClassId FROM bis.Element", (reader) => {
assert.isTrue(reader.step());
// ECInstanceId → id, ECClassId → className (resolved to a fully-qualified class name string)
const id = reader.current.id;
const className = reader.current.className;
assert.equal(id, "0x19");
// className should be in the form "SchemaName.ClassName"
assert.equal(className, "BisCore.DrawingCategory");
}, undefined, config);
});
it("checking rowFormat UseECSqlPropertyNames with convertClassIdsToClassNames - ECClassId returned as class name string", () => {
const config = new QueryOptionsBuilder()
.setRowFormat(QueryRowFormat.UseECSqlPropertyNames)
// eslint-disable-next-line @typescript-eslint/no-deprecated
.setConvertClassIdsToNames(true)
.getOptions();
iModel.withQueryReader("SELECT ECInstanceId, ECClassId FROM bis.Element", (reader) => {
assert.isTrue(reader.step());
const id = reader.current.ECInstanceId;
const classId = reader.current.ECClassId;
assert.equal(id, "0x19");
// With convertClassIdsToClassNames, ECClassId is resolved to "SchemaName.ClassName" instead of an Id
assert.equal(classId, "BisCore.DrawingCategory");
}, undefined, config);
});
describe("statement caching (regression)", () => {
// `withQueryReader` used to build a fresh `ECSqlStatement` and re-compile the ECSQL on every
// call (via `ECSqlRowExecutor`), with no caching. Callers such as `IModelTransformer` invoke
// per-element helpers (e.g. `hasSubModel`) once per element, so on large models this turned one
// cached ECSQL prepare into N full native compiles - a significant performance regression. The
// reader now reuses a prepared statement from the owning db's `_statementCache`.
const sql = "SELECT ECInstanceId FROM bis.Element WHERE ECInstanceId=?";
afterEach(() => sinon.restore());
it("reuses a single prepared statement across repeated calls instead of re-preparing", () => {
iModel.clearCaches();
const prepareSpy = sinon.spy(ECSqlStatement.prototype, "prepare"); // eslint-disable-line @typescript-eslint/no-deprecated
for (let i = 0; i < 20; i++) {
iModel.withQueryReader(sql, (reader) => {
reader.step();
}, new QueryBinder().bindId(1, "0x1"));
}
expect(prepareSpy.callCount).to.equal(1, "queries with identical ECSQL text should re-use one cached prepared statement");
});
it("keeps nested withQueryReader calls on the same SQL independent", () => {
iModel.clearCaches();
// A nested reader on the same SQL must get its own statement (findAndRemove hands the outer
// reader's statement out of the cache), so results do not corrupt each other.
const outerId = iModel.withQueryReader(sql, (outer) => {
assert.isTrue(outer.step());
const innerId = iModel.withQueryReader(sql, (inner) => {
assert.isTrue(inner.step());
return inner.current[0];
}, new QueryBinder().bindId(1, "0xe"));
expect(innerId).to.equal("0xe");
// Outer reader's cursor must be unaffected by the nested reader.
return outer.current[0];
}, new QueryBinder().bindId(1, "0x1"));
expect(outerId).to.equal("0x1");
});
it("recovers from a prepare failure without poisoning the cache", () => {
iModel.clearCaches();
const invalidSql = "SELECT * FROM bis.ThisClassDoesNotExist";
const prepareSpy = sinon.spy(ECSqlStatement.prototype, "prepare"); // eslint-disable-line @typescript-eslint/no-deprecated
expect(() => iModel.withQueryReader(invalidSql, (reader) => reader.step())).to.throw();
expect(() => iModel.withQueryReader(invalidSql, (reader) => reader.step())).to.throw();
expect(prepareSpy.callCount).to.equal(2, "a statement that failed to prepare must not be cached");
iModel.withQueryReader(sql, (reader) => {
assert.isTrue(reader.step());
expect(reader.current[0]).to.equal("0x1");
}, new QueryBinder().bindId(1, "0x1"));
});
it("releases the executor and cached statement when binding fails during reader construction", () => {
iModel.clearCaches();
const prepareSpy = sinon.spy(ECSqlStatement.prototype, "prepare"); // eslint-disable-line @typescript-eslint/no-deprecated
iModel.withQueryReader(sql, (reader) => reader.step(), new QueryBinder().bindId(1, "0x1"));
const listenerCount = iModel.onBeforeClose.numberOfListeners;
const bindStub = sinon.stub(ECSqlStatement.prototype, "bindParams").throws(new Error("Expected bind failure")); // eslint-disable-line @typescript-eslint/no-deprecated
expect(() => iModel.withQueryReader(sql, () => undefined, new QueryBinder().bindId(1, "0x1"))).to.throw("Expected bind failure");
bindStub.restore();
expect(iModel.onBeforeClose.numberOfListeners).to.equal(listenerCount);
iModel.withQueryReader(sql, (reader) => reader.step(), new QueryBinder().bindId(1, "0x1"));
expect(prepareSpy.callCount).to.equal(1, "the statement checked out before the bind failure should be returned to the cache");
});
it("also caches for ECDb.withQueryReader (shared ECSqlRowExecutor path)", () => {
const env_1 = { stack: [], error: void 0, hasError: false };
try {
const ecdb = __addDisposableResource(env_1, ECDbTestHelper.createECDb(KnownTestLocations.outputDir, "syncReaderStmtCache.ecdb", `<ECSchema schemaName="Test" alias="ts" version="01.00.00" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2">
<ECEntityClass typeName="Foo" modifier="Sealed">
<ECProperty propertyName="n" typeName="int"/>
</ECEntityClass>
</ECSchema>`), false);
ecdb.saveChanges();
const ecdbSql = "SELECT ECInstanceId FROM meta.ECClassDef WHERE ECInstanceId=?";
const prepareSpy = sinon.spy(ECSqlStatement.prototype, "prepare"); // eslint-disable-line @typescript-eslint/no-deprecated
for (let i = 0; i < 10; i++)
ecdb.withQueryReader(ecdbSql, (reader) => reader.step(), new QueryBinder().bindId(1, "0x1"));
expect(prepareSpy.callCount).to.equal(1, "ECDb.withQueryReader should also reuse a cached prepared statement");
}
catch (e_1) {
env_1.error = e_1;
env_1.hasError = true;
}
finally {
__disposeResources(env_1);
}
});
});
});
//# sourceMappingURL=ECSqlSyncReader.test.js.map