mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
perspectives - prepare for nested incremental load
This commit is contained in:
parent
8f1343bc42
commit
28e06166e0
@ -1,14 +1,23 @@
|
||||
import { RangeDefinition } from 'dbgate-types';
|
||||
import { PerspectiveDataLoadProps } from './PerspectiveDataProvider';
|
||||
import _pick from 'lodash/pick';
|
||||
import _omit from 'lodash/omit';
|
||||
import _zip from 'lodash/zip';
|
||||
import _difference from 'lodash/difference';
|
||||
import debug from 'debug';
|
||||
|
||||
const dbg = debug('dbgate:PerspectiveCache');
|
||||
|
||||
export class PerspectiveBindingGroup {
|
||||
constructor(public table: PerspectiveCacheTable) {}
|
||||
|
||||
groupSize?: number;
|
||||
loadedAll: boolean;
|
||||
loadedRows: any[] = [];
|
||||
bindingValues: any[];
|
||||
}
|
||||
|
||||
export class PerspectiveCacheTable {
|
||||
constructor(props: PerspectiveDataLoadProps) {
|
||||
constructor(props: PerspectiveDataLoadProps, public cache: PerspectiveCache) {
|
||||
this.schemaName = props.schemaName;
|
||||
this.pureName = props.pureName;
|
||||
this.bindingColumns = props.bindingColumns;
|
||||
@ -22,6 +31,8 @@ export class PerspectiveCacheTable {
|
||||
dataColumns: string[];
|
||||
loadedAll: boolean;
|
||||
loadedRows: any[] = [];
|
||||
bindingGroups: { [bindingKey: string]: PerspectiveBindingGroup } = {};
|
||||
|
||||
get loadedCount() {
|
||||
return this.loadedRows.length;
|
||||
}
|
||||
@ -32,6 +43,31 @@ export class PerspectiveCacheTable {
|
||||
incomplete: props.topCount < this.loadedCount || !this.loadedAll,
|
||||
};
|
||||
}
|
||||
|
||||
getBindingGroups(props: PerspectiveDataLoadProps): { cached: PerspectiveBindingGroup[]; uncached: any[][] } {
|
||||
const cached: PerspectiveBindingGroup[] = [];
|
||||
const uncached = [];
|
||||
for (const group in props.bindingValues) {
|
||||
const key = this.cache.stableStringify(group);
|
||||
const item = this.bindingGroups[key];
|
||||
if (item) {
|
||||
cached.push(item);
|
||||
} else {
|
||||
uncached.push(group);
|
||||
}
|
||||
}
|
||||
return { cached, uncached };
|
||||
}
|
||||
|
||||
storeGroupSize(props: PerspectiveDataLoadProps, bindingValues: any[], count: number) {
|
||||
const originalBindingValue = props.bindingValues.find(v => _zip(v, bindingValues).every(([x, y]) => x == y));
|
||||
if (originalBindingValue) {
|
||||
const key = this.cache.stableStringify(originalBindingValue);
|
||||
const group = new PerspectiveBindingGroup(this);
|
||||
group.groupSize = count;
|
||||
this.bindingGroups[key] = group;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class PerspectiveCache {
|
||||
@ -54,7 +90,7 @@ export class PerspectiveCache {
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
res = new PerspectiveCacheTable(props);
|
||||
res = new PerspectiveCacheTable(props, this);
|
||||
this.tables[tableKey] = res;
|
||||
return res;
|
||||
}
|
||||
|
@ -24,6 +24,18 @@ export class PerspectiveDataProvider {
|
||||
async loadData(props: PerspectiveDataLoadProps): Promise<{ rows: any[]; incomplete: boolean }> {
|
||||
const tableCache = this.cache.getTableCache(props);
|
||||
|
||||
if (props.bindingColumns) {
|
||||
const { cached, uncached } = tableCache.getBindingGroups(props);
|
||||
const counts = await this.loader.loadGrouping({
|
||||
...props,
|
||||
bindingValues: uncached,
|
||||
});
|
||||
for (const countItem of counts) {
|
||||
const { _perspective_group_size_, ...fields } = countItem;
|
||||
tableCache.storeGroupSize(props, fields, _perspective_group_size_);
|
||||
}
|
||||
}
|
||||
|
||||
if (props.topCount <= tableCache.loadedCount) {
|
||||
return tableCache.getRowsResult(props);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user