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,12 +94,14 @@ class ImageStorage(
val now = Instant.now()
LOGGER.info { "Updating LRU times for ${toUpdate.size} entries" }
database.batchUpdate(DbImage) {
for (id in toUpdate) {
item {
set(DbImage.accessed, now)
where {
DbImage.id eq id
synchronized(database) {
database.batchUpdate(DbImage) {
for (id in toUpdate) {
item {
set(DbImage.accessed, now)
where {
DbImage.id eq id
}
}
}
}
@ -218,20 +220,20 @@ class ImageStorage(
private fun deleteImage(id: String) {
LOGGER.trace { "Deleting image $id from cache" }
database.useTransaction {
val path = getTempPath()
val path = getTempPath()
try {
Files.move(
getPath(id),
path,
StandardCopyOption.ATOMIC_MOVE
)
try {
Files.move(
getPath(id),
path,
StandardCopyOption.ATOMIC_MOVE
)
Files.deleteIfExists(path)
} catch (e: IOException) {
// a failure means the image did not exist
} finally {
Files.deleteIfExists(path)
} 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 {
database.insert(DbImage) {
set(DbImage.id, id)
set(DbImage.accessed, Instant.now())
set(DbImage.size, metadataSize + bytes)
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