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 * Different from doForEach, provide an ordered index to each binary
*/ */
fun doForEachOrderedBinary(action: (index: Int, binary: BinaryFile) -> Unit, fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary<T>) -> Unit,
conditionToAdd: (binary: BinaryFile) -> Boolean) { 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 -> orderedBinariesWithoutDuplication(conditionToAdd).forEachIndexed { index, keyBinary ->
action.invoke(index, keyBinary.binary) action.invoke(index, keyBinary.binary)
} }
} }
fun doForEachOrderedBinary(action: (index: Int, binary: BinaryFile) -> Unit) { fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit) {
doForEachOrderedBinary(action, { true }) doForEachOrderedBinaryWithoutDuplication(action, { true })
} }
fun isEmpty(): Boolean { fun isEmpty(): Boolean {

View File

@@ -91,6 +91,14 @@ class IconsManager {
return list 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 * Clear the cache of icons
*/ */

View File

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