perspective cache - basic design

This commit is contained in:
Jan Prochazka
2022-07-21 15:43:17 +02:00
parent 0f6ec420d2
commit d71294621b
10 changed files with 218 additions and 66 deletions

View File

@@ -0,0 +1,63 @@
import { RangeDefinition } from 'dbgate-types';
import { PerspectiveDataLoadProps } from './PerspectiveDataProvider';
import _pick from 'lodash/pick';
import _omit from 'lodash/omit';
import _difference from 'lodash/difference';
import debug from 'debug';
const dbg = debug('dbgate:PerspectiveCache');
export class PerspectiveCacheTable {
constructor(props: PerspectiveDataLoadProps) {
this.schemaName = props.schemaName;
this.pureName = props.pureName;
this.bindingColumns = props.bindingColumns;
this.dataColumns = props.dataColumns;
this.loadedAll = false;
}
schemaName: string;
pureName: string;
bindingColumns?: string[];
dataColumns: string[];
loadedAll: boolean;
loadedRows: any[] = [];
get loadedCount() {
return this.loadedRows.length;
}
getRowsResult(props: PerspectiveDataLoadProps): { rows: any[]; incomplete: boolean } {
return {
rows: this.loadedRows.slice(0, props.topCount),
incomplete: props.topCount < this.loadedCount || !this.loadedAll,
};
}
}
export class PerspectiveCache {
constructor(public stableStringify) {}
tables: { [tableKey: string]: PerspectiveCacheTable } = {};
getTableCache(props: PerspectiveDataLoadProps) {
const tableKey = this.stableStringify(_omit(props, ['range', 'bindingValues', 'dataColumns']));
let res = this.tables[tableKey];
if (res && _difference(props.dataColumns, res.dataColumns).length > 0) {
dbg('Delete cache because incomplete columns', props.pureName, res.dataColumns);
// we have incomplete cache
delete this.tables[tableKey];
res = null;
}
if (!res) {
res = new PerspectiveCacheTable(props);
this.tables[tableKey] = res;
return res;
}
// cache could be used
return res;
}
}