Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private async _extractChangeSummary(changeSetId: string) {
const accessToken: AccessToken = await this._tokenStore!.getAccessToken();
if (!this._iModelDb) {
// Open a new local briefcase of the iModel at the specified version
this._iModelDb = await IModelDb.open(actx, accessToken, this._projectId!, this._iModelId!, OpenParams.pullOnly(AccessMode.Exclusive), IModelVersion.asOfChangeSet(changeSetId));
} else {
// Update the existing local briefcase of the iModel to the specified version
await this._iModelDb.pullAndMergeChanges(actx, accessToken, IModelVersion.asOfChangeSet(changeSetId));
}
// Extract summary information about the current version of the briefcase/iModel into the change cache
const changeSummaryIds: Id64String[] = await ChangeSummaryManager.extractChangeSummaries(actx, accessToken, this._iModelDb!, { currentVersionOnly: true });
Logger.logTrace(QueryAgentConfig.loggingCategory, `Extracted summary information from change set "${changeSetId}"`);
// Attach a change cache file to the iModel to enable querying the change summary
ChangeSummaryManager.attachChangeCache(this._iModelDb);
// Find the change summary that was just created
assert(changeSummaryIds.length === 1);
const changeSummary: ChangeSummary = ChangeSummaryManager.queryChangeSummary(this._iModelDb!, changeSummaryIds[0]);
/*
* Query the change summary to gather up all the content
*/
private async _extractChangeSummary(changeSetId: string) {
const accessToken: AccessToken = await this._tokenStore!.getAccessToken();
if (!this._iModelDb) {
// Open a new local briefcase of the iModel at the specified version
this._iModelDb = await IModelDb.open(actx, accessToken, this._projectId!, this._iModelId!, OpenParams.pullOnly(AccessMode.Exclusive), IModelVersion.asOfChangeSet(changeSetId));
} else {
// Update the existing local briefcase of the iModel to the specified version
await this._iModelDb.pullAndMergeChanges(actx, accessToken, IModelVersion.asOfChangeSet(changeSetId));
}
// Extract summary information about the current version of the briefcase/iModel into the change cache
const changeSummaryIds: Id64String[] = await ChangeSummaryManager.extractChangeSummaries(actx, accessToken, this._iModelDb!, { currentVersionOnly: true });
Logger.logTrace(QueryAgentConfig.loggingCategory, `Extracted summary information from change set "${changeSetId}"`);
// Attach a change cache file to the iModel to enable querying the change summary
ChangeSummaryManager.attachChangeCache(this._iModelDb);
// Find the change summary that was just created
assert(changeSummaryIds.length === 1);
const changeSummary: ChangeSummary = ChangeSummaryManager.queryChangeSummary(this._iModelDb!, changeSummaryIds[0]);
public async openIModel(contextId: string, iModelId: GuidString, openMode?: OpenMode, changeSetId?: string): Promise {
try {
// GatewayProxyApi.setAccessToken(accessToken);
const iModelConnection: IModelConnection = await IModelConnection.open(contextId, iModelId, openMode ? openMode : OpenMode.Readonly, changeSetId ? IModelVersion.asOfChangeSet(changeSetId) : IModelVersion.latest());
return iModelConnection;
} catch (e) {
alert(JSON.stringify(e));
return Promise.reject(e);
}
}
for (let i = endChangeSetIx; i >= 0; i--) {
const currentChangeSetInfo: ChangeSet = changeSetInfos[i];
const currentChangeSetId: GuidString = currentChangeSetInfo.wsgId;
Logger.logInfo(loggerCategory, `Started Change Summary extraction for changeset #${i + 1}...`, () => ({ iModelId: ctx.iModelId, changeSetId: currentChangeSetId }));
const existingSummaryId: Id64String | undefined = ChangeSummaryManager.isSummaryAlreadyExtracted(changesFile, currentChangeSetId);
if (!!existingSummaryId) {
Logger.logInfo(loggerCategory, `Change Summary for changeset #${i + 1} already exists. It is not extracted again.`, () => ({ iModelId: ctx.iModelId, changeSetId: currentChangeSetId }));
summaries.push(existingSummaryId);
continue;
}
// iModel is at end changeset, so no need to reverse for it.
if (i !== endChangeSetIx) {
perfLogger = new PerfLogger("ChangeSummaryManager.extractChangeSummaries>Roll iModel to previous changeset");
await iModel.reverseChanges(requestContext, IModelVersion.asOfChangeSet(currentChangeSetId));
requestContext.enter();
perfLogger.dispose();
Logger.logTrace(loggerCategory, `Moved iModel to changeset #${i + 1} to extract summary from.`, () => ({ iModelId: ctx.iModelId, changeSetId: currentChangeSetId }));
}
const changeSetFilePath: string = path.join(changeSetsFolder, currentChangeSetInfo.fileName!);
if (!IModelJsFs.existsSync(changeSetFilePath))
throw new IModelError(IModelStatus.FileNotFound, "Failed to extract change summary: Changeset file '" + changeSetFilePath + "' does not exist.");
perfLogger = new PerfLogger("ChangeSummaryManager.extractChangeSummaries>Extract ChangeSummary");
const stat: IModelJsNative.ErrorStatusOrResult = iModel.nativeDb.extractChangeSummary(changesFile.nativeDb, changeSetFilePath);
perfLogger.dispose();
if (stat.error && stat.error.status !== DbResult.BE_SQLITE_OK)
throw new IModelError(stat.error.status, stat.error.message);
Logger.logTrace(loggerCategory, `Actual Change summary extraction done for changeset #${i + 1}.`, () => ({ iModelId: ctx.iModelId, changeSetId: currentChangeSetId }));
Logger.logInfo(loggerCategory, `Finished Change Summary extraction for changeset #${i + 1}.`, () => ({ iModelId: ctx.iModelId, changeSetId: currentChangeSetId }));
}
changesFile.saveChanges();
return summaries;
} finally {
changesFile.dispose();
// Reattach change cache if it was attached before the extraction
if (isChangeCacheAttached)
ChangeSummaryManager.attachChangeCache(iModel);
perfLogger = new PerfLogger("ChangeSummaryManager.extractChangeSummaries>Move iModel to original changeset");
if (iModel.briefcase.currentChangeSetId !== endChangeSetId)
await iModel.reinstateChanges(requestContext, IModelVersion.asOfChangeSet(endChangeSetId));
requestContext.enter();
perfLogger.dispose();
Logger.logTrace(loggerCategory, "Moved iModel to initial changeset (the end changeset).", () => ({ iModelId: ctx.iModelId, startChangeSetId, endChangeSetId }));
totalPerf.dispose();
Logger.logInfo(loggerCategory, "Finished Change Summary extraction.", () => ({ iModelId: ctx.iModelId, startChangeSetId, endChangeSetId }));
}
}
public async openForRead(tokenProps: IModelTokenProps): Promise {
const requestContext = ClientRequestContext.current as AuthorizedClientRequestContext;
const iModelToken = IModelToken.fromJSON(tokenProps);
const openParams: OpenParams = OpenParams.fixedVersion();
openParams.timeout = 1000; // 1 second
const iModelVersion = IModelVersion.asOfChangeSet(iModelToken.changeSetId!);
const db = await IModelDb.open(requestContext, iModelToken.contextId!, iModelToken.iModelId!, openParams, iModelVersion);
return db.toJSON();
}
public static async reinstateChanges(requestContext: AuthorizedClientRequestContext, briefcase: BriefcaseEntry, reinstateToVersion?: IModelVersion): Promise {
requestContext.enter();
if (briefcase.openParams.openMode === OpenMode.Readonly)
throw new IModelError(ChangeSetStatus.ApplyError, "Cannot reinstate (or reverse) changes in a ReadOnly briefcase", Logger.logError, loggerCategory, () => briefcase.getDebugInfo());
const targetVersion: IModelVersion = reinstateToVersion || IModelVersion.asOfChangeSet(briefcase.parentChangeSetId);
const { changeSetId: targetChangeSetId, changeSetIndex: targetChangeSetIndex } = await BriefcaseManager.evaluateVersion(requestContext, targetVersion, briefcase.iModelId);
requestContext.enter();
if (targetChangeSetIndex < briefcase.currentChangeSetIndex)
return Promise.reject(new IModelError(ChangeSetStatus.ApplyError, "Can reinstate only to a later version", Logger.logError, loggerCategory, () => ({ ...briefcase.getDebugInfo(), targetChangeSetId, targetChangeSetIndex })));
return BriefcaseManager.processChangeSets(requestContext, briefcase, targetChangeSetId, targetChangeSetIndex);
}
private onBriefcaseBeforeOpenHandler(requestContext: AuthorizedClientRequestContext) {
if (!this.briefcase.iModelDb)
return;
IModelDb.onOpen.raiseEvent(requestContext, this.briefcase.contextId, this.briefcase.iModelId, this.briefcase.openParams, this.briefcase.contextId, IModelVersion.asOfChangeSet(this.briefcase.targetChangeSetId));
}