Implementing a mechanism to purge deleted docs.

This commit is contained in:
Eric van der Vlist 2023-02-12 12:19:00 +01:00
parent 534ec32312
commit 5a5931dbcd
1 changed files with 62 additions and 51 deletions

View File

@ -1,10 +1,8 @@
import { createEffect, createRoot, createSignal, Signal } from 'solid-js'; import { createEffect, createRoot, createSignal } from 'solid-js';
import PouchDB from 'pouchdb'; import PouchDB from 'pouchdb';
import indexeddb from 'pouchdb-adapter-indexeddb'; import indexeddb from 'pouchdb-adapter-indexeddb';
import { sleep, until } from '../lib/async-wait'; import { sleep, until } from '../lib/async-wait';
import { openDatabases } from './open';
import { toHex } from '../lib/to-hex';
import { isEqual } from 'lodash'; import { isEqual } from 'lodash';
PouchDB.plugin(indexeddb); PouchDB.plugin(indexeddb);
@ -108,18 +106,15 @@ export const watchDbLegacy = async () => {
}); });
} }
const timerId = setInterval(updateStatus, 10000); const timerId = setInterval(updateStatus, 15000);
// db.compact(); // db.compact();
// await sleep(10000);
const openIDb = (name: string) => const openIDb = (name: string) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const iDb = indexedDB.open(name); const iDb = indexedDB.open(name);
iDb.onerror = (event: any) => { iDb.onerror = (event: any) => {
console.error({ console.error({
caller: 'watchDb', caller: 'watchDbLegacy',
message: 'open db error', message: 'open db error',
target: event.target, target: event.target,
}); });
@ -127,11 +122,6 @@ export const watchDbLegacy = async () => {
}; };
iDb.onsuccess = (event: any) => { iDb.onsuccess = (event: any) => {
console.log({
caller: 'watchDb',
message: 'open db',
target: event.target,
});
resolve(event.target.result); resolve(event.target.result);
}; };
}); });
@ -141,7 +131,7 @@ export const watchDbLegacy = async () => {
const request = store.getAll(); const request = store.getAll();
request.onerror = (event: any) => { request.onerror = (event: any) => {
console.error({ console.error({
caller: 'watchDb', caller: 'watchDbLegacy',
message: 'getAll error', message: 'getAll error',
target: event.target, target: event.target,
}); });
@ -149,15 +139,36 @@ export const watchDbLegacy = async () => {
}; };
request.onsuccess = (event: any) => { request.onsuccess = (event: any) => {
console.log({
caller: 'watchDb',
message: 'getAll',
target: event.target,
});
resolve(event.target.result as any[]); resolve(event.target.result as any[]);
}; };
}); });
const getDeletedDocs = async () => {
const iDb = (await openIDb(`_pouch_${state().dbName}`)) as IDBDatabase;
const bySequence = iDb.transaction('docs', 'readonly');
const store = bySequence.objectStore('docs');
const allDocs = (await getAll(store)) as any[];
iDb.close();
const docs = allDocs
.filter(
(doc) =>
!doc.id.startsWith('_local') &&
doc.deleted === 1 &&
doc.seq <= state()?.sync?.lastSeq
)
.map((doc) => ({ id: doc.id, rev: doc.rev }));
// console.log({
// caller: 'watchDbLegacy / getDeletedDocs',
// // bySequence,
// // store,
// // allDocs,
// docs,
// state: state(),
// });
return docs;
};
while (true) {
await until( await until(
() => () =>
state()?.sync?.paused && state()?.sync?.paused &&
@ -166,33 +177,33 @@ export const watchDbLegacy = async () => {
); );
console.log({ caller: 'watchDbLegacy / ready to purge after sync ' }); console.log({ caller: 'watchDbLegacy / ready to purge after sync ' });
// const iDb = (await openIDb('_pouch__dyomedea_')) as IDBDatabase;
// const bySequence = iDb.transaction('docs', 'readonly'); const deletedDocs = await getDeletedDocs();
// const store = bySequence.objectStore('docs');
// console.log({ if (deletedDocs.length > 0) {
// caller: 'watchDb', console.log({
// message: 'transaction opened', caller: 'watchDbLegacy / purge',
// bySequence, // bySequence,
// store, // store,
// }); // allDocs,
deletedDocs,
state: state(),
});
// const docs = (await getAll(store)) as any[]; deletedDocs.forEach(async (doc) => {
await sleep(500);
// docs.forEach(async (doc) => { try {
// const { id, rev, deleted } = doc; await db.purge(doc.id, doc.rev);
// if (deleted !== 0) { } catch (err) {
// // const purge = await db.purge(id, rev); console.log({
// console.log({ caller: 'watchDbLegacy / purge',
// caller: 'watchDb', doc,
// message: 'purging', err,
// id, });
// rev, }
// doc, });
// // purge, }
// }); await sleep(60000);
// await sleep(1000); }
// }
// });
}); });
}; };