Skip to content

Commit

Permalink
Consolidating EventStorageBackend (#10224)
Browse files Browse the repository at this point in the history
* Switching to composed anorm queries
* Switching to data-driven prepared statements where possible

changelog_begin
changelog_end
  • Loading branch information
nmarton-da authored Jul 12, 2021
1 parent 3fd3abf commit 05e5218
Show file tree
Hide file tree
Showing 10 changed files with 846 additions and 2,248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,16 @@ package com.daml.platform.store.appendonlydao.events

import java.sql.Connection

import com.daml.lf.data.Ref.{Identifier => ApiIdentifier}
import com.daml.platform.store.backend.EventStorageBackend
import com.daml.platform.store.backend.EventStorageBackend.{FilterParams, RangeParams}

private[events] sealed abstract class EventsTableFlatEventsRangeQueries[Offset] {

import EventsTableFlatEventsRangeQueries.QueryParts

protected def singleWildcardParty(
protected def query(
offset: Offset,
party: Party,
): QueryParts

protected def singlePartyWithTemplates(
offset: Offset,
party: Party,
templateIds: Set[ApiIdentifier],
): QueryParts

protected def onlyWildcardParties(
offset: Offset,
parties: Set[Party],
): QueryParts

protected def sameTemplates(
offset: Offset,
parties: Set[Party],
templateIds: Set[ApiIdentifier],
): QueryParts

protected def mixedTemplates(
offset: Offset,
partiesAndTemplateIds: Set[(Party, ApiIdentifier)],
): QueryParts

protected def mixedTemplatesWithWildcardParties(
offset: Offset,
wildcardParties: Set[Party],
partiesAndTemplateIds: Set[(Party, ApiIdentifier)],
filterParams: FilterParams,
): QueryParts

protected def offsetRange(offset: Offset): EventsRange[Long]
Expand All @@ -55,55 +27,51 @@ private[events] sealed abstract class EventsTableFlatEventsRangeQueries[Offset]
require(filter.nonEmpty, "The request must be issued by at least one party")

// Route the request to the correct underlying query
val frqK = if (filter.size == 1) {
val filterParams = if (filter.size == 1) {
val (party, templateIds) = filter.iterator.next()
if (templateIds.isEmpty) {
// Single-party request, no specific template identifier
singleWildcardParty(offset, party)
FilterParams(
wildCardParties = Set(party),
partiesAndTemplates = Set.empty,
)
} else {
// Single-party request, restricted to a set of template identifiers
singlePartyWithTemplates(offset, party, templateIds)
FilterParams(
wildCardParties = Set.empty,
partiesAndTemplates = Set(Set(party) -> templateIds),
)
}
} else {
// Multi-party requests
// If no party requests specific template identifiers
val parties = filter.keySet
if (filter.forall(_._2.isEmpty))
onlyWildcardParties(
offset = offset,
parties = parties,
FilterParams(
wildCardParties = parties,
partiesAndTemplates = Set.empty,
)
else {
// If all parties request the same template identifier
val templateIds = filter.valuesIterator.flatten.toSet
if (filter.valuesIterator.forall(_ == templateIds)) {
sameTemplates(
offset,
parties = parties,
templateIds = templateIds,
FilterParams(
wildCardParties = Set.empty,
partiesAndTemplates = Set(parties -> templateIds),
)
} else {
// If there are different template identifier but there are no wildcard parties
val partiesAndTemplateIds = Relation.flatten(filter).toSet
val wildcardParties = filter.filter(_._2.isEmpty).keySet
if (wildcardParties.isEmpty) {
mixedTemplates(
offset,
partiesAndTemplateIds = partiesAndTemplateIds,
)
} else {
// If there are wildcard parties and different template identifiers
mixedTemplatesWithWildcardParties(
offset,
wildcardParties,
partiesAndTemplateIds,
)
}
// The generic case: passing down in the same shape, collecting wildCardParties
FilterParams(
wildCardParties = filter.filter(_._2.isEmpty).keySet,
partiesAndTemplates = filter.iterator.collect {
case (party, templateIds) if templateIds.nonEmpty => Set(party) -> templateIds
}.toSet,
)
}
}
}

frqK match {
query(offset, filterParams) match {
case QueryParts.ByArith(read) =>
EventsRange.readPage(
read,
Expand Down Expand Up @@ -140,96 +108,21 @@ private[events] object EventsTableFlatEventsRangeQueries {
storageBackend: EventStorageBackend
) extends EventsTableFlatEventsRangeQueries[EventsRange[Long]] {

override protected def singleWildcardParty(
range: EventsRange[Long],
party: Party,
override protected def query(
offset: EventsRange[Long],
filterParams: FilterParams,
): QueryParts =
QueryParts.ByArith((range, limit, fetchSizeHint) =>
storageBackend.transactionsEventsSingleWildcardParty(
startExclusive = range.startExclusive,
endInclusive = range.endInclusive,
party = party,
limit = limit,
fetchSizeHint = fetchSizeHint,
)
)

override protected def singlePartyWithTemplates(
range: EventsRange[Long],
party: Party,
templateIds: Set[ApiIdentifier],
): QueryParts =
QueryParts.ByArith((range, limit, fetchSizeHint) =>
storageBackend.transactionsEventsSinglePartyWithTemplates(
startExclusive = range.startExclusive,
endInclusive = range.endInclusive,
party = party,
templateIds = templateIds,
limit = limit,
fetchSizeHint = fetchSizeHint,
)
)

override protected def onlyWildcardParties(
range: EventsRange[Long],
parties: Set[Party],
): QueryParts =
QueryParts.ByArith((range, limit, fetchSizeHint) =>
storageBackend.transactionsEventsOnlyWildcardParties(
startExclusive = range.startExclusive,
endInclusive = range.endInclusive,
parties = parties,
limit = limit,
fetchSizeHint = fetchSizeHint,
)
)

override protected def sameTemplates(
range: EventsRange[Long],
parties: Set[Party],
templateIds: Set[ApiIdentifier],
): QueryParts =
QueryParts.ByArith((range, limit, fetchSizeHint) =>
storageBackend.transactionsEventsSameTemplates(
startExclusive = range.startExclusive,
endInclusive = range.endInclusive,
parties = parties,
templateIds = templateIds,
limit = limit,
fetchSizeHint = fetchSizeHint,
)
)

override protected def mixedTemplates(
range: EventsRange[Long],
partiesAndTemplateIds: Set[(Party, ApiIdentifier)],
): QueryParts =
QueryParts.ByArith((range, limit, fetchSizeHint) =>
storageBackend.transactionsEventsMixedTemplates(
startExclusive = range.startExclusive,
endInclusive = range.endInclusive,
partiesAndTemplateIds = partiesAndTemplateIds,
limit = limit,
fetchSizeHint = fetchSizeHint,
)
)

override protected def mixedTemplatesWithWildcardParties(
range: EventsRange[Long],
wildcardParties: Set[Party],
partiesAndTemplateIds: Set[(Party, ApiIdentifier)],
): QueryParts = {
QueryParts.ByArith((range, limit, fetchSizeHint) =>
storageBackend.transactionsEventsMixedTemplatesWithWildcardParties(
startExclusive = range.startExclusive,
endInclusive = range.endInclusive,
partiesAndTemplateIds = partiesAndTemplateIds,
wildcardParties = wildcardParties,
limit = limit,
fetchSizeHint = fetchSizeHint,
storageBackend.transactionEvents(
rangeParams = RangeParams(
startExclusive = range.startExclusive,
endInclusive = range.endInclusive,
limit = limit,
fetchSizeHint = fetchSizeHint,
),
filterParams = filterParams,
)
)
}

override protected def offsetRange(offset: EventsRange[Long]) = offset
}
Expand All @@ -238,99 +131,20 @@ private[events] object EventsTableFlatEventsRangeQueries {
storageBackend: EventStorageBackend
) extends EventsTableFlatEventsRangeQueries[EventsRange[(Offset, Long)]] {

override protected def singleWildcardParty(
range: EventsRange[(Offset, Long)],
party: Party,
): QueryParts =
QueryParts.ByLimit(limit =>
storageBackend.activeContractsEventsSingleWildcardParty(
startExclusive = range.startExclusive._2,
endInclusiveSeq = range.endInclusive._2,
endInclusiveOffset = range.endInclusive._1,
party = party,
limit = Some(limit),
fetchSizeHint = Some(limit),
)
)

override protected def singlePartyWithTemplates(
range: EventsRange[(Offset, Long)],
party: Party,
templateIds: Set[ApiIdentifier],
): QueryParts =
QueryParts.ByLimit(limit =>
storageBackend.activeContractsEventsSinglePartyWithTemplates(
startExclusive = range.startExclusive._2,
endInclusiveSeq = range.endInclusive._2,
endInclusiveOffset = range.endInclusive._1,
party = party,
templateIds = templateIds,
limit = Some(limit),
fetchSizeHint = Some(limit),
)
)

override def onlyWildcardParties(
range: EventsRange[(Offset, Long)],
parties: Set[Party],
): QueryParts =
QueryParts.ByLimit(limit =>
storageBackend.activeContractsEventsOnlyWildcardParties(
startExclusive = range.startExclusive._2,
endInclusiveSeq = range.endInclusive._2,
endInclusiveOffset = range.endInclusive._1,
parties = parties,
limit = Some(limit),
fetchSizeHint = Some(limit),
)
)

override def sameTemplates(
range: EventsRange[(Offset, Long)],
parties: Set[Party],
templateIds: Set[ApiIdentifier],
): QueryParts =
QueryParts.ByLimit(limit =>
storageBackend.activeContractsEventsSameTemplates(
startExclusive = range.startExclusive._2,
endInclusiveSeq = range.endInclusive._2,
endInclusiveOffset = range.endInclusive._1,
parties = parties,
templateIds = templateIds,
limit = Some(limit),
fetchSizeHint = Some(limit),
)
)

override def mixedTemplates(
range: EventsRange[(Offset, Long)],
partiesAndTemplateIds: Set[(Party, ApiIdentifier)],
): QueryParts =
QueryParts.ByLimit(limit =>
storageBackend.activeContractsEventsMixedTemplates(
startExclusive = range.startExclusive._2,
endInclusiveSeq = range.endInclusive._2,
endInclusiveOffset = range.endInclusive._1,
partiesAndTemplateIds = partiesAndTemplateIds,
limit = Some(limit),
fetchSizeHint = Some(limit),
)
)

override def mixedTemplatesWithWildcardParties(
override protected def query(
range: EventsRange[(Offset, Long)],
wildcardParties: Set[Party],
partiesAndTemplateIds: Set[(Party, ApiIdentifier)],
filterParams: FilterParams,
): QueryParts =
QueryParts.ByLimit(limit =>
storageBackend.activeContractsEventsMixedTemplatesWithWildcardParties(
startExclusive = range.startExclusive._2,
endInclusiveSeq = range.endInclusive._2,
storageBackend.activeContractEvents(
rangeParams = RangeParams(
startExclusive = range.startExclusive._2,
endInclusive = range.endInclusive._2,
limit = Some(limit),
fetchSizeHint = Some(limit),
),
filterParams = filterParams,
endInclusiveOffset = range.endInclusive._1,
partiesAndTemplateIds = partiesAndTemplateIds,
wildcardParties = wildcardParties,
limit = Some(limit),
fetchSizeHint = Some(limit),
)
)

Expand Down
Loading

0 comments on commit 05e5218

Please sign in to comment.