Skip to content

Commit

Permalink
gimme all your caching', all your serialized objects too
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed Feb 1, 2020
1 parent e8b3405 commit 71a3035
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 56 deletions.
25 changes: 3 additions & 22 deletions content/better-bibtex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,28 +357,9 @@ Zotero.Translate.Import.prototype.Sandbox.BetterBibTeX = {

$patch$(Zotero.Utilities.Internal, 'itemToExportFormat', original => function Zotero_Utilities_Internal_itemToExportFormat(zoteroItem, legacy, skipChildItems) {
const start = Date.now()
try {
let serialized = Serializer.fetch(zoteroItem, !!legacy, !!skipChildItems)
if (serialized) {
log.debug('fetched serialized')
return serialized
}

serialized = original.apply(this, arguments)
log.debug('native serialized')

serialized = Serializer.store(zoteroItem, serialized, !!legacy, !!skipChildItems)
log.debug('stored serialized')

return serialized

} catch (err) { // fallback for safety for non-BBT
log.error('Zotero.Utilities.Internal.itemToExportFormat', err)
} finally {
log.debug('patched serialize took', Date.now() - start)
}

return original.apply(this, arguments)
const serialized = original.apply(this, arguments)
log.debug('Zotero.Utilities.Internal.itemToExportFormat took', Date.now() - start)
return Serializer.enrich(serialized, zoteroItem)
})

$patch$(Zotero.Translate.Export.prototype, 'translate', original => function Zotero_Translate_Export_prototype_translate() {
Expand Down
9 changes: 5 additions & 4 deletions content/db/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,27 @@ class Cache extends Loki {
await this.loadDatabaseAsync()

let coll = this.schemaCollection('itemToExportFormat', {
indices: [ 'itemID', 'legacy', 'skipChildItems' ],
indices: [ 'itemID' ],
logging: false,
cloneObjects: false,
schema: {
type: 'object',
properties: {
itemID: { type: 'integer' },
legacy: { type: 'boolean' },
skipChildItems: { type: 'boolean' },
item: { type: 'object' },

// LokiJS
meta: { type: 'object' },
$loki: { type: 'integer' },
},
required: [ 'itemID', 'legacy', 'skipChildItems', 'item' ],
required: [ 'itemID', 'item' ],
additionalProperties: false,
},
})

// old cache, drop
if (coll.where(o => typeof o.legacy === 'boolean').length) coll.removeDataOnly()

clearOnUpgrade(coll, 'Zotero', Zotero.version)

// this reaps unused cache entries -- make sure that cacheFetchs updates the object
Expand Down
64 changes: 35 additions & 29 deletions content/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@ export let Serializer = new class { // tslint:disable-line:variable-name
this.cache = this.enabled && Cache.getCollection('itemToExportFormat')
}

public fetch(item, legacy, skipChildItems) {
public fetch(item) {
if (!this.cache) return null

const query = { itemID: item.id, legacy: !!legacy, skipChildItems: !!skipChildItems}
const cached = this.cache.findOne(query)
const cached = this.cache.findOne({ itemID: item.id })
if (!cached) return null

return this.enrich(cached.item, item)
}

public store(item, serialized, legacy, skipChildItems) {
public store(item, serialized) {
// come on -- these are used in the collections export but not provided on the items?!
serialized.itemID = item.id
serialized.key = item.key

if (this.cache) {
this.cache.insert({itemID: item.id, legacy: !!legacy, skipChildItems: !!skipChildItems, item: serialized})
this.cache.insert({ itemID: item.id, item: serialized })
} else {
if (this.enabled) Zotero.debug('Serializer.store ignored, DB not yet loaded')
}
Expand All @@ -43,30 +42,36 @@ export let Serializer = new class { // tslint:disable-line:variable-name
public serialize(item) { return Zotero.Utilities.Internal.itemToExportFormat(item, false, true) }

public fast(item) {
let serialized = item.toJSON()
serialized.uri = Zotero.URI.getItemURI(item)
serialized.itemID = item.id

switch (serialized.itemType) {
case 'note':
break

case 'attachment':
serialized = this.fastAttachment(serialized, item)
break

default:
serialized.attachments = item.getAttachments().map(id => {
const att = Zotero.Items.get(id)
return this.fastAttachment({ ...att.toJSON(), uri: Zotero.URI.getItemURI(att) }, att)
})

serialized.notes = item.getNotes().map(id => {
const note = Zotero.Items.get(id)
return { ...note.toJSON(), uri: Zotero.URI.getItemURI(note) }
})
let serialized = this.fetch(item)

if (!serialized) {
serialized = item.toJSON()
serialized.uri = Zotero.URI.getItemURI(item)
serialized.itemID = item.id

switch (serialized.itemType) {
case 'note':
break

case 'attachment':
serialized = this.fastAttachment(serialized, item)
break

default:
serialized.attachments = item.getAttachments().map(id => {
const att = Zotero.Items.get(id)
return this.fastAttachment({ ...att.toJSON(), uri: Zotero.URI.getItemURI(att) }, att)
})

serialized.notes = item.getNotes().map(id => {
const note = Zotero.Items.get(id)
return { ...note.toJSON(), uri: Zotero.URI.getItemURI(note) }
})
}
this.store(item, serialized)
}

// since the cache doesn't clone, these will be written into the cache, but since we override them always anyways, that's OK
return this.enrich(serialized, item)
}

Expand All @@ -78,7 +83,7 @@ export let Serializer = new class { // tslint:disable-line:variable-name
return serialized
}

private enrich(serialized, item) {
public enrich(serialized, item) {
switch (serialized.itemType) {
case 'note':
case 'attachment':
Expand All @@ -88,9 +93,10 @@ export let Serializer = new class { // tslint:disable-line:variable-name
serialized.citekey = KeyManager.get(item.id).citekey
serialized.citationKey = serialized.citationKey || serialized.citekey // prepare for https://github.com/zotero/translators/pull/1810#issuecomment-456219750
serialized.journalAbbreviation = JournalAbbrev.get(serialized)
serialized.libraryID = item.libraryID
break
}

serialized.libraryID = item.libraryID
return serialized
}
}
2 changes: 1 addition & 1 deletion content/translators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export let Translators = new class { // tslint:disable-line:variable-name
prep.exported = (Object.keys(config.cache).length * 100) / config.items.length // tslint:disable-line:no-magic-numbers

try {
worker.postMessage(JSON.parse(JSON.stringify(config, (name, val) => typeof val === 'function' ? undefined : val)))
worker.postMessage(JSON.parse(JSON.stringify(config)))
} catch (err) {
worker.terminate()
this.workers.running.delete(id)
Expand Down

0 comments on commit 71a3035

Please sign in to comment.