Prevent duplication during database saving

This commit is contained in:
J-Jamet
2021-03-08 13:29:02 +01:00
parent e6be8c23fb
commit 81ba7f0721
3 changed files with 29 additions and 7 deletions

View File

@@ -162,15 +162,29 @@ abstract class BinaryPool<T> {
/**
* Different from doForEach, provide an ordered index to each binary
*/
fun doForEachOrderedBinary(action: (index: Int, binary: BinaryFile) -> Unit,
conditionToAdd: (binary: BinaryFile) -> Boolean) {
fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary<T>) -> Unit,
conditionToAdd: (binary: BinaryFile) -> Boolean) {
orderedBinariesWithoutDuplication(conditionToAdd).forEach { keyBinary ->
action.invoke(keyBinary)
}
}
fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary<T>) -> Unit) {
doForEachBinaryWithoutDuplication(action, { true })
}
/**
* Different from doForEach, provide an ordered index to each binary
*/
fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit,
conditionToAdd: (binary: BinaryFile) -> Boolean) {
orderedBinariesWithoutDuplication(conditionToAdd).forEachIndexed { index, keyBinary ->
action.invoke(index, keyBinary.binary)
}
}
fun doForEachOrderedBinary(action: (index: Int, binary: BinaryFile) -> Unit) {
doForEachOrderedBinary(action, { true })
fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit) {
doForEachOrderedBinaryWithoutDuplication(action, { true })
}
fun isEmpty(): Boolean {

View File

@@ -91,6 +91,14 @@ class IconsManager {
return list
}
fun doForEachCustomIconWithoutBinaryDuplication(action: (IconImageCustom) -> Unit) {
customCache.doForEachBinaryWithoutDuplication { keyBinary ->
keyBinary.keys.forEach { key ->
action.invoke(IconImageCustom(key, keyBinary.binary))
}
}
}
/**
* Clear the cache of icons
*/

View File

@@ -138,7 +138,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX,
val binaryCipherKey = database.loadedCipherKey
?: throw IOException("Unable to retrieve cipher key to write binaries")
database.binaryPool.doForEachOrderedBinary { _, binary ->
database.binaryPool.doForEachOrderedBinaryWithoutDuplication { _, binary ->
// Force decompression to add binary in header
binary.decompress(binaryCipherKey)
// Write type binary
@@ -497,7 +497,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX,
xml.startTag(null, DatabaseKDBXXML.ElemBinaries)
// Use indexes because necessarily (binary header ref is the order)
mDatabaseKDBX.binaryPool.doForEachOrderedBinary { index, binary ->
mDatabaseKDBX.binaryPool.doForEachOrderedBinaryWithoutDuplication { index, binary ->
xml.startTag(null, DatabaseKDBXXML.ElemBinary)
xml.attribute(null, DatabaseKDBXXML.AttrId, index.toString())
if (binary.length > 0) {
@@ -701,7 +701,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX,
xml.startTag(null, DatabaseKDBXXML.ElemCustomIcons)
mDatabaseKDBX.iconsManager.getCustomIconList().forEach { iconCustom ->
mDatabaseKDBX.iconsManager.doForEachCustomIconWithoutBinaryDuplication { iconCustom ->
xml.startTag(null, DatabaseKDBXXML.ElemCustomIconItem)
writeUuid(DatabaseKDBXXML.ElemCustomIconItemID, iconCustom.uuid)