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 config = HikariConfig()
val db = databaseFolder.resolve("metadata.db") val db = databaseFolder.resolve("metadata.db")
config.jdbcUrl = "jdbc:sqlite:$db" config.jdbcUrl = "jdbc:sqlite:$db"
config.maximumPoolSize = 1
config.addDataSourceProperty("cachePrepStmts", "true") config.addDataSourceProperty("cachePrepStmts", "true")
config.addDataSourceProperty("prepStmtCacheSize", "100") config.addDataSourceProperty("prepStmtCacheSize", "100")
config.addDataSourceProperty("prepStmtCacheSqlLimit", "1000") config.addDataSourceProperty("prepStmtCacheSqlLimit", "1000")

View file

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