1
0
Fork 1
mirror of https://gitlab.com/mangadex-pub/mangadex_at_home.git synced 2024-01-19 02:48:37 +00:00

Add lockes to database

This commit is contained in:
carbotaniuman 2021-02-21 11:47:47 -06:00
parent 964b97b1d5
commit 0a05cf05c1
2 changed files with 28 additions and 25 deletions

View file

@ -70,7 +70,6 @@ class MangaDexClient(private val settingsFile: File, databaseFolder: Path, cache
val config = HikariConfig()
val db = databaseFolder.resolve("metadata.db")
config.jdbcUrl = "jdbc:sqlite:$db"
config.maximumPoolSize = 1
config.addDataSourceProperty("cachePrepStmts", "true")
config.addDataSourceProperty("prepStmtCacheSize", "100")
config.addDataSourceProperty("prepStmtCacheSqlLimit", "1000")

View file

@ -31,7 +31,7 @@ import org.ktorm.dsl.*
import org.slf4j.LoggerFactory
import java.io.*
import java.nio.file.*
import java.sql.SQLIntegrityConstraintViolationException
import java.sql.SQLException
import java.time.Instant
import java.util.UUID
import java.util.concurrent.*
@ -94,6 +94,7 @@ class ImageStorage(
val now = Instant.now()
LOGGER.info { "Updating LRU times for ${toUpdate.size} entries" }
synchronized(database) {
database.batchUpdate(DbImage) {
for (id in toUpdate) {
item {
@ -104,6 +105,7 @@ class ImageStorage(
}
}
}
}
calculateSize()
},
1, 1, TimeUnit.MINUTES
@ -218,7 +220,6 @@ class ImageStorage(
private fun deleteImage(id: String) {
LOGGER.trace { "Deleting image $id from cache" }
database.useTransaction {
val path = getTempPath()
try {
@ -232,6 +233,7 @@ class ImageStorage(
} catch (e: IOException) {
// a failure means the image did not exist
} finally {
synchronized(database) {
database.delete(DbImage) {
DbImage.id eq id
}
@ -312,12 +314,14 @@ class ImageStorage(
Files.createDirectories(getPath(id).parent)
try {
synchronized(database) {
database.insert(DbImage) {
set(DbImage.id, id)
set(DbImage.accessed, Instant.now())
set(DbImage.size, metadataSize + bytes)
}
} catch (e: SQLIntegrityConstraintViolationException) {
}
} catch (e: SQLException) {
// someone got to us before this (TOCTOU)
// there are 2 situations here
// one is that the