|
| 1 | +/** |
| 2 | + * In-memory data store with indexed maps for O(1) lookups and |
| 3 | + * sorted insertion order for stable cursor-based pagination. |
| 4 | + * |
| 5 | + * Indexes maintained: |
| 6 | + * - byGlobalSeq: Map<global_seq, Policy> — primary cursor index |
| 7 | + * - byHolder: Map<holder, Policy[]> — filter by holder |
| 8 | + * - byComposite: Map<"holder:policy_id", Policy> — point lookup |
| 9 | + * |
| 10 | + * All writes go through `insertPolicy` / `upsertPolicy` to keep indexes |
| 11 | + * consistent. Concurrent inserts are safe because JS is single-threaded. |
| 12 | + */ |
| 13 | + |
| 14 | +import { Claim, Policy } from "../types/policy"; |
| 15 | + |
| 16 | +let _seq = 0; |
| 17 | + |
| 18 | +const byGlobalSeq = new Map<number, Policy>(); |
| 19 | +const byHolder = new Map<string, Policy[]>(); |
| 20 | +const byComposite = new Map<string, Policy>(); |
| 21 | + |
| 22 | +const claims = new Map<number, Claim>(); |
| 23 | +/** claim_id → policy composite key, for join without N+1 */ |
| 24 | +const claimsByComposite = new Map<string, Claim[]>(); |
| 25 | + |
| 26 | +function compositeKey(holder: string, policy_id: number): string { |
| 27 | + return `${holder}:${policy_id}`; |
| 28 | +} |
| 29 | + |
| 30 | +export function insertPolicy(p: Omit<Policy, "global_seq">): Policy { |
| 31 | + _seq += 1; |
| 32 | + const record: Policy = { ...p, global_seq: _seq }; |
| 33 | + const ck = compositeKey(p.holder, p.policy_id); |
| 34 | + |
| 35 | + byGlobalSeq.set(_seq, record); |
| 36 | + byComposite.set(ck, record); |
| 37 | + |
| 38 | + const holderList = byHolder.get(p.holder) ?? []; |
| 39 | + holderList.push(record); |
| 40 | + byHolder.set(p.holder, holderList); |
| 41 | + |
| 42 | + return record; |
| 43 | +} |
| 44 | + |
| 45 | +export function upsertPolicy(p: Omit<Policy, "global_seq">): Policy { |
| 46 | + const ck = compositeKey(p.holder, p.policy_id); |
| 47 | + const existing = byComposite.get(ck); |
| 48 | + if (existing) { |
| 49 | + // Update in-place, preserving global_seq for stable pagination |
| 50 | + Object.assign(existing, p); |
| 51 | + return existing; |
| 52 | + } |
| 53 | + return insertPolicy(p); |
| 54 | +} |
| 55 | + |
| 56 | +export function getPolicyByComposite( |
| 57 | + holder: string, |
| 58 | + policy_id: number |
| 59 | +): Policy | undefined { |
| 60 | + return byComposite.get(compositeKey(holder, policy_id)); |
| 61 | +} |
| 62 | + |
| 63 | +export function getPolicyBySeq(seq: number): Policy | undefined { |
| 64 | + return byGlobalSeq.get(seq); |
| 65 | +} |
| 66 | + |
| 67 | +/** Returns all policies in ascending global_seq order. */ |
| 68 | +export function allPoliciesOrdered(): Policy[] { |
| 69 | + return Array.from(byGlobalSeq.values()); |
| 70 | +} |
| 71 | + |
| 72 | +export function getPoliciesByHolder(holder: string): Policy[] { |
| 73 | + return byHolder.get(holder) ?? []; |
| 74 | +} |
| 75 | + |
| 76 | +// ── Claims ─────────────────────────────────────────────────────────────────── |
| 77 | + |
| 78 | +export function insertClaim(c: Claim): void { |
| 79 | + claims.set(c.claim_id, c); |
| 80 | + const ck = compositeKey(c.claimant, c.policy_id); |
| 81 | + const list = claimsByComposite.get(ck) ?? []; |
| 82 | + list.push(c); |
| 83 | + claimsByComposite.set(ck, list); |
| 84 | +} |
| 85 | + |
| 86 | +export function getClaimsByPolicy(holder: string, policy_id: number): Claim[] { |
| 87 | + return claimsByComposite.get(compositeKey(holder, policy_id)) ?? []; |
| 88 | +} |
| 89 | + |
| 90 | +/** Bulk-fetch claims for a list of (holder, policy_id) pairs — avoids N+1. */ |
| 91 | +export function getClaimsBatch( |
| 92 | + keys: Array<{ holder: string; policy_id: number }> |
| 93 | +): Map<string, Claim[]> { |
| 94 | + const result = new Map<string, Claim[]>(); |
| 95 | + for (const { holder, policy_id } of keys) { |
| 96 | + const ck = compositeKey(holder, policy_id); |
| 97 | + result.set(ck, claimsByComposite.get(ck) ?? []); |
| 98 | + } |
| 99 | + return result; |
| 100 | +} |
| 101 | + |
| 102 | +/** Reset store — used in tests only. */ |
| 103 | +export function _resetStore(): void { |
| 104 | + _seq = 0; |
| 105 | + byGlobalSeq.clear(); |
| 106 | + byHolder.clear(); |
| 107 | + byComposite.clear(); |
| 108 | + claims.clear(); |
| 109 | + claimsByComposite.clear(); |
| 110 | +} |
0 commit comments