Starting to refactor to store track points independently from their tracks (WIP) (#3)
This commit is contained in:
1 parent
aea83fac5d
commit
fafde7b831
10 files changed
+172
-69
No files matched your search
@@ -1,16 +1,13 @@
|
||||
import {
|
||||
Sha256,
|
||||
Sha512,
|
||||
string_to_bytes,
|
||||
bytes_to_base64,
|
||||
} from '@openpgp/asmcrypto.js';
|
||||
import { DBWrapper } from 'workbox-core/_private';
|
||||
|
||||
export const pushTrack = (db: any, payload: any) => {
|
||||
export const pushGpx = async (db: any, payload: any) => {
|
||||
const gpxString = JSON.stringify(payload.gpx);
|
||||
const sha = new Sha256();
|
||||
const result = sha
|
||||
.process(string_to_bytes(JSON.stringify(payload.track)))
|
||||
.finish().result;
|
||||
const result = sha.process(string_to_bytes(gpxString)).finish().result;
|
||||
var _id;
|
||||
if (result === null) {
|
||||
console.log(`Can't hash`);
|
||||
@@ -19,16 +16,48 @@ export const pushTrack = (db: any, payload: any) => {
|
||||
_id = bytes_to_base64(result);
|
||||
console.log(`Digest: ${_id}`);
|
||||
}
|
||||
const doc = { _id, type: 'Track', ...payload };
|
||||
// console.log(JSON.stringify(doc));
|
||||
db.put(doc);
|
||||
|
||||
try {
|
||||
await db.get(_id);
|
||||
alert('This file has alerady been imported.');
|
||||
return;
|
||||
} catch {}
|
||||
|
||||
var gpx = JSON.parse(gpxString);
|
||||
var points: any[] = [];
|
||||
|
||||
const prune = (object: any) => {
|
||||
for (var key in object) {
|
||||
if (key === 'trkpt') {
|
||||
points.push(...object[key]);
|
||||
object[key] = [];
|
||||
} else {
|
||||
if (typeof object[key] === 'object') {
|
||||
prune(object[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
prune(gpx);
|
||||
|
||||
const doc = { ...payload, _id, type: 'gpx', gpx };
|
||||
console.log(JSON.stringify(doc));
|
||||
await db.put(doc);
|
||||
|
||||
for (var point in points) {
|
||||
const docPoint = { type: 'trkpt', gpx: _id, trkpt: points[point] };
|
||||
console.log(JSON.stringify(docPoint));
|
||||
const response = await db.post(docPoint);
|
||||
console.log(JSON.stringify(docPoint));
|
||||
}
|
||||
};
|
||||
|
||||
const CURRENT_TRACK = '---current track---';
|
||||
const CURRENT_GPX = '---current gpx---';
|
||||
|
||||
const initialTrack = {
|
||||
_id: CURRENT_TRACK,
|
||||
type: 'Track',
|
||||
_id: CURRENT_GPX,
|
||||
type: 'GPX',
|
||||
track: {
|
||||
$: {
|
||||
version: '1.1',
|
||||
@@ -65,18 +94,18 @@ const initialTrack = {
|
||||
export const appendTrkpt = async (db: any, trkpt: any) => {
|
||||
var track: any;
|
||||
await db
|
||||
.get(CURRENT_TRACK)
|
||||
.get(CURRENT_GPX)
|
||||
.then((result: any) => {
|
||||
track = result;
|
||||
})
|
||||
.catch((error: any) => {
|
||||
if (error.status='404') {
|
||||
if ((error.status = '404')) {
|
||||
track = initialTrack;
|
||||
} else {
|
||||
console.log(`db.get(CURRENT_TRACK), ERROR: ${JSON.stringify(error)}`);
|
||||
alert(`db.get(CURRENT_TRACK), ERROR: ${JSON.stringify(error)}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
track.metadata.lastModified = trkpt.time;
|
||||
const currentTrkseg = track.track.trk.at(-1).trkseg.at(-1);
|
||||
currentTrkseg.trkpt.push(trkpt);
|
||||
@@ -0,0 +1,58 @@
|
||||
const DBDEFINITION = '--db-definition--';
|
||||
|
||||
const dbDefinition = {
|
||||
_id: DBDEFINITION,
|
||||
type: DBDEFINITION,
|
||||
version: '0.000001',
|
||||
};
|
||||
|
||||
export const initDb = async (db: any) => {
|
||||
try {
|
||||
await db.get(DBDEFINITION);
|
||||
// TODO: support migrations
|
||||
} catch (error: any) {
|
||||
if (error.status !== 404) {
|
||||
console.log(
|
||||
`Unexpected error fetching db definition: ${JSON.stringify(error)}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await db.createIndex({
|
||||
index: {
|
||||
name: 'type',
|
||||
fields: ['type'],
|
||||
},
|
||||
});
|
||||
|
||||
await db.createIndex({
|
||||
index: {
|
||||
name: 'trkpt-time',
|
||||
fields: ['trkpt.time'],
|
||||
partial_filter_selector: {
|
||||
type: 'trkpt',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await db.createIndex({
|
||||
index: {
|
||||
name: 'type-trkpt-gpx-time3',
|
||||
fields: ['type', 'gpx', 'trkpt.time'],
|
||||
},
|
||||
});
|
||||
|
||||
const indexes = await db.getIndexes();
|
||||
console.log(`indexes: ${JSON.stringify(indexes)}`);
|
||||
|
||||
const explain = await db.explain({
|
||||
selector: {
|
||||
type: 'trkpt',
|
||||
gpx: 'xxxx',
|
||||
},
|
||||
// sort: ['trkpt.time'],
|
||||
// use_index: 'type-trkpt-gpx-time',
|
||||
});
|
||||
console.log(`explain: ${JSON.stringify(explain)}`);
|
||||
};
|
||||
Reference in new issue
Block a user