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 1 commit
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,9 +50,10 @@ 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.

highestMarkedSequenceNrOpt <- highestMarkedSequenceNr(persistenceId)
highestMarkedSequenceNr = highestMarkedSequenceNrOpt.getOrElse(0L)
_ <- queries.delete(persistenceId, highestMarkedSequenceNr - 1)
_ <- queries.markAsDeleted(persistenceId, highestMarkedSequenceNr)
} yield ()

db.run(actions.transactionally)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ class JournalQueries(
JournalTable.filter(_.persistenceId === persistenceId).filter(_.sequenceNumber <= toSequenceNr).delete
}

def markJournalMessagesAsDeleted(persistenceId: String, maxSequenceNr: Long) =
def markAsDeleted(persistenceId: String, highestMarkedSequenceNr: Long) =
JournalTable
.filter(_.persistenceId === persistenceId)
.filter(_.sequenceNumber <= maxSequenceNr)
.filter(_.sequenceNumber === highestMarkedSequenceNr)
Copy link
Member Author

Choose a reason for hiding this comment

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

being more explicitly and only marking a single message as delete.

Only used when deleted events though and what we want is to delete the latest.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, this will probably not work as expected. Looking further in the code.

.filter(_.deleted === false)
.map(_.deleted)
.update(true)
Expand Down
Loading