Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#7954) - Connection is reopened, with retry, if automatically closed (idb adapter) #8455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 135 additions & 135 deletions packages/node_modules/pouchdb-adapter-idb/src/allDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
decodeMetadata,
fetchAttachmentsIfNecessary,
postProcessAttachments,
openTransactionSafely,
idbError
} from './utils';
import runBatchedCursor from './runBatchedCursor';
Expand Down Expand Up @@ -65,21 +64,21 @@ function createKeyRange(start, end, inclusiveEnd, key, descending) {
return null;
}

function idbAllDocs(opts, idb, callback) {
function idbAllDocs(opts, openTransactionSafely, callback) {
var start = 'startkey' in opts ? opts.startkey : false;
var end = 'endkey' in opts ? opts.endkey : false;
var key = 'key' in opts ? opts.key : false;
var keys = 'keys' in opts ? opts.keys : false;
var keys = 'keys' in opts ? opts.keys : false;
var skip = opts.skip || 0;
var limit = typeof opts.limit === 'number' ? opts.limit : -1;
var inclusiveEnd = opts.inclusive_end !== false;

var keyRange ;
var keyRange;
var keyRangeError;
if (!keys) {
keyRange = createKeyRange(start, end, inclusiveEnd, key, opts.descending);
keyRangeError = keyRange && keyRange.error;
if (keyRangeError &&
if (keyRangeError &&
!(keyRangeError.name === "DataError" && keyRangeError.code === 0)) {
// DataError with error code 0 indicates start is less than end, so
// can just do an empty query. Else need to throw
Expand All @@ -93,160 +92,161 @@ function idbAllDocs(opts, idb, callback) {
if (opts.attachments) {
stores.push(ATTACH_STORE);
}
var txnResult = openTransactionSafely(idb, stores, 'readonly');
if (txnResult.error) {
return callback(txnResult.error);
}
var txn = txnResult.txn;
txn.oncomplete = onTxnComplete;
txn.onabort = idbError(callback);
var docStore = txn.objectStore(DOC_STORE);
var seqStore = txn.objectStore(BY_SEQ_STORE);
var metaStore = txn.objectStore(META_STORE);
var docIdRevIndex = seqStore.index('_doc_id_rev');
var results = [];
var docCount;
var updateSeq;

metaStore.get(META_STORE).onsuccess = function (e) {
docCount = e.target.result.docCount;
};

/* istanbul ignore if */
if (opts.update_seq) {
getMaxUpdateSeq(seqStore, function (e) {
if (e.target.result && e.target.result.length > 0) {
updateSeq = e.target.result[0];
}
});
}
openTransactionSafely(stores, 'readonly', function (txnResult) {
if (txnResult.error) {
return callback(txnResult.error);
}
var txn = txnResult.txn;
txn.oncomplete = onTxnComplete;
txn.onabort = idbError(callback);
var docStore = txn.objectStore(DOC_STORE);
var seqStore = txn.objectStore(BY_SEQ_STORE);
var metaStore = txn.objectStore(META_STORE);
var docIdRevIndex = seqStore.index('_doc_id_rev');
var results = [];
var docCount;
var updateSeq;

metaStore.get(META_STORE).onsuccess = function (e) {
docCount = e.target.result.docCount;
};

function getMaxUpdateSeq(objectStore, onSuccess) {
function onCursor(e) {
var cursor = e.target.result;
var maxKey = undefined;
if (cursor && cursor.key) {
maxKey = cursor.key;
}
return onSuccess({
target: {
result: [maxKey]
/* istanbul ignore if */
if (opts.update_seq) {
getMaxUpdateSeq(seqStore, function (e) {
if (e.target.result && e.target.result.length > 0) {
updateSeq = e.target.result[0];
}
});
}
objectStore.openCursor(null, 'prev').onsuccess = onCursor;
}

// if the user specifies include_docs=true, then we don't
// want to block the main cursor while we're fetching the doc
function fetchDocAsynchronously(metadata, row, winningRev) {
var key = metadata.id + "::" + winningRev;
docIdRevIndex.get(key).onsuccess = function onGetDoc(e) {
row.doc = decodeDoc(e.target.result) || {};
if (opts.conflicts) {
var conflicts = collectConflicts(metadata);
if (conflicts.length) {
row.doc._conflicts = conflicts;
function getMaxUpdateSeq(objectStore, onSuccess) {
function onCursor(e) {
var cursor = e.target.result;
var maxKey = undefined;
if (cursor && cursor.key) {
maxKey = cursor.key;
}
return onSuccess({
target: {
result: [maxKey]
}
});
}
fetchAttachmentsIfNecessary(row.doc, opts, txn);
};
}
objectStore.openCursor(null, 'prev').onsuccess = onCursor;
}

function allDocsInner(winningRev, metadata) {
var row = {
id: metadata.id,
key: metadata.id,
value: {
rev: winningRev
}
};
var deleted = metadata.deleted;
if (deleted) {
if (keys) {
// if the user specifies include_docs=true, then we don't
// want to block the main cursor while we're fetching the doc
function fetchDocAsynchronously(metadata, row, winningRev) {
var key = metadata.id + "::" + winningRev;
docIdRevIndex.get(key).onsuccess = function onGetDoc(e) {
row.doc = decodeDoc(e.target.result) || {};
if (opts.conflicts) {
var conflicts = collectConflicts(metadata);
if (conflicts.length) {
row.doc._conflicts = conflicts;
}
}
fetchAttachmentsIfNecessary(row.doc, opts, txn);
};
}

function allDocsInner(winningRev, metadata) {
var row = {
id: metadata.id,
key: metadata.id,
value: {
rev: winningRev
}
};
var deleted = metadata.deleted;
if (deleted) {
if (keys) {
results.push(row);
// deleted docs are okay with "keys" requests
row.value.deleted = true;
row.doc = null;
}
} else if (skip-- <= 0) {
results.push(row);
// deleted docs are okay with "keys" requests
row.value.deleted = true;
row.doc = null;
if (opts.include_docs) {
fetchDocAsynchronously(metadata, row, winningRev);
}
}
} else if (skip-- <= 0) {
results.push(row);
if (opts.include_docs) {
fetchDocAsynchronously(metadata, row, winningRev);
}

function processBatch(batchValues) {
for (var i = 0, len = batchValues.length; i < len; i++) {
if (results.length === limit) {
break;
}
var batchValue = batchValues[i];
if (batchValue.error && keys) {
// key was not found with "keys" requests
results.push(batchValue);
continue;
}
var metadata = decodeMetadata(batchValue);
var winningRev = metadata.winningRev;
allDocsInner(winningRev, metadata);
}
}
}

function processBatch(batchValues) {
for (var i = 0, len = batchValues.length; i < len; i++) {
if (results.length === limit) {
break;
function onBatch(batchKeys, batchValues, cursor) {
if (!cursor) {
return;
}
var batchValue = batchValues[i];
if (batchValue.error && keys) {
// key was not found with "keys" requests
results.push(batchValue);
continue;
processBatch(batchValues);
if (results.length < limit) {
cursor.continue();
}
var metadata = decodeMetadata(batchValue);
var winningRev = metadata.winningRev;
allDocsInner(winningRev, metadata);
}
}

function onBatch(batchKeys, batchValues, cursor) {
if (!cursor) {
return;
}
processBatch(batchValues);
if (results.length < limit) {
cursor.continue();
function onGetAll(e) {
var values = e.target.result;
if (opts.descending) {
values = values.reverse();
}
processBatch(values);
}
}

function onGetAll(e) {
var values = e.target.result;
if (opts.descending) {
values = values.reverse();
}
processBatch(values);
}
function onResultsReady() {
var returnVal = {
total_rows: docCount,
offset: opts.skip,
rows: results
};

function onResultsReady() {
var returnVal = {
total_rows: docCount,
offset: opts.skip,
rows: results
};

/* istanbul ignore if */
if (opts.update_seq && updateSeq !== undefined) {
returnVal.update_seq = updateSeq;
/* istanbul ignore if */
if (opts.update_seq && updateSeq !== undefined) {
returnVal.update_seq = updateSeq;
}
callback(null, returnVal);
}
callback(null, returnVal);
}

function onTxnComplete() {
if (opts.attachments) {
postProcessAttachments(results, opts.binary).then(onResultsReady);
} else {
onResultsReady();
function onTxnComplete() {
if (opts.attachments) {
postProcessAttachments(results, opts.binary).then(onResultsReady);
} else {
onResultsReady();
}
}
}

// don't bother doing any requests if start > end or limit === 0
if (keyRangeError || limit === 0) {
return;
}
if (keys) {
return allDocsKeys(opts.keys, docStore, onBatch);
}
if (limit === -1) { // just fetch everything
return getAll(docStore, keyRange, onGetAll);
}
// else do a cursor
// choose a batch size based on the skip, since we'll need to skip that many
runBatchedCursor(docStore, keyRange, opts.descending, limit + skip, onBatch);
// don't bother doing any requests if start > end or limit === 0
if (keyRangeError || limit === 0) {
return;
}
if (keys) {
return allDocsKeys(opts.keys, docStore, onBatch);
}
if (limit === -1) { // just fetch everything
return getAll(docStore, keyRange, onGetAll);
}
// else do a cursor
// choose a batch size based on the skip, since we'll need to skip that many
runBatchedCursor(docStore, keyRange, opts.descending, limit + skip, onBatch);
});
}

export default idbAllDocs;
Loading