-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd41bf0
commit ce717a4
Showing
6 changed files
with
92 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 40 additions & 6 deletions
46
...ain/kotlin/dev/inmo/micro_utils/fsm/repos/common/KeyValueBasedDefaultStatesManagerRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,59 @@ | ||
package dev.inmo.micro_utils.fsm.repos.common | ||
|
||
import dev.inmo.kslog.common.TagLogger | ||
import dev.inmo.kslog.common.i | ||
import dev.inmo.micro_utils.coroutines.SmartRWLocker | ||
import dev.inmo.micro_utils.coroutines.withReadAcquire | ||
import dev.inmo.micro_utils.coroutines.withWriteLock | ||
import dev.inmo.micro_utils.fsm.common.State | ||
import dev.inmo.micro_utils.fsm.common.managers.DefaultStatesManagerRepo | ||
import dev.inmo.micro_utils.repos.* | ||
import dev.inmo.micro_utils.repos.pagination.getAll | ||
import dev.inmo.micro_utils.repos.unset | ||
|
||
class KeyValueBasedDefaultStatesManagerRepo<T : State>( | ||
private val keyValueRepo: KeyValueRepo<Any, T> | ||
) : DefaultStatesManagerRepo<T> { | ||
private val locker = SmartRWLocker() | ||
private val logger = TagLogger("KeyValueBasedDefaultStatesManagerRepo") | ||
override suspend fun set(state: T) { | ||
keyValueRepo.set(state.context, state) | ||
locker.withWriteLock { | ||
keyValueRepo.set(state.context, state) | ||
logger.i { "Set ${state.context} value to $state" } | ||
} | ||
} | ||
|
||
override suspend fun removeState(state: T) { | ||
if (keyValueRepo.get(state.context) == state) { | ||
keyValueRepo.unset(state.context) | ||
locker.withWriteLock { | ||
if (keyValueRepo.get(state.context) == state) { | ||
keyValueRepo.unset(state.context) | ||
logger.i { "Unset $state" } | ||
} | ||
} | ||
} | ||
|
||
override suspend fun getStates(): List<T> = keyValueRepo.getAll { keys(it) }.map { it.second } | ||
override suspend fun getContextState(context: Any): T? = keyValueRepo.get(context) | ||
override suspend fun removeAndSet(toRemove: T, toSet: T) { | ||
locker.withWriteLock { | ||
when { | ||
toRemove.context == toSet.context -> { | ||
keyValueRepo.set(toSet.context, toSet) | ||
} | ||
else -> { | ||
keyValueRepo.set(toSet.context, toSet) | ||
keyValueRepo.unset(toRemove) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override suspend fun contains(context: Any): Boolean = keyValueRepo.contains(context) | ||
override suspend fun getStates(): List<T> = locker.withReadAcquire { | ||
keyValueRepo.getAll { keys(it) }.map { it.second } | ||
} | ||
override suspend fun getContextState(context: Any): T? = locker.withReadAcquire { | ||
keyValueRepo.get(context) | ||
} | ||
|
||
override suspend fun contains(context: Any): Boolean = locker.withReadAcquire { | ||
keyValueRepo.contains(context) | ||
} | ||
} |