@itwin/core-backend 5.9.0 → 5.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,58 @@
6
6
  /** @packageDocumentation
7
7
  * @module iModels
8
8
  */
9
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
10
+ if (value !== null && value !== void 0) {
11
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
12
+ var dispose, inner;
13
+ if (async) {
14
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
15
+ dispose = value[Symbol.asyncDispose];
16
+ }
17
+ if (dispose === void 0) {
18
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
19
+ dispose = value[Symbol.dispose];
20
+ if (async) inner = dispose;
21
+ }
22
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
23
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
24
+ env.stack.push({ value: value, dispose: dispose, async: async });
25
+ }
26
+ else if (async) {
27
+ env.stack.push({ async: true });
28
+ }
29
+ return value;
30
+ };
31
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
32
+ return function (env) {
33
+ function fail(e) {
34
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
35
+ env.hasError = true;
36
+ }
37
+ var r, s = 0;
38
+ function next() {
39
+ while (r = env.stack.pop()) {
40
+ try {
41
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
42
+ if (r.dispose) {
43
+ var result = r.dispose.call(r.value);
44
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
45
+ }
46
+ else s |= 1;
47
+ }
48
+ catch (e) {
49
+ fail(e);
50
+ }
51
+ }
52
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
53
+ if (env.hasError) throw env.error;
54
+ }
55
+ return next();
56
+ };
57
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
58
+ var e = new Error(message);
59
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
60
+ });
9
61
  Object.defineProperty(exports, "__esModule", { value: true });
10
62
  exports.TxnManager = exports.RebaseManager = void 0;
11
63
  exports.setMaxEntitiesPerEvent = setMaxEntitiesPerEvent;
@@ -17,6 +69,8 @@ const IpcHost_1 = require("./IpcHost");
17
69
  const Symbols_1 = require("./internal/Symbols");
18
70
  const ChangesetConflictArgs_1 = require("./internal/ChangesetConflictArgs");
19
71
  const BriefcaseManager_1 = require("./BriefcaseManager");
72
+ const ChangesetReader_1 = require("./ChangesetReader");
73
+ const PartialChangeUnifier_1 = require("./PartialChangeUnifier");
20
74
  /** Strictly for tests. @internal */
21
75
  function setMaxEntitiesPerEvent(max) {
22
76
  const prevMax = ChangedEntitiesProc.maxPerEvent;
@@ -518,16 +572,15 @@ class RebaseManager {
518
572
  if (!BriefcaseManager_1.BriefcaseManager.semanticRebaseDataFolderExists(this._iModel, txnProps.id)) {
519
573
  throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, `Local folder does not exist for transaction ${txnProps.id}`);
520
574
  }
521
- const changedInstances = BriefcaseManager_1.BriefcaseManager.getChangedInstancesDataForTxn(this._iModel, txnProps.id);
522
- changedInstances.forEach((instance) => {
523
- if (instance.isIndirect) {
575
+ for await (const instance of BriefcaseManager_1.BriefcaseManager.getChangedInstancesDataForTxn(this._iModel, txnProps.id)) {
576
+ if (instance.$meta.isIndirectChange) {
524
577
  this._iModel.txns.withIndirectTxnMode(() => {
525
578
  this.applyInstancePatch(instance);
526
579
  });
527
- return;
580
+ continue;
528
581
  }
529
582
  this.applyInstancePatch(instance);
530
- });
583
+ }
531
584
  BriefcaseManager_1.BriefcaseManager.deleteTxnDataFolder(this._iModel, txnProps.id); // delete the folder after importing
532
585
  }
533
586
  else {
@@ -541,27 +594,34 @@ class RebaseManager {
541
594
  */
542
595
  applyInstancePatch(instance) {
543
596
  const nativeDb = this._iModel[Symbols_1._nativeDb];
544
- switch (instance.op) {
597
+ const { $meta, ...props } = instance;
598
+ switch ($meta.op) {
545
599
  case "Inserted": {
546
- if (!instance.props)
600
+ if (!props)
547
601
  throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "InstancePatch with op 'Inserted' must have props");
548
602
  const options = { forceUseId: true, useJsNames: true };
549
- nativeDb.insertInstance(instance.props, options);
603
+ const id = nativeDb.insertInstance(props, options);
604
+ if (!core_bentley_1.Id64.isValidId64(id))
605
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, `Failed to insert instance with id ${props.id}`);
550
606
  break;
551
607
  }
552
608
  case "Updated": {
553
- if (!instance.props)
609
+ if (!props)
554
610
  throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "InstancePatch with op 'Updated' must have props");
555
- nativeDb.updateInstance(instance.props, { useJsNames: true });
611
+ const isSuccess = nativeDb.updateInstance(props, { useJsNames: true });
612
+ if (!isSuccess)
613
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, `Failed to update instance with id ${props.id}`);
556
614
  break;
557
615
  }
558
616
  case "Deleted": {
559
- const key = { id: instance.key.id, classFullName: instance.key.classFullName };
560
- nativeDb.deleteInstance(key, { useJsNames: true });
617
+ const key = { id: props.id, classFullName: props.className };
618
+ const isSuccess = nativeDb.deleteInstance(key, { useJsNames: true });
619
+ if (!isSuccess)
620
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, `Failed to delete instance with id ${props.id}`);
561
621
  break;
562
622
  }
563
623
  default:
564
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, `Unknown InstancePatch op '${instance.op}'`);
624
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, `Unknown InstancePatch op '${$meta.op}'`);
565
625
  }
566
626
  }
567
627
  /**
@@ -805,6 +865,28 @@ class TxnManager {
805
865
  this.onEndValidation.raiseEvent();
806
866
  // TODO: if (this.validationErrors.length !== 0) throw new IModelError(validation ...)
807
867
  }
868
+ /** Called by native code during semantic rebase while reversing local changes to create instance patches to be used for reinstating changes.
869
+ * @internal */
870
+ _captureInstanceChanges(id) {
871
+ const env_1 = { stack: [], error: void 0, hasError: false };
872
+ try {
873
+ if (BriefcaseManager_1.BriefcaseManager.semanticRebaseDataFolderExists(this._iModel, id))
874
+ return; // if folder already exists that means we have already captured the changes for this txn during this rebase so we can skip capturing again
875
+ const reader = __addDisposableResource(env_1, ChangesetReader_1.ChangesetReader.openTxn({ db: this._iModel, txnId: id, rowOptions: { useJsName: true, abbreviateBlobs: false } }), false);
876
+ const pcu = __addDisposableResource(env_1, new PartialChangeUnifier_1.PartialChangeUnifier(PartialChangeUnifier_1.ChangeUnifierCache.createSqliteBackedCache()), false);
877
+ while (reader.step()) {
878
+ pcu.appendFrom(reader);
879
+ }
880
+ BriefcaseManager_1.BriefcaseManager.storeChangedInstancesForSemanticRebase(this._iModel, id, pcu.instances);
881
+ }
882
+ catch (e_1) {
883
+ env_1.error = e_1;
884
+ env_1.hasError = true;
885
+ }
886
+ finally {
887
+ __disposeResources(env_1);
888
+ }
889
+ }
808
890
  /** @internal */
809
891
  _onGeometryChanged(modelProps) {
810
892
  this.onGeometryChanged.raiseEvent(modelProps);