v0.1.7 release for all platforms on kotlin 2.0.20 - important fix in wasmJS
This commit is contained in:
1 parent
e07ebf564a
commit
fc9d4c5070
9 files changed
+260
-456
No files matched your search
@@ -2,11 +2,18 @@ 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
|
||||
|
||||
class DataKVStorage(private val provider: DataProvider) : KVStorage {
|
||||
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()
|
||||
@@ -20,8 +27,6 @@ class DataKVStorage(private val provider: DataProvider) : KVStorage {
|
||||
exclusive.withLock {
|
||||
if (readerCount == 0) {
|
||||
return f()
|
||||
} else {
|
||||
println("can't lock $this: count is $readerCount")
|
||||
}
|
||||
}
|
||||
pulses.await()
|
||||
@@ -49,19 +54,16 @@ class DataKVStorage(private val provider: DataProvider) : KVStorage {
|
||||
access.withLock {
|
||||
// TODO: read keys
|
||||
for (fn in provider.list()) {
|
||||
println("Scanning: $fn")
|
||||
debug { "Scanning: $fn" }
|
||||
if (fn.endsWith(".d")) {
|
||||
val id = fn.dropLast(2).toInt(16)
|
||||
println("found data record: $fn -> $id")
|
||||
|
||||
debug { "found data record: $fn -> $id" }
|
||||
val name = provider.read(fn) { BipackDecoder.decode<String>(it) }
|
||||
println("Key=$name")
|
||||
keyIds[name] = id
|
||||
if (id > lastId) lastId = id
|
||||
} else println("ignoring record $fn")
|
||||
} else debug { "ignoring record $fn" }
|
||||
}
|
||||
}
|
||||
println("initialized, ${keyIds.size} records found, lastId=$lastId")
|
||||
}
|
||||
|
||||
|
||||
@@ -97,7 +99,7 @@ class DataKVStorage(private val provider: DataProvider) : KVStorage {
|
||||
lock.lockExclusive { provider.write(recordName(id), f) }
|
||||
}
|
||||
|
||||
fun deleteEntry(name: String) {
|
||||
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:
|
||||
|
||||
@@ -119,7 +119,6 @@ class KVStorageDelegate<T>(
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
|
||||
if (cacheReady) return cachedValue
|
||||
val data = storage.get(name(property))
|
||||
println("Got data: ${data?.toDump()}")
|
||||
if (data == null)
|
||||
cachedValue = defaultValue
|
||||
else
|
||||
@@ -132,7 +131,6 @@ class KVStorageDelegate<T>(
|
||||
// if (!cacheReady || value != cachedValue) {
|
||||
cachedValue = value
|
||||
cacheReady = true
|
||||
println("set ${name(property)} to ${BipackEncoder.encode(serializer, value).toDump()}")
|
||||
storage[name(property)] = BipackEncoder.encode(serializer, value)
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
@file:Suppress("unused")
|
||||
|
||||
package net.sergeych.synctools
|
||||
|
||||
import java.nio.channels.CompletionHandler
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
|
||||
/**
|
||||
* Helper class to handle Java continuation with Kotlin coroutines.
|
||||
* Usage sample:
|
||||
* ```kotlin
|
||||
* val socket = withContext(Dispatchers.IO) {
|
||||
* AsynchronousSocketChannel.open()
|
||||
* }
|
||||
* suspendCoroutine { cont ->
|
||||
* socket.connect(address.socketAddress, cont, VoidCompletionHandler)
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
open class ContinuationHandler<T> : CompletionHandler<T, Continuation<T>> {
|
||||
override fun completed(result: T, attachment: Continuation<T>) {
|
||||
attachment.resume(result)
|
||||
}
|
||||
|
||||
override fun failed(exc: Throwable, attachment: Continuation<T>) {
|
||||
attachment.resumeWithException(exc)
|
||||
}
|
||||
}
|
||||
|
||||
object VoidCompletionHandler : ContinuationHandler<Void>()
|
||||
|
||||
object IntCompletionHandler : ContinuationHandler<Int>()
|
||||
@@ -1,9 +0,0 @@
|
||||
import kotlin.test.Test
|
||||
|
||||
class EmptyTest {
|
||||
@Test
|
||||
fun emptyTest() {
|
||||
println("hello!")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,10 +5,6 @@ import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class StorageTest {
|
||||
@Test
|
||||
fun emptyTest() {
|
||||
println("hello")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun storageTest() {
|
||||
@@ -27,7 +23,6 @@ class StorageTest {
|
||||
assertEquals(answer, 42)
|
||||
answer = 43
|
||||
|
||||
println("----------------------------------------------------------------")
|
||||
val s2 = defaultNamedStorage("test_mp_bintools")
|
||||
val foo1 by s2.stored("?", "foo")
|
||||
val answer1: Int? by s2.optStored("answer")
|
||||
|
||||
Reference in new issue
Block a user