All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lightningkite.ktordb.InMemoryUnsafePersistentFieldCollection.kt Maven / Gradle / Ivy

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))
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy