Skip to content

Commit

Permalink
fix AbstractMethodException by using bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
an-tex committed Sep 16, 2024
1 parent 831ee84 commit 60a1bf6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import akka.persistence.r2dbc.query.scaladsl.R2dbcReadJournal
import akka.persistence.typed.PersistenceId
import akka.projection.ProjectionId
import akka.projection.eventsourced.scaladsl.EventSourcedProvider
import akka.projection.r2dbc.scaladsl.{R2dbcProjection, R2dbcSession}
import akka.projection.r2dbc.scaladsl.{R2dbcProjection => AkkaR2dbcProjection, R2dbcSession}
import akka.projection.scaladsl.SourceProvider
import akka.stream.scaladsl.Source
import akka.{Done, NotUsed}
Expand All @@ -19,11 +19,21 @@ import scala.concurrent.{ExecutionContext, Future}

private[r2dbc] trait R2dbcProjection extends EventSourcedT.ProjectionT {
_: EventSourcedT#EventSourcedBaseComponentT
with net.sc8s.akka.components.ClusterComponent.ComponentT.EventSourcedT#BaseComponent =>
with EventSourcedT#BaseComponent =>

val numberOfProjectionInstances = 1
}

object R2dbcProjection {
private[r2dbc] trait FromSnapshot {
_: EventSourcedT#EventSourcedBaseComponentT
with EventSourcedT.SnapshotsT#SnapshotsBaseComponentT =>

// without the type parameter you "sometimes" get an AbstractMethodError exception :( https://github.com/scala/bug/issues/11833
def transformSnapshot[State <: StateT](state: State): EventT
}
}

trait R2dbcShardedProjection extends R2dbcProjection {
_: EventSourcedT#EventSourcedBaseComponentT
with net.sc8s.akka.components.ClusterComponent.Sharded.EventSourced#BaseComponent =>
Expand Down Expand Up @@ -56,7 +66,7 @@ trait R2dbcShardedProjection extends R2dbcProjection {
val projectionId = projectionIds(i)
val minSlice = sliceRanges(i).min
val maxSlice = sliceRanges(i).max
R2dbcProjection
AkkaR2dbcProjection
.atLeastOnce(
projectionId,
None,
Expand All @@ -82,13 +92,10 @@ trait R2dbcShardedProjection extends R2dbcProjection {
}

object R2dbcShardedProjection {
trait FromSnapshot {
_: R2dbcShardedProjection
with net.sc8s.akka.components.ClusterComponent.Sharded.EventSourced#BaseComponent
trait FromSnapshot extends R2dbcShardedProjection with R2dbcProjection.FromSnapshot {
_: net.sc8s.akka.components.ClusterComponent.Sharded.EventSourced#BaseComponent
with EventSourcedT.SnapshotsT#SnapshotsBaseComponentT =>

def transformSnapshot(state: StateT): EventT

override private[r2dbc] def createSourceProvider(minSlice: Int, maxSlice: Int, actorSystem: ActorSystem[_]): SourceProvider[Offset, EventEnvelope[EventT]] =
EventSourcedProvider.eventsBySlicesStartingFromSnapshots(
actorSystem,
Expand Down Expand Up @@ -152,7 +159,7 @@ trait R2dbcSingletonProjection extends R2dbcProjection {

override def projectionFactory(i: Int) = {
val projectionId = projectionIds(i)
R2dbcProjection
AkkaR2dbcProjection
.atLeastOnce(
projectionId,
None,
Expand All @@ -172,10 +179,10 @@ trait R2dbcSingletonProjection extends R2dbcProjection {
}

object R2dbcSingletonProjection {
trait FromSnapshot {
_: R2dbcSingletonProjection with EventSourcedT.SnapshotsT#SnapshotsBaseComponentT =>

def transformSnapshot(state: StateT): EventT
trait FromSnapshot extends R2dbcSingletonProjection with R2dbcProjection.FromSnapshot {
_: EventSourcedT#EventSourcedBaseComponentT
with net.sc8s.akka.components.ClusterComponent.Singleton.EventSourced#BaseComponent
with EventSourcedT.SnapshotsT#SnapshotsBaseComponentT =>

override private[r2dbc] def createEventSource(persistenceId: PersistenceId, sequence: Sequence, eventQueries: R2dbcReadJournal) =
eventQueries.currentEventsByPersistenceIdStartingFromSnapshot(persistenceId.id, sequence.value, Long.MaxValue, transformSnapshot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class R2DbcProjectionSpec extends ScalaTestWithActorTestKit(ConfigFactory.parseS

override val retentionCriteria = RetentionCriteria.snapshotEvery(100, 2)

override def transformSnapshot(state: State) = Event()
override def transformSnapshot[State](state: State) = Event()
}

override val name = "singleton"
Expand Down Expand Up @@ -155,7 +155,7 @@ class R2DbcProjectionSpec extends ScalaTestWithActorTestKit(ConfigFactory.parseS
case class State()
implicit val stateCodec: Codec[State] = deriveCodec

class Component(dependency: Dependency) extends BaseComponent with R2dbcShardedProjection with R2dbcShardedProjection.FromSnapshot {
class Component(dependency: Dependency) extends BaseComponent with R2dbcShardedProjection.FromSnapshot {
override val behavior = componentContext => EventSourcedBehavior(
componentContext.persistenceId,
State(),
Expand All @@ -176,7 +176,7 @@ class R2DbcProjectionSpec extends ScalaTestWithActorTestKit(ConfigFactory.parseS

override val retentionCriteria = RetentionCriteria.snapshotEvery(100, 2)

override def transformSnapshot(state: State) = Event()
override def transformSnapshot[State](state: State) = Event()
}

override val name = randomName
Expand Down
4 changes: 2 additions & 2 deletions akka-components/src/main/scala/ClusterComponent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ object ClusterComponent {

private[components] trait EventSourcedBaseComponentT extends super.BaseComponentT {

// for mixin traits accessibility, why don't they see the already defined types/objects?
// for mixin traits accessibility
private[components] type EventT = Event
private[components] val componentName = name

Expand Down Expand Up @@ -127,7 +127,7 @@ object ClusterComponent {
sealed trait SnapshotsT extends EventSourcedT {

private[components] sealed trait SnapshotsBaseComponentT extends super.EventSourcedBaseComponentT {
// for mixin traits accessibility, why don't they see the already defined types/objects?
// for mixin traits accessibility
private[components] type StateT = State

override private[components] def behaviorTransformer = (context, behavior) => super.behaviorTransformer(context, behavior).withRetention(retentionCriteria)
Expand Down

0 comments on commit 60a1bf6

Please sign in to comment.