com.lightningkite.ktordb.InMemoryUnsafePersistentFieldCollection.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of db Show documentation
Show all versions of db Show documentation
An abstract tool for communicating with different types of databases.
The newest version!
package com.lightningkite.ktordb
import kotlinx.serialization.KSerializer
import kotlinx.serialization.StringFormat
import kotlinx.serialization.builtins.ListSerializer
import java.io.Closeable
import java.io.File
class InMemoryUnsafePersistentFieldCollection(
val encoding: StringFormat,
val serializer: KSerializer,
val file: File
) : InMemoryFieldCollection(), Closeable {
init {
data.addAll(
encoding.decodeFromString(
ListSerializer(serializer),
file.takeIf { it.exists() }?.readText() ?: "[]"
)
)
val shutdownHook = Thread {
this.close()
}
Runtime.getRuntime().addShutdownHook(shutdownHook)
}
override fun close() {
file.writeText(encoding.encodeToString(ListSerializer(serializer), data))
}
}