-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
indexeddb.get(open_revs): don't re-fetch docs #8801
Draft
alxndrsn
wants to merge
5
commits into
pouchdb:master
Choose a base branch
from
alxndrsn:indexeddb-get-oneshot-1
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { | ||
rev, | ||
guardedConsole, | ||
isRemote | ||
isRemote, | ||
processAttachments, | ||
processRevsRevsInfo, | ||
} from 'pouchdb-utils'; | ||
import EventEmitter from 'events'; | ||
import Changes from './changes'; | ||
|
@@ -17,7 +19,6 @@ import { | |
import { | ||
traverseRevTree, | ||
collectLeaves, | ||
rootToLeaf, | ||
collectConflicts, | ||
isDeleted, | ||
isLocalId, | ||
|
@@ -555,6 +556,7 @@ class AbstractPouchDB extends EventEmitter { | |
for (var i = 0; i < leaves.length; i++) { | ||
var l = leaves[i]; | ||
// looks like it's the only thing couchdb checks | ||
// TODO replace with !isValidRev(l); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function needs to be created. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now exists... |
||
if (!(typeof (l) === "string" && /^\d+-/.test(l))) { | ||
return cb(createError(INVALID_REV)); | ||
} | ||
|
@@ -589,89 +591,14 @@ class AbstractPouchDB extends EventEmitter { | |
} | ||
|
||
if (opts.revs || opts.revs_info) { | ||
var splittedRev = doc._rev.split('-'); | ||
var revNo = parseInt(splittedRev[0], 10); | ||
var revHash = splittedRev[1]; | ||
|
||
var paths = rootToLeaf(metadata.rev_tree); | ||
var path = null; | ||
|
||
for (var i = 0; i < paths.length; i++) { | ||
var currentPath = paths[i]; | ||
var hashIndex = currentPath.ids.map(function (x) { return x.id; }) | ||
.indexOf(revHash); | ||
var hashFoundAtRevPos = hashIndex === (revNo - 1); | ||
|
||
if (hashFoundAtRevPos || (!path && hashIndex !== -1)) { | ||
path = currentPath; | ||
} | ||
} | ||
|
||
/* istanbul ignore if */ | ||
if (!path) { | ||
err = new Error('invalid rev tree'); | ||
err.docId = id; | ||
try { | ||
processRevsRevsInfo(id, doc, metadata.rev_tree, opts); | ||
} catch (err) { | ||
return cb(err); | ||
} | ||
|
||
var indexOfRev = path.ids.map(function (x) { return x.id; }) | ||
.indexOf(doc._rev.split('-')[1]) + 1; | ||
var howMany = path.ids.length - indexOfRev; | ||
path.ids.splice(indexOfRev, howMany); | ||
path.ids.reverse(); | ||
|
||
if (opts.revs) { | ||
doc._revisions = { | ||
start: (path.pos + path.ids.length) - 1, | ||
ids: path.ids.map(function (rev) { | ||
return rev.id; | ||
}) | ||
}; | ||
} | ||
if (opts.revs_info) { | ||
var pos = path.pos + path.ids.length; | ||
doc._revs_info = path.ids.map(function (rev) { | ||
pos--; | ||
return { | ||
rev: pos + '-' + rev.id, | ||
status: rev.opts.status | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
if (opts.attachments && doc._attachments) { | ||
var attachments = doc._attachments; | ||
var count = Object.keys(attachments).length; | ||
if (count === 0) { | ||
return cb(null, doc); | ||
} | ||
Object.keys(attachments).forEach((key) => { | ||
this._getAttachment(doc._id, key, attachments[key], { | ||
binary: opts.binary, | ||
metadata: metadata, | ||
ctx: ctx | ||
}, function (err, data) { | ||
var att = doc._attachments[key]; | ||
att.data = data; | ||
delete att.stub; | ||
delete att.length; | ||
if (!--count) { | ||
cb(null, doc); | ||
} | ||
}); | ||
}); | ||
} else { | ||
if (doc._attachments) { | ||
for (var key in doc._attachments) { | ||
/* istanbul ignore else */ | ||
if (Object.prototype.hasOwnProperty.call(doc._attachments, key)) { | ||
doc._attachments[key].stub = true; | ||
} | ||
} | ||
} | ||
cb(null, doc); | ||
} | ||
processAttachments(this, metadata, doc, opts, ctx, cb); | ||
}); | ||
}).bind(this); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/node_modules/pouchdb-utils/src/processAttachments.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
/** | ||
* Process doc._attachments as requested in opts. | ||
*/ | ||
|
||
function processAttachments(api, metadata, doc, opts, ctx, cb) { | ||
if (opts.attachments && doc._attachments) { | ||
const attachments = doc._attachments; | ||
let count = Object.keys(attachments).length; | ||
if (count === 0) { | ||
return cb(null, doc); | ||
} | ||
Object.keys(attachments).forEach((key) => { | ||
api._getAttachment(doc._id, key, attachments[key], { | ||
binary: opts.binary, | ||
metadata: metadata, | ||
ctx: ctx, | ||
}, function (err, data) { | ||
const att = doc._attachments[key]; | ||
att.data = data; | ||
delete att.stub; | ||
delete att.length; | ||
if (!--count) { | ||
cb(null, doc); | ||
} | ||
}); | ||
}); | ||
} else { | ||
if (doc._attachments) { | ||
for (const key in doc._attachments) { | ||
/* istanbul ignore else */ | ||
if (Object.prototype.hasOwnProperty.call(doc._attachments, key)) { | ||
doc._attachments[key].stub = true; | ||
} | ||
} | ||
} | ||
cb(null, doc); | ||
} | ||
} | ||
|
||
export default processAttachments; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look like code you would write... is it copied from somewhere? If so, can it be reused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC it mostly came from
adapter.js
inpouchd-core
. You're right that there's probably more code that could be shared.