Manage order with sorts and indexing rather than keys

The jury is still out on this.  Indexes are the "right" way to do this but are actually slower in some circumstances.
This commit is contained in:
Timothy Farrell 2018-01-12 11:50:56 -06:00
parent 6fcf2a88c0
commit 2929e08ecb
4 changed files with 43 additions and 22 deletions

View File

@ -25,10 +25,6 @@ class ImageSpec extends TypeSpec {
return doc.digest.substr(0, 16); return doc.digest.substr(0, 16);
} }
static getSequence(doc) {
return new Date(doc.originalDate).getTime();
}
async delete(cascade = true) { async delete(cascade = true) {
if (cascade) { if (cascade) {
new Set(Object.keys(this.sizes)).forEach(async key => { new Set(Object.keys(this.sizes)).forEach(async key => {
@ -126,4 +122,6 @@ const processImportables = backgroundTask(async function _processImportables(ima
export const ImageType = PouchDB.registerType('Image', ImageSpec); export const ImageType = PouchDB.registerType('Image', ImageSpec);
ImageType.index('originalDate', ['originalDate', 'id']);
ImageType.find({ importing: true }).then(results => results.forEach(processImportables)); ImageType.find({ importing: true }).then(results => results.forEach(processImportables));

View File

@ -107,10 +107,32 @@ export function FocusView(vm, params) {
} }
doc(await ImageType.find(_id)); doc(await ImageType.find(_id));
const n = await ImageType.next(_id); const n = await ImageType.find(
nextLink(n.length ? router.href('focus', { id: n[0].id }) : null); {
const p = await ImageType.next(_id, true); originalDate: { $gte: doc().originalDate }
prevLink(p.length ? router.href('focus', { id: p[0].id }) : null); },
{
limit: 1,
skip: 1,
index: 'originalDate',
sort: [{ originalDate: 'asc' }]
}
);
const p = await ImageType.find(
{
originalDate: { $lte: doc().originalDate }
},
{
limit: 1,
skip: 1,
index: 'originalDate',
sort: [{ originalDate: 'desc' }]
}
);
nextLink(n.length ? router.href('focus', { id: n[0]._id }) : null);
prevLink(p.length ? router.href('focus', { id: p[0]._id }) : null);
}) })
], ],
true true

View File

@ -60,6 +60,8 @@ export function SectionView(vm, params, key) {
[photoArray, availableViewportSize] [photoArray, availableViewportSize]
); );
photos.sort((a, b) => a.originalDate.localeCompare(b.originalDate));
subscribeToRender(vm, [sections]); subscribeToRender(vm, [sections]);
return function render(vm, params) { return function render(vm, params) {

View File

@ -23,10 +23,6 @@ export class TypeSpec {
Object.assign(this, { $links: {} }, props, { type: this._prefix }); Object.assign(this, { $links: {} }, props, { type: this._prefix });
} }
static getSequence(doc) {
return '';
}
static getUniqueID(doc) { static getUniqueID(doc) {
throw 'NotImplemented'; throw 'NotImplemented';
} }
@ -39,7 +35,7 @@ export class TypeSpec {
_populateId(doc) { _populateId(doc) {
if (!doc._id) { if (!doc._id) {
doc._id = `${this._prefix}_${this._cls.getSequence(doc)}_${this._cls.getUniqueID(doc)}`; doc._id = `${this._prefix}_${this._cls.getUniqueID(doc)}`;
} }
return doc; return doc;
} }
@ -116,6 +112,10 @@ export function PouchORM(PouchDB) {
isSelector && idOrSelector._deleted ? { _deleted: true } : { _deleted: { exists: false } }, isSelector && idOrSelector._deleted ? { _deleted: true } : { _deleted: { exists: false } },
isSelector ? idOrSelector : _baseSelector isSelector ? idOrSelector : _baseSelector
); );
if (opts.index) {
opts.use_index = [prefix, opts.index];
delete opts.index;
}
if (opts.live) { if (opts.live) {
opts.mapper = instantiate; opts.mapper = instantiate;
return LiveArray(_db, idOrSelector, opts); return LiveArray(_db, idOrSelector, opts);
@ -148,15 +148,14 @@ export function PouchORM(PouchDB) {
} }
} }
async function next(key, previous = false, limit = 1, inclusive = false) { async function _index(name, fields) {
const res = await _db.allDocs({ return _db.createIndex({
startkey: key, index: {
descending: previous, ddoc: prefix,
sort: ['id'], fields,
skip: inclusive ? 0 : 1, name
limit }
}); });
return res.rows;
} }
Object.defineProperties(cls.prototype, { Object.defineProperties(cls.prototype, {
@ -170,7 +169,7 @@ export function PouchORM(PouchDB) {
Object.defineProperties(cls, { Object.defineProperties(cls, {
getOrCreate: { value: getOrCreate }, getOrCreate: { value: getOrCreate },
find: { value: find }, find: { value: find },
next: { value: next }, index: { value: _index },
delete: { value: _delete }, delete: { value: _delete },
subscribe: { value: watch }, subscribe: { value: watch },
db: { value: _db }, db: { value: _db },