@finos/legend-data-cube
Version:
109 lines • 3.59 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { IllegalStateError, hashObject, pruneObject, uuid, } from '@finos/legend-shared';
export class DataCubeSnapshot {
uuid = uuid();
data;
_isPatchChange = false;
_finalized = false;
_hashCode;
constructor(configuration) {
this.data = {
configuration,
sourceColumns: [],
leafExtendedColumns: [],
selectColumns: [],
filter: undefined,
groupBy: undefined,
pivot: undefined,
groupExtendedColumns: [],
sortColumns: [],
limit: undefined,
};
}
static create(configuration) {
return new DataCubeSnapshot(configuration);
}
/**
* When we support undo/redo, patch changes should be grouped
* together with the most recent non-patch change snapshot and treated
* as a single step.
*
* e.g. if we have a stack of snapshots [A, B, C, D] where D is the current
* snapshot and C is a patch change. When undo, we should go back to C.
* When undo again, we should go back to A instead of B.
*/
markAsPatchChange() {
this._isPatchChange = true;
}
isPatchChange() {
return this._isPatchChange;
}
isFinalized() {
return this._finalized;
}
finalize() {
if (this._finalized) {
return this;
}
/**
* NOTE: if this becomes a performance bottleneck, we can consider
* more granular hashing strategy
*
* Here, we are just hashing the raw object, but we must ensure
* to properly prune the snapshot data object before hashing
* else there would be mismatch
*/
this._hashCode = hashObject(pruneObject(this.data));
this._finalized = true;
return this;
}
get hashCode() {
if (!this._finalized || !this._hashCode) {
throw new IllegalStateError('Snapshot is not finalized');
}
return this._hashCode;
}
clone() {
const clone = new DataCubeSnapshot({});
clone.data = JSON.parse(JSON.stringify(this.data));
return clone;
}
/**
* Only use this if an absolute identical clone is needed.
* This should rarely be used, and ideally by core engine only.
*/
INTERNAL__fullClone() {
const clone = new DataCubeSnapshot({});
clone.uuid = this.uuid;
clone.data = JSON.parse(JSON.stringify(this.data));
clone._isPatchChange = this._isPatchChange;
clone._finalized = this._finalized;
clone._hashCode = this._hashCode;
return clone;
}
serialize() {
const clone = this.clone();
return {
uuid: clone.uuid,
data: clone.data,
_isPatchChange: clone._isPatchChange,
_finalized: clone._finalized,
_hashCode: clone._hashCode,
};
}
}
//# sourceMappingURL=DataCubeSnapshot.js.map