Remove SqlSequence, cleanup code (#10042)

changelog_begin
changelog_end
This commit is contained in:
Marton Nagy 2021-06-17 10:14:58 +02:00 committed by GitHub
parent 8ea240f72a
commit 591176ccb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 69 deletions

View File

@ -68,25 +68,19 @@ private[events] object EventsRange {
) => Connection => Vector[A], // takes range, limit, fetchSize hint
range: EventsRange[Long],
pageSize: Int,
): SqlSequence[Vector[A]] = {
): Connection => Vector[A] = connection => {
val minPageSize = 10 min pageSize max (pageSize / 10)
val guessedPageEnd = range.endInclusive min (range.startExclusive + pageSize)
SqlSequence
.plainQuery(read(range copy (endInclusive = guessedPageEnd), None, Some(pageSize)))
.flatMap { arithPage =>
val found = arithPage.size
if (guessedPageEnd == range.endInclusive || found >= minPageSize)
SqlSequence point arithPage
else
SqlSequence
.plainQuery(
read(
range copy (startExclusive = guessedPageEnd),
Some(minPageSize - found),
Some(minPageSize - found),
)
)
.map(arithPage ++ _)
}
val arithPage =
read(range copy (endInclusive = guessedPageEnd), None, Some(pageSize))(connection)
val found = arithPage.size
if (guessedPageEnd == range.endInclusive || found >= minPageSize)
arithPage
else
arithPage ++ read(
range copy (startExclusive = guessedPageEnd),
Some(minPageSize - found),
Some(minPageSize - found),
)(connection)
}
}

View File

@ -51,7 +51,7 @@ private[events] sealed abstract class EventsTableFlatEventsRangeQueries[Offset]
offset: Offset,
filter: FilterRelation,
pageSize: Int,
): SqlSequence[Vector[EventsTable.Entry[Raw.FlatEvent]]] = {
): Connection => Vector[EventsTable.Entry[Raw.FlatEvent]] = {
require(filter.nonEmpty, "The request must be issued by at least one party")
// Route the request to the correct underlying query
@ -111,7 +111,7 @@ private[events] sealed abstract class EventsTableFlatEventsRangeQueries[Offset]
pageSize,
)
case QueryParts.ByLimit(sql) =>
SqlSequence.plainQuery(sql(pageSize))
sql(pageSize)
}
}
}

View File

@ -1,44 +0,0 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.platform.store.appendonlydao.events
import scalaz.{-\/, Free, Functor, \/-}
import java.sql.Connection
object SqlSequence {
/** A sequence of `SimpleSql`s, terminating in A. */
type T[A] = Free[Element, A]
// this representation is just trampolined Reader, but exposing that
// would be unsound because it is _not_ distributive, and anyway
// we may want to make the representation more explicit for more complex
// analysis (e.g. applying polymorphic transforms to all contained SimpleSqls...)
final class Element[+A] private[SqlSequence] (private[SqlSequence] val run: Connection => A)
object Element {
implicit final class syntax[A](private val self: T[A]) extends AnyVal {
def executeSql(implicit conn: Connection): A = {
@annotation.tailrec
def go(self: T[A]): A =
self.resume match {
case -\/(elt) => go(elt.run(conn))
case \/-(a) => a
}
go(self)
}
}
implicit val covariant: Functor[Element] = new Functor[Element] {
override def map[A, B](fa: Element[A])(f: A => B) = new Element(run = fa.run andThen f)
}
}
// TODO append-only: This probably defeats the purpose, only needed to limit the impact of the SQL query abstraction change. Consider to remove SqlSequence.
def plainQuery[A](query: Connection => A): T[A] =
Free liftF new Element(query)
def point[A](a: A): T[A] = Free point a
}

View File

@ -107,7 +107,7 @@ private[appendonlydao] final class TransactionsReader(
EventsRange(range.startExclusive._2, range.endInclusive._2),
filter,
pageSize,
).executeSql,
)(connection),
range.startExclusive._1,
pruned =>
s"Transactions request from ${range.startExclusive._1.toHexString} to ${range.endInclusive._1.toHexString} precedes pruned offset ${pruned.toHexString}",
@ -210,7 +210,7 @@ private[appendonlydao] final class TransactionsReader(
range = EventsRange(range.startExclusive._2, range.endInclusive._2),
pageSize = pageSize,
),
).executeSql,
)(connection),
range.startExclusive._1,
pruned =>
s"Transactions request from ${range.startExclusive._1.toHexString} to ${range.endInclusive._1.toHexString} precedes pruned offset ${pruned.toHexString}",
@ -359,7 +359,7 @@ private[appendonlydao] final class TransactionsReader(
range,
filter,
pageSize,
).executeSql,
)(connection),
activeAt,
pruned =>
s"Active contracts request after ${activeAt.toHexString} precedes pruned offset ${pruned.toHexString}",

View File

@ -11,8 +11,6 @@ import com.daml.lf.transaction.Node.KeyWithMaintainers
*/
package object events {
type SqlSequence[A] = SqlSequence.T[A]
import com.daml.lf.value.{Value => lfval}
type ContractId = lfval.ContractId
val ContractId = com.daml.lf.value.Value.ContractId