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 | ||| Looks up an element by index.
178 | |||
179 | ||| Behavior:
180 | ||| - Reads the current immutable snapshot.
181 | ||| - Returns Nothing if the index is out of bounds.
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 | ||| - Concurrent writes published after acquisition are not visible.
190 | |||
191 | ||| Complexity:
192 | ||| - O(log n)
193 | |||
194 | export
202 | ||| Tests whether the current published snapshot is empty.
203 | |||
204 | ||| Behavior:
205 | ||| - Reads the current immutable snapshot.
206 | ||| - Returns True when no elements exist.
207 | |||
208 | ||| Properties:
209 | ||| - Observes a consistent snapshot.
210 | ||| - Does not block writers or rebuild activity.
211 | ||| - Reader participation is cleaned up automatically.
212 | |||
213 | ||| Notes:
214 | ||| - Concurrent writes published after acquisition are not visible.
215 | |||
216 | ||| Complexity:
217 | ||| - O(1)
218 | |||
219 | export
226 | --------------------------------------------------------------------------------
227 | -- Default Config
228 | --------------------------------------------------------------------------------
230 | ||| Default log-structured merge vector configuration.
231 | |||
232 | ||| Current defaults favor balanced throughput and latency.
233 | |||
234 | export
238 | --------------------------------------------------------------------------------
239 | -- Creating Log-Structured Merge RRB-Vectors
240 | --------------------------------------------------------------------------------
242 | ||| Run an empty log-structured merge vector using a user-provided configuration.
243 | |||
244 | ||| Parameters:
245 | ||| - initialbatchwindow: Starting adaptive batching target.
246 | |||
247 | ||| Notes:
248 | ||| - Smaller values rebuild more aggressively.
249 | ||| - Larger values favor write throughput.
250 | |||
254 | -> List (LSMRRBVector World a -> RebuildService Poll -> RebuildServiceState -> Async Poll [Errno] ())
259 | combinedsnapshotstate <- newref (MkCombinedSnapshotState (MkSnapshotState Z Empty) [] Data.SortedMap.empty 0 False config.initialbatchwindow)
262 | let rebuilderservice = rebuilderService lsmrrbvector initialRebuildServiceState rebuilderactions
265 | app n [SIGINT] posixPoller $ handle handlers (rebuilderAndLSMRRBVectorService rebuilderservice lsmrrbvectorservice)
266 | where
270 | ||| Runs an empty log-structured merge vector tuned for high sustained write throughput.
271 | |||
272 | ||| Configuration:
273 | ||| - Initial adaptive batch window: 512
274 | |||
275 | ||| Behavior:
276 | ||| - Favors larger rebuild batches.
277 | ||| - Reduces rebuild frequency under heavy write load.
278 | ||| - May increase visibility latency for newly written values.
279 | |||
280 | ||| Notes:
281 | ||| - Intended for write-heavy workloads.
282 | |||
285 | => List (LSMRRBVector World a -> RebuildService Poll -> RebuildServiceState -> Async Poll [Errno] ())
291 | ||| Runs an empty log-structured merge vector tuned for low publication latency.
292 | |||
293 | ||| Configuration:
294 | ||| - Initial adaptive batch window: 16
295 | |||
296 | ||| Behavior:
297 | ||| - Favors frequent rebuild cycles.
298 | ||| - Reduces time between writes and publication.
299 | ||| - May increase rebuild overhead under heavy load.
300 | |||
301 | ||| Notes:
302 | ||| - Intended for latency-sensitive workloads.
303 | |||
306 | => List (LSMRRBVector World a -> RebuildService Poll -> RebuildServiceState -> Async Poll [Errno] ())
312 | ||| Runs an empty log-structured merge vector.
313 | |||
316 | => List (LSMRRBVector World a -> RebuildService Poll -> RebuildServiceState -> Async Poll [Errno] ())