def byte_batch.measure.loop

mylsm-lsm-store@0.6.0.1 · src/DurableBatchPolicy.bend:69

import mylsm-lsm-store@0.6.0.1/src/DurableBatchPolicy.bend as DurableBatchPolicy
import Base
import mylsm-lsm-store@0.6.0.1/src/ByteWal.bend as ByteWal
import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes

69# Advances a bounded no-copy byte-batch size pass.
70def byte_batch.measure.loop(
71  fuel: Nat,
72  state: ByteBatchMeasureState
73) -> ByteBatchMeasurement:
74  match fuel:
75    case 0n:
76      match state:
77        case MeasureNeed{Nil{}, reversed, count, record_bytes}:
78          BatchMeasured{List.reverse(&1, ByteWal.Mutation, reversed), count,
79            frame_bytes(record_bytes)}
80        case MeasureNeed{Con{_, _}, _, _, _}: BatchTooMany{}
81    case 1n+rest:
82      match state:
83        case MeasureNeed{Nil{}, reversed, count, record_bytes}:
84          BatchMeasured{List.reverse(&1, ByteWal.Mutation, reversed), count,
85            frame_bytes(record_bytes)}
86        case MeasureNeed{Con{ByteWal.Delete{key}, tail}, reversed, count,
87            record_bytes}:
88          match key:
89            case Bytes.Bytes{+key_len, key_buffer}:
90              byte_batch.measure.loop(rest, MeasureNeed{tail,
91                Con{ByteWal.Delete{Bytes.Bytes{key_len, key_buffer}}, reversed},
92                Nat.add(count, 1n),
93                Nat.add(record_bytes,
94                  delete_record_bytes(U32.to_nat(key_len)))})
95        case MeasureNeed{Con{ByteWal.Put{key, value}, tail}, reversed, count,
96            record_bytes}:
97          match key:
98            case Bytes.Bytes{+key_len, key_buffer}:
99              match value:
100                case Bytes.Bytes{+value_len, value_buffer}:
101                  byte_batch.measure.loop(rest, MeasureNeed{tail,
102                    Con{ByteWal.Put{Bytes.Bytes{key_len, key_buffer},
103                      Bytes.Bytes{value_len, value_buffer}}, reversed},
104                    Nat.add(count, 1n), Nat.add(record_bytes,
105                      put_record_bytes(U32.to_nat(key_len),
106                        U32.to_nat(value_len)))})

Uses (12)

Used by (1)