MCPcopy Index your code
hub / github.com/apache/pouchdb / localViewCleanup

Function localViewCleanup

lib/index-browser.es.js:8975–9037  ·  view source on GitHub ↗
(db)

Source from the content-addressed store, hash-verified

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