kvutils: Make migration to the new key and value types easier. (#8483)

* kvutils: Make migration to the new key and value types easier.

CHANGELOG_BEGIN
CHANGELOG_END

* kvutils: Extract out common implicit conversions into a trait.
This commit is contained in:
Samir Talwar 2021-01-13 15:38:33 +01:00 committed by GitHub
parent e6d1f1399b
commit 544b0a2caa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 5 deletions

View File

@ -65,10 +65,10 @@ object Queries {
}
implicit val columnToRawKey: Column[Raw.Key] =
columnToByteString.map(Raw.Key)
columnToByteString.map(Raw.Key.apply)
implicit val columnToRawValue: Column[Raw.Value] =
columnToByteString.map(Raw.Value)
columnToByteString.map(Raw.Value.apply)
def rawKey(columnName: String): RowParser[Raw.Key] =
SqlParser.get(columnName)(columnToRawKey)

View File

@ -5,20 +5,62 @@ package com.daml.ledger.participant.state.kvutils
import com.google.protobuf.ByteString
import scala.language.implicitConversions
object Raw {
sealed trait Bytes {
trait Bytes {
def bytes: ByteString
final def size: Long = bytes.size.toLong
}
object Bytes {
/** This implicit conversion exists to aid in migration.
* It will be deprecated and subsequently removed in the future.
*/
implicit def `ByteString to Raw.Bytes`(bytes: ByteString): Bytes = Unknown(bytes)
/** This implicit conversion exists to aid in migration.
* It will be deprecated and subsequently removed in the future.
*/
implicit def `Raw.Bytes to ByteString`(raw: Bytes): ByteString = raw.bytes
}
trait Companion[Self] {
def apply(bytes: ByteString): Self
/** This implicit conversion exists to aid in migration.
* It will be deprecated and subsequently removed in the future.
*/
implicit def `ByteString to Self`(bytes: ByteString): Self = apply(bytes)
/** This implicit conversion exists to aid in migration.
* It will be deprecated and subsequently removed in the future.
*/
implicit def `Raw.Bytes to Self`(rawBytes: Bytes): Self = apply(rawBytes.bytes)
}
/** This case class only exists to preserve some functionality of
* [[com.daml.ledger.participant.state.kvutils.Bytes]].
*
* It will be removed in the future.
*/
private final case class Unknown(override val bytes: ByteString) extends Bytes
final case class Key(override val bytes: ByteString) extends Bytes
object Key extends Companion[Key] {
implicit val `Key Ordering`: Ordering[Key] = Ordering.by(_.bytes.asReadOnlyByteBuffer)
}
final case class Value(override val bytes: ByteString) extends Bytes
object Value extends Companion[Value]
type KeyValuePair = (Key, Value)
implicit val `Key Ordering`: Ordering[Key] = Ordering.by(_.bytes.asReadOnlyByteBuffer)
}

View File

@ -28,6 +28,11 @@ import com.daml.metrics.MetricName
*/
package object kvutils {
/** Alias for [[Raw.Bytes]] to aid in migration.
* It will be deprecated and subsequently removed in the future.
*/
type Bytes = Raw.Bytes
type DamlStateMap = Map[DamlStateKey, Option[DamlStateValue]]
type CorrelationId = String

View File

@ -74,6 +74,20 @@ trait LedgerStateOperations[+LogResult] {
)(implicit executionContext: ExecutionContext): Future[LogResult]
}
object LedgerStateOperations {
/** Alias for [[Raw.Key]] to aid in migration.
* It will be deprecated and subsequently removed in the future.
*/
type Key = Raw.Key
/** Alias for [[Raw.Value]] to aid in migration.
* It will be deprecated and subsequently removed in the future.
*/
type Value = Raw.Value
}
/** Convenience class for implementing read and write operations on a backing store that supports batched operations.
*/
abstract class BatchingLedgerStateOperations[LogResult] extends LedgerStateOperations[LogResult] {