Cleanup indexes at start time and waith till the cleanup is done before rendering the whiteboard.

This commit is contained in:
Eric van der Vlist 2022-09-26 11:51:27 +02:00
parent fafde7b831
commit e1da4a1e4a
2 changed files with 53 additions and 23 deletions

View File

@ -1,4 +1,4 @@
import react, { useMemo, useEffect } from 'react'; import react, { useMemo, useEffect, useState } from 'react';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { mapActions } from '../../store/map'; import { mapActions } from '../../store/map';
import _ from 'lodash'; import _ from 'lodash';
@ -36,9 +36,12 @@ const Map: react.FC<{}> = (props: {}) => {
); );
const db = useDB(); const db = useDB();
const [dbReady, setDbReady] = useState(false);
useEffect(() => { useEffect(() => {
window.addEventListener('resize', debouncedResizeHandler); window.addEventListener('resize', debouncedResizeHandler);
initDb(db); initDb(db, setDbReady);
}, []); }, []);
return ( return (
@ -48,7 +51,7 @@ const Map: react.FC<{}> = (props: {}) => {
<Slippy> <Slippy>
<Whiteboard> <Whiteboard>
<CurrentLocation /> <CurrentLocation />
<Gpxes /> dbReady && <Gpxes />
</Whiteboard> </Whiteboard>
<Layer> <Layer>
<TiledMap /> <TiledMap />

View File

@ -1,3 +1,5 @@
import _ from 'lodash';
const DBDEFINITION = '--db-definition--'; const DBDEFINITION = '--db-definition--';
const dbDefinition = { const dbDefinition = {
@ -6,7 +8,7 @@ const dbDefinition = {
version: '0.000001', version: '0.000001',
}; };
export const initDb = async (db: any) => { export const initDb = async (db: any, setDbReady: any) => {
try { try {
await db.get(DBDEFINITION); await db.get(DBDEFINITION);
// TODO: support migrations // TODO: support migrations
@ -19,29 +21,54 @@ export const initDb = async (db: any) => {
} }
} }
await db.createIndex({ // WARNING: defs must use the canonical form andbe identical to what will be returned by db.getIndexes
index: { const requiredIndexes: any = [
{
name: 'type', name: 'type',
fields: ['type'], def: {
fields: [{ type: 'asc' }],
}, },
},
{
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);
}); });
await db.createIndex({ for (var index of existingIndexes) {
index: { if (index.type === 'json') {
name: 'trkpt-time', // Non system indexes
fields: ['trkpt.time'], console.log(`Checking existing index :${JSON.stringify(index)}`);
partial_filter_selector: { if (!findIndex(requiredIndexes, index)) {
type: 'trkpt', console.log(`db.deleteIndex(${JSON.stringify(index)})`);
}, await db.deleteIndex(index);
}, }
}); }
}
await db.createIndex({ for (var index of requiredIndexes) {
index: { if (!findIndex(existingIndexes, index)) {
name: 'type-trkpt-gpx-time3', console.log(`db.createIndex(${JSON.stringify(index)})`);
fields: ['type', 'gpx', 'trkpt.time'], await db.createIndex({ name: index.name, ...index.def });
}, }
}); }
setDbReady(true);
const indexes = await db.getIndexes(); const indexes = await db.getIndexes();
console.log(`indexes: ${JSON.stringify(indexes)}`); console.log(`indexes: ${JSON.stringify(indexes)}`);