Replace lists by sets in the apply action cache

This commit is contained in:
Jonas Lochmann 2020-09-28 02:00:00 +02:00
parent 8d65c5d777
commit 8ebeadad5a
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
24 changed files with 55 additions and 46 deletions

View file

@ -15,11 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { memoize, uniq } from 'lodash'
import { memoize } from 'lodash'
import * as Sequelize from 'sequelize'
import { config } from '../../../config'
import { VisibleConnectedDevicesManager } from '../../../connected-devices'
import { Database } from '../../../database'
import { setToList } from '../../../util/list'
import { generateVersionId } from '../../../util/token'
import { SourceUserNotFoundException } from './exception/illegal-state'
import { InvalidChildActionIntegrityValue } from './exception/integrity'
@ -32,12 +33,12 @@ export class Cache {
readonly connectedDevicesManager: VisibleConnectedDevicesManager
private shouldTriggerFullSync = false
categoriesWithModifiedApps: Array<string> = []
categoriesWithModifiedBaseData: Array<string> = []
categoriesWithModifiedTimeLimitRules: Array<string> = []
categoriesWithModifiedUsedTimes: Array<string> = []
categoriesWithModifiedApps = new Set<string>()
categoriesWithModifiedBaseData = new Set<string>()
categoriesWithModifiedTimeLimitRules = new Set<string>()
categoriesWithModifiedUsedTimes = new Set<string>()
devicesWithModifiedInstalledApps: Array<string> = []
devicesWithModifiedInstalledApps = new Set<string>()
devicesWithModifiedShowDeviceConnected = new Map<string, boolean>()
invalidiateUserList = false
@ -144,84 +145,84 @@ export class Cache {
async saveModifiedVersionNumbers () {
const { database, transaction, familyId } = this
if (this.categoriesWithModifiedApps.length > 0) {
if (this.categoriesWithModifiedApps.size > 0) {
await database.category.update({
assignedAppsVersion: generateVersionId()
}, {
where: {
familyId,
categoryId: {
[Sequelize.Op.in]: uniq(this.categoriesWithModifiedApps)
[Sequelize.Op.in]: setToList(this.categoriesWithModifiedApps)
}
},
transaction
})
this.categoriesWithModifiedApps = []
this.categoriesWithModifiedApps.clear()
}
if (this.categoriesWithModifiedBaseData.length > 0) {
if (this.categoriesWithModifiedBaseData.size > 0) {
await database.category.update({
baseVersion: generateVersionId()
}, {
where: {
familyId,
categoryId: {
[Sequelize.Op.in]: uniq(this.categoriesWithModifiedBaseData)
[Sequelize.Op.in]: setToList(this.categoriesWithModifiedBaseData)
}
},
transaction
})
this.categoriesWithModifiedBaseData = []
this.categoriesWithModifiedBaseData.clear()
}
if (this.categoriesWithModifiedTimeLimitRules.length > 0) {
if (this.categoriesWithModifiedTimeLimitRules.size > 0) {
await database.category.update({
timeLimitRulesVersion: generateVersionId()
}, {
where: {
familyId,
categoryId: {
[Sequelize.Op.in]: uniq(this.categoriesWithModifiedTimeLimitRules)
[Sequelize.Op.in]: setToList(this.categoriesWithModifiedTimeLimitRules)
}
},
transaction
})
this.categoriesWithModifiedTimeLimitRules = []
this.categoriesWithModifiedTimeLimitRules.clear()
}
if (this.categoriesWithModifiedUsedTimes.length > 0) {
if (this.categoriesWithModifiedUsedTimes.size > 0) {
await database.category.update({
usedTimesVersion: generateVersionId()
}, {
where: {
familyId,
categoryId: {
[Sequelize.Op.in]: uniq(this.categoriesWithModifiedUsedTimes)
[Sequelize.Op.in]: setToList(this.categoriesWithModifiedUsedTimes)
}
},
transaction
})
this.categoriesWithModifiedUsedTimes = []
this.categoriesWithModifiedUsedTimes.clear()
}
if (this.devicesWithModifiedInstalledApps.length > 0) {
if (this.devicesWithModifiedInstalledApps.size > 0) {
await database.device.update({
installedAppsVersion: generateVersionId()
}, {
where: {
familyId,
deviceId: {
[Sequelize.Op.in]: uniq(this.devicesWithModifiedInstalledApps)
[Sequelize.Op.in]: setToList(this.devicesWithModifiedInstalledApps)
}
},
transaction
})
this.devicesWithModifiedInstalledApps = []
this.devicesWithModifiedInstalledApps.clear()
}
if (this.invalidiateUserList) {

View file

@ -48,5 +48,5 @@ export async function dispatchAddInstalledApps ({ deviceId, action, cache }: {
{ transaction: cache.transaction }
)
cache.devicesWithModifiedInstalledApps.push(deviceId)
cache.devicesWithModifiedInstalledApps.add(deviceId)
}

View file

@ -110,10 +110,10 @@ export async function dispatchAddUsedTime ({ action, cache }: {
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(categoryId)
cache.categoriesWithModifiedBaseData.add(categoryId)
}
cache.categoriesWithModifiedUsedTimes.push(categoryId)
cache.categoriesWithModifiedUsedTimes.add(categoryId)
}
await handleAddUsedTime({

View file

@ -202,7 +202,7 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
}
}
cache.categoriesWithModifiedUsedTimes.push(item.categoryId)
cache.categoriesWithModifiedUsedTimes.add(item.categoryId)
if (item.extraTimeToSubtract !== 0) {
await cache.database.category.update({
@ -215,7 +215,7 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(item.categoryId)
cache.categoriesWithModifiedBaseData.add(item.categoryId)
}
if (addUsedTimeForADifferentUserThanTheCurrentUserOfTheDevice) {

View file

@ -35,5 +35,5 @@ export async function dispatchRemoveInstalledApps ({ deviceId, action, cache }:
transaction: cache.transaction
})
cache.devicesWithModifiedInstalledApps.push(deviceId)
cache.devicesWithModifiedInstalledApps.add(deviceId)
}

View file

@ -77,5 +77,5 @@ export async function dispatchUpdateAppActivities ({ deviceId, action, cache }:
}
}
cache.devicesWithModifiedInstalledApps.push(deviceId)
cache.devicesWithModifiedInstalledApps.add(deviceId)
}

View file

@ -177,7 +177,7 @@ export async function dispatchAddCategoryApps ({ action, cache, fromChildSelfLim
}
)
oldCategories.forEach((categoryId) => cache.categoriesWithModifiedApps.push(categoryId))
cache.categoriesWithModifiedApps.push(action.categoryId)
oldCategories.forEach((categoryId) => cache.categoriesWithModifiedApps.add(categoryId))
cache.categoriesWithModifiedApps.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -74,6 +74,6 @@ export async function dispatchAddCategoryNetworkId ({ action, cache }: {
hashedNetworkId: action.hashedNetworkId
}, { transaction: cache.transaction })
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -57,6 +57,6 @@ export async function dispatchCreateTimeLimitRule ({ action, cache, fromChildSel
sessionPauseMilliseconds: action.rule.sessionPauseMilliseconds
}, { transaction: cache.transaction })
cache.categoriesWithModifiedTimeLimitRules.push(action.rule.categoryId)
cache.categoriesWithModifiedTimeLimitRules.add(action.rule.categoryId)
cache.areChangesImportant = true
}

View file

@ -37,6 +37,6 @@ export async function dispatchDeleteTimeLimitRule ({ action, cache }: {
await ruleEntry.destroy({ transaction: cache.transaction })
cache.categoriesWithModifiedTimeLimitRules.push(ruleEntry.categoryId)
cache.categoriesWithModifiedTimeLimitRules.add(ruleEntry.categoryId)
cache.areChangesImportant = true
}

View file

@ -40,7 +40,7 @@ export async function dispatchIncrementCategoryExtraTime ({ action, cache }: {
await category.save({ transaction: cache.transaction })
cache.categoriesWithModifiedBaseData.push(category.categoryId)
cache.categoriesWithModifiedBaseData.add(category.categoryId)
cache.areChangesImportant = true
}

View file

@ -41,6 +41,6 @@ export async function dispatchRemoveCategoryApps ({ action, cache }: {
})
}
cache.categoriesWithModifiedApps.push(action.categoryId)
cache.categoriesWithModifiedApps.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -44,6 +44,6 @@ export async function dispatchResetCategoryNetworkIds ({ action, cache }: {
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -51,6 +51,6 @@ export async function dispatchSetCategoryExtraTime ({ action, cache }: {
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -117,6 +117,6 @@ export async function dispatchSetParentCategory ({ action, cache, fromChildSelfL
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -45,6 +45,6 @@ export async function dispatchUpdateCategoryBatteryLimit ({ action, cache }: {
await categoryEntry.save({ transaction: cache.transaction })
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -59,7 +59,7 @@ export async function dispatchUpdateCategoryBlockAllNotifications ({ action, cac
})
if (affectedRows !== 0) {
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}
}

View file

@ -69,6 +69,6 @@ export async function dispatchUpdateCategoryBlockedTimes ({ action, cache, fromC
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -40,6 +40,6 @@ export async function dispatchUpdateCategorySorting ({ action, cache }: {
}
})
cache.categoriesWithModifiedBaseData.push(categoryId)
cache.categoriesWithModifiedBaseData.add(categoryId)
}
}

View file

@ -79,7 +79,7 @@ export async function dispatchUpdateCategoryTemporarilyBlocked ({ action, cache,
})
if (affectedRows !== 0) {
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}
}

View file

@ -43,6 +43,6 @@ export async function dispatchUpdateCategoryTimeWarnings ({ action, cache }: {
await categoryEntry.save({ transaction: cache.transaction })
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}

View file

@ -46,7 +46,7 @@ export async function dispatchUpdateCategoryTitle ({ action, cache }: {
})
if (affectedRows !== 0) {
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}
}

View file

@ -45,6 +45,6 @@ export async function dispatchUpdateTimelimitRule ({ action, cache }: {
await ruleEntry.save({ transaction: cache.transaction })
cache.categoriesWithModifiedTimeLimitRules.push(ruleEntry.categoryId)
cache.categoriesWithModifiedTimeLimitRules.add(ruleEntry.categoryId)
cache.areChangesImportant = true
}

View file

@ -20,3 +20,11 @@ import { uniq } from 'lodash'
export function hasDuplicates (list: Array<string>): boolean {
return uniq(list).length !== list.length
}
export function setToList<T> (set: Set<T>): Array<T> {
const result: Array<T> = []
set.forEach((item) => result.push(item))
return result
}