|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package applicationpb |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/asn1" |
| 11 | + |
| 12 | + "github.com/cockroachdb/errors" |
| 13 | +) |
| 14 | + |
| 15 | +// ASN1Marshal marshals a transactions for a given namespace index. |
| 16 | +// It uses the schema described in asn1_tx_schema.asn. |
| 17 | +func (ns *TxNamespace) ASN1Marshal(txID string) ([]byte, error) { |
| 18 | + ret, err := asn1.Marshal(*ns.translate(txID)) |
| 19 | + return ret, errors.Wrap(err, "failed to marshal tx namespace") |
| 20 | +} |
| 21 | + |
| 22 | +// translate translates a [TxNamespace] to a stab struct for asn1_tx_schema.asn. |
| 23 | +// Any change to [TxNamespace] requires a change to this method. |
| 24 | +func (ns *TxNamespace) translate(txID string) *asn1Namespace { |
| 25 | + n := asn1Namespace{ |
| 26 | + TxID: txID, |
| 27 | + NamespaceID: ns.NsId, |
| 28 | + NamespaceVersion: protoToAsnVersion(&ns.NsVersion), |
| 29 | + ReadsOnly: make([]asn1Read, len(ns.ReadsOnly)), |
| 30 | + ReadWrites: make([]asn1ReadWrite, len(ns.ReadWrites)), |
| 31 | + BlindWrites: make([]asn1Write, len(ns.BlindWrites)), |
| 32 | + } |
| 33 | + for i, r := range ns.ReadsOnly { |
| 34 | + n.ReadsOnly[i] = asn1Read{ |
| 35 | + Key: r.Key, |
| 36 | + Version: protoToAsnVersion(r.Version), |
| 37 | + } |
| 38 | + } |
| 39 | + for i, rw := range ns.ReadWrites { |
| 40 | + n.ReadWrites[i] = asn1ReadWrite{ |
| 41 | + Key: rw.Key, |
| 42 | + Version: protoToAsnVersion(rw.Version), |
| 43 | + Value: rw.Value, |
| 44 | + } |
| 45 | + } |
| 46 | + for i, w := range ns.BlindWrites { |
| 47 | + n.BlindWrites[i] = asn1Write{ |
| 48 | + Key: w.Key, |
| 49 | + Value: w.Value, |
| 50 | + } |
| 51 | + } |
| 52 | + return &n |
| 53 | +} |
| 54 | + |
| 55 | +// protoToAsnVersion converts the proto version to ASN.1 version. |
| 56 | +// ASN.1 uses -1 to encode nil version. |
| 57 | +func protoToAsnVersion(ver *uint64) int64 { |
| 58 | + if ver == nil { |
| 59 | + return -1 |
| 60 | + } |
| 61 | + return int64(*ver) //nolint:gosec // ASN.1 does not support unsigned numbers. |
| 62 | +} |
| 63 | + |
| 64 | +// asnToProtoVersion converts the ASN.1 version to proto version. |
| 65 | +// ASN.1 uses -1 to encode nil version. |
| 66 | +func asnToProtoVersion(ver int64) *uint64 { |
| 67 | + if ver < 0 { |
| 68 | + return nil |
| 69 | + } |
| 70 | + protoVer := uint64(ver) |
| 71 | + return &protoVer |
| 72 | +} |
| 73 | + |
| 74 | +type ( |
| 75 | + // asn1Namespace is a stab for [Tx] and [TxNamespace]. |
| 76 | + // Any change to these protobuf requires a change to these structures. |
| 77 | + // It conforms with asn1_tx_schema.asn. |
| 78 | + // We force the ASN.1 library to use UTF8 strings to avoid incompatibility with the schema. |
| 79 | + // If not specified, the library choose to use ASCII (PrintableString) for simple strings, |
| 80 | + // and UTF8 otherwise. |
| 81 | + asn1Namespace struct { |
| 82 | + TxID string `asn1:"utf8"` |
| 83 | + NamespaceID string `asn1:"utf8"` |
| 84 | + NamespaceVersion int64 |
| 85 | + ReadsOnly []asn1Read |
| 86 | + ReadWrites []asn1ReadWrite |
| 87 | + BlindWrites []asn1Write |
| 88 | + } |
| 89 | + // asn1Read is a stab for [Read]. |
| 90 | + // Any change to this protobuf requires a change to these structures. |
| 91 | + asn1Read struct { |
| 92 | + Key []byte |
| 93 | + Version int64 `asn1:"optional,default:-1"` |
| 94 | + } |
| 95 | + // asn1ReadWrite is a stab for [ReadWrite]. |
| 96 | + // Any change to this protobuf requires a change to these structures. |
| 97 | + asn1ReadWrite struct { |
| 98 | + Key []byte |
| 99 | + Value []byte |
| 100 | + Version int64 `asn1:"optional,default:-1"` |
| 101 | + } |
| 102 | + // asn1Write is a stab for [Write]. |
| 103 | + // Any change to this protobuf requires a change to these structures. |
| 104 | + asn1Write struct { |
| 105 | + Key []byte |
| 106 | + Value []byte |
| 107 | + } |
| 108 | +) |
0 commit comments