1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
85
86
87
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
258
259
260
261
262
263
264
265
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
308
309
310
311
312
313
314
315
316
317
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! The seam between a key changing and the search indexes hearing about it.
//!
//! `yo-search` knows what to do with a key that changed and `yo-kv` knows that
//! one did, and neither can reach the other: a hash command is handed one
//! database and the registry lives on the server. So the dispatcher is where
//! the two meet, and this is that meeting.
//!
//! Four ways in. [`changed`] is a key that has just been written, which is
//! read back out of the keyspace and handed to every index that follows it.
//! [`touched`] is the same thing for the commands that name more than one key,
//! which write down what they did rather than answering with it. [`scan`] is
//! the other way round, one index walking every key that was already there.
//! [`sweep`] is the other way round again, an index that has just been dropped
//! taking the keys it was holding out of the keyspace with it.
//!
//! # Why the key is read again
//!
//! A hash command knows the fields it touched and that is not enough. A
//! document is read from nothing every time, so what an index needs is the
//! whole of what is under the key now, and `HDEL` of one field would otherwise
//! hand over nothing at all. Reading it back is one more lookup on a stripe
//! that was warm a moment ago, and it only happens when an index actually
//! follows the key, so a server with no indexes on it never pays for this.
//!
//! # Why the fields are copied
//!
//! Reading a hash holds its stripe, and writing the registry cannot happen with
//! a stripe held: another connection would be waiting on a lock while a
//! document is tokenized. So the fields come out into one buffer and the lock
//! goes, which costs one copy of a document per write and buys back the
//! contention that would otherwise land on whichever stripe is busiest.
//!
//! # One lock at a time
//!
//! The registry is behind its own lock and so is every stripe, and nothing here
//! ever holds both. That is not tidiness, it is the only thing keeping the two
//! orders apart: a write takes the registry to ask whether the key matters and
//! then the stripe to read it, and the scan would otherwise take the stripe to
//! walk the keys and then the registry to ask about each one, which is the same
//! pair the other way round and is how a deadlock is built. So the scan lists
//! the names first and asks about them afterwards.
use Db;
use Text;
use Source;
use Server;
use Fill;
/// What a hash command left behind, in the terms a search index needs.
///
/// Two states would nearly do, and the other two are there because a real
/// server does not treat every way of changing a hash the same. A command is
/// one or more pieces of news, each of which sends the indexes back to the key,
/// and what they find when the key has gone depends on which piece of news it
/// was. All of it is measured against 8.10.1 and all of it shows up in
/// `FT.INFO`, which is the only reason any of it is knowable.
pub
/// What a keyspace command did to the keys it named.
///
/// A hash command is one key and one answer and these are not. `DEL a b c` is
/// three keys, `COPY a b DB 1` writes into a database the connection is not on,
/// and `RENAME` is two keys in one move and is not the same thing as erasing
/// one and writing the other. So the commands in that group write down what
/// they did as they go, and the dispatcher reads it back once the reply is
/// written.
///
/// Nothing is written down on a server with no index on it, which is nearly
/// every server, so `DEL` there is the command it always was.
pub
/// One thing that happened to one key.
/// The database the keyspace group reads a key back from, whatever database the
/// command that named it ran on.
///
/// A hash command reads the database it ran on and this does not, which is
/// measured and is stranger than it sounds. `COPY p:1 p:2 DB 1` from database
/// zero leaves nothing indexed and takes away whatever `p:2` had, because the
/// indexes go and look for `p:2` on database zero and it is not there. The same
/// copy the other way round, into database zero from database one, is indexed.
/// So is a `RESTORE` on database zero, and the same `RESTORE` on database one
/// is not.
///
/// It reads as a bug and it is at worst a shortcut: an index belongs to the
/// database it was made on, `FT.CREATE` is refused anywhere but database zero,
/// so an index reading database zero is an index reading its own database. The
/// hash path is the odd one out rather than this.
const INDEXED: usize = 0;
/// Tells the indexes everything a keyspace command did.
///
/// After the reply, the same as [`changed`], and for the same reason: an index
/// that cannot read a key is a number in `FT.INFO` and not an error a client
/// hears about.
pub
/// One key that became another.
///
/// The ordinary case is a rename from one covered key to another, and it costs
/// two questions and a move: the document is already held and the value under
/// it did not change, so there is nothing to read and no number to spend. The
/// key is only read back when it is arriving from outside a prefix or when the
/// index had no document for it, which is what [`Registry::rereads`] answers.
///
/// [`Registry::rereads`]: yo_search::Registry::rereads
/// A hash lifted out of the keyspace so an index can be handed it.
///
/// One buffer with the ends beside it rather than a vector of vectors, so a
/// hash of forty fields is two allocations and not eighty. The fields and the
/// values alternate, the way they arrived.
pub
/// Reads a hash back, or `None` when the key is not there or is not a hash.
///
/// A key of the wrong type is `None` and not an error. An index `ON HASH` walks
/// past a string sitting under its prefix without a word and without counting a
/// failure, which is measured against a real server and is the opposite of the
/// obvious guess.
pub
/// One key has changed, so every index that follows it reads it again.
///
/// Called after the command has already written its reply, because indexing is
/// not something a client can be told went wrong: a document that will not read
/// is counted in `FT.INFO` and the `HSET` that caused it still answers `OK`.
pub
/// One piece of news about one key, which is all of them but the `HSETEX` that
/// writes a field already past its deadline.
/// An index reads every key that was already there.
///
/// Two commands ask for this. `FT.CREATE` runs it over an index with nothing in
/// it, which is the initial scan and is what `SKIPINITIALSCAN` on the create
/// turns off. `FT.SYNUPDATE` runs it over an index that is already full, so
/// every document in it is written again under a number it did not have before,
/// which is what a real server does and is the only way a group added today
/// reaches a document written yesterday.
///
/// One database and not all of them, which is the odd half of a pair. An index
/// follows a key by name across every database once it is running, so a `HSET`
/// on database one reaches an index made on database zero. The scan does not:
/// it reads the database `FT.CREATE` was run on and no other, so the same key
/// on database one is invisible until something writes to it. Both halves are
/// measured against 8.10.1, and the asymmetry is what falls out of a real
/// server walking one keyspace while its notifications are server wide.
///
/// Every key is listed before any of them is asked about, for two reasons. The
/// walk holds a stripe and reading a key back wants the same stripe, and asking
/// the registry with a stripe held is the lock order a write does not use. So
/// the names come out first and the prefixes are matched afterwards, which
/// costs a list of the names in one database on a command nobody sends twice.
pub
/// Deletes the keys a dropped index was holding.
///
/// The list comes from the index's own document table rather than from its
/// prefix, so only what it actually read is deleted and anything else under the
/// same prefix stays. Which database is the one the drop ran on and not the one
/// the index was made on, measured: an `FT.DROPINDEX i DD` sent from database
/// one takes nothing off database zero.
///
/// Every other index hears about each key going, because two indexes can follow
/// the same prefix and the one still standing would otherwise keep answering
/// with documents whose keys are not there any more.
pub