Skip to content

Commit

Permalink
make file restore numbers add up by showing duplicates and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
grote committed Aug 16, 2024
1 parent 0251119 commit b141c1d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import de.grobox.storagebackuptester.backup.getSpeed
import org.calyxos.backup.storage.api.BackupFile
import org.calyxos.backup.storage.restore.NotificationRestoreObserver
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import kotlin.time.toDuration

data class RestoreProgress(
Expand Down Expand Up @@ -41,6 +40,10 @@ class RestoreStats(
liveData.postValue(RestoreProgress(filesProcessed, totalFiles, text))
}

override fun onFileDuplicatesRemoved(num: Int) {
// no-op
}

override fun onFileRestored(
file: BackupFile,
bytesWritten: Long,
Expand Down Expand Up @@ -68,7 +71,6 @@ class RestoreStats(
liveData.postValue(RestoreProgress(filesProcessed, totalFiles))
}

@OptIn(ExperimentalTime::class)
override fun onRestoreComplete(restoreDuration: Long) {
super.onRestoreComplete(restoreDuration)
val sb = StringBuilder("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.calyxos.backup.storage.api

public interface RestoreObserver {
public fun onRestoreStart(numFiles: Int, totalSize: Long)
public fun onFileDuplicatesRemoved(num: Int)
public fun onFileRestored(file: BackupFile, bytesWritten: Long, tag: String)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ internal data class RestorableChunk(

/**
* Call this after the RestorableChunk is complete and **before** using it for restore.
*
* @return the number of duplicate files removed
*/
fun finalize() {
fun finalize(): Int {
// entries in the zip chunk need to be sorted by their index in the zip
files.sortBy { it.zipIndex }
// There might be duplicates in case the *exact* same set of files exists more than once
// so they'll produce the same chunk ID.
// But since the content is there and this is an unlikely scenario, we drop the duplicates.
var lastIndex = 0
var numRemoved = 0
val iterator = files.iterator()
while (iterator.hasNext()) {
val file = iterator.next()
val i = file.zipIndex
when {
i < lastIndex -> error("unsorted list")
i == lastIndex -> iterator.remove() // remove duplicate
i == lastIndex -> { // remove duplicate
numRemoved++
iterator.remove()
}

i > lastIndex -> lastIndex = i // gaps are possible when we don't restore all files
}
}
return numRemoved
}
}

Expand Down Expand Up @@ -87,6 +95,14 @@ internal data class FileSplitterResult(
* Files referenced in [multiChunkMap] sorted for restoring.
*/
val multiChunkFiles: Collection<RestorableFile>,
/**
* The number of duplicate files that was removed from [zipChunks].
* Duplicate files in [zipChunks] with the same chunk ID will have the same index in the ZIP.
* So we remove them to make restore easier.
* With some extra work, we could restore those files,
* but by not doing so we are probably doing a favor to the user.
*/
val numRemovedDuplicates: Int,
)

/**
Expand Down Expand Up @@ -121,14 +137,15 @@ internal object FileSplitter {
}
}
// entries in the zip chunk need to be sorted by their index in the zip, duplicated removed
zipChunkMap.values.forEach { zipChunk -> zipChunk.finalize() }
val numRemovedDuplicates = zipChunkMap.values.sumOf { zipChunk -> zipChunk.finalize() }
val singleChunks = chunkMap.values.filter { it.isSingle }
val multiChunks = chunkMap.filterValues { !it.isSingle }
return FileSplitterResult(
zipChunks = zipChunkMap.values,
singleChunks = singleChunks,
multiChunkMap = multiChunks,
multiChunkFiles = getMultiFiles(multiChunks),
numRemovedDuplicates = numRemovedDuplicates,
)
}

Expand All @@ -145,6 +162,7 @@ internal object FileSplitter {
f1.chunkIdsCount == f2.chunkIdsCount -> {
f1.chunkIds.joinToString().compareTo(f2.chunkIds.joinToString())
}

else -> 1
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ public open class NotificationRestoreObserver internal constructor(private val n

private var totalFiles = 0
private var filesRestored = 0
private var filesRemovedAsDuplicates = 0
private var filesWithError = 0

override fun onRestoreStart(numFiles: Int, totalSize: Long) {
totalFiles = numFiles
n.updateRestoreNotification(filesRestored + filesWithError, totalFiles)
}

override fun onFileDuplicatesRemoved(num: Int) {
filesRemovedAsDuplicates = num
}

override fun onFileRestored(file: BackupFile, bytesWritten: Long, tag: String) {
filesRestored++
n.updateRestoreNotification(filesRestored + filesWithError, totalFiles)
Expand All @@ -36,7 +41,13 @@ public open class NotificationRestoreObserver internal constructor(private val n
}

override fun onRestoreComplete(restoreDuration: Long) {
n.showRestoreCompleteNotification(filesRestored, totalFiles, getRestoreCompleteIntent())
n.showRestoreCompleteNotification(
restored = filesRestored,
duplicates = filesRemovedAsDuplicates,
errors = filesWithError,
total = totalFiles,
intent = getRestoreCompleteIntent(),
)
}

protected open fun getRestoreCompleteIntent(): PendingIntent? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ internal class Restore(
observer?.onRestoreStart(filesTotal, totalSize)

val split = FileSplitter.splitSnapshot(snapshot)
observer?.onFileDuplicatesRemoved(split.numRemovedDuplicates)
var restoredFiles = split.numRemovedDuplicates // count removed dups, so numbers add up

val version = snapshot.version
var restoredFiles = 0
val smallFilesDuration = measure {
restoredFiles += zipChunkRestore.restore(
version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,25 @@ internal class Notifications(private val context: Context) {

internal fun showRestoreCompleteNotification(
restored: Int,
duplicates: Int,
errors: Int,
total: Int,
intent: PendingIntent?,
) {
val title = context.getString(R.string.notification_restore_complete_title, restored, total)
val msg = StringBuilder().apply {
if (duplicates > 0) {
append(context.getString(R.string.notification_restore_complete_dups, duplicates))
}
if (errors > 0) {
if (duplicates > 0) append("\n")
append(context.getString(R.string.notification_restore_complete_errors, errors))
}
}.toString().ifEmpty { null }
val notification = NotificationCompat.Builder(context, CHANNEL_ID_BACKUP).apply {
setSmallIcon(R.drawable.ic_cloud_done)
setContentTitle(title)
setContentText(msg)
setOngoing(false)
setShowWhen(true)
setAutoCancel(true)
Expand Down
2 changes: 2 additions & 0 deletions storage/lib/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<string name="notification_restore_title">Restoring files…</string>
<string name="notification_restore_info">%1$d/%2$d</string>
<string name="notification_restore_complete_title">%1$d of %2$d files restored</string>
<string name="notification_restore_complete_dups">%1$d files were duplicates.</string>
<string name="notification_restore_complete_errors">%1$d files had errors.</string>

<string name="snapshots_title">Available storage backups</string>
<string name="snapshots_empty">No storage backups found\n\nSorry, but there is nothing that can be restored.</string>
Expand Down

0 comments on commit b141c1d

Please sign in to comment.