Line data Source code
1 : /**
2 : * V2 Draft Cache - Pure functions for draft index manipulation.
3 : *
4 : * This module provides immutable operations on the draft index structure.
5 : * All functions return new objects rather than mutating inputs.
6 : */
7 :
8 : import type { DraftEntryMeta, DraftIndexV2 } from './draftTypes'
9 : import { MAX_DRAFTS } from './draftTypes'
10 : import { hashPath } from './hashUtil'
11 :
12 : /**
13 : * Creates an empty draft index.
14 : */
15 2256 : export function createEmptyIndex(): DraftIndexV2 {
16 2256 : return {
17 : v: 2,
18 : updatedAt: Date.now(),
19 : order: [],
20 : entries: {}
21 : }
22 : }
23 :
24 : /**
25 : * Moves a draft key to the end of the LRU order (most recently used).
26 : */
27 5606 : export function touchOrder(order: string[], draftKey: string): string[] {
28 : const filtered = order.filter((key) => key !== draftKey)
29 5606 : return [...filtered, draftKey]
30 : }
31 :
32 : /**
33 : * Adds or updates a draft entry in the index.
34 : * Handles LRU eviction if over the limit.
35 : *
36 : * @returns Object with updated index and list of evicted draft keys
37 : */
38 5524 : export function upsertEntry(
39 : index: DraftIndexV2,
40 : path: string,
41 : meta: Omit<DraftEntryMeta, 'path'>,
42 : limit: number = MAX_DRAFTS
43 : ): { index: DraftIndexV2; evicted: string[] } {
44 5524 : const draftKey = hashPath(path)
45 5524 : const effectiveLimit = Math.max(1, limit)
46 :
47 5524 : const entries = {
48 : ...index.entries,
49 : [draftKey]: { ...meta, path }
50 : }
51 :
52 5524 : const touchedOrder = touchOrder(index.order, draftKey)
53 5524 : const evicted: string[] = []
54 :
55 5524 : let evictCount = 0
56 5524 : while (touchedOrder.length - evictCount > effectiveLimit) {
57 0 : const oldest = touchedOrder[evictCount]
58 0 : if (oldest && oldest !== draftKey) {
59 0 : delete entries[oldest]
60 0 : evicted.push(oldest)
61 : }
62 0 : evictCount++
63 : }
64 :
65 5524 : const finalOrder = touchedOrder.slice(evictCount)
66 :
67 5524 : return {
68 : index: {
69 : v: 2,
70 : updatedAt: Date.now(),
71 : order: finalOrder,
72 : entries
73 : },
74 : evicted
75 : }
76 : }
77 :
78 : /**
79 : * Removes a draft entry from the index.
80 : *
81 : * @returns Object with updated index and the removed draft key (if any)
82 : */
83 326 : export function removeEntry(
84 : index: DraftIndexV2,
85 : path: string
86 : ): { index: DraftIndexV2; removedKey: string | null } {
87 326 : const draftKey = hashPath(path)
88 :
89 326 : if (!(draftKey in index.entries)) {
90 65 : return { index, removedKey: null }
91 : }
92 :
93 261 : const entries = { ...index.entries }
94 261 : delete entries[draftKey]
95 :
96 261 : return {
97 : index: {
98 : v: 2,
99 : updatedAt: Date.now(),
100 : order: index.order.filter((key) => key !== draftKey),
101 : entries
102 : },
103 : removedKey: draftKey
104 : }
105 : }
106 :
107 : /**
108 : * Moves a draft from one path to another (rename operation).
109 : * Updates index recency only; callers keep the payload timestamp unchanged.
110 : *
111 : * @returns Object with updated index and keys involved
112 : */
113 100 : export function moveEntry(
114 : index: DraftIndexV2,
115 : oldPath: string,
116 : newPath: string,
117 : newName: string
118 : ): { index: DraftIndexV2; oldKey: string; newKey: string } | null {
119 100 : const oldKey = hashPath(oldPath)
120 100 : const newKey = hashPath(newPath)
121 :
122 100 : const entriesByKey: Partial<Record<string, DraftEntryMeta>> = index.entries
123 100 : const oldEntry = entriesByKey[oldKey]
124 100 : if (!oldEntry) return null
125 100 : if (oldKey !== newKey && entriesByKey[newKey]) return null
126 :
127 2 : const entries = { ...index.entries }
128 2 : delete entries[oldKey]
129 :
130 2 : entries[newKey] = {
131 : ...oldEntry,
132 : path: newPath,
133 : name: newName,
134 : updatedAt: Date.now()
135 : }
136 :
137 2 : const order = index.order
138 : .filter((key) => key !== oldKey && key !== newKey)
139 : .concat(newKey)
140 :
141 2 : return {
142 : index: {
143 : v: 2,
144 : updatedAt: Date.now(),
145 : order,
146 : entries
147 : },
148 : oldKey,
149 : newKey
150 : }
151 : }
152 :
153 : /**
154 : * Gets the most recently used draft key.
155 : */
156 2295 : export function getMostRecentKey(index: DraftIndexV2): string | null {
157 2295 : return index.order.length > 0 ? index.order[index.order.length - 1] : null
158 : }
159 :
160 : /**
161 : * Gets entry metadata by path.
162 : */
163 266700 : export function getEntryByPath(
164 : index: DraftIndexV2,
165 : path: string
166 : ): DraftEntryMeta | null {
167 266700 : const draftKey = hashPath(path)
168 266700 : return index.entries[draftKey] ?? null
169 : }
170 :
171 : /**
172 : * Removes entries from index that don't have corresponding payloads.
173 : * Used for index/payload drift recovery.
174 : *
175 : * @param index - The draft index
176 : * @param existingPayloadKeys - Set of draft keys that have payloads in storage
177 : * @returns Updated index with orphaned entries removed
178 : */
179 98 : export function removeOrphanedEntries(
180 : index: DraftIndexV2,
181 : existingPayloadKeys: Set<string>
182 : ): DraftIndexV2 {
183 98 : const entries: Record<string, DraftEntryMeta> = {}
184 98 : const order: string[] = []
185 98 : const entriesByKey: Partial<Record<string, DraftEntryMeta>> = index.entries
186 :
187 98 : for (const key of index.order) {
188 160 : const entry = entriesByKey[key]
189 160 : if (existingPayloadKeys.has(key) && entry) {
190 160 : entries[key] = entry
191 160 : order.push(key)
192 : }
193 : }
194 :
195 98 : return {
196 : v: 2,
197 : updatedAt: Date.now(),
198 : order,
199 : entries
200 : }
201 : }
|