UNPKG

@itwin/core-backend

Version:
536 lines • 27.5 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module iModels */ // cspell:ignore cset csets ecchanges import * as path from "path"; import { BeDuration, ChangeSetStatus, DbResult, IModelHubStatus, IModelStatus, Logger, OpenMode, StopWatch } from "@itwin/core-bentley"; import { BriefcaseIdValue, ChangesetType, IModelError, IModelVersion, } from "@itwin/core-common"; import { BackendLoggerCategory } from "./BackendLoggerCategory"; import { CheckpointManager } from "./CheckpointManager"; import { IModelDb } from "./IModelDb"; import { IModelHost } from "./IModelHost"; import { IModelJsFs } from "./IModelJsFs"; import { SchemaSync } from "./SchemaSync"; import { _hubAccess, _nativeDb, _releaseAllLocks } from "./internal/Symbols"; import { IModelNative } from "./internal/NativePlatform"; const loggerCategory = BackendLoggerCategory.IModelDb; /** Manages downloading Briefcases and downloading and uploading changesets. * @public */ export class BriefcaseManager { /** Get the local path of the folder storing files that are associated with an imodel */ static getIModelPath(iModelId) { return path.join(this._cacheDir, iModelId); } /** @internal */ static getChangeSetsPath(iModelId) { return path.join(this.getIModelPath(iModelId), "changesets"); } /** @internal */ static getChangeCachePathName(iModelId) { return path.join(this.getIModelPath(iModelId), iModelId.concat(".bim.ecchanges")); } /** @internal */ static getChangedElementsPathName(iModelId) { return path.join(this.getIModelPath(iModelId), iModelId.concat(".bim.elems")); } static _briefcaseSubDir = "briefcases"; /** Get the local path of the folder storing briefcases associated with the specified iModel. */ static getBriefcaseBasePath(iModelId) { return path.join(this.getIModelPath(iModelId), this._briefcaseSubDir); } /** Get the name of the local file that holds, or will hold, a local briefcase in the briefcase cache. * @note The briefcase cache is a local directory established in the call to [[BriefcaseManager.initialize]]. * @param briefcase the iModelId and BriefcaseId for the filename * @see getIModelPath */ static getFileName(briefcase) { return path.join(this.getBriefcaseBasePath(briefcase.iModelId), `${briefcase.briefcaseId}.bim`); } static setupCacheDir(cacheRootDir) { this._cacheDir = cacheRootDir; IModelJsFs.recursiveMkDirSync(this._cacheDir); } static _initialized; /** Initialize BriefcaseManager * @param cacheRootDir The root directory for storing a cache of downloaded briefcase files on the local computer. * Briefcases are stored relative to this path in sub-folders organized by IModelId. * @note It is perfectly valid for applications to store briefcases in locations they manage, outside of `cacheRootDir`. */ static initialize(cacheRootDir) { if (this._initialized) return; this.setupCacheDir(cacheRootDir); IModelHost.onBeforeShutdown.addOnce(() => this.finalize()); this._initialized = true; } static finalize() { this._initialized = false; } /** Get a list of the local briefcase held in the briefcase cache, optionally for a single iModelId * @param iModelId if present, only briefcases for this iModelId are returned, otherwise all briefcases for all * iModels in the briefcase cache are returned. * @note usually there should only be one briefcase per iModel. */ static getCachedBriefcases(iModelId) { const briefcaseList = []; const iModelDirs = IModelJsFs.readdirSync(this._cacheDir); for (const iModelDir of iModelDirs) { if (iModelId && iModelId !== iModelDir) continue; const bcPath = path.join(this._cacheDir, iModelDir, this._briefcaseSubDir); try { if (!IModelJsFs.lstatSync(bcPath)?.isDirectory) continue; } catch { continue; } const briefcases = IModelJsFs.readdirSync(bcPath); for (const briefcaseName of briefcases) { if (briefcaseName.endsWith(".bim")) { try { const fileName = path.join(bcPath, briefcaseName); const fileSize = IModelJsFs.lstatSync(fileName)?.size ?? 0; const db = IModelDb.openDgnDb({ path: fileName }, OpenMode.Readonly); briefcaseList.push({ fileName, iTwinId: db.getITwinId(), iModelId: db.getIModelId(), briefcaseId: db.getBriefcaseId(), changeset: db.getCurrentChangeset(), fileSize }); db.closeFile(); } catch { } } } } return briefcaseList; } static _cacheDir; /** Get the root directory for the briefcase cache */ static get cacheDir() { return this._cacheDir; } /** Determine whether the supplied briefcaseId is in the range of assigned BriefcaseIds issued by iModelHub * @note this does check whether the id was actually acquired by the caller. */ static isValidBriefcaseId(id) { return id >= BriefcaseIdValue.FirstValid && id <= BriefcaseIdValue.LastValid; } /** Acquire a new briefcaseId from iModelHub for the supplied iModelId * @note usually there should only be one briefcase per iModel per user. If a single user acquires more than one briefcaseId, * it's a good idea to supply different aliases for each of them. */ static async acquireNewBriefcaseId(arg) { return IModelHost[_hubAccess].acquireNewBriefcaseId(arg); } /** * Download a new briefcase from iModelHub for the supplied iModelId. * * Briefcases are local files holding a copy of an iModel. * Briefcases may either be specific to an individual user (so that it can be modified to create changesets), or it can be readonly so it can accept but not create changesets. * Every briefcase internally holds its [[BriefcaseId]]. Writeable briefcases have a `BriefcaseId` "assigned" to them by iModelHub. No two users will ever have the same BriefcaseId. * Readonly briefcases are "unassigned" with the special value [[BriefcaseId.Unassigned]]. * * Typically a given user will have only one briefcase on their machine for a given iModelId. Rarely, it may be necessary to use more than one * briefcase to make isolated independent sets of changes, but that is exceedingly complicated and rare. * * Callers of this method may supply a BriefcaseId, or if none is supplied, a new one is acquired from iModelHub. * * @param arg The arguments that specify the briefcase file to be downloaded. * @returns The properties of the local briefcase in a Promise that is resolved after the briefcase is fully downloaded and the briefcase file is ready for use via [BriefcaseDb.open]($backend). * @note The location of the local file to hold the briefcase is arbitrary and may be any valid *local* path on your machine. If you don't supply * a filename, the local briefcase cache is used by creating a file with the briefcaseId as its name in the `briefcases` folder below the folder named * for the IModelId. * @note *It is invalid to edit briefcases on a shared network drive* and that is a sure way to corrupt your briefcase (see https://www.sqlite.org/howtocorrupt.html) */ static async downloadBriefcase(arg) { const briefcaseId = arg.briefcaseId ?? await this.acquireNewBriefcaseId(arg); const fileName = arg.fileName ?? this.getFileName({ ...arg, briefcaseId }); if (IModelJsFs.existsSync(fileName)) throw new IModelError(IModelStatus.FileAlreadyExists, `Briefcase "${fileName}" already exists`); const asOf = arg.asOf ?? IModelVersion.latest().toJSON(); const changeset = await IModelHost[_hubAccess].getChangesetFromVersion({ ...arg, version: IModelVersion.fromJSON(asOf) }); const checkpoint = { ...arg, changeset }; try { await CheckpointManager.downloadCheckpoint({ localFile: fileName, checkpoint, onProgress: arg.onProgress }); } catch (error) { const errorMessage = `Failed to download briefcase to ${fileName}, errorMessage: ${error.message}`; if (arg.accessToken && arg.briefcaseId === undefined) { Logger.logInfo(loggerCategory, `${errorMessage}, releasing the briefcaseId...`); await this.releaseBriefcase(arg.accessToken, { briefcaseId, iModelId: arg.iModelId }); } if (IModelJsFs.existsSync(fileName)) { if (arg.accessToken && arg.briefcaseId === undefined) Logger.logTrace(loggerCategory, `Deleting the file: ${fileName}...`); else Logger.logInfo(loggerCategory, `${errorMessage}, deleting the file...`); try { IModelJsFs.unlinkSync(fileName); Logger.logInfo(loggerCategory, `Deleted ${fileName}`); } catch (deleteError) { Logger.logWarning(loggerCategory, `Failed to delete ${fileName}. errorMessage: ${deleteError.message}`); } } throw error; } const fileSize = IModelJsFs.lstatSync(fileName)?.size ?? 0; const response = { fileName, briefcaseId, iModelId: arg.iModelId, iTwinId: arg.iTwinId, changeset: checkpoint.changeset, fileSize, }; // now open the downloaded checkpoint and reset its BriefcaseId const nativeDb = new IModelNative.platform.DgnDb(); try { nativeDb.openIModel(fileName, OpenMode.ReadWrite); } catch (err) { throw new IModelError(err.errorNumber, `Could not open downloaded briefcase for write access: ${fileName}, err=${err.message}`); } try { nativeDb.enableWalMode(); // local briefcases should use WAL journal mode nativeDb.resetBriefcaseId(briefcaseId); if (nativeDb.getCurrentChangeset().id !== checkpoint.changeset.id) throw new IModelError(IModelStatus.InvalidId, `Downloaded briefcase has wrong changesetId: ${fileName}`); } finally { nativeDb.saveChanges(); nativeDb.closeFile(); } return response; } /** Deletes change sets of an iModel from local disk * @internal */ static deleteChangeSetsFromLocalDisk(iModelId) { const changesetsPath = BriefcaseManager.getChangeSetsPath(iModelId); BriefcaseManager.deleteFolderAndContents(changesetsPath); } /** Releases a briefcaseId from iModelHub. After this call it is illegal to generate changesets for the released briefcaseId. * @note generally, this method should not be called directly. Instead use [[deleteBriefcaseFiles]]. * @see deleteBriefcaseFiles */ static async releaseBriefcase(accessToken, briefcase) { if (this.isValidBriefcaseId(briefcase.briefcaseId)) return IModelHost[_hubAccess].releaseBriefcase({ accessToken, iModelId: briefcase.iModelId, briefcaseId: briefcase.briefcaseId }); } /** * Delete and clean up a briefcase and all of its associated files. First, this method opens the supplied filename to determine its briefcaseId. * Then, if a requestContext is supplied, it releases a BriefcaseId from iModelHub. Finally it deletes the local briefcase file and * associated files (that is, all files in the same directory that start with the briefcase name). * @param filePath the full file name of the Briefcase to delete * @param accessToken for releasing the briefcaseId */ static async deleteBriefcaseFiles(filePath, accessToken) { try { const db = IModelDb.openDgnDb({ path: filePath }, OpenMode.Readonly); const briefcase = { iModelId: db.getIModelId(), briefcaseId: db.getBriefcaseId(), }; db.closeFile(); if (accessToken) { if (this.isValidBriefcaseId(briefcase.briefcaseId)) { await BriefcaseManager.releaseBriefcase(accessToken, briefcase); } } } catch { } // first try to delete the briefcase file try { if (IModelJsFs.existsSync(filePath)) IModelJsFs.unlinkSync(filePath); } catch (err) { throw new IModelError(IModelStatus.BadRequest, `cannot delete briefcase file ${err}`); } // next, delete all files that start with the briefcase's filePath (e.g. "a.bim-locks", "a.bim-journal", etc.) try { const dirName = path.dirname(filePath); const fileName = path.basename(filePath); const files = IModelJsFs.readdirSync(dirName); for (const file of files) { if (file.startsWith(fileName)) this.deleteFile(path.join(dirName, file)); // don't throw on error } } catch { } } /** Deletes a file * - Does not throw any error, but logs it instead * - Returns true if the delete was successful */ static deleteFile(pathname) { try { IModelJsFs.unlinkSync(pathname); } catch (error) { Logger.logError(loggerCategory, `Cannot delete file ${pathname}, ${error}`); return false; } return true; } /** Deletes a folder, checking if it's empty * - Does not throw any error, but logs it instead * - Returns true if the delete was successful */ static deleteFolderIfEmpty(folderPathname) { try { const files = IModelJsFs.readdirSync(folderPathname); if (files.length > 0) return false; IModelJsFs.rmdirSync(folderPathname); } catch { Logger.logError(loggerCategory, `Cannot delete folder: ${folderPathname}`); return false; } return true; } /** Deletes the contents of a folder, but not the folder itself * - Does not throw any errors, but logs them. * - returns true if the delete was successful. */ static deleteFolderContents(folderPathname) { if (!IModelJsFs.existsSync(folderPathname)) return false; let status = true; const files = IModelJsFs.readdirSync(folderPathname); for (const file of files) { const curPath = path.join(folderPathname, file); const locStatus = (IModelJsFs.lstatSync(curPath)?.isDirectory) ? BriefcaseManager.deleteFolderAndContents(curPath) : BriefcaseManager.deleteFile(curPath); if (!locStatus) status = false; } return status; } /** Download all the changesets in the specified range. * @beta */ static async downloadChangesets(arg) { return IModelHost[_hubAccess].downloadChangesets(arg); } /** Download a single changeset. * @beta */ static async downloadChangeset(arg) { return IModelHost[_hubAccess].downloadChangeset(arg); } /** Query the hub for the properties for a ChangesetIndex or ChangesetId */ static async queryChangeset(arg) { return IModelHost[_hubAccess].queryChangeset({ ...arg, accessToken: await IModelHost.getAccessToken() }); } /** Query the hub for an array of changeset properties given a ChangesetRange */ static async queryChangesets(arg) { return IModelHost[_hubAccess].queryChangesets({ ...arg, accessToken: await IModelHost.getAccessToken() }); } /** Query the hub for the ChangesetProps of the most recent changeset */ static async getLatestChangeset(arg) { return IModelHost[_hubAccess].getLatestChangeset({ ...arg, accessToken: await IModelHost.getAccessToken() }); } /** Query the Id of an iModel by name. * @param arg Identifies the iModel of interest * @returns the Id of the corresponding iModel, or `undefined` if no such iModel exists. */ static async queryIModelByName(arg) { return IModelHost[_hubAccess].queryIModelByName(arg); } /** Deletes a folder and all it's contents. * - Does not throw any errors, but logs them. * - returns true if the delete was successful. */ static deleteFolderAndContents(folderPathname) { if (!IModelJsFs.existsSync(folderPathname)) return true; let status = false; status = BriefcaseManager.deleteFolderContents(folderPathname); if (!status) return false; status = BriefcaseManager.deleteFolderIfEmpty(folderPathname); return status; } static async applySingleChangeset(db, changesetFile, fastForward) { if (changesetFile.changesType === ChangesetType.Schema || changesetFile.changesType === ChangesetType.SchemaSync) db.clearCaches(); // for schema changesets, statement caches may become invalid. Do this *before* applying, in case db needs to be closed (open statements hold db open.) db[_nativeDb].applyChangeset(changesetFile, fastForward); db.changeset = db[_nativeDb].getCurrentChangeset(); // we're done with this changeset, delete it IModelJsFs.removeSync(changesetFile.pathname); } /** @internal */ static async revertTimelineChanges(db, arg) { if (!db.isOpen || db[_nativeDb].isReadonly()) throw new IModelError(ChangeSetStatus.ApplyError, "Briefcase must be open ReadWrite to revert timeline changes"); let currentIndex = db.changeset.index; if (currentIndex === undefined) currentIndex = (await IModelHost[_hubAccess].queryChangeset({ accessToken: arg.accessToken, iModelId: db.iModelId, changeset: { id: db.changeset.id } })).index; if (!arg.toIndex) { throw new IModelError(ChangeSetStatus.ApplyError, "toIndex must be specified to revert changesets"); } if (arg.toIndex > currentIndex) { throw new IModelError(ChangeSetStatus.ApplyError, "toIndex must be less than or equal to the current index"); } if (!db.holdsSchemaLock) { throw new IModelError(ChangeSetStatus.ApplyError, "Cannot revert timeline changesets without holding a schema lock"); } // Download change sets const changesets = await IModelHost[_hubAccess].downloadChangesets({ accessToken: arg.accessToken, iModelId: db.iModelId, range: { first: arg.toIndex, end: currentIndex }, targetDir: BriefcaseManager.getChangeSetsPath(db.iModelId), progressCallback: arg.onProgress, }); if (changesets.length === 0) return; changesets.reverse(); db.clearCaches(); const stopwatch = new StopWatch(`Reverting changes`, true); Logger.logInfo(loggerCategory, `Starting reverting timeline changes from ${arg.toIndex} to ${currentIndex}`); /** * Revert timeline changes from the current index to the specified index. * It does not change parent of the current changeset. * All changes during revert operation are stored in a new changeset. * Revert operation require schema lock as we do not acquire individual locks for each element. * Optionally schema changes can be skipped (required for schema sync case). */ db[_nativeDb].revertTimelineChanges(changesets, arg.skipSchemaChanges ?? false); Logger.logInfo(loggerCategory, `Reverted timeline changes from ${arg.toIndex} to ${currentIndex} (${stopwatch.elapsedSeconds} seconds)`); changesets.forEach((changeset) => { IModelJsFs.removeSync(changeset.pathname); }); db.notifyChangesetApplied(); } /** @internal */ static async pullAndApplyChangesets(db, arg) { if (!db.isOpen || db[_nativeDb].isReadonly()) // don't use db.isReadonly - we reopen the file writable just for this operation but db.isReadonly is still true throw new IModelError(ChangeSetStatus.ApplyError, "Briefcase must be open ReadWrite to process change sets"); let currentIndex = db.changeset.index; if (currentIndex === undefined) currentIndex = (await IModelHost[_hubAccess].queryChangeset({ accessToken: arg.accessToken, iModelId: db.iModelId, changeset: { id: db.changeset.id } })).index; const reverse = (arg.toIndex && arg.toIndex < currentIndex) ? true : false; if (db[_nativeDb].hasPendingTxns() && reverse) { throw new IModelError(ChangeSetStatus.ApplyError, "Cannot reverse changesets when there are pending changes"); } // Download change sets const changesets = await IModelHost[_hubAccess].downloadChangesets({ accessToken: arg.accessToken, iModelId: db.iModelId, range: { first: reverse ? arg.toIndex + 1 : currentIndex + 1, end: reverse ? currentIndex : arg.toIndex }, // eslint-disable-line @typescript-eslint/no-non-null-assertion targetDir: BriefcaseManager.getChangeSetsPath(db.iModelId), progressCallback: arg.onProgress, }); if (changesets.length === 0) return; // nothing to apply if (reverse) changesets.reverse(); let appliedChangesets = -1; if (db[_nativeDb].hasPendingTxns() && !reverse && !arg.noFastForward) { // attempt to perform fast forward for (const changeset of changesets) { // do not waste time on schema changesets. They cannot be fastforwarded. if (changeset.changesType === ChangesetType.Schema || changeset.changesType === ChangesetType.SchemaSync) break; try { const stopwatch = new StopWatch(`[${changeset.id}]`, true); Logger.logInfo(loggerCategory, `Starting application of changeset with id ${stopwatch.description} using fast forward method`); await this.applySingleChangeset(db, changeset, true); Logger.logInfo(loggerCategory, `Applied changeset with id ${stopwatch.description} (${stopwatch.elapsedSeconds} seconds)`); appliedChangesets++; db.saveChanges(); } catch { db.abandonChanges(); break; } } } if (appliedChangesets < changesets.length - 1) { db[_nativeDb].pullMergeBegin(); for (const changeset of changesets.filter((_, index) => index > appliedChangesets)) { const stopwatch = new StopWatch(`[${changeset.id}]`, true); Logger.logInfo(loggerCategory, `Starting application of changeset with id ${stopwatch.description}`); try { await this.applySingleChangeset(db, changeset, false); Logger.logInfo(loggerCategory, `Applied changeset with id ${stopwatch.description} (${stopwatch.elapsedSeconds} seconds)`); } catch (err) { if (err instanceof Error) { Logger.logError(loggerCategory, `Error applying changeset with id ${stopwatch.description}: ${err.message}`); } db.abandonChanges(); db[_nativeDb].pullMergeEnd(); throw err; } } db[_nativeDb].pullMergeEnd(); if (!db.isReadonly) { db.saveChanges("Merge."); } } // notify listeners db.notifyChangesetApplied(); } /** create a changeset from the current changes, and push it to iModelHub */ static async pushChanges(db, arg) { const changesetProps = db[_nativeDb].startCreateChangeset(); changesetProps.briefcaseId = db.briefcaseId; changesetProps.description = arg.description; const fileSize = IModelJsFs.lstatSync(changesetProps.pathname)?.size; if (!fileSize) // either undefined or 0 means error throw new IModelError(IModelStatus.NoContent, "error creating changeset"); changesetProps.size = fileSize; const id = IModelNative.platform.DgnDb.computeChangesetId(changesetProps); if (id !== changesetProps.id) { throw new IModelError(DbResult.BE_SQLITE_ERROR_InvalidChangeSetVersion, `Changeset id ${changesetProps.id} does not match computed id ${id}.`); } let retryCount = arg.pushRetryCount ?? 3; while (true) { try { const accessToken = await IModelHost.getAccessToken(); const index = await IModelHost[_hubAccess].pushChangeset({ accessToken, iModelId: db.iModelId, changesetProps }); db[_nativeDb].completeCreateChangeset({ index }); db.changeset = db[_nativeDb].getCurrentChangeset(); if (!arg.retainLocks) await db.locks[_releaseAllLocks](); return; } catch (err) { const shouldRetry = () => { if (retryCount-- <= 0) return false; switch (err.errorNumber) { case IModelHubStatus.AnotherUserPushing: case IModelHubStatus.DatabaseTemporarilyLocked: case IModelHubStatus.OperationFailed: return true; } return false; }; if (!shouldRetry()) { db[_nativeDb].abandonCreateChangeset(); throw err; } } finally { IModelJsFs.removeSync(changesetProps.pathname); } } } /** Pull/merge (if necessary), then push all local changes as a changeset. Called by [[BriefcaseDb.pushChanges]] * @internal */ static async pullMergePush(db, arg) { let retryCount = arg.mergeRetryCount ?? 5; while (true) { try { await BriefcaseManager.pullAndApplyChangesets(db, arg); if (!db.skipSyncSchemasOnPullAndPush) await SchemaSync.pull(db); return await BriefcaseManager.pushChanges(db, arg); } catch (err) { if (retryCount-- <= 0 || err.errorNumber !== IModelHubStatus.PullIsRequired) throw (err); await (arg.mergeRetryDelay ?? BeDuration.fromSeconds(3)).wait(); } } } } //# sourceMappingURL=BriefcaseManager.js.map