Skip to content

Commit

Permalink
optimized query
Browse files Browse the repository at this point in the history
  • Loading branch information
Roiocam committed Apr 19, 2024
1 parent 4fcceb1 commit 689038f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,14 @@ class JournalQueries(
}

def delete(persistenceId: String, toSequenceNr: Long) = {
selectByPersistenceIdAndMaxSequenceNr(persistenceId, toSequenceNr).delete
JournalTable.filter(_.persistenceId === persistenceId).filter(_.sequenceNumber <= toSequenceNr).delete
}

private def _selectAllJournalForPersistenceId(persistenceId: Rep[String]) =
_selectByPersistenceId(persistenceId).sortBy(_.sequenceNumber.desc)

private def _selectByPersistenceId(persistenceId: Rep[String]) =
JournalTable.filter(_.persistenceId === persistenceId)

private def _selectByPersistenceIdAndMaxSequenceNr(persistenceId: Rep[String], maxSeqNr: Rep[Long]) =
_selectByPersistenceId(persistenceId).filter(_.sequenceNumber <= maxSeqNr)

val selectByPersistenceIdAndMaxSequenceNr = Compiled(_selectByPersistenceIdAndMaxSequenceNr _)

private def _highestSequenceNrForPersistenceId(persistenceId: Rep[String]): Rep[Option[Long]] =
_selectAllJournalForPersistenceId(persistenceId).take(1).map(_.sequenceNumber).max
JournalTable.filter(_.persistenceId === persistenceId).take(1).map(_.sequenceNumber).max

val highestSequenceNrForPersistenceId = Compiled(_highestSequenceNrForPersistenceId _)

private def _allPersistenceIdsDistinct: Query[Rep[String], String, Seq] =
JournalTable.map(_.persistenceId).distinct

val allPersistenceIdsDistinct = Compiled(_allPersistenceIdsDistinct)

private def _messagesQuery(
persistenceId: Rep[String],
fromSequenceNr: Rep[Long],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ class JournalQueries(val profile: JdbcProfile, override val journalTableCfg: Leg
def writeJournalRows(xs: Seq[JournalRow]) =
JournalTableC ++= xs.sortBy(_.sequenceNumber)

private def selectAllJournalForPersistenceId(persistenceId: Rep[String]) =
_selectByPersistenceId(persistenceId).sortBy(_.sequenceNumber.desc)

def delete(persistenceId: String, toSequenceNr: Long) = {
selectByPersistenceIdAndMaxSequenceNr(persistenceId, toSequenceNr).delete
JournalTable.filter(_.persistenceId === persistenceId).filter(_.sequenceNumber <= toSequenceNr).delete
}

/**
Expand All @@ -44,23 +41,11 @@ class JournalQueries(val profile: JdbcProfile, override val journalTableCfg: Leg
baseQuery.map(_.message).update(replacement)
}

private def _selectByPersistenceId(persistenceId: Rep[String]) =
JournalTable.filter(_.persistenceId === persistenceId)

private def _selectByPersistenceIdAndMaxSequenceNr(persistenceId: Rep[String], maxSeqNr: Rep[Long]) =
_selectByPersistenceId(persistenceId).filter(_.sequenceNumber <= maxSeqNr)
val selectByPersistenceIdAndMaxSequenceNr = Compiled(_selectByPersistenceIdAndMaxSequenceNr _)

private def _highestSequenceNrForPersistenceId(persistenceId: Rep[String]): Rep[Option[Long]] =
selectAllJournalForPersistenceId(persistenceId).take(1).map(_.sequenceNumber).max
JournalTable.filter(_.persistenceId === persistenceId).take(1).map(_.sequenceNumber).max

val highestSequenceNrForPersistenceId = Compiled(_highestSequenceNrForPersistenceId _)

private def _allPersistenceIdsDistinct: Query[Rep[String], String, Seq] =
JournalTable.map(_.persistenceId).distinct

val allPersistenceIdsDistinct = Compiled(_allPersistenceIdsDistinct)

private def _messagesQuery(
persistenceId: Rep[String],
fromSequenceNr: Rep[Long],
Expand Down

0 comments on commit 689038f

Please sign in to comment.