Skip to content

Commit

Permalink
perf: don't recompute book hash during scan if filesize is different
Browse files Browse the repository at this point in the history
  • Loading branch information
gotson committed Feb 17, 2022
1 parent 1faaf12 commit 33cd19a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class LibraryContentLifecycle(
existingBooks.find { it.url == newBook.url && it.deletedDate == null }?.let { existingBook ->
logger.debug { "Matched existing book: $existingBook" }
if (newBook.fileLastModified.notEquals(existingBook.fileLastModified)) {
val hash = if (existingBook.fileHash.isNotBlank()) {
val hash = if (existingBook.fileSize == newBook.fileSize && existingBook.fileHash.isNotBlank()) {
hasher.computeHash(newBook.path)
} else null
if (hash == existingBook.fileHash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,44 @@ class LibraryContentLifecycleTest(
assertThat(media.status).isEqualTo(Media.Status.OUTDATED)
}

@Test
fun `given existing series when scanning and updated files have a different size then books are marked outdated`() {
// given
val library = makeLibrary()
libraryRepository.insert(library)

every { mockScanner.scanRootFolder(any()) }
.returnsMany(
mapOf(makeSeries(name = "series") to listOf(makeBook("book1").copy(fileSize = 1))).toScanResult(),
mapOf(makeSeries(name = "series") to listOf(makeBook("book1").copy(fileSize = 2))).toScanResult(),
)
libraryContentLifecycle.scanRootFolder(library)

bookRepository.findAll().first().let { book ->
bookRepository.update(book.copy(fileHash = "hashed"))
mediaRepository.update(mediaRepository.findById(book.id).copy(status = Media.Status.READY))
}

// when
libraryContentLifecycle.scanRootFolder(library)

// then
val allSeries = seriesRepository.findAll()
val allBooks = bookRepository.findAll()

verify(exactly = 2) { mockScanner.scanRootFolder(any()) }
verify(exactly = 0) { mockHasher.computeHash(any<Path>()) }

assertThat(allSeries).hasSize(1)
assertThat(allBooks).hasSize(1)
val book = allBooks.first()
assertThat(book.name).isEqualTo("book1")
assertThat(book.lastModifiedDate).isNotEqualTo(book.createdDate)
assertThat(book.fileHash).isBlank
val media = mediaRepository.findById(book.id)
assertThat(media.status).isEqualTo(Media.Status.OUTDATED)
}

@Test
fun `given existing series when scanning and updated files have the same hash then books are not marked outdated`() {
// given
Expand Down

0 comments on commit 33cd19a

Please sign in to comment.