hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The `board_stroke` table: every mark a board ever carried, plus the `clear` |
| 2 | //! markers that index its epochs, and the board's two stroke counters — all |
| 3 | //! written through the guarded claims in here and nowhere else. |
| 4 | |
| 5 | use crate |
| 6 | |
| 7 | ; |
| 8 | use crate; |
| 9 | use cratecap; |
| 10 | use cratePagedList; |
| 11 | use crate; |
| 12 | use crate |
| 13 | |
| 14 | |
| 15 | ; |
| 16 | use crateTimestamp; |
| 17 | use crateUserId; |
| 18 | use crateAppError; |
| 19 | use cratevalidate_required; |
| 20 | |
| 21 | /// Append one mark to `board`'s current `epoch`. |
| 22 | /// |
| 23 | /// Both stroke counters and the row commit together or not at all: the |
| 24 | /// resettable epoch counter, the lifetime counter, and the open-board guard |
| 25 | /// are one conditional write (the `cap::claim_two_when_and_create` recipe — |
| 26 | /// the lifetime cap's conditions first, both counters bumped by the single |
| 27 | /// `UPDATE` — spelled out at its call site), so a locked or closed board can |
| 28 | /// never be drawn on by a writer that read it a moment earlier. |
| 29 | /// |
| 30 | /// `epoch` rides in that guard too. Without it a clear landing between the |
| 31 | /// caller's board read and this write filed the row under the epoch that |
| 32 | /// just ended while its increment counted against the *new* epoch's |
| 33 | /// counter — one race, two markers corrupted for good (the closed one short |
| 34 | /// by a stroke, the next one claiming a stroke that is not there), and |
| 35 | /// markers are never rewritten. Now such a stroke is refused instead. |
| 36 | pub async |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | /// The one place `closed` is ordered above `locked` lives in |
| 85 | /// [`state_refusal`]; both refusal paths re-read the board and |
| 86 | /// ask it. |
| 87 | async |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | /// End the current epoch: the canvas empties, the history does not. |
| 131 | /// |
| 132 | /// The bump and the marker are one transaction, because the marker carries |
| 133 | /// the epoch's final count — written apart, a crash between them would |
| 134 | /// leave an epoch no marker indexes, and the playback would silently join |
| 135 | /// two sessions into one. |
| 136 | /// |
| 137 | /// **The marker pays the lifetime counter.** It is a real `board_stroke` |
| 138 | /// row, so a marker that claimed nothing left `total_stroke_count` counting |
| 139 | /// strokes *drawn* rather than rows *stored*, and `MAX_BOARD_STROKES` |
| 140 | /// stopped bounding the table. Charged here, the counter is exactly the row |
| 141 | /// count again — the transaction takes the board row's write lock (`SELECT |
| 142 | /// … FOR UPDATE`) and reads the epoch and its count from the locked row, so |
| 143 | /// the increment cannot be split off from the marker it pays for, and a |
| 144 | /// stroke claiming under a concurrent writer either lands before this read |
| 145 | /// or waits for this commit — never inside it. |
| 146 | /// |
| 147 | /// **A blank canvas cannot be cleared.** Without it every call minted a row |
| 148 | /// for free, one per press of a button the creator can hold down. Requiring |
| 149 | /// the epoch counter to be non-zero pays for each marker with at least one |
| 150 | /// stroke it closes — and "there is nothing on this canvas to clear" is the |
| 151 | /// honest answer to the call it refuses. The epochs index keeps its meaning |
| 152 | /// too: no zero-stroke epoch can enter it. |
| 153 | /// |
| 154 | /// **A locked board cannot be cleared.** The lock is the creator's own |
| 155 | /// pause on the canvas, and it holds against every write to it including |
| 156 | /// theirs — the guard is the same condition the stroke path claims under, |
| 157 | /// so "locked" means one thing everywhere. The cost is accepted: a locked |
| 158 | /// board sitting at `MAX_EPOCH_STROKES` is recovered by unlock, clear, |
| 159 | /// relock. |
| 160 | /// |
| 161 | /// The marker's own increment is not capped: the last stroke of a board's |
| 162 | /// budget may be closed by a marker, so a board holds at most |
| 163 | /// `MAX_BOARD_STROKES + 1` rows. Capping it would refuse the clear that |
| 164 | /// files the final epoch in the index, which is the one clear the history |
| 165 | /// needs. |
| 166 | pub async |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | /// What a joining socket draws: the current epoch's marks only, in mint |
| 258 | /// order, one [`BOARD_REPLAY_CHUNK`] at a time. |
| 259 | /// |
| 260 | /// Paged by `id`, never `created_at` — a millisecond stamp has ties by |
| 261 | /// construction, and the UUIDv7 monotonic time field is exactly what breaks |
| 262 | /// them. `after` is the key of the last stroke already drawn; a key that |
| 263 | /// parses as no UUID reads as the nil id, which every real stroke sorts |
| 264 | /// above — the same full replay a cursor from before the epoch would get. |
| 265 | pub async |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | /// The whole log, oldest first — every epoch, or one named epoch. Both |
| 308 | /// kinds of row come back: the `clear` markers are what tell a reader where |
| 309 | /// one epoch ended and the next began. |
| 310 | /// |
| 311 | /// `marks_only` drops the markers, which is what the *live canvas* wants: |
| 312 | /// the current epoch holds no marker by construction, except in the one |
| 313 | /// race where a clear commits between the board read and this read and |
| 314 | /// files its marker under the epoch just named. The room's replay filters |
| 315 | /// to `kind = 'stroke'` ([`replay_current`]), so without this the two |
| 316 | /// views disagree in exactly that window. |
| 317 | pub async |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | /// The epoch index: every `clear` marker this board has, oldest first. |
| 350 | pub async |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 |