ImageRepository.page(id): constant time complexity
This commit is contained in:
@@ -51,7 +51,10 @@ class ImageRepository(
|
|||||||
|
|
||||||
private val metadataFile = File(scanDir, "document.json")
|
private val metadataFile = File(scanDir, "document.json")
|
||||||
|
|
||||||
private var pages: MutableList<PageV2> = loadPages()
|
private val pagesById = mutableMapOf<String, PageV2>()
|
||||||
|
private var pages: MutableList<PageV2> = loadPages().also {
|
||||||
|
pagesById.putAll(it.associateBy { p -> p.id })
|
||||||
|
}
|
||||||
|
|
||||||
private val json = Json {
|
private val json = Json {
|
||||||
prettyPrint = false
|
prettyPrint = false
|
||||||
@@ -125,8 +128,7 @@ class ImageRepository(
|
|||||||
ScanPage(it.id, it.toMetadata())
|
ScanPage(it.id, it.toMetadata())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO time complexity should be constant
|
private fun page(id: String): PageV2? = pagesById[id]
|
||||||
private fun page(id: String): PageV2? = pages.find { p -> p.id == id }
|
|
||||||
|
|
||||||
fun add(pageBytes: ByteArray, sourceBytes: ByteArray, metadata: PageMetadata) {
|
fun add(pageBytes: ByteArray, sourceBytes: ByteArray, metadata: PageMetadata) {
|
||||||
val id = "${System.currentTimeMillis()}"
|
val id = "${System.currentTimeMillis()}"
|
||||||
@@ -135,15 +137,15 @@ class ImageRepository(
|
|||||||
file.writeBytes(pageBytes)
|
file.writeBytes(pageBytes)
|
||||||
writeThumbnail(file)
|
writeThumbnail(file)
|
||||||
File(sourceDir, fileName).writeBytes(sourceBytes)
|
File(sourceDir, fileName).writeBytes(sourceBytes)
|
||||||
pages.add(
|
val page = PageV2(
|
||||||
PageV2(
|
|
||||||
id = id,
|
id = id,
|
||||||
quad = metadata.normalizedQuad.toSerializable(),
|
quad = metadata.normalizedQuad.toSerializable(),
|
||||||
baseRotationDegrees = metadata.baseRotation.degrees,
|
baseRotationDegrees = metadata.baseRotation.degrees,
|
||||||
manualRotationDegrees = metadata.manualRotation.degrees,
|
manualRotationDegrees = metadata.manualRotation.degrees,
|
||||||
isColored = metadata.isColored
|
isColored = metadata.isColored
|
||||||
)
|
)
|
||||||
)
|
pagesById[page.id] = page
|
||||||
|
pages.add(page)
|
||||||
saveMetadata()
|
saveMetadata()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,9 +185,9 @@ class ImageRepository(
|
|||||||
transformations.rotate(inputFile, outputFile, clockwise)
|
transformations.rotate(inputFile, outputFile, clockwise)
|
||||||
}
|
}
|
||||||
|
|
||||||
pages[index] = page.copy(
|
val updated = page.copy(manualRotationDegrees = newManualRotation.degrees)
|
||||||
manualRotationDegrees = newManualRotation.degrees,
|
pagesById[id] = updated
|
||||||
)
|
pages[index] = updated
|
||||||
saveMetadata()
|
saveMetadata()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,6 +250,7 @@ class ImageRepository(
|
|||||||
return
|
return
|
||||||
pages.removeAt(index)
|
pages.removeAt(index)
|
||||||
saveMetadata()
|
saveMetadata()
|
||||||
|
pagesById.remove(id)
|
||||||
|
|
||||||
getSourceFile(id).delete()
|
getSourceFile(id).delete()
|
||||||
scanDir.listFiles()
|
scanDir.listFiles()
|
||||||
@@ -261,6 +264,8 @@ class ImageRepository(
|
|||||||
fun clear() {
|
fun clear() {
|
||||||
pages.clear()
|
pages.clear()
|
||||||
saveMetadata() // "empty" json file
|
saveMetadata() // "empty" json file
|
||||||
|
pagesById.clear()
|
||||||
|
|
||||||
thumbnailDir.listFiles()?.forEach {
|
thumbnailDir.listFiles()?.forEach {
|
||||||
file -> file.delete()
|
file -> file.delete()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user