Replacing PouchDB indexes (used only to get the list of Gpxes by a custom implementation.

This commit is contained in:
vdv committed 2022-12-11 10:45:08 +01:00
1 parent 8c82b9c040
commit 3a5f5260c8
8 files changed
+225 -213

No files matched your search

+20
View File
@@ -1,6 +1,7 @@
import { getWpt } from '../db/wpt';
import { returnAgain } from '../workers/dispatcher-worker';
import { getAllGpxes, getGpx } from './gpx';
import { put } from './lib';
import { getRte } from './rte';
import { getTrk } from './trk';
import { getTrkseg } from './trkseg';
@@ -38,6 +39,25 @@ const changeHandler = async (change: any) => {
sendUpdate(globalThis.watches.get(id));
const parentId = id.substring(0, id.lastIndexOf('/'));
if (parentId === 'gpx') {
const gpxes = await getAllGpxes();
console.log({ caller: 'changeHandler / gpxes', gpxes, id });
if (!(id in gpxes)) {
await put(
parentId,
`idx-${parentId}`,
(doc) => {
console.log({ caller: 'changeHandler / gpxes', doc });
doc.push(id);
doc.sort().reverse();
return doc;
},
[]
);
}
}
sendUpdate(globalThis.watches.get(parentId));
};
+6 -26
View File
@@ -186,6 +186,7 @@ const extensionsFromGpx = (gpx: Gpx) => {
};
export const pruneAndSaveImportedGpx = async (params: any) => {
console.log({ caller: 'pruneAndSaveImportedGpx', params });
const { id, gpx, extensions } = params;
let docs: any[] = [
{ _id: getUri('gpx', id), type: 'gpx', doc: gpx },
@@ -206,32 +207,11 @@ export const pruneAndSaveImportedGpx = async (params: any) => {
};
export const getAllGpxes = async () => {
return (await getDocsByType('gpx')).map((doc: any) => doc._id);
};
export const getGpxesForViewport = async (params: any) => {
const { viewport, zoomLevel } = params;
const zoomedViewport: Rectangle = {
topLeft: {
x: viewport.topLeft.x / 2 ** zoomLevel,
y: viewport.topLeft.y / 2 ** zoomLevel,
},
bottomRight: {
x: (viewport.bottomRight.x + 1) / 2 ** zoomLevel,
y: (viewport.bottomRight.y + 1) / 2 ** zoomLevel,
},
};
const allExtensions = await getDocsByType('extensions');
console.log(
`getGpxesForViewport, allExtensions: ${JSON.stringify(allExtensions)}`
);
return allExtensions
.filter((extensions: any) => {
return rectanglesIntersect(zoomedViewport, extensions.doc.viewport);
})
.map((extensions: any) =>
getUri('gpx', getUri('extensions', extensions._id))
);
try {
return (await get('gpx')).doc;
} catch {
return [];
}
};
export const appendToArray = (target: any, key: string, value: any) => {
+1 -68
View File
@@ -1,11 +1,8 @@
import _ from 'lodash';
import PouchDB from 'pouchdb';
import PouchDBFind from 'pouchdb-find';
import uri from '../lib/ids';
import changeHandler from './change-handler';
PouchDB.plugin(PouchDBFind);
const dbDefinitionId = uri('dbdef', {});
const currentDbDefinition = {
@@ -25,7 +22,7 @@ export const initDb = async (params: any) => {
if (globalThis.db === undefined) {
globalThis.db = new PouchDB('dyomedea', {
auto_compaction: false,
// revs_limit: 10,
// revs_limit: 10,
});
}
const db = globalThis.db;
@@ -51,48 +48,6 @@ export const initDb = async (params: any) => {
// TODO: support migrations
}
//await await db.compact();
await db.viewCleanup();
// WARNING: defs must use the canonical form and be identical to what will be returned by db.getIndexes
const requiredIndexes: any = [
{
name: 'type',
def: {
fields: [{ type: '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 (index of requiredIndexes) {
if (!findIndex(existingIndexes, index)) {
// console.log(`db.createIndex(${JSON.stringify(index)})`);
await db.createIndex({ name: index.name, ...index.def });
}
}
globalThis.dbReady = true;
@@ -111,26 +66,4 @@ export const initDb = async (params: any) => {
// console.log({ caller: 'initDb / back from db.changes', changes });
// changes.cancel();
/* const indexes = await db.getIndexes();
console.log(`indexes: ${JSON.stringify(indexes)}`);
const explain1 = await db.explain({
selector: {
type: 'trkpt',
gpx: 'xxxx',
},
// sort: ['trkpt.time'],
// use_index: 'type-trkpt-gpx-time',
});
console.log(`explain1: ${JSON.stringify(explain1)}`);
const explain2 = await db.explain({
selector: {
type: 'gpx',
},
// sort: ['trkpt.time'],
// use_index: 'type-trkpt-gpx-time',
});
console.log(`explain2: ${JSON.stringify(explain2)}`);
*/
};
-4
View File
@@ -46,7 +46,3 @@ export const get = async (id: string) => {
export const putAll = async (docs: any[]) => {
return await db.bulkDocs(docs);
};
export const getDocsByType = async (type: string) => {
return (await db.find({ selector: { type: type } })).docs;
};
+1 -2
View File
@@ -50,6 +50,5 @@ const halfMinDate = minDate / 2;
const maxDate = 8640000000000000;
const halfMaxDate = maxDate / 2;
export const intToGpxId = (i: number) => halfMaxDate - Math.round(i / 2);
export const intToTrkptId = (i: number) => Math.round(i / 2) - halfMinDate;
export const intToGpxId = intToTrkptId;
-2
View File
@@ -5,7 +5,6 @@ import {
putNewGpx,
existsGpx,
pruneAndSaveImportedGpx,
getGpxesForViewport,
getGpx,
getAllGpxes,
} from '../db/gpx';
@@ -25,7 +24,6 @@ onmessage = async function (e) {
existsGpx,
pruneAndSaveImportedGpx,
getAllGpxes,
getGpxesForViewport,
getGpx,
getTrk,
getTrkseg,