Initial implementation of DurableStateStore and one test
This commit is contained in:
parent
2168cec497
commit
78ff85d0fb
4 changed files with 192 additions and 0 deletions
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package akka.persistence.state.inmem
|
||||
|
||||
import akka.persistence.state.DurableStateStoreProvider
|
||||
import akka.persistence.state.scaladsl.DurableStateStore
|
||||
import akka.persistence.state.inmem.scaladsl.InmemDurableStateStore
|
||||
import akka.persistence.state.javadsl.{ DurableStateStore => JDurableStateStore }
|
||||
import akka.persistence.state.inmem.javadsl.{ InmemDurableStateStore => JInmemDurableStateStore }
|
||||
|
||||
class InmemDurableStateStoreProvider extends DurableStateStoreProvider {
|
||||
override def scaladslDurableStateStore(): DurableStateStore[Any] =
|
||||
new InmemDurableStateStore[Any]
|
||||
|
||||
override def javadslDurableStateStore(): JDurableStateStore[AnyRef] =
|
||||
new JInmemDurableStateStore[AnyRef]
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package akka.persistence.state.inmem.javadsl
|
||||
|
||||
import java.util.Optional
|
||||
import java.util.concurrent.{ CompletionStage, ConcurrentHashMap }
|
||||
|
||||
import scala.concurrent.Future
|
||||
import scala.compat.java8.FutureConverters._
|
||||
|
||||
import akka.Done
|
||||
|
||||
import akka.persistence.state.javadsl.{ DurableStateUpdateStore, GetObjectResult }
|
||||
|
||||
class InmemDurableStateStore[A] extends DurableStateUpdateStore[A] {
|
||||
val store = new ConcurrentHashMap[String, A]()
|
||||
|
||||
def getObject(persistenceId: String): CompletionStage[GetObjectResult[A]] =
|
||||
toJava(Future.successful(GetObjectResult(Optional.ofNullable(store.get(persistenceId)), 0)))
|
||||
|
||||
def upsertObject(persistenceId: String, seqNr: Long, value: A, tag: String): CompletionStage[Done] =
|
||||
toJava(Future.successful(store.put(persistenceId, value) match {
|
||||
case _ => Done
|
||||
}))
|
||||
|
||||
def deleteObject(persistenceId: String): CompletionStage[Done] =
|
||||
toJava(Future.successful(store.remove(persistenceId) match {
|
||||
case _ => Done
|
||||
}))
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package akka.persistence.state.inmem.scaladsl
|
||||
|
||||
import scala.collection.concurrent.TrieMap
|
||||
import scala.concurrent.Future
|
||||
|
||||
import akka.Done
|
||||
import akka.persistence.state.scaladsl.{ DurableStateUpdateStore, GetObjectResult }
|
||||
|
||||
class InmemDurableStateStore[A] extends DurableStateUpdateStore[A] {
|
||||
val store = new TrieMap[String, A]()
|
||||
|
||||
def getObject(persistenceId: String): Future[GetObjectResult[A]] =
|
||||
Future.successful(GetObjectResult(store.get(persistenceId), 0))
|
||||
|
||||
def upsertObject(persistenceId: String, seqNr: Long, value: A, tag: String): Future[Done] =
|
||||
Future.successful(store.put(persistenceId, value) match {
|
||||
case _ => Done
|
||||
})
|
||||
|
||||
def deleteObject(persistenceId: String): Future[Done] =
|
||||
Future.successful(store.remove(persistenceId) match {
|
||||
case _ => Done
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue