| 1 | import { Cursor, Reader } from './cursor.js'; |
| 2 | import { bytesToUlid, ulidToBytes |
| 3 | import { WalCorruptError } |
| 4 | import { |
| 5 | OID_LENGTH, |
| 6 | PACK_SHA_LENGTH, |
| 7 | type RefTransition, |
| 8 | type WalEntryHeader, |
| 9 | type WalIndex, |
| 10 | type WalLayer, |
| 11 | } from './wal.types.js'; |
| 12 | |
| 13 | // ASCII "GWAL" and "GENT". Distinct per format so an entry read as an index fails at byte 0 instead of passing the version check and decoding garbage. |
| 14 | const INDEX_MAGIC = 0x4757414c |
| 15 | const ENTRY_MAGIC = 0x47454e54 |
| 16 | const FORMAT_VERSION = 1 |
| 17 | const ULID_BYTES = 16 |
| 18 | const FLAG_DELETED = 0b1 |
| 19 | |
| 20 | export const INDEX_CONTENT_TYPE = |
| 21 | export const ENTRY_CONTENT_TYPE = |
| 22 | |
| 23 | /** Enough to cover any plausible header, for ranged metadata reads. */ |
| 24 | export const ENTRY_HEADER_PROBE_BYTES = |
| 25 | |
| 26 | export function encodeIndex(index |
| 27 | let size = |
| 28 | for (const ref |
| 29 | size += 2 + |
| 30 | } |
| 31 | size += 4 + |
| 32 | |
| 33 | const cursor = |
| 34 | cursor.u32(INDEX_MAGIC |
| 35 | cursor.u8(FORMAT_VERSION |
| 36 | cursor.u8(index |
| 37 | cursor.u64(index |
| 38 | cursor.u64(index |
| 39 | |
| 40 | cursor.u32(index |
| 41 | for (const [ref |
| 42 | cursor.str(ref |
| 43 | cursor.bytes(oid |
| 44 | } |
| 45 | |
| 46 | cursor.u32(index |
| 47 | for (const layer |
| 48 | cursor.bytes(ulidToBytes |
| 49 | cursor.bytes(layer |
| 50 | cursor.u64(layer |
| 51 | } |
| 52 | |
| 53 | return cursor.buffer |
| 54 | } |
| 55 | |
| 56 | export function decodeIndex(buffer |
| 57 | const reader = |
| 58 | if (reader.u32 |
| 59 | throw new WalCorruptError( |
| 60 | |
| 61 | const version = |
| 62 | if (version !== |
| 63 | throw new WalCorruptError( |
| 64 | } |
| 65 | const flags = |
| 66 | |
| 67 | const seq = |
| 68 | const compactedThroughSeq = |
| 69 | |
| 70 | const refs = |
| 71 | for (let remaining |
| 72 | refs.set(reader |
| 73 | } |
| 74 | |
| 75 | const layers: |
| 76 | for (let remaining |
| 77 | layers.push({ |
| 78 | ulid: bytesToUlid(reader |
| 79 | packSha: reader.bytes |
| 80 | size: reader.u64 |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | return { |
| 85 | seq, |
| 86 | compactedThroughSeq, |
| 87 | refs, |
| 88 | layers, |
| 89 | deleted: (flags & |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | export function encodeEntryHeader(header |
| 94 | const pushedBy = |
| 95 | let size = |
| 96 | 4 + 1 + 4 |
| 97 | for (const transition |
| 98 | size += 2 + |
| 99 | } |
| 100 | |
| 101 | const cursor = |
| 102 | cursor.u32(ENTRY_MAGIC |
| 103 | cursor.u8(FORMAT_VERSION |
| 104 | cursor.u32(size |
| 105 | cursor.bytes(ulidToBytes |
| 106 | cursor.u64(header |
| 107 | cursor.str(pushedBy |
| 108 | cursor.u32(header |
| 109 | for (const transition |
| 110 | cursor.str(transition |
| 111 | cursor.bytes(transition |
| 112 | cursor.bytes(transition |
| 113 | } |
| 114 | |
| 115 | return cursor.buffer |
| 116 | } |
| 117 | |
| 118 | export function decodeEntryHeader(buffer |
| 119 | header: WalEntryHeader |
| 120 | packOffset: number; |
| 121 | } { |
| 122 | const reader = |
| 123 | if (reader.u32 |
| 124 | throw new WalCorruptError( |
| 125 | |
| 126 | const version = |
| 127 | if (version !== |
| 128 | throw new WalCorruptError( |
| 129 | } |
| 130 | |
| 131 | const packOffset = |
| 132 | if (packOffset > |
| 133 | throw new WalCorruptError( |
| 134 | } |
| 135 | |
| 136 | const ulid = |
| 137 | const createdAt = |
| 138 | const pushedBy = |
| 139 | |
| 140 | const transitions: |
| 141 | for (let remaining |
| 142 | transitions.push({ |
| 143 | ref: reader.str |
| 144 | oldOid: reader.bytes |
| 145 | newOid: reader.bytes |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | return { |
| 150 | header: { ulid, |
| 151 | packOffset, |
| 152 | }; |
| 153 | } |