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,50 +139,71 @@ 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[]);
}; };
}); });
await until( const getDeletedDocs = async () => {
() => const iDb = (await openIDb(`_pouch_${state().dbName}`)) as IDBDatabase;
state()?.sync?.paused && const bySequence = iDb.transaction('docs', 'readonly');
state()?.sync?.lastSeq >= state()?.localUpdateSeq, const store = bySequence.objectStore('docs');
1000 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;
};
console.log({ caller: 'watchDbLegacy / ready to purge after sync ' }); while (true) {
// const iDb = (await openIDb('_pouch__dyomedea_')) as IDBDatabase; await until(
() =>
state()?.sync?.paused &&
state()?.sync?.lastSeq >= state()?.localUpdateSeq,
1000
);
// const bySequence = iDb.transaction('docs', 'readonly'); console.log({ caller: 'watchDbLegacy / ready to purge after sync ' });
// const store = bySequence.objectStore('docs');
// console.log({
// caller: 'watchDb',
// message: 'transaction opened',
// bySequence,
// store,
// });
// const docs = (await getAll(store)) as any[]; const deletedDocs = await getDeletedDocs();
// docs.forEach(async (doc) => { if (deletedDocs.length > 0) {
// const { id, rev, deleted } = doc; console.log({
// if (deleted !== 0) { caller: 'watchDbLegacy / purge',
// // const purge = await db.purge(id, rev); // bySequence,
// console.log({ // store,
// caller: 'watchDb', // allDocs,
// message: 'purging', deletedDocs,
// id, state: state(),
// rev, });
// doc,
// // purge, deletedDocs.forEach(async (doc) => {
// }); await sleep(500);
// await sleep(1000); try {
// } await db.purge(doc.id, doc.rev);
// }); } catch (err) {
console.log({
caller: 'watchDbLegacy / purge',
doc,
err,
});
}
});
}
await sleep(60000);
}
}); });
}; };