add small kdoc for map repos

This commit is contained in:
InsanusMokrassar 2023-08-24 17:15:29 +06:00
parent 8a61193500
commit 5b070a8478
3 changed files with 86 additions and 0 deletions

View File

@ -6,6 +6,13 @@ import dev.inmo.micro_utils.coroutines.withWriteLock
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
/**
* [Map]-based [ReadMapCRUDRepo]. All internal operations will be locked with [locker] (mostly with
* [SmartRWLocker.withReadAcquire])
*
* **Warning**: It is not recommended to use constructor with both [Map] and [SmartRWLocker]. Besides, in case
* you are using your own [Map] as a [map] you should be careful with operations on this [map]
*/
class ReadMapCRUDRepo<ObjectType, IdType>( class ReadMapCRUDRepo<ObjectType, IdType>(
private val map: Map<IdType, ObjectType> = emptyMap(), private val map: Map<IdType, ObjectType> = emptyMap(),
private val locker: SmartRWLocker = SmartRWLocker() private val locker: SmartRWLocker = SmartRWLocker()
@ -47,6 +54,13 @@ class ReadMapCRUDRepo<ObjectType, IdType>(
} }
} }
/**
* [MutableMap]-based [WriteMapCRUDRepo]. All internal operations will be locked with [locker] (mostly with
* [SmartRWLocker.withWriteLock])
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [map] you should be careful with operations on this [map]
*/
abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>( abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
protected val map: MutableMap<IdType, ObjectType> = mutableMapOf(), protected val map: MutableMap<IdType, ObjectType> = mutableMapOf(),
protected val locker: SmartRWLocker = SmartRWLocker() protected val locker: SmartRWLocker = SmartRWLocker()
@ -98,6 +112,12 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
} }
/**
* [MutableMap]-based [MapCRUDRepo]. All internal operations will be locked with [locker]
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [map] you should be careful with operations on this [map]
*/
abstract class MapCRUDRepo<ObjectType, IdType, InputValueType>( abstract class MapCRUDRepo<ObjectType, IdType, InputValueType>(
map: MutableMap<IdType, ObjectType>, map: MutableMap<IdType, ObjectType>,
locker: SmartRWLocker = SmartRWLocker() locker: SmartRWLocker = SmartRWLocker()
@ -105,6 +125,12 @@ abstract class MapCRUDRepo<ObjectType, IdType, InputValueType>(
ReadCRUDRepo<ObjectType, IdType> by ReadMapCRUDRepo(map, locker), ReadCRUDRepo<ObjectType, IdType> by ReadMapCRUDRepo(map, locker),
WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(map, locker) WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(map, locker)
/**
* [MutableMap]-based [MapCRUDRepo]. All internal operations will be locked with [locker]
*
* **Warning**: Besides, in case you are using your own [MutableMap] as a [map] you should be careful with operations
* on this [map]
*/
fun <ObjectType, IdType, InputValueType> MapCRUDRepo( fun <ObjectType, IdType, InputValueType> MapCRUDRepo(
map: MutableMap<IdType, ObjectType>, map: MutableMap<IdType, ObjectType>,
updateCallback: suspend MutableMap<IdType, ObjectType>.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, updateCallback: suspend MutableMap<IdType, ObjectType>.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType,
@ -120,12 +146,21 @@ fun <ObjectType, IdType, InputValueType> MapCRUDRepo(
override suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType> = map.createCallback(newValue) override suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType> = map.createCallback(newValue)
} }
/**
* [MutableMap]-based [MapCRUDRepo]. All internal operations will be locked with [locker]
*/
fun <ObjectType, IdType, InputValueType> MapCRUDRepo( fun <ObjectType, IdType, InputValueType> MapCRUDRepo(
updateCallback: suspend MutableMap<IdType, ObjectType>.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, updateCallback: suspend MutableMap<IdType, ObjectType>.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType,
locker: SmartRWLocker = SmartRWLocker(), locker: SmartRWLocker = SmartRWLocker(),
createCallback: suspend MutableMap<IdType, ObjectType>.(newValue: InputValueType) -> Pair<IdType, ObjectType> createCallback: suspend MutableMap<IdType, ObjectType>.(newValue: InputValueType) -> Pair<IdType, ObjectType>
) = MapCRUDRepo(mutableMapOf(), updateCallback, locker, createCallback) ) = MapCRUDRepo(mutableMapOf(), updateCallback, locker, createCallback)
/**
* [MutableMap]-based [MapCRUDRepo]. All internal operations will be locked with [locker]
*
* **Warning**: Besides, in case you are using your own [MutableMap] as a [this] you should be careful with operations
* on this [this]
*/
fun <ObjectType, IdType, InputValueType> MutableMap<IdType, ObjectType>.asCrudRepo( fun <ObjectType, IdType, InputValueType> MutableMap<IdType, ObjectType>.asCrudRepo(
updateCallback: suspend MutableMap<IdType, ObjectType>.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, updateCallback: suspend MutableMap<IdType, ObjectType>.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType,
locker: SmartRWLocker = SmartRWLocker(), locker: SmartRWLocker = SmartRWLocker(),

View File

@ -10,6 +10,13 @@ import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
/**
* [Map]-based [ReadKeyValueRepo]. All internal operations will be locked with [locker] (mostly with
* [SmartRWLocker.withReadAcquire])
*
* **Warning**: It is not recommended to use constructor with both [Map] and [SmartRWLocker]. Besides, in case you are
* using your own [Map] as a [map] you should be careful with operations on this [map]
*/
class ReadMapKeyValueRepo<Key, Value>( class ReadMapKeyValueRepo<Key, Value>(
protected val map: Map<Key, Value>, protected val map: Map<Key, Value>,
private val locker: SmartRWLocker private val locker: SmartRWLocker
@ -70,6 +77,13 @@ class ReadMapKeyValueRepo<Key, Value>(
override suspend fun count(): Long = locker.withReadAcquire { map.size.toLong() } override suspend fun count(): Long = locker.withReadAcquire { map.size.toLong() }
} }
/**
* [MutableMap]-based [WriteKeyValueRepo]. All internal operations will be locked with [locker] (mostly with
* [SmartRWLocker.withWriteLock])
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [map] you should be careful with operations on this [map]
*/
class WriteMapKeyValueRepo<Key, Value>( class WriteMapKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, Value>, private val map: MutableMap<Key, Value>,
private val locker: SmartRWLocker private val locker: SmartRWLocker
@ -107,6 +121,12 @@ class WriteMapKeyValueRepo<Key, Value>(
} }
} }
/**
* [MutableMap]-based [KeyValueRepo]. All internal operations will be locked with [locker]
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [map] you should be careful with operations on this [map]
*/
@Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE") @Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE")
class MapKeyValueRepo<Key, Value>( class MapKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, Value>, private val map: MutableMap<Key, Value>,
@ -120,4 +140,10 @@ class MapKeyValueRepo<Key, Value>(
} }
} }
/**
* [MutableMap]-based [KeyValueRepo]. All internal operations will be locked with [locker]
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [this] you should be careful with operations on this [this]
*/
fun <K, V> MutableMap<K, V>.asKeyValueRepo(locker: SmartRWLocker = SmartRWLocker()): KeyValueRepo<K, V> = MapKeyValueRepo(this, locker) fun <K, V> MutableMap<K, V>.asKeyValueRepo(locker: SmartRWLocker = SmartRWLocker()): KeyValueRepo<K, V> = MapKeyValueRepo(this, locker)

View File

@ -8,6 +8,13 @@ import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
/**
* [Map]-based [ReadKeyValuesRepo]. All internal operations will be locked with [locker] (mostly with
* [SmartRWLocker.withReadAcquire])
*
* **Warning**: It is not recommended to use constructor with both [Map] and [SmartRWLocker]. Besides, in case
* you are using your own [Map] as a [map] you should be careful with operations on this [map]
*/
class MapReadKeyValuesRepo<Key, Value>( class MapReadKeyValuesRepo<Key, Value>(
private val map: Map<Key, List<Value>> = emptyMap(), private val map: Map<Key, List<Value>> = emptyMap(),
private val locker: SmartRWLocker = SmartRWLocker() private val locker: SmartRWLocker = SmartRWLocker()
@ -63,6 +70,13 @@ class MapReadKeyValuesRepo<Key, Value>(
override suspend fun count(): Long = locker.withReadAcquire { map.size }.toLong() override suspend fun count(): Long = locker.withReadAcquire { map.size }.toLong()
} }
/**
* [MutableMap]-based [WriteKeyValuesRepo]. All internal operations will be locked with [locker] (mostly with
* [SmartRWLocker.withWriteLock])
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [map] you should be careful with operations on this [map]
*/
class MapWriteKeyValuesRepo<Key, Value>( class MapWriteKeyValuesRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf(), private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf(),
private val locker: SmartRWLocker = SmartRWLocker() private val locker: SmartRWLocker = SmartRWLocker()
@ -133,6 +147,12 @@ class MapWriteKeyValuesRepo<Key, Value>(
} }
} }
/**
* [MutableMap]-based [KeyValuesRepo]. All internal operations will be locked with [locker]
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [map] you should be careful with operations on this [map]
*/
@Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE") @Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE")
class MapKeyValuesRepo<Key, Value>( class MapKeyValuesRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf(), private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf(),
@ -141,6 +161,11 @@ class MapKeyValuesRepo<Key, Value>(
ReadKeyValuesRepo<Key, Value> by MapReadKeyValuesRepo(map, locker), ReadKeyValuesRepo<Key, Value> by MapReadKeyValuesRepo(map, locker),
WriteKeyValuesRepo<Key, Value> by MapWriteKeyValuesRepo(map, locker) WriteKeyValuesRepo<Key, Value> by MapWriteKeyValuesRepo(map, locker)
/**
* [MutableMap]-based [KeyValuesRepo]. All internal operations will be locked with [locker]
*
* **Warning**: In case you are using your own [MutableMap] as [this] receiver you should be careful with operations on [this] map
*/
fun <K, V> MutableMap<K, List<V>>.asKeyValuesRepo(locker: SmartRWLocker = SmartRWLocker()): KeyValuesRepo<K, V> = MapKeyValuesRepo( fun <K, V> MutableMap<K, List<V>>.asKeyValuesRepo(locker: SmartRWLocker = SmartRWLocker()): KeyValuesRepo<K, V> = MapKeyValuesRepo(
map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap(), map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap(),
locker locker