blob: 2ca24e04cb40a99feb53f15cc7ed234fe15fb8f1 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tuweni.kv
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import org.apache.tuweni.bytes.Bytes
import org.apache.tuweni.concurrent.AsyncCompletion
import org.apache.tuweni.concurrent.AsyncResult
import org.apache.tuweni.concurrent.coroutines.asyncCompletion
import org.apache.tuweni.concurrent.coroutines.asyncResult
import java.io.Closeable
/**
* A key-value store.
*/
interface KeyValueStore : Closeable {
/**
* Retrieves data from the store.
*
* @param key The key for the content.
* @return The stored data, or null if no data was stored under the specified key.
*/
suspend fun get(key: Bytes): Bytes?
/**
* Retrieves data from the store.
*
* @param key The key for the content.
* @return An [AsyncResult] that will complete with the stored content,
* or an empty optional if no content was available.
*/
fun getAsync(key: Bytes): AsyncResult<Bytes?> = getAsync(Dispatchers.Default, key)
/**
* Retrieves data from the store.
*
* @param key The key for the content.
* @param dispatcher The co-routine dispatcher for asynchronous tasks.
* @return An [AsyncResult] that will complete with the stored content,
* or an empty optional if no content was available.
*/
fun getAsync(dispatcher: CoroutineDispatcher, key: Bytes): AsyncResult<Bytes?> =
GlobalScope.asyncResult(dispatcher) { get(key) }
/**
* Puts data into the store.
*
* @param key The key to associate with the data, for use when retrieving.
* @param value The data to store.
*/
suspend fun put(key: Bytes, value: Bytes)
/**
* Puts data into the store.
*
* Note: if the storage implementation already contains content for the given key, it does not need to replace the
* existing content.
*
* @param key The key to associate with the data, for use when retrieving.
* @param value The data to store.
* @return An [AsyncCompletion] that will complete when the content is stored.
*/
fun putAsync(key: Bytes, value: Bytes): AsyncCompletion = putAsync(Dispatchers.Default, key, value)
/**
* Puts data into the store.
*
* Note: if the storage implementation already contains content for the given key, it does not need to replace the
* existing content.
*
* @param key The key to associate with the data, for use when retrieving.
* @param value The data to store.
* @param dispatcher The co-routine dispatcher for asynchronous tasks.
* @return An [AsyncCompletion] that will complete when the content is stored.
*/
fun putAsync(dispatcher: CoroutineDispatcher, key: Bytes, value: Bytes): AsyncCompletion =
GlobalScope.asyncCompletion(dispatcher) { put(key, value) }
}