Skip to content
Myr

memory.ts

This file

Path

packages/project/src/project-store/memory.ts

238 lines · 6.6 KiB · TypeScript

Last changed by

Merge observability into mainoscabrielbefore adoption1985111

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–161why, one step back
1import { Array, Effect, Layer, Option, Order, SynchronizedRef } from "effect";
23import {4 keptEvents,5 ProjectStore,6 snapshotLandings,7} from "../project-store.ts";8import type {9 Committed,10 ProjectSnapshot,11 ProjectWrites,12} from "../project-store.ts";13import type { EventRow, LedgerRow } from "./rows.ts";14import {15 EventTable,16 LayerReadTable,17 ProjectTable,18 QueueTable,19 Tables,20 writeAll,21} from "./tables.ts";22import type { Column, Table, Upsert } from "./tables.ts";2324interface Stored {25 readonly body: string;26 readonly columns: Readonly<Record<string, Column>>;27}2829type Rows = ReadonlyMap<string, Stored>;3031interface State {32 readonly lastRead: number;33 readonly lastSeq: number;34 readonly tables: ReadonlyMap<string, Rows>;35}3637const keyOf = <A>(table: Table<A>, columns: Stored["columns"]) =>38 JSON.stringify(table.key.map((name) => columns[name]));3940const rowsIn = <A>(state: State, table: Table<A>): Rows =>41 state.tables.get(table.name) ?? new Map();4243const decodeAll = <A>(table: Table<A>, stored: Iterable<Stored>) =>44 Effect.forEach(stored, (row) => table.decode(row.body));4546const all = <A>(state: State, table: Table<A>) =>47 decodeAll(table, rowsIn(state, table).values());4849type Placed = readonly [index: number, row: Stored];5051const newestFirst = Order.combine(52 Order.mapInput(Order.flip(Order.Number), ([, row]: Placed) =>53 Number(row.columns.landed_at)54 ),55 Order.mapInput(Order.flip(Order.Number), ([index]: Placed) => index)56);5758const bySeq = Order.mapInput(Order.Number, (entry: LedgerRow) => entry.seq);5960const snapshotOf = (state: State) =>61 Effect.gen(function* () {62 const placed = Array.map(63 [...rowsIn(state, Tables.landings).values()],64 (row, index): Placed => [index, row]65 );6667 const newest = Array.take(68 Array.sort(placed, newestFirst),69 snapshotLandings70 );7172 return {73 answers: yield* all(state, Tables.answers),74 asks: yield* all(state, Tables.asks),75 attempts: yield* all(state, Tables.attempts),76 budgetDays: yield* all(state, Tables.budgetDays),77 claims: yield* all(state, Tables.claims),78 decisions: yield* all(state, Tables.decisions),79 envValues: yield* all(state, Tables.envValues),80 landings: yield* decodeAll(81 Tables.landings,82 Array.map(newest, ([, row]) => row)83 ),84 lastSeq: state.lastSeq,85 layers: yield* all(state, Tables.layers),86 ledger: Array.sort(yield* all(state, Tables.ledger), bySeq),87 outbox: yield* all(state, Tables.outbox),88 project: Array.head(yield* all(state, ProjectTable)),89 proposals: yield* all(state, Tables.proposals),90 queue: Option.getOrNull(Array.head(yield* all(state, QueueTable))),91 ratchets: yield* all(state, Tables.ratchets),92 } satisfies ProjectSnapshot;93 });9495const seenIn = (state: State, writes: ProjectWrites) => {96 const seen = rowsIn(state, Tables.pushEvents);9798 return Array.dedupe(99 Array.flatMap(writes.pushEvents ?? [], (event) =>100 seen.has(keyOf(Tables.pushEvents, { event_id: event.eventId }))101 ? [event.eventId]102 : []103 )104 );105};106107/** Writes rows into copies of `tables`' maps, never into the maps themselves. */108const upsertInto =109 (tables: Map<string, Rows>): Upsert<never, never> =>110 (table, rows) =>111 Effect.gen(function* () {112 const kept = new Map(tables.get(table.name));113114 for (const row of rows) {115 const columns = table.columns(row);116117 kept.set(keyOf(table, columns), {118 body: yield* table.encode(row),119 columns,120 });121 }122123 tables.set(table.name, kept);124 });125126const written = (state: State, writes: ProjectWrites) =>127 Effect.gen(function* () {128 const tables = new Map(state.tables);129130 const seqs = yield* writeAll(131 writes,132 { read: state.lastRead, seq: state.lastSeq },133 upsertInto(tables)134 );135136 const events = [...(tables.get(EventTable.name) ?? [])];137138 tables.set(EventTable.name, new Map(events.slice(-keptEvents)));139140 const committed: Committed = { duplicatePushEvents: [], seqs };141142 const next: State = {143 lastRead: state.lastRead + (writes.layerReads?.length ?? 0),144 lastSeq: state.lastSeq + seqs.length,145 tables,146 };147148 return [committed, next] as const;149 });150151export const ProjectStoreMemory = Layer.effect(152 ProjectStore,153 Effect.gen(function* () {154 const ref = yield* SynchronizedRef.make<State>({155 lastRead: 0,156 lastSeq: 0,157 tables: new Map(),158 });159160 return ProjectStore.of({161 ackOutbox: (ids) =>

Merge observability into mainoscabrielbefore adoption1985111

line 162why, one step back
162 SynchronizedRef.modify(ref, (state) => {

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

line 163why, one step back
163 const kept = new Map(rowsIn(state, Tables.outbox));

Merge observability into mainoscabrielbefore adoption1985111

line 164why, one step back
164 const taken: string[] = [];

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 165–166why, one step back
165
166 for (const id of ids) {

Merge observability into mainoscabrielbefore adoption1985111

lines 167–169why, one step back
167 if (kept.delete(keyOf(Tables.outbox, { id }))) {
168 taken.push(id);169 }

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 170–171why, one step back
170 }
171

Merge observability into mainoscabrielbefore adoption1985111

lines 172–178why, one step back
172 return [
173 taken,174 {175 ...state,176 tables: new Map(state.tables).set(Tables.outbox.name, kept),177 },178 ] as const;

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 179–217why, one step back
179 }),
180 commit: (writes) =>181 SynchronizedRef.modifyEffect(ref, (state) => {182 const duplicatePushEvents = seenIn(state, writes);183184 if (duplicatePushEvents.length > 0) {185 const replay: Committed = { duplicatePushEvents, seqs: [] };186187 return Effect.succeed([replay, state] as const);188 }189190 return written(state, writes);191 }),192 eventsSince: (seq, limit) =>193 Effect.gen(function* () {194 const events = yield* all(195 yield* SynchronizedRef.get(ref),196 EventTable197 );198199 return Array.take(200 Array.flatMap(events, (event): readonly EventRow[] =>201 event.seq > seq ? [{ event, seq: event.seq }] : []202 ),203 limit204 );205 }),206 landing: (sha) =>207 Effect.gen(function* () {208 const state = yield* SynchronizedRef.get(ref);209210 const row = rowsIn(state, Tables.landings).get(211 keyOf(Tables.landings, { sha })212 );213214 return row === undefined215 ? Option.none()216 : Option.some(yield* Tables.landings.decode(row.body));217 }),

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 218–222why, one step back
218 landingCount: () =>
219 Effect.map(220 SynchronizedRef.get(ref),221 (state) => rowsIn(state, Tables.landings).size222 ),

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 223–238why, one step back
223 layerReadsSince: (seq, limit) =>
224 Effect.gen(function* () {225 const reads = yield* all(226 yield* SynchronizedRef.get(ref),227 LayerReadTable228 );229230 return Array.take(231 reads.filter((read) => read.seq > seq),232 limit233 );234 }),235 load: () => Effect.flatMap(SynchronizedRef.get(ref), snapshotOf),236 });237 })238);