refactor database

This commit is contained in:
Nolan Lawson 2018-02-08 21:49:52 -08:00
parent 476a9644f1
commit 08b3754746
1 changed files with 85 additions and 41 deletions

View File

@ -57,37 +57,25 @@ async function setGenericEntityWithId(store, cache, instanceName, entity) {
// timelines/statuses/notifications // timelines/statuses/notifications
// //
function getTimelineVariables(timeline) { function createKeyRange(timeline, maxId) {
if (timeline === 'notifications') {
return {
stores: [NOTIFICATION_TIMELINES_STORE, NOTIFICATIONS_STORE, ACCOUNTS_STORE],
remoteId: 'notificationId',
itemsCache: notificationsCache
}
}
return {
stores: [STATUS_TIMELINES_STORE, STATUSES_STORE, ACCOUNTS_STORE],
remoteId: 'statusId',
itemsCache: statusesCache
}
}
export async function getTimeline(instanceName, timeline, maxId = null, limit = 20) {
let { stores, remoteId } = getTimelineVariables(timeline)
const db = await getDatabase(instanceName)
return await dbPromise(db, stores, 'readonly', (stores, callback) => {
let [ timelineStore, itemsStore ] = stores
let negBigInt = maxId && toReversePaddedBigInt(maxId) let negBigInt = maxId && toReversePaddedBigInt(maxId)
let start = negBigInt ? (timeline + '\u0000' + negBigInt) : (timeline + '\u0000') let start = negBigInt ? (timeline + '\u0000' + negBigInt) : (timeline + '\u0000')
let end = timeline + '\u0000\uffff' let end = timeline + '\u0000\uffff'
let query = IDBKeyRange.bound(start, end, false, false) return IDBKeyRange.bound(start, end, false, false)
}
timelineStore.getAll(query, limit).onsuccess = e => { async function getNotificationTimeline(instanceName, timeline, maxId, limit) {
let storeNames = [NOTIFICATION_TIMELINES_STORE, NOTIFICATIONS_STORE]
const db = await getDatabase(instanceName)
return await dbPromise(db, storeNames, 'readonly', (stores, callback) => {
let [ timelineStore, notificationsStore ] = stores
let keyRange = createKeyRange(timeline, maxId)
timelineStore.getAll(keyRange, limit).onsuccess = e => {
let timelineResults = e.target.result let timelineResults = e.target.result
let res = new Array(timelineResults.length) let res = new Array(timelineResults.length)
timelineResults.forEach((timelineResult, i) => { timelineResults.forEach((timelineResult, i) => {
itemsStore.get(timelineResult[remoteId]).onsuccess = e => { notificationsStore.get(timelineResult.notificationId).onsuccess = e => {
res[i] = e.target.result res[i] = e.target.result
} }
}) })
@ -96,33 +84,89 @@ export async function getTimeline(instanceName, timeline, maxId = null, limit =
}) })
} }
export async function insertTimelineItems(instanceName, timeline, timelineItems) { async function getStatusTimeline(instanceName, timeline, maxId, limit) {
let { stores, remoteId, itemsCache } = getTimelineVariables(timeline) let storeNames = [STATUS_TIMELINES_STORE, STATUSES_STORE]
for (let timelineItem of timelineItems) { const db = await getDatabase(instanceName)
setInCache(itemsCache, instanceName, timelineItem.id, timelineItem) return await dbPromise(db, storeNames, 'readonly', (stores, callback) => {
setInCache(accountsCache, instanceName, timelineItem.account.id, timelineItem.account) let [ timelineStore, statusesStore ] = stores
if (timelineItem.reblog) { let keyRange = createKeyRange(timeline, maxId)
setInCache(accountsCache, instanceName, timelineItem.reblog.account.id, timelineItem.reblog.account)
timelineStore.getAll(keyRange, limit).onsuccess = e => {
let timelineResults = e.target.result
let res = new Array(timelineResults.length)
timelineResults.forEach((timelineResult, i) => {
statusesStore.get(timelineResult.statusId).onsuccess = e => {
res[i] = e.target.result
}
})
callback(res)
}
})
}
export async function getTimeline(instanceName, timeline, maxId = null, limit = 20) {
return timeline === 'notifications' ?
await getNotificationTimeline(instanceName, timeline, maxId, limit) :
await getStatusTimeline(instanceName, timeline, maxId, limit)
}
function createTimelineId(timeline, id) {
// reverse chronological order, prefixed by timeline
return timeline + '\u0000' + toReversePaddedBigInt(id)
}
async function insertTimelineNotifications(instanceName, timeline, notifications) {
let storeNames = [NOTIFICATION_TIMELINES_STORE, NOTIFICATIONS_STORE, ACCOUNTS_STORE]
for (let notification of notifications) {
setInCache(notificationsCache, instanceName, notification.id, notification)
setInCache(accountsCache, instanceName, notification.account.id, notification.account)
}
const db = await getDatabase(instanceName)
await dbPromise(db, storeNames, 'readwrite', (stores) => {
let [ timelineStore, notificationsStore, accountsStore ] = stores
for (let notification of notifications) {
notificationsStore.put(notification)
timelineStore.put({
id: createTimelineId(timeline, notification.id),
notificationId: notification.id
})
accountsStore.put(notification.account)
}
})
}
async function insertTimelineStatuses(instanceName, timeline, statuses) {
let storeNames = [STATUS_TIMELINES_STORE, STATUSES_STORE, ACCOUNTS_STORE]
for (let status of statuses) {
setInCache(statusesCache, instanceName, status.id, status)
setInCache(accountsCache, instanceName, status.account.id, status.account)
if (status.reblog) {
setInCache(accountsCache, instanceName, status.reblog.account.id, status.reblog.account)
} }
} }
const db = await getDatabase(instanceName) const db = await getDatabase(instanceName)
await dbPromise(db, stores, 'readwrite', (stores) => { await dbPromise(db, storeNames, 'readwrite', (stores) => {
let [ timelineStore, itemsStore, accountsStore ] = stores let [ timelineStore, statusesStore, accountsStore ] = stores
for (let item of timelineItems) { for (let status of statuses) {
itemsStore.put(item) statusesStore.put(status)
// reverse chronological order, prefixed by timeline
timelineStore.put({ timelineStore.put({
id: (timeline + '\u0000' + toReversePaddedBigInt(item.id)), id: createTimelineId(timeline, status.id),
[remoteId]: item.id statusId: status.id
}) })
accountsStore.put(item.account) accountsStore.put(status.account)
if (item.reblog) { if (status.reblog) {
accountsStore.put(item.reblog.account) accountsStore.put(status.reblog.account)
} }
} }
}) })
} }
export async function insertTimelineItems(instanceName, timeline, timelineItems) {
return timeline === 'notifications' ?
await insertTimelineNotifications(instanceName, timeline, timelineItems) :
await insertTimelineStatuses(instanceName, timeline, timelineItems)
}
export async function getStatus(instanceName, statusId) { export async function getStatus(instanceName, statusId) {
return await getGenericEntityWithId(STATUSES_STORE, statusesCache, instanceName, statusId) return await getGenericEntityWithId(STATUSES_STORE, statusesCache, instanceName, statusId)
} }