Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete all but last #847

Merged
merged 8 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ class DefaultJournalDao(

override def delete(persistenceId: String, maxSequenceNr: Long): Future[Unit] = {
val actions: DBIOAction[Unit, NoStream, Effect.Write with Effect.Read] = for {
_ <- queries.markJournalMessagesAsDeleted(persistenceId, maxSequenceNr)
highestMarkedSequenceNr <- highestMarkedSequenceNr(persistenceId)
_ <- queries.delete(persistenceId, highestMarkedSequenceNr.getOrElse(0L) - 1)
Comment on lines -53 to -55
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was indeed not efficient and there was also a little bug here.

For a journal with events: 1, 2, 3, 4, 5

Deleting up to 3, would first:
logically delete: 1, 2, 3
collect the highest logically deleted nr, thus 3
delete everything until 3, so delete 1 and 2.

The journal remains with: 3, 4, 5 while 3 is invisible because logically deleted.

There is no reason to keep 3, because it's not the highest anyway.

highestMarkedSequenceNr <- highestSequenceNrAction(persistenceId)
_ <- queries.delete(persistenceId, highestMarkedSequenceNr - 1)
_ <- queries.markAsDeleted(persistenceId, highestMarkedSequenceNr)
} yield ()

db.run(actions.transactionally)
}

override def highestSequenceNr(persistenceId: String, fromSequenceNr: Long): Future[Long] = {
for {
maybeHighestSeqNo <- db.run(queries.highestSequenceNrForPersistenceId(persistenceId).result)
} yield maybeHighestSeqNo.getOrElse(0L)
}
override def highestSequenceNr(persistenceId: String, fromSequenceNr: Long): Future[Long] =
db.run(highestSequenceNrAction(persistenceId))

private def highestSequenceNrAction(persistenceId: String): DBIOAction[Long, NoStream, Effect.Read] =
queries.highestSequenceNrForPersistenceId(persistenceId).result.map(_.getOrElse(0))

private def highestMarkedSequenceNr(persistenceId: String) =
queries.highestMarkedSequenceNrForPersistenceId(persistenceId).result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class JournalQueries(
JournalTable.filter(_.persistenceId === persistenceId).filter(_.sequenceNumber <= toSequenceNr).delete
}

private[akka] def markAsDeleted(persistenceId: String, highestMarkedSequenceNr: Long) =
JournalTable
.filter(_.persistenceId === persistenceId)
.filter(_.sequenceNumber === highestMarkedSequenceNr)
.filter(_.deleted === false)
.map(_.deleted)
.update(true)
octonato marked this conversation as resolved.
Show resolved Hide resolved

def markJournalMessagesAsDeleted(persistenceId: String, maxSequenceNr: Long) =
octonato marked this conversation as resolved.
Show resolved Hide resolved
JournalTable
.filter(_.persistenceId === persistenceId)
Expand Down
Loading