hashmap.HashMapStore.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of port_store Show documentation
Show all versions of port_store Show documentation
The atoms of your platform
package com.hexagonkt.store.hashmap
import com.hexagonkt.helpers.filterEmpty
import com.hexagonkt.store.IndexOrder
import com.hexagonkt.store.Mapper
import com.hexagonkt.store.Store
import kotlin.UnsupportedOperationException
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
class HashMapStore(
override val type: KClass,
override val key: KProperty1,
override val name: String = type.java.simpleName,
private val store: HashMap> = hashMapOf(),
override val mapper: Mapper = HashMapMapper(type)) : Store {
override fun createIndex(unique: Boolean, fields: Map): String {
throw UnsupportedOperationException("Cannot create index on HashMap")
}
override fun insertOne(instance: T): K {
store[key.get(instance)] = map(instance)
return key.get(instance)
}
override fun insertMany(instances: List): List {
instances.forEach {
store[key.get(it)] = map(it)
}
return instances.map { key.get(it) }
}
override fun saveOne(instance: T): K? {
if (store.containsKey(key.get(instance))) {
store[key.get(instance)] = map(instance)
return null
}
store[key.get(instance)] = map(instance)
@Suppress("UNCHECKED_CAST")
return mapper.fromStore(key.name, key.get(instance)) as K
}
override fun saveMany(instances: List): List {
return instances.map(::saveOne)
}
override fun replaceOne(instance: T): Boolean =
store.replace(key.get(instance), map(instance)) != null
override fun replaceMany(instances: List): List =
instances.mapNotNull { if (replaceOne(it)) it else null }
override fun updateOne(key: K, updates: Map): Boolean {
if (!store.containsKey(key)) return false
val instance = store[key]!!.toMutableMap()
updates
.filterEmpty()
.forEach {
instance[it.key] = mapper.toStore(it.key, it.value)
}
return store.replace(key, instance) != null
}
override fun updateMany(filter: Map, updates: Map): Long {
val filteredInstances = store.filter(filter)
return filteredInstances.map { updateOne(it, updates) }.count { it }.toLong()
}
override fun deleteOne(id: K): Boolean =
store.remove(id) != null
override fun deleteMany(filter: Map): Long {
val filteredInstances = store.filter(filter)
return filteredInstances.map { deleteOne(it) }.count { it }.toLong()
}
override fun findOne(key: K): T? {
val result = store[key]
return result?.let { mapper.fromStore(result) }
}
override fun findOne(key: K, fields: List): Map? {
val instance = store[key]
return if (instance == null) null else fields.map { it to instance[it] }.toMap()
}
override fun findMany(
filter: Map,
limit: Int?,
skip: Int?,
sort: Map
): List {
val filteredInstances = store.filter(filter)
@Suppress("UNCHECKED_CAST")
return filteredInstances
.map { store[it]!! }
.sort(sort)
.paginate(skip ?: 0, limit ?: filteredInstances.size)
.map { mapper.fromStore(it as Map) }
}
override fun findMany(
filter: Map,
fields: List,
limit: Int?,
skip: Int?,
sort: Map
): List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy