Skip to content

Commit

Permalink
support MapStore, which any map can be used with (eg. MapDB)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Sep 26, 2020
1 parent f29bdd0 commit 7d9a69e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'kweb'
version '0.4.1'
version '0.4.2'

buildscript {
ext.kotlin_version = '1.4.0'
Expand Down Expand Up @@ -54,7 +54,6 @@ dependencies {
implementation 'org.lmdbjava:lmdbjava:0.7.0'
compile "org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0-RC"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.0.0-RC"
implementation "org.mapdb:mapdb:3.0.8"

testImplementation "io.kotest:kotest-runner-junit5-jvm:4.2.3"
testImplementation "io.kotest:kotest-assertions-core-jvm:4.2.3"
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/kweb/shoebox/stores/LmdbStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ val defaultGson: Gson = Converters.registerAll(GsonBuilder()).let {
it.registerTypeAdapter(object : TypeToken<Duration>() {}.type, DurationConverter())
}.create()
*/
@Deprecated("Should be used via MapStore")
class LmdbStore<T : Any>(val name: String, private val kSerializer: KSerializer<T>) : Store<T> {

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.protobuf.ProtoBuf
import kweb.shoebox.KeyValue
import kweb.shoebox.Store
import org.mapdb.DB
import org.mapdb.HTreeMap
import org.mapdb.Serializer

/**
* Created by ian on 3/22/17.
*/
@ExperimentalSerializationApi
class MapDBStore<T : Any>(private val db: DB, private val name: String, private val serializer: KSerializer<T>) : Store<T> {
private val map: HTreeMap<String, ByteArray> = db
.hashMap(name, Serializer.STRING, Serializer.BYTE_ARRAY)
.createOrOpen()
class MapStore<T : Any>(private val map : MutableMap<String, ByteArray>, private val name: String, private val serializer: KSerializer<T>) : Store<T> {

override val entries: Iterable<KeyValue<T>>
get() = map.map { (k, v) ->
Expand All @@ -33,7 +27,7 @@ class MapDBStore<T : Any>(private val db: DB, private val name: String, private
}

override fun get(key: String): T? {
val v = map.get(key)
val v = map[key]
return if (v == null) {
null
} else {
Expand All @@ -42,9 +36,8 @@ class MapDBStore<T : Any>(private val db: DB, private val name: String, private
}

override fun set(key: String, value: T): T? {
val v = map.get(key)
val v = map[key]
map.set(key, ProtoBuf.encodeToByteArray(serializer, value))
db.commit()
return if (v == null) {
null
} else {
Expand Down

0 comments on commit 7d9a69e

Please sign in to comment.