|
| 1 | +package io.github.alstanchev.pekko.persistence.inmemory.extension |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2016 Dennis Vriend |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import org.apache.pekko.actor.{ Actor, ActorLogging, ActorRef, NoSerializationVerificationNeeded } |
| 20 | +import org.apache.pekko.event.LoggingReceive |
| 21 | +import org.apache.pekko.persistence.PersistentRepr |
| 22 | +import io.github.alstanchev.pekko.persistence.inmemory.JournalEntry |
| 23 | +import io.github.alstanchev.pekko.persistence.inmemory.util.UUIDs |
| 24 | +import org.apache.pekko.persistence.query.{ NoOffset, Offset, Sequence, TimeBasedUUID } |
| 25 | +import org.apache.pekko.serialization.Serialization |
| 26 | + |
| 27 | +import scala.collection.immutable._ |
| 28 | +import scalaz.syntax.semigroup._ |
| 29 | +import scalaz.std.AllInstances._ |
| 30 | + |
| 31 | +import io.github.alstanchev.pekko.persistence.inmemory.mapSeqToVector |
| 32 | + |
| 33 | +object InMemoryJournalStorage { |
| 34 | + sealed trait JournalCommand extends NoSerializationVerificationNeeded |
| 35 | + case object PersistenceIds extends JournalCommand |
| 36 | + final case class HighestSequenceNr(persistenceId: String, fromSequenceNr: Long) extends JournalCommand |
| 37 | + final case class EventsByTag(tag: String, offset: Offset) extends JournalCommand |
| 38 | + final case class PersistenceIds(queryListOfPersistenceIds: Seq[String]) extends JournalCommand |
| 39 | + final case class WriteList(xs: Seq[JournalEntry]) extends JournalCommand |
| 40 | + final case class Delete(persistenceId: String, toSequenceNr: Long) extends JournalCommand |
| 41 | + final case class GetJournalEntriesExceptDeleted(persistenceId: String, fromSequenceNr: Long, toSequenceNr: Long, max: Long) extends JournalCommand |
| 42 | + final case class GetAllJournalEntries(persistenceId: String, fromSequenceNr: Long, toSequenceNr: Long, max: Long) extends JournalCommand |
| 43 | + |
| 44 | + /** |
| 45 | + * Java API |
| 46 | + */ |
| 47 | + def clearJournal(): ClearJournal = ClearJournal |
| 48 | + |
| 49 | + sealed abstract class ClearJournal |
| 50 | + case object ClearJournal extends ClearJournal with JournalCommand |
| 51 | + |
| 52 | + def getPersistenceId(prod: (String, Vector[JournalEntry])): String = prod._1 |
| 53 | + def getEntries(prod: (String, Vector[JournalEntry])): Vector[JournalEntry] = prod._2 |
| 54 | + def getEventsByPid(pid: String, journal: Map[String, Vector[JournalEntry]]): Option[Vector[JournalEntry]] = |
| 55 | + journal.find(_._1 == pid).map(_._2) |
| 56 | + def getAllEvents(journal: Map[String, Vector[JournalEntry]]): Vector[JournalEntry] = |
| 57 | + journal.values.flatten[JournalEntry].toVector |
| 58 | + def getMaxSequenceNr(xs: Vector[JournalEntry]): Long = xs.map(_.sequenceNr).max |
| 59 | +} |
| 60 | + |
| 61 | +class InMemoryJournalStorage(serialization: Serialization) extends Actor with ActorLogging { |
| 62 | + import InMemoryJournalStorage._ |
| 63 | + |
| 64 | + var ordering: Long = 0L |
| 65 | + |
| 66 | + def incrementAndGet: Long = { |
| 67 | + ordering += 1 |
| 68 | + ordering |
| 69 | + } |
| 70 | + |
| 71 | + var journal = Map.empty[String, Vector[JournalEntry]] |
| 72 | + |
| 73 | + def allPersistenceIds(ref: ActorRef): Unit = |
| 74 | + ref ! org.apache.pekko.actor.Status.Success(journal.keySet) |
| 75 | + |
| 76 | + def highestSequenceNr(ref: ActorRef, persistenceId: String, fromSequenceNr: Long): Unit = { |
| 77 | + val highestSequenceNrJournal = getEventsByPid(persistenceId, journal).map(getMaxSequenceNr).getOrElse(0L) |
| 78 | + ref ! org.apache.pekko.actor.Status.Success(highestSequenceNrJournal) |
| 79 | + } |
| 80 | + |
| 81 | + def eventsByTag(ref: ActorRef, tag: String, offset: Offset): Unit = { |
| 82 | + def increment(offset: Long): Long = offset + 1 |
| 83 | + def getByOffset(p: JournalEntry => Boolean): List[JournalEntry] = { |
| 84 | + val xs = getAllEvents(journal) |
| 85 | + .filter(_.tags.contains(tag)).toList |
| 86 | + .sortBy(_.ordering) |
| 87 | + .zipWithIndex.map { |
| 88 | + case (entry, index) => |
| 89 | + entry.copy(offset = Option(increment(index))) |
| 90 | + } |
| 91 | + |
| 92 | + xs.filter(p) |
| 93 | + } |
| 94 | + |
| 95 | + val xs: List[JournalEntry] = offset match { |
| 96 | + case NoOffset => getByOffset(_.offset.exists(_ >= 0L)) |
| 97 | + case Sequence(value) => getByOffset(_.offset.exists(_ > value)) |
| 98 | + case value: TimeBasedUUID => getByOffset(p => UUIDs.TimeBasedUUIDOrdering.gt(p.timestamp, value)) |
| 99 | + } |
| 100 | + |
| 101 | + ref ! org.apache.pekko.actor.Status.Success(xs) |
| 102 | + } |
| 103 | + |
| 104 | + def writelist(ref: ActorRef, xs: Seq[JournalEntry]): Unit = { |
| 105 | + val ys = xs.map(_.copy(ordering = incrementAndGet)).groupBy(_.persistenceId) |
| 106 | + journal = journal |+| ys |
| 107 | + |
| 108 | + ref ! org.apache.pekko.actor.Status.Success(()) |
| 109 | + } |
| 110 | + |
| 111 | + def delete(ref: ActorRef, persistenceId: String, toSequenceNr: Long): Unit = { |
| 112 | + val pidEntries = journal.filter(_._1 == persistenceId) |
| 113 | + val notDeleted = pidEntries.view.mapValues(_.filterNot(_.sequenceNr <= toSequenceNr)) |
| 114 | + |
| 115 | + val deleted = pidEntries |
| 116 | + .view.mapValues(_.filter(_.sequenceNr <= toSequenceNr).map { journalEntry => |
| 117 | + val updatedRepr: PersistentRepr = journalEntry.repr.update(deleted = true) |
| 118 | + val byteArray: Array[Byte] = serialization.serialize(updatedRepr) match { |
| 119 | + case scala.util.Success(arr) => arr |
| 120 | + case scala.util.Failure(cause) => throw cause |
| 121 | + } |
| 122 | + journalEntry.copy(deleted = true).copy(serialized = byteArray).copy(repr = updatedRepr) |
| 123 | + }) |
| 124 | + |
| 125 | + journal = journal.filterNot(_._1 == persistenceId) |+| deleted.toMap |+| notDeleted.toMap |
| 126 | + |
| 127 | + ref ! org.apache.pekko.actor.Status.Success("") |
| 128 | + } |
| 129 | + |
| 130 | + def messages(ref: ActorRef, persistenceId: String, fromSequenceNr: Long, toSequenceNr: Long, max: Long, all: Boolean): Unit = { |
| 131 | + def toTake = if (max >= Int.MaxValue) Int.MaxValue else max.toInt |
| 132 | + val pidEntries: Map[String, Vector[JournalEntry]] = journal.filter(_._1 == persistenceId) |
| 133 | + val xs: List[JournalEntry] = pidEntries.flatMap(_._2) |
| 134 | + .filter(_.sequenceNr >= fromSequenceNr) |
| 135 | + .filter(_.sequenceNr <= toSequenceNr) |
| 136 | + .toList.sortBy(_.sequenceNr) |
| 137 | + |
| 138 | + val ys = if (all) xs else xs.filterNot(_.deleted) |
| 139 | + |
| 140 | + val zs = ys.take(toTake) |
| 141 | + |
| 142 | + ref ! org.apache.pekko.actor.Status.Success(zs) |
| 143 | + } |
| 144 | + |
| 145 | + def clear(ref: ActorRef): Unit = { |
| 146 | + ordering = 0L |
| 147 | + journal = Map.empty[String, Vector[JournalEntry]] |
| 148 | + |
| 149 | + ref ! org.apache.pekko.actor.Status.Success("") |
| 150 | + } |
| 151 | + |
| 152 | + override def receive: Receive = LoggingReceive { |
| 153 | + case PersistenceIds => allPersistenceIds(sender()) |
| 154 | + case HighestSequenceNr(persistenceId, fromSequenceNr) => highestSequenceNr(sender(), persistenceId, fromSequenceNr) |
| 155 | + case EventsByTag(tag, offset) => eventsByTag(sender(), tag, offset) |
| 156 | + case WriteList(xs) => writelist(sender(), xs) |
| 157 | + case Delete(persistenceId, toSequenceNr) => delete(sender(), persistenceId, toSequenceNr) |
| 158 | + case GetJournalEntriesExceptDeleted(persistenceId, fromSequenceNr, toSequenceNr, max) => messages(sender(), persistenceId, fromSequenceNr, toSequenceNr, max, all = false) |
| 159 | + case GetAllJournalEntries(persistenceId, fromSequenceNr, toSequenceNr, max) => messages(sender(), persistenceId, fromSequenceNr, toSequenceNr, max, all = true) |
| 160 | + case ClearJournal => clear(sender()) |
| 161 | + } |
| 162 | +} |
0 commit comments