Better search backup group implementation

This commit is contained in:
J-Jamet
2021-04-19 11:11:07 +02:00
parent e73b9b7f1c
commit 6397feffff
2 changed files with 22 additions and 13 deletions

View File

@@ -178,20 +178,10 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
* Retrieve backup group with his name
*/
private fun retrieveBackup(): GroupKDB? {
var backupGroup: GroupKDB? = null
val groupHandler = object: NodeHandler<GroupKDB>() {
override fun operate(node: GroupKDB): Boolean {
return if (node.title.equals(BACKUP_FOLDER_TITLE, ignoreCase = true)) {
backupGroup = node
false
} else {
true
return rootGroup?.searchChildGroup {
it.title.equals(BACKUP_FOLDER_TITLE, ignoreCase = true)
}
}
}
rootGroup?.doForEachChild(null, groupHandler)
return backupGroup
}
/**
* Ensure that the backup tree exists if enabled, and create it

View File

@@ -62,8 +62,27 @@ interface GroupVersionedInterface<Group: GroupVersionedInterface<Group, Entry>,
return false
}
if (doActionForChild)
group.doForEachChild(entryHandler, groupHandler)
group.doForEachChild(entryHandler, groupHandler, stopIterationWhenGroupHandlerOperateFalse)
}
return true
}
fun searchChildGroup(criteria: (group: Group) -> Boolean): Group? {
return searchChildGroup(this, criteria)
}
private fun searchChildGroup(rootGroup: GroupVersionedInterface<Group, Entry>,
criteria: (group: Group) -> Boolean): Group? {
for (childGroup in rootGroup.getChildGroups()) {
if (criteria.invoke(childGroup)) {
return childGroup
} else {
val subGroup = searchChildGroup(childGroup, criteria)
if (subGroup != null) {
return subGroup
}
}
}
return null
}
}