135 lines
4.0 KiB
Kotlin
135 lines
4.0 KiB
Kotlin
package net.sergeych.bintools
|
|
|
|
import net.sergeych.bipack.BipackDecoder
|
|
import net.sergeych.bipack.BipackEncoder
|
|
import net.sergeych.mp_logger.LogTag
|
|
import net.sergeych.mp_logger.Loggable
|
|
import net.sergeych.mp_logger.debug
|
|
import net.sergeych.synctools.AtomicCounter
|
|
import net.sergeych.synctools.ProtectedOp
|
|
import net.sergeych.synctools.WaitHandle
|
|
import net.sergeych.synctools.withLock
|
|
|
|
private val ac = AtomicCounter(0)
|
|
|
|
class DataKVStorage(private val provider: DataProvider) : KVStorage,
|
|
Loggable by LogTag("DKVS${ac.incrementAndGet()}") {
|
|
|
|
data class Lock(val name: String) {
|
|
private val exclusive = ProtectedOp()
|
|
|
|
private var readerCount = 0
|
|
|
|
private var pulses = WaitHandle()
|
|
|
|
fun <T> lockExclusive(f: () -> T): T {
|
|
while (true) {
|
|
exclusive.withLock {
|
|
if (readerCount == 0) {
|
|
return f()
|
|
}
|
|
}
|
|
pulses.await()
|
|
}
|
|
}
|
|
|
|
fun <T> lockRead(f: () -> T): T {
|
|
try {
|
|
exclusive.withLock { readerCount++ }
|
|
return f()
|
|
} finally {
|
|
exclusive.withLock { readerCount-- }
|
|
if (readerCount == 0) pulses.wakeUp()
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private val locks = mutableMapOf<String, Lock>()
|
|
private val access = ProtectedOp()
|
|
private val keyIds = mutableMapOf<String, Int>()
|
|
private var lastId: Int = 0
|
|
|
|
init {
|
|
access.withLock {
|
|
// TODO: read keys
|
|
for (fn in provider.list()) {
|
|
debug { "Scanning: $fn" }
|
|
if (fn.endsWith(".d")) {
|
|
val id = fn.dropLast(2).toInt(16)
|
|
debug { "found data record: $fn -> $id" }
|
|
val name = provider.read(fn) { BipackDecoder.decode<String>(it) }
|
|
keyIds[name] = id
|
|
if (id > lastId) lastId = id
|
|
} else debug { "ignoring record $fn" }
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Important: __it must be called with locked access op!__
|
|
*/
|
|
private fun lockFor(name: String): Lock = locks.getOrPut(name) { Lock(name) }
|
|
|
|
private fun recordName(id: Int) = "${id.toString(16)}.d"
|
|
private fun <T> read(name: String, f: (DataSource) -> T): T {
|
|
val lock: Lock
|
|
val id: Int
|
|
// global lock: fast
|
|
access.withLock {
|
|
id = keyIds[name] ?: throw DataProvider.NotFoundException()
|
|
lock = lockFor(name)
|
|
}
|
|
// per-name lock: slow
|
|
return lock.lockRead {
|
|
provider.read(recordName(id), f)
|
|
}
|
|
}
|
|
|
|
private fun write(name: String, f: (DataSink) -> Unit) {
|
|
val lock: Lock
|
|
val id: Int
|
|
// global lock: fast
|
|
access.withLock {
|
|
id = keyIds[name] ?: (++lastId).also { keyIds[name] = it }
|
|
lock = lockFor(name)
|
|
}
|
|
// per-name lock: slow
|
|
lock.lockExclusive { provider.write(recordName(id), f) }
|
|
}
|
|
|
|
private fun deleteEntry(name: String) {
|
|
// fast pre-check:
|
|
if (name !in keyIds) return
|
|
// global lock: we can't now detect concurrent delete + write ops, so exclusive:
|
|
access.withLock {
|
|
val id = keyIds[name] ?: return
|
|
provider.delete(recordName(id))
|
|
locks.remove(name)
|
|
keyIds.remove(name)
|
|
}
|
|
}
|
|
|
|
override fun get(key: String): ByteArray? = try {
|
|
read(key) {
|
|
BipackDecoder.decode<String>(it)
|
|
// notice: not nullable byte array here!
|
|
BipackDecoder.decode<ByteArray>(it)
|
|
}
|
|
} catch (_: DataProvider.NotFoundException) {
|
|
null
|
|
}
|
|
|
|
override fun set(key: String, value: ByteArray?) {
|
|
if (value == null) {
|
|
deleteEntry(key)
|
|
} else write(key) {
|
|
BipackEncoder.encode(key, it)
|
|
BipackEncoder.encode(value, it)
|
|
}
|
|
}
|
|
|
|
override val keys: Set<String>
|
|
get() = access.withLock { keyIds.keys }
|
|
} |