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