0 | ||| Log-Structured Merge RRB Vector (LSMRRBVector)
35 | %hide Control.Monad.Elin.Elin.(.run)
36 | %hide Control.Monad.Elin.Elin.run
37 | %hide Prelude.null
38 | %hide Prelude.Ops.infixr.(<|)
39 | %hide Prelude.Ops.infixl.(|>)
43 | --------------------------------------------------------------------------------
44 | -- Mutation Operations
45 | --------------------------------------------------------------------------------
47 | ||| Appends a value onto the logical end of the vector.
48 | |||
49 | ||| Effect:
50 | ||| - Adds an Append operation to the thread-local buffer.
51 | |||
52 | export
59 | shouldtrigger <- liftIO (enqueueOperation lsmrrbvector.buffers lsmrrbvector.combinedsnapshotstate tid (Append x))
62 | ||| Prepends a value onto the logical beginning of the vector.
63 | |||
64 | ||| Effect:
65 | ||| - Adds a Prepend operation to the thread-local buffer.
66 | |||
67 | export
74 | shouldtrigger <- liftIO (enqueueOperation lsmrrbvector.buffers lsmrrbvector.combinedsnapshotstate tid (Prepend x))
77 | ||| Inserts a value at a specified logical index.
78 | |||
79 | ||| Effect:
80 | ||| - Adds an Insert operation to the thread-local buffer.
81 | |||
82 | export
90 | shouldtrigger <- liftIO (enqueueOperation lsmrrbvector.buffers lsmrrbvector.combinedsnapshotstate tid (Insert i x))
93 | ||| Removes a value at a specified logical index.
94 | |||
95 | ||| Effect:
96 | ||| - Adds a Delete operation to the thread-local buffer.
97 | |||
98 | export
105 | shouldtrigger <- liftIO (enqueueOperation lsmrrbvector.buffers lsmrrbvector.combinedsnapshotstate tid (Delete i))
108 | ||| Replaces a value at a specified logical index.
109 | |||
110 | ||| Effect:
111 | ||| - Adds an Update operation to the thread-local buffer.
112 | |||
113 | export
121 | shouldtrigger <- liftIO (enqueueOperation lsmrrbvector.buffers lsmrrbvector.combinedsnapshotstate tid (Update i x))
124 | --------------------------------------------------------------------------------
125 | -- Read Operations
126 | --------------------------------------------------------------------------------
128 | ||| Converts the current published snapshot into a list.
129 | |||
130 | ||| Behavior:
131 | ||| - Reads the current immutable snapshot.
132 | ||| - Converts the snapshot contents into a List.
133 | |||
134 | ||| Properties:
135 | ||| - Observes a consistent snapshot.
136 | ||| - Does not block writers or rebuild activity.
137 | ||| - Reader participation is cleaned up automatically.
138 | |||
139 | ||| Notes:
140 | ||| - Concurrent writes published after acquisition are not visible.
141 | |||
142 | ||| Complexity:
143 | ||| - Snapshot acquisition: O(1)
144 | ||| - Conversion: O(n)
145 | |||
146 | export
153 | ||| Returns the number of elements in the current published snapshot.
154 | |||
155 | ||| Behavior:
156 | ||| - Reads the current immutable snapshot.
157 | ||| - Returns its logical length.
158 | |||
159 | ||| Properties:
160 | ||| - Observes a consistent snapshot.
161 | ||| - Does not block writers or rebuild activity.
162 | ||| - Reader participation is cleaned up automatically.
163 | |||
164 | ||| Notes:
165 | ||| - Concurrent writes published after acquisition are not visible.
166 | |||
167 | ||| Complexity:
168 | ||| - O(1)
169 | |||
170 | export
177 | ||| Returns the element at a given index.
178 | |||
179 | ||| Behavior:
180 | ||| - Reads the current immutable snapshot.
181 | ||| - Retrieves the element at the specified index.
182 | |||
183 | ||| Properties:
184 | ||| - Observes a consistent snapshot.
185 | ||| - Does not block writers or rebuild activity.
186 | ||| - Reader participation is cleaned up automatically.
187 | |||
188 | ||| Notes:
189 | ||| - Out-of-bounds behavior matches RRBVector.index.
190 | ||| - Concurrent writes published after acquisition are not visible.
191 | |||
192 | ||| Complexity:
193 | ||| - O(log n)
194 | |||
195 | export
203 | ||| Looks up an element by index.
204 | |||
205 | ||| Behavior:
206 | ||| - Reads the current immutable snapshot.
207 | ||| - Returns Nothing if the index is out of bounds.
208 | |||
209 | ||| Properties:
210 | ||| - Observes a consistent snapshot.
211 | ||| - Does not block writers or rebuild activity.
212 | ||| - Reader participation is cleaned up automatically.
213 | |||
214 | ||| Notes:
215 | ||| - Concurrent writes published after acquisition are not visible.
216 | |||
217 | ||| Complexity:
218 | ||| - O(log n)
219 | |||
220 | export
228 | ||| Tests whether the current published snapshot is empty.
229 | |||
230 | ||| Behavior:
231 | ||| - Reads the current immutable snapshot.
232 | ||| - Returns True when no elements exist.
233 | |||
234 | ||| Properties:
235 | ||| - Observes a consistent snapshot.
236 | ||| - Does not block writers or rebuild activity.
237 | ||| - Reader participation is cleaned up automatically.
238 | |||
239 | ||| Notes:
240 | ||| - Concurrent writes published after acquisition are not visible.
241 | |||
242 | ||| Complexity:
243 | ||| - O(1)
244 | |||
245 | export
252 | --------------------------------------------------------------------------------
253 | -- Default Config
254 | --------------------------------------------------------------------------------
256 | ||| Default log-structured merge vector configuration.
257 | |||
258 | ||| Current defaults favor balanced throughput and latency.
259 | |||
260 | export
264 | --------------------------------------------------------------------------------
265 | -- Creating Log-Structured Merge RRB-Vectors
266 | --------------------------------------------------------------------------------
268 | ||| Run an empty log-structured merge vector using a user-provided configuration.
269 | |||
270 | ||| Parameters:
271 | ||| - initialbatchwindow: Starting adaptive batching target.
272 | |||
273 | ||| Notes:
274 | ||| - Smaller values rebuild more aggressively.
275 | ||| - Larger values favor write throughput.
276 | |||
280 | -> List (LSMRRBVector World a -> RebuildService Poll -> RebuildServiceState -> Async Poll [Errno] ())
285 | combinedsnapshotstate <- newref (MkCombinedSnapshotState (MkSnapshotState Z Empty) [] Data.SortedMap.empty 0 False config.initialbatchwindow)
288 | let rebuilderservice = rebuilderService lsmrrbvector initialRebuildServiceState rebuilderactions
291 | app n [SIGINT] posixPoller $ handle handlers (rebuilderAndLSMRRBVectorService rebuilderservice lsmrrbvectorservice)
292 | where
296 | ||| Runs an empty log-structured merge vector tuned for high sustained write throughput.
297 | |||
298 | ||| Configuration:
299 | ||| - Initial adaptive batch window: 512
300 | |||
301 | ||| Behavior:
302 | ||| - Favors larger rebuild batches.
303 | ||| - Reduces rebuild frequency under heavy write load.
304 | ||| - May increase visibility latency for newly written values.
305 | |||
306 | ||| Notes:
307 | ||| - Intended for write-heavy workloads.
308 | |||
311 | => List (LSMRRBVector World a -> RebuildService Poll -> RebuildServiceState -> Async Poll [Errno] ())
317 | ||| Runs an empty log-structured merge vector tuned for low publication latency.
318 | |||
319 | ||| Configuration:
320 | ||| - Initial adaptive batch window: 16
321 | |||
322 | ||| Behavior:
323 | ||| - Favors frequent rebuild cycles.
324 | ||| - Reduces time between writes and publication.
325 | ||| - May increase rebuild overhead under heavy load.
326 | |||
327 | ||| Notes:
328 | ||| - Intended for latency-sensitive workloads.
329 | |||
332 | => List (LSMRRBVector World a -> RebuildService Poll -> RebuildServiceState -> Async Poll [Errno] ())
338 | ||| Runs an empty log-structured merge vector.
339 | |||
342 | => List (LSMRRBVector World a -> RebuildService Poll -> RebuildServiceState -> Async Poll [Errno] ())