From 088ca231f34f4c9cd5b1976b01c775a175db7384 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 24 Jul 2022 15:34:26 +0200 Subject: [PATCH] uniqe binding values --- packages/datalib/src/PerspectiveCache.ts | 11 ++++++----- packages/datalib/src/PerspectiveTreeNode.ts | 12 ++++++++++-- packages/web/src/tabs/PerspectiveTab.svelte | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/datalib/src/PerspectiveCache.ts b/packages/datalib/src/PerspectiveCache.ts index f14d3f33..1350195a 100644 --- a/packages/datalib/src/PerspectiveCache.ts +++ b/packages/datalib/src/PerspectiveCache.ts @@ -4,6 +4,7 @@ import _pick from 'lodash/pick'; import _zip from 'lodash/zip'; import _difference from 'lodash/difference'; import debug from 'debug'; +import stableStringify from 'json-stable-stringify'; const dbg = debug('dbgate:PerspectiveCache'); @@ -49,14 +50,14 @@ export class PerspectiveCacheTable { } getBindingGroup(groupValues: any[]) { - const key = this.cache.stableStringify(groupValues); + const key = stableStringify(groupValues); return this.bindingGroups[key]; } getUncachedBindingGroups(props: PerspectiveDataLoadProps): any[][] { const uncached = []; for (const group of props.bindingValues) { - const key = this.cache.stableStringify(group); + const key = stableStringify(group); const item = this.bindingGroups[key]; if (!item) { uncached.push(group); @@ -68,7 +69,7 @@ export class PerspectiveCacheTable { 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 key = stableStringify(originalBindingValue); // console.log('SET SIZE', originalBindingValue, bindingValues, key, count); const group = new PerspectiveBindingGroup(this); group.bindingValues = bindingValues; @@ -81,12 +82,12 @@ export class PerspectiveCacheTable { } export class PerspectiveCache { - constructor(public stableStringify) {} + constructor() {} tables: { [tableKey: string]: PerspectiveCacheTable } = {}; getTableCache(props: PerspectiveDataLoadProps) { - const tableKey = this.stableStringify( + const tableKey = stableStringify( _pick(props, ['schemaName', 'pureName', 'bindingColumns', 'databaseConfig', 'orderBy']) ); let res = this.tables[tableKey]; diff --git a/packages/datalib/src/PerspectiveTreeNode.ts b/packages/datalib/src/PerspectiveTreeNode.ts index 91cc1f6f..3fb79b07 100644 --- a/packages/datalib/src/PerspectiveTreeNode.ts +++ b/packages/datalib/src/PerspectiveTreeNode.ts @@ -6,11 +6,13 @@ import _cloneDeep from 'lodash/cloneDeep'; import _compact from 'lodash/compact'; import _uniq from 'lodash/uniq'; import _flatten from 'lodash/flatten'; +import _uniqBy from 'lodash/uniqBy'; import { PerspectiveDatabaseConfig, PerspectiveDataLoadProps, PerspectiveDataProvider, } from './PerspectiveDataProvider'; +import stableStringify from 'json-stable-stringify'; export interface PerspectiveDataLoadPropsWithNode { props: PerspectiveDataLoadProps; @@ -171,7 +173,10 @@ export class PerspectiveTableColumnNode extends PerspectiveTreeNode { schemaName: this.foreignKey.refSchemaName, pureName: this.foreignKey.refTableName, bindingColumns: [this.foreignKey.columns[0].refColumnName], - bindingValues: parentRows.map(row => [row[this.foreignKey.columns[0].columnName]]), + bindingValues: _uniqBy( + parentRows.map(row => [row[this.foreignKey.columns[0].columnName]]), + stableStringify + ), dataColumns: this.getDataLoadColumns(), databaseConfig: this.databaseConfig, orderBy: this.table.primaryKey?.columns.map(x => x.columnName) || [this.table.columns[0].columnName], @@ -304,7 +309,10 @@ export class PerspectiveTableReferenceNode extends PerspectiveTableNode { schemaName: this.table.schemaName, pureName: this.table.pureName, bindingColumns: [this.foreignKey.columns[0].columnName], - bindingValues: parentRows.map(row => [row[this.foreignKey.columns[0].refColumnName]]), + bindingValues: _uniqBy( + parentRows.map(row => [row[this.foreignKey.columns[0].refColumnName]]), + stableStringify + ), dataColumns: this.getDataLoadColumns(), databaseConfig: this.databaseConfig, orderBy: this.table.primaryKey?.columns.map(x => x.columnName) || [this.table.columns[0].columnName], diff --git a/packages/web/src/tabs/PerspectiveTab.svelte b/packages/web/src/tabs/PerspectiveTab.svelte index 56e1e940..594e6d57 100644 --- a/packages/web/src/tabs/PerspectiveTab.svelte +++ b/packages/web/src/tabs/PerspectiveTab.svelte @@ -13,7 +13,7 @@ export let pureName; const config = usePerspectiveConfig(tabid); - const cache = new PerspectiveCache(stableStringify); + const cache = new PerspectiveCache(); const loadedCounts = writable({});