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

.port_store.1.2.22.source-code.Store.kt Maven / Gradle / Ivy

There is a newer version: 1.4.4
Show newest version
package com.hexagonkt.store

import com.hexagonkt.helpers.Resource
import com.hexagonkt.helpers.ensureSize
import com.hexagonkt.serialization.parseObjects
import com.hexagonkt.store.IndexOrder.ASCENDING
import java.io.File
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1

/**
 * TODO All methods accepting maps rely on `mapOf` returning an insertion ordered map. Take care of
 *   this in the future to avoid possible bugs
 */
interface Store {
    val type: KClass
    val key: KProperty1
    val name: String
    val mapper: Mapper

    fun createIndex(unique: Boolean, fields: Map): String

    fun createIndex(unique: Boolean, vararg fields: Pair, IndexOrder>): String =
        createIndex(unique, fields.map { it.first.name to it.second }.toMap())

    fun createIndex(unique: Boolean, vararg fields: KProperty1): String =
        createIndex(unique, *fields.map { it to ASCENDING }.toTypedArray())

    fun insertOne(instance: T): K

    fun insertMany(instances: List): List

    fun insertMany(vararg instances: T): List =
        insertMany(instances.toList())

    fun saveOne(instance: T): K? // returns key if created, null if updated

    fun saveMany(instances: List): List

    fun replaceOne(instance: T): Boolean

    fun replaceMany(instances: List): List

    fun replaceMany(vararg instances: T): List =
        replaceMany(instances.toList())

    fun updateOne(key: K, updates: Map): Boolean

    fun updateOne(key: K, vararg updates: Pair, *>): Boolean =
        updateOne(key, fields(*updates))

    fun updateMany(filter: Map, updates: Map): Long

    fun deleteOne(id: K): Boolean

    fun deleteMany(filter: Map): Long

    fun findOne(key: K): T?

    fun findOne(key: K, fields: List): Map?

    fun findOne(filter: Map): T? =
        findMany(filter).ensureSize(0..1).firstOrNull()

    fun findOne(filter: Map, fields: List): Map? =
        findMany(filter, fields).ensureSize(0..1).firstOrNull()

    fun findMany(
        filter: Map,
        limit: Int? = null,
        skip: Int? = null,
        sort: Map = emptyMap()): List

    fun findMany(
        filter: Map,
        fields: List,
        limit: Int? = null,
        skip: Int? = null,
        sort: Map = emptyMap()): List>

    fun findAll(
        limit: Int? = null,
        skip: Int? = null,
        sort: Map = emptyMap()): List =
            findMany(emptyMap(), limit, skip, sort)

    fun findAll(
        fields: List,
        limit: Int? = null,
        skip: Int? = null,
        sort: Map = emptyMap()): List> =
            findMany(emptyMap(), fields, limit, skip, sort)

    fun count(filter: Map = emptyMap()): Long

    fun drop()

    fun fields(updates: Map, *>): Map =
        updates.mapKeys { it.key.name }

    fun fields(vararg updates: Pair, *>): Map =
        fields(updates.toMap())

    fun import(input: File) {
        insertMany(input.parseObjects(type))
    }

    fun import(input: Resource) {
        insertMany(input.parseObjects(type))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy