Initial implementation of DurableStateStore and one test

This commit is contained in:
Debasish Ghosh 2021-06-03 12:02:43 +05:30 committed by Patrik Nordwall
parent 2168cec497
commit 78ff85d0fb
4 changed files with 192 additions and 0 deletions

View file

@ -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]
}

View file

@ -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
}))
}

View file

@ -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
})
}