def batch.stats.loop

mylsm-lsm-store@0.6.0.1 · src/ByteDurableDb.bend:1656

import mylsm-lsm-store@0.6.0.1/src/ByteDurableDb.bend as ByteDurableDb
import Base
import mylsm-lsm-store@0.6.0.1/src/ByteMemTable.bend as ByteMemTable
import mylsm-lsm-store@0.6.0.1/src/scan/VisibleStats.bend as VisibleStats
import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes

1656def batch.stats.loop(fuel: Nat, state: BatchStatsState) -> IO(Handle & Result<&1, &1, Error, BatchPrepared>):
1657  match fuel:
1658    case 0n: batch.stats.exhausted(state)
1659    case 1n+rest:
1660      match state:
1661        case BatchNeed{handle, mutations, entries, original_stats, base_stats}:
1662          match handle:
1663            case Handle{path, lock, table, state, _}:
1664              match entries:
1665                case Nil{}:
1666                  IO.pure(Handle & Result<&1, &1, Error, BatchPrepared>,
1667                    (Handle{path, lock, table, state, original_stats},
1668                      Done{Prepared{mutations, base_stats}}))
1669                case Con{ByteMemTable.Entry{key, _}, tail}:
1670                  batch.stats.loop(rest, BatchKey{
1671                    Handle{path, lock, table, state, VisibleStats.Stats{0n, 0n}},
1672                    mutations, tail, original_stats, base_stats, batch.keys(key)})
1673        case BatchKey{handle, mutations, entries, original_stats, base_stats, keys}:
1674          match handle:
1675            case Handle{path, lock, table, state, _}:
1676              match keys:
1677                case (probe_key, (read_key, stats_key)):
1678                  match stats_key:
1679                    case Bytes.Bytes{+key_len, _}:
1680                      batch.stats.loop(rest, BatchProbe{
1681                        Handle{path, lock, ByteMemTable.empty(), state,
1682                          VisibleStats.Stats{0n, 0n}},
1683                        mutations, entries, original_stats, base_stats,
1684                        U32.to_nat(key_len), read_key,
1685                        ByteMemTable.probe(table, probe_key)})
1686        case BatchProbe{handle, mutations, entries, original_stats,
1687            base_stats, key_bytes, key, result}:
1688          match handle:
1689            case Handle{path, lock, _, state, _}:
1690              match result:
1691                case (table, ByteMemTable.Absent{}):
1692                  do IO<Handle & Result<&1, &1, Error, BatchPrepared>>:
1693                    pair : Handle & Result<&1, &1, Error, Maybe<&1, Bytes.Bytes>> <-
1694                      get(Handle{path, lock, table, state,
1695                        VisibleStats.Stats{0n, 0n}}, key)
1696                    batch.stats.loop(rest, BatchRead{pair, mutations, entries,
1697                      original_stats, base_stats, key_bytes})
1698                case (table, _):
1699                  batch.stats.loop(rest, BatchNeed{
1700                    Handle{path, lock, table, state, VisibleStats.Stats{0n, 0n}},
1701                    mutations, entries, original_stats, base_stats})
1702        case BatchRead{pair, mutations, entries, original_stats, base_stats, key_bytes}:
1703          match pair:
1704            case (handle, Fail{error}):
1705              IO.pure(Handle & Result<&1, &1, Error, BatchPrepared>,
1706                (batch.restore.handle(original_stats, handle), Fail{error}))
1707            case (Handle{path, lock, table, state, _}, Done{old_value}):
1708              batch.stats.loop(rest, BatchNeed{
1709                Handle{path, lock, table, state, VisibleStats.Stats{0n, 0n}},
1710                mutations, entries, original_stats,
1711                stats.remove.base.length(base_stats, key_bytes, old_value)})

Uses (25)

Used by (1)