From e1da4a1e4a29bfcac50bb410099605d05fc42134 Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 26 Sep 2022 11:51:27 +0200 Subject: [PATCH] Cleanup indexes at start time and waith till the cleanup is done before rendering the whiteboard. --- src/components/map/map.tsx | 9 +++-- src/db/index.ts | 67 ++++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/components/map/map.tsx b/src/components/map/map.tsx index 7efe2a7..d5ab94e 100644 --- a/src/components/map/map.tsx +++ b/src/components/map/map.tsx @@ -1,4 +1,4 @@ -import react, { useMemo, useEffect } from 'react'; +import react, { useMemo, useEffect, useState } from 'react'; import { useDispatch } from 'react-redux'; import { mapActions } from '../../store/map'; import _ from 'lodash'; @@ -36,9 +36,12 @@ const Map: react.FC<{}> = (props: {}) => { ); const db = useDB(); + + const [dbReady, setDbReady] = useState(false); + useEffect(() => { window.addEventListener('resize', debouncedResizeHandler); - initDb(db); + initDb(db, setDbReady); }, []); return ( @@ -48,7 +51,7 @@ const Map: react.FC<{}> = (props: {}) => { - + dbReady && diff --git a/src/db/index.ts b/src/db/index.ts index 58ac9b7..c405b4b 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,3 +1,5 @@ +import _ from 'lodash'; + const DBDEFINITION = '--db-definition--'; const dbDefinition = { @@ -6,7 +8,7 @@ const dbDefinition = { version: '0.000001', }; -export const initDb = async (db: any) => { +export const initDb = async (db: any, setDbReady: any) => { try { await db.get(DBDEFINITION); // TODO: support migrations @@ -19,29 +21,54 @@ export const initDb = async (db: any) => { } } - await db.createIndex({ - index: { + // WARNING: defs must use the canonical form andbe identical to what will be returned by db.getIndexes + const requiredIndexes: any = [ + { name: 'type', - fields: ['type'], - }, - }); - - await db.createIndex({ - index: { - name: 'trkpt-time', - fields: ['trkpt.time'], - partial_filter_selector: { - type: 'trkpt', + def: { + fields: [{ type: 'asc' }], }, }, - }); - - await db.createIndex({ - index: { - name: 'type-trkpt-gpx-time3', - fields: ['type', 'gpx', 'trkpt.time'], + { + name: 'type-trkpt-gpx-time', + def: { + fields: [{ type: 'asc' }, { gpx: 'asc' }, { 'trkpt.time': 'asc' }], + }, }, - }); + ]; + + const existingIndexes = (await db.getIndexes()).indexes; + + const pruneIndex = ({ name, def }: any) => ({ name, def }); + + const isSameIndex = (idx1: any, idx2: any) => { + return _.isEqual(pruneIndex(idx1), pruneIndex(idx2)); + }; + + const findIndex = (targetIndexes: any, index: any) => + targetIndexes.find((targetIndex: any) => { + return isSameIndex(targetIndex, index); + }); + + for (var index of existingIndexes) { + if (index.type === 'json') { + // Non system indexes + console.log(`Checking existing index :${JSON.stringify(index)}`); + if (!findIndex(requiredIndexes, index)) { + console.log(`db.deleteIndex(${JSON.stringify(index)})`); + await db.deleteIndex(index); + } + } + } + + for (var index of requiredIndexes) { + if (!findIndex(existingIndexes, index)) { + console.log(`db.createIndex(${JSON.stringify(index)})`); + await db.createIndex({ name: index.name, ...index.def }); + } + } + + setDbReady(true); const indexes = await db.getIndexes(); console.log(`indexes: ${JSON.stringify(indexes)}`);