MCPcopy
hub / github.com/apache/pouchdb / localViewCleanup

Function localViewCleanup

lib/index-browser.js:8979–9041  ·  view source on GitHub ↗
(db)

Source from the content-addressed store, hash-verified

8977 }
8978
8979 async function localViewCleanup(db) {
8980 try {
8981 const metaDoc = await db.get('_local/' + localDocName);
8982 const docsToViews = new Map();
8983
8984 for (const fullViewName of Object.keys(metaDoc.views)) {
8985 const parts = parseViewName(fullViewName);
8986 const designDocName = '_design/' + parts[0];
8987 const viewName = parts[1];
8988 let views = docsToViews.get(designDocName);
8989 if (!views) {
8990 views = new Set();
8991 docsToViews.set(designDocName, views);
8992 }
8993 views.add(viewName);
8994 }
8995 const opts = {
8996 keys : mapToKeysArray(docsToViews),
8997 include_docs : true
8998 };
8999
9000 const res = await db.allDocs(opts);
9001 const viewsToStatus = {};
9002 for (const row of res.rows) {
9003 const ddocName = row.key.substring(8); // cuts off '_design/'
9004 for (const viewName of docsToViews.get(row.key)) {
9005 let fullViewName = ddocName + '/' + viewName;
9006 /* istanbul ignore if */
9007 if (!metaDoc.views[fullViewName]) {
9008 // new format, without slashes, to support PouchDB 2.2.0
9009 // migration test in pouchdb's browser.migration.js verifies this
9010 fullViewName = viewName;
9011 }
9012 const viewDBNames = Object.keys(metaDoc.views[fullViewName]);
9013 // design doc deleted, or view function nonexistent
9014 const statusIsGood = row.doc && row.doc.views &&
9015 row.doc.views[viewName];
9016 for (const viewDBName of viewDBNames) {
9017 viewsToStatus[viewDBName] = viewsToStatus[viewDBName] || statusIsGood;
9018 }
9019 }
9020 }
9021
9022 const dbsToDelete = Object.keys(viewsToStatus)
9023 .filter(function (viewDBName) { return !viewsToStatus[viewDBName]; });
9024
9025 const destroyPromises = dbsToDelete.map(function (viewDBName) {
9026 return sequentialize(getQueue(viewDBName), function () {
9027 return new db.constructor(viewDBName, db.__opts).destroy();
9028 })();
9029 });
9030
9031 return Promise.all(destroyPromises).then(function () {
9032 return {ok: true};
9033 });
9034 } catch (err) {
9035 if (err.status === 404) {
9036 return {ok: true};

Callers 1

createAbstractMapReduceFunction · 0.70

Calls 7

parseViewNameFunction · 0.70
mapToKeysArrayFunction · 0.70
sequentializeFunction · 0.70
getQueueFunction · 0.70
getMethod · 0.45
addMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…