From 7e6ab43b006a2fb32587fe80bf54c3999f78c649 Mon Sep 17 00:00:00 2001 From: Xc Date: Mon, 18 Apr 2022 16:14:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=B0=8F=E7=AC=94=E8=AF=AF?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E5=8F=8A=E5=8A=A0=E4=B8=8A=E4=BA=86store?= =?UTF-8?q?=E7=9A=84commit=E6=AC=A1=E6=95=B0=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store.ts | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/store.ts b/src/store.ts index 4ec3150..2ad9300 100644 --- a/src/store.ts +++ b/src/store.ts @@ -1,4 +1,4 @@ -import { assign, cloneDeep, get, last, set, unset } from 'lodash'; +import { assign, cloneDeep, get, keys, last, set, unset } from 'lodash'; import assert from 'assert'; import { SelectionResult2, DeduceCreateSingleOperation, DeduceFilter, DeduceSelection, EntityShape, DeduceRemoveOperation, DeduceUpdateOperation, DeduceSorter, DeduceSorterAttr, OperationResult, OperateParams, OpRecord, DeduceCreateOperationData, DeduceUpdateOperationData, UpdateOpResult, RemoveOpResult, SelectOpResult, EntityDict, SelectRowShape } from "oak-domain/lib/types/Entity"; import { ExpressionKey, EXPRESSION_PREFIX, NodeId, RefAttr } from 'oak-domain/lib/types/Demand'; @@ -27,13 +27,12 @@ function obscurePass(row: any, attr: string, params: OperateParams): boolean { } export default class TreeStore extends CascadeStore { - store: { + private store: { [T in keyof ED]?: { [ID: string]: RowNode; }; }; - immutable: boolean; - activeTxnDict: { + private activeTxnDict: { [T: string]: { nodeHeader?: RowNode; create: number; @@ -41,6 +40,12 @@ export default class TreeStore extends CascadeStore { remove: number; }; }; + private stat: { + create: number; + update: number; + remove: number; + commit: number; + }; setInitialData(data: { [T in keyof ED]?: { @@ -52,7 +57,7 @@ export default class TreeStore extends CascadeStore { this.store[entity] = {}; } for (const rowId in data[entity]) { - set(this.store, `${entity}.${rowId}.#current`, data[entity]![rowId]); + set(this.store, `${entity}.${rowId}.$current`, data[entity]![rowId]); } } } @@ -76,18 +81,28 @@ export default class TreeStore extends CascadeStore { return result; } - constructor(storageSchema: StorageSchema, immutable: boolean = false, initialData?: { + constructor(storageSchema: StorageSchema, initialData?: { [T in keyof ED]?: { [ID: string]: ED[T]['OpSchema']; }; + }, stat?: { + create: number; + update: number; + remove: number; + commit: number; }) { super(storageSchema); - this.immutable = immutable; this.store = {}; if (initialData) { this.setInitialData(initialData); } this.activeTxnDict = {}; + this.stat = stat || { + create: 0, + update: 0, + remove: 0, + commit: 0, + }; } private constructRow(node: RowNode, context: Context) { @@ -1104,6 +1119,10 @@ export default class TreeStore extends CascadeStore { txnNode[action]++; } + getStat() { + return this.stat; + } + async begin() { const uuid = v4({ random: await getRandomValues(16) }) assert(!this.activeTxnDict.hasOwnProperty(uuid)); @@ -1138,6 +1157,13 @@ export default class TreeStore extends CascadeStore { } node = node2; } + if (this.activeTxnDict[uuid].create || this.activeTxnDict[uuid].update || this.activeTxnDict[uuid].remove) { + this.stat.create += this.activeTxnDict[uuid].create; + this.stat.update += this.activeTxnDict[uuid].update; + this.stat.remove += this.activeTxnDict[uuid].remove; + this.stat.commit ++; + } + unset(this.activeTxnDict, uuid); }