resetInitalData

This commit is contained in:
Xu Chang 2022-08-12 12:26:52 +08:00
parent 384e645f7d
commit 4a91954aa0
14 changed files with 76 additions and 108 deletions

View File

@ -5,8 +5,8 @@ import { TreeStore } from 'oak-memory-tree-store';
export declare class CacheStore<ED extends EntityDict, Cxt extends Context<ED>> extends TreeStore<ED, Cxt> {
private executor;
private getFullDataFn?;
private setInitialDataFn?;
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (cxtString: string) => (store: CacheStore<ED, Cxt>) => Cxt, getFullDataFn?: () => any, setInitialDataFn?: () => any);
private resetInitialDataFn?;
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (cxtString: string) => (store: CacheStore<ED, Cxt>) => Cxt, getFullDataFn?: () => any, resetInitialDataFn?: () => void);
operate<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: Cxt, option?: OP): Promise<OperationResult<ED>>;
sync(opRecords: Array<OpRecord<ED>>, context: Cxt): Promise<void>;
select<T extends keyof ED, S extends ED[T]['Selection'], OP extends SelectOption>(entity: T, selection: S, context: Cxt, option?: OP): Promise<import("oak-domain/lib/types").SelectionResult<ED[T]["Schema"], S["data"]>>;
@ -20,5 +20,5 @@ export declare class CacheStore<ED extends EntityDict, Cxt extends Context<ED>>
* debug下用来初始化debugStore的数据release下不能使用
* @returns
*/
setInitialData(): any;
resetInitialData(): void;
}

View File

@ -56,13 +56,13 @@ var TriggerExecutor_1 = require("oak-domain/lib/store/TriggerExecutor");
var oak_memory_tree_store_1 = require("oak-memory-tree-store");
var CacheStore = /** @class */ (function (_super) {
__extends(CacheStore, _super);
function CacheStore(storageSchema, contextBuilder, getFullDataFn, setInitialDataFn) {
function CacheStore(storageSchema, contextBuilder, getFullDataFn, resetInitialDataFn) {
var _this = _super.call(this, storageSchema) || this;
_this.executor = new TriggerExecutor_1.TriggerExecutor(function (cxtStr) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, contextBuilder(cxtStr)(this)];
}); }); });
_this.getFullDataFn = getFullDataFn;
_this.setInitialDataFn = setInitialDataFn;
_this.resetInitialDataFn = resetInitialDataFn;
return _this;
}
CacheStore.prototype.operate = function (entity, operation, context, option) {
@ -190,8 +190,8 @@ var CacheStore = /** @class */ (function (_super) {
* 这个函数是在debug下用来初始化debugStore的数据release下不能使用
* @returns
*/
CacheStore.prototype.setInitialData = function () {
return this.setInitialDataFn();
CacheStore.prototype.resetInitialData = function () {
return this.resetInitialDataFn();
};
return CacheStore;
}(oak_memory_tree_store_1.TreeStore));

View File

@ -10,14 +10,7 @@ interface DebugStoreSelectOption extends SelectOption {
export declare class DebugStore<ED extends EntityDict, Cxt extends Context<ED>> extends TreeStore<ED, Cxt> {
private executor;
private rwLock;
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt, initialData?: {
[T in keyof ED]?: ED[T]['OpSchema'][];
}, initialStat?: {
create: number;
update: number;
remove: number;
commit: number;
});
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt);
protected cascadeUpdate<T extends keyof ED, OP extends DebugStoreOperateOption>(entity: T, operation: DeduceCreateOperation<ED[T]["Schema"]> | DeduceUpdateOperation<ED[T]["Schema"]> | DeduceRemoveOperation<ED[T]["Schema"]>, context: Cxt, option?: OP): Promise<import("oak-domain/lib/types").OperationResult<ED>>;
protected cascadeSelect<T extends keyof ED, S extends ED[T]["Selection"], OP extends DebugStoreSelectOption>(entity: T, selection: S, context: Cxt, option?: OP): Promise<SelectRowShape<ED[T]['Schema'], S['data']>[]>;
operate<T extends keyof ED, OP extends DebugStoreOperateOption>(entity: T, operation: ED[T]['Operation'], context: Cxt, option?: OP): Promise<import("oak-domain/lib/types").OperationResult<ED>>;

View File

@ -59,8 +59,8 @@ var concurrent_1 = require("oak-domain/lib/utils/concurrent");
;
var DebugStore = /** @class */ (function (_super) {
__extends(DebugStore, _super);
function DebugStore(storageSchema, contextBuilder, initialData, initialStat) {
var _this = _super.call(this, storageSchema, initialData, initialStat) || this;
function DebugStore(storageSchema, contextBuilder) {
var _this = _super.call(this, storageSchema) || this;
_this.executor = new TriggerExecutor_1.TriggerExecutor(function (cxtString) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, contextBuilder(cxtString)(this)];
}); }); });

View File

@ -1,5 +1,8 @@
import { DebugStore } from './debugStore';
import { Checker, Trigger, StorageSchema, Context, EntityDict, RowStore, ActionDictOfEntityDict, Watcher } from "oak-domain/lib/types";
export declare function resetDebugStore<ED extends EntityDict, Cxt extends Context<ED>>(store: DebugStore<ED, Cxt>, data: {
[T in keyof ED]?: Array<ED[T]['OpSchema']>;
}): void;
export declare function createDebugStore<ED extends EntityDict, Cxt extends Context<ED>>(storageSchema: StorageSchema<ED>, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt, triggers: Array<Trigger<ED, keyof ED, Cxt>>, checkers: Array<Checker<ED, keyof ED, Cxt>>, watchers: Array<Watcher<ED, keyof ED, Cxt>>, initialData?: {
[T in keyof ED]?: Array<ED[T]['OpSchema']>;
}, actionDict?: ActionDictOfEntityDict<ED>): DebugStore<ED, Cxt>;

View File

@ -47,35 +47,17 @@ var __values = (this && this.__values) || function(o) {
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDebugStore = void 0;
exports.createDebugStore = exports.resetDebugStore = void 0;
var debugStore_1 = require("./debugStore");
var actionDef_1 = require("oak-domain/lib/store/actionDef");
var assert_1 = require("oak-domain/lib/utils/assert");
function initDataInStore(store, contextBuilder, initialData) {
function initDataInStore(store, initialData, stat) {
return __awaiter(this, void 0, void 0, function () {
var context_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
store.startInitializing();
if (!false) return [3 /*break*/, 1];
return [3 /*break*/, 4];
case 1:
context_1 = contextBuilder()(store);
return [4 /*yield*/, context_1.begin()];
case 2:
_a.sent();
if (initialData) {
store.setInitialData(initialData);
}
return [4 /*yield*/, context_1.commit()];
case 3:
_a.sent();
_a.label = 4;
case 4:
store.endInitializing();
return [2 /*return*/];
}
store.startInitializing();
store.resetInitialData(initialData, stat);
store.endInitializing();
return [2 /*return*/];
});
});
}
@ -277,9 +259,18 @@ function initializeWatchers(store, contextBuilder, watchers) {
}
doWatchers();
}
function resetDebugStore(store, data) {
initDataInStore(store, data, {
create: 0,
update: 0,
remove: 0,
commit: 0
});
materializeData(data, store.getStat());
}
exports.resetDebugStore = resetDebugStore;
function createDebugStore(storageSchema, contextBuilder, triggers, checkers, watchers, initialData, actionDict) {
var data = getMaterializedData();
var store = new debugStore_1.DebugStore(storageSchema, contextBuilder, data && data.data, data && data.stat);
var store = new debugStore_1.DebugStore(storageSchema, contextBuilder);
triggers.forEach(function (ele) { return store.registerTrigger(ele); });
checkers.forEach(function (ele) { return store.registerChecker(ele); });
(0, assert_1.assert)(actionDict);
@ -287,11 +278,13 @@ function createDebugStore(storageSchema, contextBuilder, triggers, checkers, wat
adTriggers.forEach(function (ele) { return store.registerTrigger(ele); });
adCheckers.forEach(function (ele) { return store.registerChecker(ele); });
// 如果没有物化数据则使用initialData初始化debugStore
var data = getMaterializedData();
if (!data) {
console.log('使用初始化数据建立debugStore');
initDataInStore(store, contextBuilder, initialData);
initDataInStore(store, initialData);
console.log('使用初始化数据建立debugStore', initialData);
}
else {
initDataInStore(store, data.data, data.stat);
console.log('使用物化数据建立debugStore', data);
}
lastMaterializedVersion = store.getStat().commit;

View File

@ -28,5 +28,5 @@ export declare class Cache<ED extends EntityDict, Cxt extends Context<ED>, AD ex
unbindOnSync(callback: (opRecords: OpRecord<ED>[]) => Promise<void>): void;
getCachedData(): { [T in keyof ED]?: ED[T]["OpSchema"][] | undefined; };
getFullData(): any;
setInitialData(): any;
resetInitialData(): void;
}

View File

@ -189,8 +189,8 @@ var Cache = /** @class */ (function (_super) {
Cache.prototype.getFullData = function () {
return this.cacheStore.getFullData();
};
Cache.prototype.setInitialData = function () {
return this.cacheStore.setInitialData();
Cache.prototype.resetInitialData = function () {
return this.cacheStore.resetInitialData();
};
__decorate([
Feature_1.Action

View File

@ -68,27 +68,7 @@ function initialize(storageSchema, createFeatures, contextBuilder, aspectDict, t
}
var aspectDict2 = Object.assign({}, oak_common_aspect_1.default, aspectDict);
var debugStore = (0, debugStore_1.createDebugStore)(storageSchema, contextBuilder, triggers || [], checkers || [], watchers || [], initialData, actionDict);
var cacheStore = new CacheStore_1.CacheStore(storageSchema, contextBuilder, function () { return debugStore.getCurrentData(); }, function () { return __awaiter(_this, void 0, void 0, function () {
var context;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
debugStore.startInitializing();
context = contextBuilder()(debugStore);
return [4 /*yield*/, context.begin()];
case 1:
_a.sent();
if (initialData) {
debugStore.setInitialData(initialData);
}
return [4 /*yield*/, context.commit()];
case 2:
_a.sent();
debugStore.endInitializing();
return [2 /*return*/];
}
});
}); });
var cacheStore = new CacheStore_1.CacheStore(storageSchema, contextBuilder, function () { return debugStore.getCurrentData(); }, function () { return (0, debugStore_1.resetDebugStore)(debugStore, initialData || {}); });
if (checkers) {
checkers.forEach(function (checker) { return cacheStore.registerChecker(checker); });
}

View File

@ -10,7 +10,7 @@ export class CacheStore<
> extends TreeStore<ED, Cxt> {
private executor: TriggerExecutor<ED, Cxt>;
private getFullDataFn?: () => any;
private setInitialDataFn?: () => any;
private resetInitialDataFn?: () => void;
constructor(
storageSchema: StorageSchema<ED>,
@ -18,14 +18,14 @@ export class CacheStore<
cxtString: string
) => (store: CacheStore<ED, Cxt>) => Cxt,
getFullDataFn?: () => any,
setInitialDataFn?: () => any
resetInitialDataFn?: () => void
) {
super(storageSchema);
this.executor = new TriggerExecutor(async (cxtStr) =>
contextBuilder(cxtStr)(this)
);
this.getFullDataFn = getFullDataFn;
this.setInitialDataFn = setInitialDataFn;
this.resetInitialDataFn = resetInitialDataFn;
}
async operate<T extends keyof ED, OP extends OperateOption>(
@ -111,7 +111,7 @@ export class CacheStore<
* debug下用来初始化debugStore的数据release下不能使用
* @returns
*/
setInitialData() {
return this.setInitialDataFn!();
resetInitialData() {
return this.resetInitialDataFn!();
}
}

View File

@ -15,10 +15,8 @@ interface DebugStoreSelectOption extends SelectOption {
export class DebugStore<ED extends EntityDict, Cxt extends Context<ED>> extends TreeStore<ED, Cxt> {
private executor: TriggerExecutor<ED, Cxt>;
private rwLock: RWLock;
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt, initialData?: {
[T in keyof ED]?: ED[T]['OpSchema'][];
}, initialStat?: { create: number, update: number, remove: number, commit: number }) {
super(storageSchema, initialData, initialStat);
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt) {
super(storageSchema);
this.executor = new TriggerExecutor(async (cxtString) => contextBuilder(cxtString)(this));
this.rwLock = new RWLock();
}

View File

@ -9,22 +9,16 @@ import { assert } from 'oak-domain/lib/utils/assert';
async function initDataInStore<ED extends EntityDict, Cxt extends Context<ED>>(
store: DebugStore<ED, Cxt>,
contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt,
initialData?: {
initialData: {
[T in keyof ED]?: Array<ED[T]['OpSchema']>;
}, stat?: {
create: number;
update: number;
remove: number;
commit: number;
}) {
store.startInitializing();
if (false) {
// todo 在不同环境下读取相应的store数据并初始化
}
else {
const context = contextBuilder()(store);
await context.begin();
if (initialData) {
store.setInitialData(initialData);
}
await context.commit();
}
store.resetInitialData(initialData, stat);
store.endInitializing();
}
@ -158,6 +152,21 @@ function initializeWatchers<ED extends EntityDict, Cxt extends Context<ED>>(
doWatchers();
}
export function resetDebugStore<ED extends EntityDict, Cxt extends Context<ED>>(
store: DebugStore<ED, Cxt>,
data: {
[T in keyof ED]?: Array<ED[T]['OpSchema']>;
}
) {
initDataInStore(store, data, {
create: 0,
update: 0,
remove: 0,
commit: 0
});
materializeData(data, store.getStat());
}
export function createDebugStore<ED extends EntityDict, Cxt extends Context<ED>>(
storageSchema: StorageSchema<ED>,
contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt,
@ -168,8 +177,7 @@ export function createDebugStore<ED extends EntityDict, Cxt extends Context<ED>>
[T in keyof ED]?: Array<ED[T]['OpSchema']>;
},
actionDict?: ActionDictOfEntityDict<ED>) {
const data = getMaterializedData();
const store = new DebugStore<ED, Cxt>(storageSchema, contextBuilder, data && data.data, data && data.stat);
const store = new DebugStore<ED, Cxt>(storageSchema, contextBuilder);
triggers.forEach(
ele => store.registerTrigger(ele)
@ -189,11 +197,13 @@ export function createDebugStore<ED extends EntityDict, Cxt extends Context<ED>>
);
// 如果没有物化数据则使用initialData初始化debugStore
const data = getMaterializedData();
if (!data) {
console.log('使用初始化数据建立debugStore');
initDataInStore(store, contextBuilder, initialData);
initDataInStore(store, initialData!);
console.log('使用初始化数据建立debugStore', initialData);
}
else {
initDataInStore(store, data.data, data.stat);
console.log('使用物化数据建立debugStore', data);
}
lastMaterializedVersion = store.getStat().commit;

View File

@ -130,7 +130,7 @@ export class Cache<
return this.cacheStore.getFullData();
}
setInitialData() {
return this.cacheStore.setInitialData();
resetInitialData() {
return this.cacheStore.resetInitialData();
}
}

View File

@ -12,7 +12,7 @@ import {
import { EntityDict } from 'oak-domain/lib/types/Entity';
import { Feature } from './types/Feature';
import { createDebugStore } from './debugStore';
import { createDebugStore, resetDebugStore } from './debugStore';
import { initialize as createBasicFeatures, BasicFeatures } from './features';
import { intersection } from 'oak-domain/lib/utils/lodash';
@ -79,16 +79,7 @@ export function initialize<
storageSchema,
contextBuilder,
() => debugStore.getCurrentData(),
async () => {
debugStore.startInitializing();
const context = contextBuilder()(debugStore);
await context.begin();
if (initialData) {
debugStore.setInitialData(initialData);
}
await context.commit();
debugStore.endInitializing();
}
() => resetDebugStore(debugStore, initialData || {}),
);
if (checkers) {
checkers.forEach((checker) => cacheStore.registerChecker(checker));