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

Function createAbstractMapReduce

lib/index-browser.es.js:8140–9146  ·  view source on GitHub ↗

* Returns an "abstract" mapreduce object of the form: * * { * query: queryFun, * viewCleanup: viewCleanupFun * } * * Arguments are: * * localDoc: string * This is for the local doc that gets saved in order to track the * "dependent" DBs and clean them up for viewCleanup. I

(localDocName, mapper, reducer, ddocValidator)

Source from the content-addressed store, hash-verified

8138 * indexer is invalid.
8139 */
8140function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
8141
8142 function tryMap(db, fun, doc) {
8143 // emit an event if there was an error thrown by a map function.
8144 // putting try/catches in a single function also avoids deoptimizations.
8145 try {
8146 fun(doc);
8147 } catch (e) {
8148 emitError(db, e, {fun, doc});
8149 }
8150 }
8151
8152 function tryReduce(db, fun, keys, values, rereduce) {
8153 // same as above, but returning the result or an error. there are two separate
8154 // functions to avoid extra memory allocations since the tryCode() case is used
8155 // for custom map functions (common) vs this function, which is only used for
8156 // custom reduce functions (rare)
8157 try {
8158 return {output : fun(keys, values, rereduce)};
8159 } catch (e) {
8160 emitError(db, e, {fun, keys, values, rereduce});
8161 return {error: e};
8162 }
8163 }
8164
8165 function sortByKeyThenValue(x, y) {
8166 const keyCompare = collate(x.key, y.key);
8167 return keyCompare !== 0 ? keyCompare : collate(x.value, y.value);
8168 }
8169
8170 function sliceResults(results, limit, skip) {
8171 skip = skip || 0;
8172 if (typeof limit === 'number') {
8173 return results.slice(skip, limit + skip);
8174 } else if (skip > 0) {
8175 return results.slice(skip);
8176 }
8177 return results;
8178 }
8179
8180 function rowToDocId(row) {
8181 const val = row.value;
8182 // Users can explicitly specify a joined doc _id, or it
8183 // defaults to the doc _id that emitted the key/value.
8184 const docId = (val && typeof val === 'object' && val._id) || row.id;
8185 return docId;
8186 }
8187
8188 function readAttachmentsAsBlobOrBuffer(res) {
8189 for (const row of res.rows) {
8190 const atts = row.doc && row.doc._attachments;
8191 if (!atts) {
8192 continue;
8193 }
8194 for (const filename of Object.keys(atts)) {
8195 const att = atts[filename];
8196 atts[filename].data = b64ToBluffer(att.data, att.content_type);
8197 }

Callers 1

Calls 5

callbackifyFunction · 0.70
customViewCleanupFunction · 0.70
isRemoteFunction · 0.70
httpViewCleanupFunction · 0.70
localViewCleanupFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…