yo-resp 0.3.23

The RESP2 and RESP3 codec: borrowed request frames in, wire bytes out, no allocation on the hot path.
Documentation
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
//! The JSON commands, on the wire.
//!
//! A key holds one document, encoded as YOJB, and every command here is a path
//! against it. The path grammar is [`yo_doc::Path`], the text at both doors is
//! [`yo_doc::from_json`] and [`Value::write_json_at`], and a write is
//! [`yo_doc::edit`] over the offsets the path matched. There is very little
//! left for this file to do beyond deciding what shape the reply is, which is
//! most of what RedisJSON compatibility actually consists of.
//!
//! # The two path syntaxes and why they are half the work
//!
//! RedisJSON has two. A path that starts with `$` is a JSONPath and answers a
//! set, so every reply is an array with one entry per match and no match is an
//! empty array. Anything else is what it calls a legacy path, answers at most
//! one value, and a path that matched nothing is an error rather than an empty
//! answer. The two are matched by the same code and only the reply differs,
//! which is why [`Path::legacy`] exists and why nearly every command here ends
//! in a branch on it.
//!
//! It matters more than it looks. A client that sends `.` and one that sends
//! `$` are asking the same question and get different types back, so a
//! compatibility layer that picks one shape is wrong for half of the clients in
//! the world. The default when no path is given is the legacy root and not `$`,
//! so `JSON.GET key` answers the document and `JSON.GET key $` answers a one
//! element array holding it.
//!
//! # Two errors that go out without a prefix
//!
//! Every error this server sends is written by `write_error`, which puts `ERR`
//! or `WRONGTYPE` in front of the message because that prefix is what a client
//! branches on. RedisJSON sends two lines that have neither: a key of the wrong
//! type is `Existing key has wrong Redis type`, and a path that cannot create
//! what it names is `Err wrong static path`. They are odd, and they are what
//! `yo-compat` compares against byte for byte, so the two places that send them
//! write the line themselves and answer `Ok`.
//!
//! # What holds the document
//!
//! [`JsonBody`], through [`yo_kv::Foreign`], the same escape the graph and the
//! vector set go through. The payoff is the same too: `DEL`, `EXISTS`, `TYPE`,
//! `KEYS`, `SCAN`, `RANDOMKEY`, `EXPIRE`, `DBSIZE` and `FLUSHDB` all work on a
//! JSON key without a line here.
//!
//! `TYPE` answers `ReJSON-RL`, which is the name RedisJSON registers and which
//! clients test for by string.
//!
//! # An object comes back in key order
//!
//! Members are stored sorted by length and then by bytes, because that is what
//! makes a lookup a binary search, so the order a client wrote them in is not
//! kept anywhere. RedisJSON keeps insertion order. Every reply that writes an
//! object out shows it, and it is D-34 in the register.

use yo_common::{Code, Error, Result};
use yo_doc::{
    Builder, Computed, Edit, Format, Kind, Path, Step, Value, edit, text::write_resp_float,
};
use yo_kv::{Db, Foreign, Keyspace};

use super::args::{self, Args};
use super::table::Spec;
use crate::reply::Out;

/// What a key holding something that is not a document gets. No prefix, which
/// is RedisJSON's and not a slip here.
const WRONG_TYPE: &[u8] = b"Existing key has wrong Redis type";
/// What a path that would have to invent a place gets. Also unprefixed, and the
/// capitalisation is theirs.
const STATIC_PATH: &[u8] = b"Err wrong static path";
/// What a client gets for asking to create something below a key that is not
/// there.
const NOT_AT_ROOT: &str = "new objects must be created at the root";
/// The path when the command allows one and the client did not give one. The
/// legacy root and not `$`, which decides the shape of most of the replies
/// here.
const ROOT: &[u8] = b".";

/// One document under a key.
#[derive(Debug, Default)]
pub(super) struct JsonBody {
    /// The whole document, as one encoded value.
    ///
    /// One buffer and not a tree, because every read is a path walk over these
    /// bytes and every write rebuilds them. Empty only between the moment
    /// `JSON.DEL $` empties it and the moment the keyspace reaps the key.
    doc: Vec<u8>,
}

impl Foreign for JsonBody {
    fn type_name(&self) -> &'static str {
        "ReJSON-RL"
    }

    fn encoding(&self) -> &'static str {
        // RedisJSON reports `raw` here whatever it is holding, so a client that
        // reads `OBJECT ENCODING` off a document key gets the same word from
        // both servers even though neither one is telling it anything.
        "raw"
    }

    fn memory_bytes(&self) -> usize {
        self.doc.capacity()
    }

    fn is_empty(&self) -> bool {
        self.doc.is_empty()
    }
}

/// Run one JSON command.
pub(super) fn execute(db: &Db, spec: &Spec, args: Args<'_>, out: &mut Out) -> Result<()> {
    // The two that name more than one key take the whole database and find a
    // stripe per key. Every other command here names its key first and reads
    // one document, so it is handed the stripe that key is on and never sees
    // the rest.
    let key = args.get(1);
    match spec.name {
        "json.mset" => mset(db, args, out),
        "json.mget" => mget(db, args, out),
        "json.set" => set(&mut db.hold(key), args, out),
        "json.merge" => merge(&mut db.hold(key), args, out),
        "json.get" => get(&mut db.hold(key), args, out),
        "json.resp" => resp(&mut db.hold(key), args, out),
        "json.debug" => debug(&mut db.hold(key), args, out),
        "json.del" | "json.forget" => del(&mut db.hold(key), args, out),
        "json.type" => kind(&mut db.hold(key), args, out),
        "json.toggle" => toggle(&mut db.hold(key), args, out),
        "json.clear" => clear(&mut db.hold(key), args, out),
        "json.arrlen" => sized(&mut db.hold(key), args, out, Asked::ArrayLen),
        "json.objlen" => sized(&mut db.hold(key), args, out, Asked::ObjectLen),
        "json.strlen" => sized(&mut db.hold(key), args, out, Asked::TextLen),
        "json.objkeys" => sized(&mut db.hold(key), args, out, Asked::ObjectKeys),
        "json.arrappend" => arrappend(&mut db.hold(key), args, out),
        "json.arrinsert" => arrinsert(&mut db.hold(key), args, out),
        "json.arrtrim" => arrtrim(&mut db.hold(key), args, out),
        "json.arrpop" => arrpop(&mut db.hold(key), args, out),
        "json.arrindex" => arrindex(&mut db.hold(key), args, out),
        "json.numincrby" => arith(&mut db.hold(key), args, out, Arith::Add),
        "json.nummultby" => arith(&mut db.hold(key), args, out, Arith::Mul),
        "json.numpowby" => arith(&mut db.hold(key), args, out, Arith::Pow),
        "json.strappend" => strappend(&mut db.hold(key), args, out),
        other => unreachable!("{other} is not a JSON command"),
    }
}

// ------------------------------------------------------------------ the writes

/// `JSON.SET key path value [NX|XX]`.
fn set(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let (key, raw, text) = (args.get(1), args.get(2), args.get(3));
    let mut only = Only::Either;
    if args.len() == 5 {
        if args::is(args.get(4), b"nx") {
            only = Only::Missing;
        } else if args::is(args.get(4), b"xx") {
            only = Only::Present;
        } else {
            return Err(args::syntax());
        }
    } else if args.len() != 4 {
        return Err(args::wrong_arity("json.set"));
    }
    let path = path_of(raw)?;
    // The document is parsed before the key is touched, so a client that sent
    // text that is not JSON changes nothing.
    let value = match yo_doc::from_json(text) {
        Ok(value) => value,
        Err(e) => {
            unprefixed(&e, out);
            return Ok(());
        }
    };
    match plan_one(db, key, &path, &value, only, out)? {
        Plan::Store(doc) => {
            store(db, key, doc);
            out.ok();
        }
        Plan::Nothing => out.nil(),
        Plan::Refused => {}
    }
    Ok(())
}

/// What one `key path value` write would do, worked out without doing it.
///
/// Three answers and not two, because the line a path that cannot say where a
/// value goes gets has no prefix and so is written where it happens rather than
/// returned, and the caller has to know the reply is already out.
enum Plan {
    /// Put this document under the key.
    Store(Vec<u8>),
    /// The path named nowhere the value could go.
    Nothing,
    /// Nothing is to be written and the reply is already out.
    Refused,
}

/// One `key path value` write, which is all of `JSON.SET` and one of
/// `JSON.MSET`'s triples.
///
/// It answers the document rather than storing it because `JSON.MSET` has to
/// know that every triple works before the first one is written.
fn plan_one<'v>(
    db: &mut Keyspace,
    key: &[u8],
    path: &Path<'v>,
    value: &'v [u8],
    only: Only,
    out: &mut Out,
) -> Result<Plan> {
    let body = match doc(db, key, out)? {
        Doc::Wrong => return Ok(Plan::Refused),
        Doc::Gone => {
            // Nothing is there. The only path that says where a whole document
            // goes is the root, and this is checked before `NX` and `XX`
            // because RedisJSON checks it there.
            if !path.is_root() {
                return Err(Error::new(Code::Invalid, NOT_AT_ROOT));
            }
            if only == Only::Present {
                return Ok(Plan::Nothing);
            }
            return Ok(Plan::Store(value.to_vec()));
        }
        Doc::Here(body) => body,
    };

    let root = readable(&body.doc)?;
    let mut hits = Vec::new();
    path.select(&root, &mut hits);
    if hits.is_empty() {
        if only == Only::Present {
            return Ok(Plan::Nothing);
        }
        // A path that matched nothing can still say where a value goes, but
        // only if it says exactly one place. A wildcard or a descent would
        // have to invent one, and that is the unprefixed error.
        if !path.is_definite() {
            out.error(STATIC_PATH);
            return Ok(Plan::Refused);
        }
        let Some(at) = grow(&root, path, value)? else {
            // The container that would hold it is not there, or is not an
            // object. Not an error on either syntax, just a write that did
            // not happen.
            return Ok(Plan::Nothing);
        };
        Ok(Plan::Store(edit(&root, &at)?))
    } else {
        if only == Only::Missing {
            return Ok(Plan::Nothing);
        }
        let at: Vec<_> = offsets(&root, &hits)?
            .into_iter()
            .map(|off| (off, Edit::Set(value)))
            .collect();
        Ok(Plan::Store(edit(&root, &at)?))
    }
}

/// Put a document under a key, whether or not one is there already.
///
/// Only ever called for a key a [`Plan`] was worked out against, so the key
/// either holds a document or holds nothing, and the wrong type is somebody
/// else's error to write.
fn store(db: &mut Keyspace, key: &[u8], doc: Vec<u8>) {
    if let Ok(Some(body)) = db.foreign_mut(key)
        && let Some(body) = body.downcast_mut::<JsonBody>()
    {
        body.doc = doc;
        return;
    }
    db.put_foreign(key, Box::new(JsonBody { doc }));
}

/// Whether a `NX` or an `XX` was given.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Only {
    Either,
    Missing,
    Present,
}

/// Where a path that matched nothing would put a value, if anywhere.
///
/// Only ever called for a definite path, so the parent names at most one place
/// and the answer is at most one edit. `$.a.b` on a document with an `a` and no
/// `b` is the case this exists for.
fn grow<'v>(
    root: &Value<'_>,
    path: &Path<'v>,
    value: &'v [u8],
) -> Result<Option<Vec<(usize, Edit<'v>)>>> {
    let Some((parent, step)) = path.split_last() else {
        return Ok(None);
    };
    let Step::Key(name) = step else {
        // An index into an array, and the array either is not there or does not
        // reach that far. RedisJSON does not append there and neither does
        // this: `JSON.ARRAPPEND` is the command that appends and it says so in
        // its name.
        return Err(Error::new(Code::Invalid, "array index out of range"));
    };
    let Some(holder) = parent.first(root) else {
        return Ok(None);
    };
    if holder.kind() != Kind::Object {
        return Ok(None);
    }
    Ok(Some(vec![(offset(root, &holder)?, Edit::Put(name, value))]))
}

/// `JSON.MSET key path value [key path value ...]`.
///
/// Every triple is worked out against the keyspace as it was before the command
/// and nothing is written until all of them are known to work, so a client that
/// saw an error knows that nothing happened. What is not an error is a path that
/// names nowhere to put its value: that triple is skipped, the others are still
/// written, and the reply is a nil instead of `OK`. Read off 8.10.1 both ways
/// round, with the failing triple first and last, because a naive loop and this
/// are the same for one order and not for the other.
///
/// Working every triple out against the state the command started in is also
/// what the reference does, so two triples on one key do not see each other and
/// the last one written is the one that stays.
fn mset(db: &Db, args: Args<'_>, out: &mut Out) -> Result<()> {
    if args.len() < 4 || !(args.len() - 1).is_multiple_of(3) {
        return Err(args::wrong_arity("json.mset"));
    }
    let mut jobs = Vec::with_capacity((args.len() - 1) / 3);
    for i in (1..args.len()).step_by(3) {
        let path = path_of(args.get(i + 1))?;
        let value = match yo_doc::from_json(args.get(i + 2)) {
            Ok(value) => value,
            Err(e) => {
                unprefixed(&e, out);
                return Ok(());
            }
        };
        jobs.push((args.get(i), path, value));
    }
    let mut plans = Vec::with_capacity(jobs.len());
    for (key, path, value) in &jobs {
        match plan_one(&mut db.hold(key), key, path, value, Only::Either, out)? {
            Plan::Refused => return Ok(()),
            plan => plans.push(plan),
        }
    }
    let mut all = true;
    for ((key, _, _), plan) in jobs.iter().zip(plans) {
        match plan {
            Plan::Store(doc) => store(&mut db.hold(key), key, doc),
            Plan::Nothing => all = false,
            // Every one of these was turned into an early return above.
            Plan::Refused => unreachable!("a refused triple is answered before any is written"),
        }
    }
    if all {
        out.ok();
    } else {
        out.nil();
    }
    Ok(())
}

/// `JSON.MERGE key path value`.
///
/// The value is a merge patch, RFC 7386: a patch that is not an object replaces
/// what it is merged onto, and an object patch is applied member by member with
/// a `null` deleting the member of that name. It is the one write here whose
/// argument is not the value being stored.
fn merge(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    if args.len() != 4 {
        return Err(args::syntax());
    }
    let (key, raw, text) = (args.get(1), args.get(2), args.get(3));
    let path = path_of(raw)?;
    let patch = match yo_doc::from_json(text) {
        Ok(patch) => patch,
        Err(e) => {
            unprefixed(&e, out);
            return Ok(());
        }
    };
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => {
            if !path.is_root() {
                return Err(Error::new(Code::Invalid, NOT_AT_ROOT));
            }
            // Stored as it stands, nulls and all. A merge patch says a null
            // deletes the member of that name, and RFC 7386 goes on to say that
            // a patch applied to nothing has its nulls dropped, but RedisJSON
            // only does that where there is something to merge onto: a key that
            // is not there and a member the path is about to create both keep
            // the null. Read off 8.10.1 with `{"x":null,"y":1}` on both.
            db.put_foreign(key, Box::new(JsonBody { doc: patch }));
            out.ok();
            return Ok(());
        }
        Doc::Here(body) => body,
    };

    // The merged values, held out here because the edits below borrow them.
    let made: Vec<Vec<u8>>;
    let after = {
        let root = readable(&body.doc)?;
        let patched = readable(&patch)?;
        let mut hits = Vec::new();
        path.select(&root, &mut hits);
        if hits.is_empty() {
            // The same rule `JSON.SET` follows for a path that matched nothing,
            // down to the unprefixed line for a path that would have to invent
            // the place it names.
            if !path.is_definite() {
                out.error(STATIC_PATH);
                return Ok(());
            }
            let Some(at) = grow(&root, &path, &patch)? else {
                out.nil();
                return Ok(());
            };
            edit(&root, &at)?
        } else {
            made = folded(&root, &hits, &patched)?;
            let at: Vec<_> = offsets(&root, &hits)?
                .into_iter()
                .zip(&made)
                .map(|(off, bytes)| (off, Edit::Set(bytes)))
                .collect();
            edit(&root, &at)?
        }
    };
    body.doc = after;
    out.ok();
    Ok(())
}

/// The patch merged onto every match, innermost match first.
///
/// A `$..` path is the only one that matches a value and something inside that
/// same value, and the two are not independent: the reference merges the inner
/// match first and then merges the outer one onto the result, so the inner
/// change is still there at the end. `$..*` with `{"m":1}` on `{"a":{"b":1}}`
/// comes out as `{"a":{"b":{"m":1},"m":1}}` and not as `{"a":{"b":1,"m":1}}`.
/// Working outermost first would throw the inner one away, which is what
/// [`edit`] does with an edit inside an edit and is right for `JSON.SET`.
///
/// The answers line up with `hits`. Sorting by offset the other way round is
/// what puts an inner match before the outer one, since a match is always
/// further into the document than anything that holds it.
fn folded(root: &Value<'_>, hits: &[Value<'_>], patch: &Value<'_>) -> Result<Vec<Vec<u8>>> {
    let spans = hits
        .iter()
        .map(|v| {
            let at = offset(root, v)?;
            Ok((at, at + v.encoded_len().ok_or_else(damaged)?))
        })
        .collect::<Result<Vec<_>>>()?;
    let mut order: Vec<usize> = (0..hits.len()).collect();
    order.sort_by_key(|&i| core::cmp::Reverse(spans[i].0));

    let mut made: Vec<Vec<u8>> = vec![Vec::new(); hits.len()];
    for &i in &order {
        let (at, end) = spans[i];
        // Everything already merged that sits inside this match. The ones
        // nested inside another of these are dropped by `edit`, which is right,
        // because the one that holds them already has them folded in.
        let inside: Vec<_> = (0..hits.len())
            .filter(|&j| j != i && spans[j].0 > at && spans[j].0 < end)
            .map(|j| Ok((offset(&hits[i], &hits[j])?, Edit::Set(made[j].as_slice()))))
            .collect::<Result<Vec<_>>>()?;
        let with = if inside.is_empty() {
            None
        } else {
            Some(edit(&hits[i], &inside)?)
        };
        let target = match &with {
            Some(bytes) => readable(bytes)?,
            None => hits[i],
        };
        let mut b = Builder::new();
        merged(Some(&target), patch, &mut b)?;
        made[i] = b.finish()?.to_vec();
    }
    Ok(made)
}

/// `patch` merged onto `target`, which is RFC 7386 and nothing more.
///
/// A patch that is not an object replaces the target outright, whatever the
/// target was. An object patch merged onto anything that is not an object
/// starts from an empty object, which is what makes `{"x":null,"y":1}` onto a
/// `7` come out as `{"y":1}`: the deletion has nothing to delete and is dropped
/// rather than kept as a null.
fn merged(target: Option<&Value<'_>>, patch: &Value<'_>, b: &mut Builder) -> Result<()> {
    if patch.kind() != Kind::Object {
        return b.embed(patch);
    }
    let was = target.filter(|t| t.kind() == Kind::Object);
    b.begin_object()?;
    if let Some(t) = was {
        for i in 0..t.len() {
            // A member the patch names is written by the loop below, or is a
            // deletion and is written by nobody.
            let key = t.key_at(i).ok_or_else(damaged)?;
            if patch.get(key).is_some() {
                continue;
            }
            b.key(key)?;
            b.embed(&t.at(i).ok_or_else(damaged)?)?;
        }
    }
    for i in 0..patch.len() {
        let key = patch.key_at(i).ok_or_else(damaged)?;
        let v = patch.at(i).ok_or_else(damaged)?;
        if v.is_null() {
            continue;
        }
        b.key(key)?;
        merged(was.and_then(|t| t.get(key)).as_ref(), &v, b)?;
    }
    b.end_object()
}

/// `JSON.DEL key [path]` and `JSON.FORGET`, which is the same command.
fn del(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let key = args.get(1);
    // Anything past the path is ignored rather than refused, which is
    // RedisJSON's behaviour and not an accident of the arity: it registers the
    // command as taking one path and never looks at the rest.
    let raw = args.opt(2).unwrap_or(ROOT);
    let path = path_of(raw)?;
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => {
            out.int(0);
            return Ok(());
        }
        Doc::Here(body) => body,
    };
    // The root is the whole key, and a document with no value in it is not a
    // document, so this is the keyspace's business rather than the editor's.
    if path.is_root() {
        body.doc.clear();
        db.reap_foreign(key);
        out.int(1);
        return Ok(());
    }
    let (after, gone) = {
        let root = readable(&body.doc)?;
        let mut hits = Vec::new();
        path.select(&root, &mut hits);
        if hits.is_empty() {
            out.int(0);
            return Ok(());
        }
        let at: Vec<_> = offsets(&root, &hits)?
            .into_iter()
            .map(|off| (off, Edit::Remove))
            .collect();
        (edit(&root, &at)?, at.len())
    };
    // A delete that empties the root container empties the key, which is the
    // same rule the sets and the hashes follow and is why `EXISTS` on a list
    // somebody popped the last element off answers zero. It is a delete rule
    // and not a shape rule: a document that was written as an empty object by
    // `JSON.SET` stays, because nothing removed anything from it.
    let empty_root = matches!(
        readable(&after).map(|v| (v.kind(), v.is_empty())),
        Ok((Kind::Object | Kind::Array, true))
    );
    body.doc = after;
    if empty_root {
        body.doc.clear();
        db.reap_foreign(key);
    }
    out.int(gone as i64);
    Ok(())
}

/// `JSON.TOGGLE key path`, which flips every boolean the path names.
fn toggle(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let (key, raw) = (args.get(1), args.get(2));
    let path = path_of(raw)?;
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(no_key()),
        Doc::Here(body) => body,
    };
    let (after, flipped) = {
        let root = readable(&body.doc)?;
        let mut hits = Vec::new();
        path.select(&root, &mut hits);
        // The two encoded booleans, kept here so the edits can borrow them.
        let (yes, no) = (yo_doc::from_json(b"true")?, yo_doc::from_json(b"false")?);
        let mut at = Vec::new();
        let mut flipped = Vec::new();
        for v in &hits {
            match v.as_bool() {
                Some(was) => {
                    let now: &[u8] = if was { &no } else { &yes };
                    at.push((offset(&root, v)?, Edit::Set(now)));
                    flipped.push(Some(!was));
                }
                // A path that matched something that is not a boolean is a hole
                // in the array for a JSONPath, and for a legacy path it is a
                // match this skips over on the way to the next one.
                None => flipped.push(None),
            }
        }
        // Nothing to flip anywhere is the error, and it is one sentence for a
        // path that matched nothing and a path that matched no boolean, because
        // a legacy reply has no room to say which.
        if path.legacy() && !flipped.iter().any(Option::is_some) {
            return Err(not_a_bool());
        }
        (edit(&root, &at)?, flipped)
    };
    body.doc = after;
    if path.legacy() {
        match Pick::Last.of(&flipped) {
            Some(now) => out.bulk(if *now { b"true" } else { b"false" }),
            None => out.nil(),
        }
        return Ok(());
    }
    out.array(flipped.len());
    for f in flipped {
        match f {
            Some(now) => out.int(i64::from(now)),
            None => out.nil(),
        }
    }
    Ok(())
}

/// `JSON.CLEAR key [path]`, which empties containers and zeroes numbers.
///
/// A string, a boolean and a null are left alone, which is RedisJSON's rule and
/// not an obvious one: clearing a string to `""` would be just as defensible.
fn clear(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let key = args.get(1);
    let raw = args.opt(2).unwrap_or(ROOT);
    let path = path_of(raw)?;
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(no_key()),
        Doc::Here(body) => body,
    };
    let (after, cleared) = {
        let root = readable(&body.doc)?;
        let mut hits = Vec::new();
        path.select(&root, &mut hits);
        let (empty_object, empty_array, zero) = (
            yo_doc::from_json(b"{}")?,
            yo_doc::from_json(b"[]")?,
            yo_doc::from_json(b"0")?,
        );
        let mut at = Vec::new();
        for v in &hits {
            let to: &[u8] = match v.kind() {
                // Already empty is already clear, and rewriting it would count
                // a change that did not happen. The count is what the client
                // sees, so this is the whole difference.
                Kind::Object if v.is_empty() => continue,
                Kind::Array if v.is_empty() => continue,
                Kind::Object => &empty_object,
                Kind::Array => &empty_array,
                Kind::Int if v.as_int() == Some(0) => continue,
                Kind::Int | Kind::Float => &zero,
                _ => continue,
            };
            at.push((offset(&root, v)?, Edit::Set(to)));
        }
        (edit(&root, &at)?, at.len())
    };
    body.doc = after;
    out.int(cleared as i64);
    Ok(())
}

// ------------------------------------------------------------------- the reads

/// `JSON.GET key [INDENT s] [NEWLINE s] [SPACE s] [path ...]`.
fn get(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let key = args.get(1);
    let (f, from) = format(args, 2)?;
    let raws: Vec<&[u8]> = if from < args.len() {
        (from..args.len()).map(|i| args.get(i)).collect()
    } else {
        vec![ROOT]
    };
    // Parsed before the key is read, so a path that does not parse is the same
    // error whether or not the key is there.
    let paths: Vec<Path<'_>> = raws.iter().map(|r| Path::parse(r)).collect::<Result<_>>()?;

    let body = match doc(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => {
            out.nil();
            return Ok(());
        }
        Doc::Here(body) => body,
    };
    let root = readable(&body.doc)?;
    let mut text = Vec::new();
    if paths.len() == 1 {
        one(&root, &paths[0], &f, &mut text, 0)?;
    } else {
        // More than one path answers an object keyed by the paths as they were
        // written, so a client that asked three questions can tell which answer
        // is which. The keys come back in the order they were asked in, which
        // RedisJSON does not promise because it builds the reply out of a hash
        // map, and that is the same D-34 the document's own key order is.
        text.push(b'{');
        for (i, (path, raw)) in paths.iter().zip(&raws).enumerate() {
            if i > 0 {
                text.push(b',');
            }
            line(&f, &mut text, 1);
            quote(raw, &mut text);
            text.push(b':');
            text.extend_from_slice(f.space);
            one(&root, path, &f, &mut text, 1)?;
        }
        line(&f, &mut text, 0);
        text.push(b'}');
    }
    out.bulk(&text);
    Ok(())
}

/// A path for a command that cannot take a projection, which is every one of
/// them but `JSON.GET`, `JSON.MGET` and `JSON.RESP`.
///
/// A projection answers numbers and key names that are nowhere in the document,
/// so there is nothing for a write to write to and nothing for `JSON.TYPE` to
/// describe. The reference refuses it in the same words.
fn path_of(raw: &[u8]) -> Result<Path<'_>> {
    let path = Path::parse(raw)?;
    if path.is_projection() {
        return Err(Error::new(
            Code::Invalid,
            "computed/projection expressions are only supported by JSON.GET/JSON.MGET/JSON.RESP",
        ));
    }
    Ok(path)
}

/// One path's answer, as the text it contributes, written as if it sat `depth`
/// levels inside whatever wrapper the caller has already opened.
fn one<'d>(
    root: &Value<'d>,
    path: &'d Path<'d>,
    f: &Format<'_>,
    text: &mut Vec<u8>,
    depth: usize,
) -> Result<()> {
    if path.is_projection() {
        // A projection always answers an array, however the path was written,
        // and a projection that worked nothing out answers an empty one rather
        // than the error a legacy path that matched nothing would.
        let got = path.project(root);
        let laid_out = !f.is_plain() && !got.is_empty();
        text.push(b'[');
        for (i, v) in got.iter().enumerate() {
            if i > 0 {
                text.push(b',');
            }
            if laid_out {
                line(f, text, depth + 1);
            }
            v.write_json_at(f, text, depth + 1)?;
        }
        if laid_out {
            line(f, text, depth);
        }
        text.push(b']');
        return Ok(());
    }
    let mut hits = Vec::new();
    path.select(root, &mut hits);
    if path.legacy() {
        let Some(v) = hits.first() else {
            return Err(missing());
        };
        return v.write_json_at(f, text, depth);
    }
    let laid_out = !f.is_plain() && !hits.is_empty();
    text.push(b'[');
    for (i, v) in hits.iter().enumerate() {
        if i > 0 {
            text.push(b',');
        }
        if laid_out {
            line(f, text, depth + 1);
        }
        v.write_json_at(f, text, depth + 1)?;
    }
    if laid_out {
        line(f, text, depth);
    }
    text.push(b']');
    Ok(())
}

/// End a line and indent the next one, for the wrapper this file builds itself.
///
/// The same rule the document writer follows, which is that the whole thing is
/// one line unless the client asked for something.
fn line(f: &Format<'_>, text: &mut Vec<u8>, depth: usize) {
    if f.is_plain() {
        return;
    }
    text.extend_from_slice(f.newline);
    for _ in 0..depth {
        text.extend_from_slice(f.indent);
    }
}

/// `JSON.MGET key [key ...] path`, one path against many documents.
fn mget(db: &Db, args: Args<'_>, out: &mut Out) -> Result<()> {
    let last = args.len() - 1;
    let path = Path::parse(args.get(last))?;
    let f = Format::default();
    out.array(last - 1);
    let mut text = Vec::new();
    // Every stripe at once, so the answer is these documents as they were at
    // one moment rather than each as it was when the walk reached it.
    let mut held = db.hold_keys((1..last).map(|i| args.get(i)));
    for i in 1..last {
        let key = args.get(i);
        // A key that is not there and a key holding something that is not a
        // document are both a hole in the array. `MGET` does the same with a
        // key holding a hash, and for the same reason: one bad key in a hundred
        // should not lose the other ninety nine answers.
        let stripe = held.stripe_mut(db.stripe_of(key));
        let Ok(Some(body)) = stripe.foreign(key) else {
            out.nil();
            continue;
        };
        let Some(body) = body.downcast_ref::<JsonBody>() else {
            out.nil();
            continue;
        };
        let root = readable(&body.doc)?;
        text.clear();
        match one(&root, &path, &f, &mut text, 0) {
            Ok(()) => out.bulk(&text),
            Err(_) => out.nil(),
        }
    }
    Ok(())
}

/// `JSON.TYPE key [path]`.
fn kind(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let key = args.get(1);
    let raw = args.opt(2).unwrap_or(ROOT);
    let path = path_of(raw)?;
    let body = match doc(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => {
            out.nil();
            return Ok(());
        }
        Doc::Here(body) => body,
    };
    let root = readable(&body.doc)?;
    let mut hits = Vec::new();
    path.select(&root, &mut hits);
    if path.legacy() {
        // The one command here where a legacy path that matched nothing is a
        // nil rather than an error, which lines up with the key that is not
        // there answering nil too.
        match hits.first() {
            Some(v) => out.bulk(word(v)),
            None => out.nil(),
        }
        return Ok(());
    }
    out.array(hits.len());
    for v in &hits {
        out.bulk(word(v));
    }
    Ok(())
}

/// The word `JSON.TYPE` answers for a value.
///
/// `integer` and `number` are two words for what the encoding stores as two
/// kinds, which is the one place RedisJSON's type names line up with ours
/// exactly rather than by translation.
fn word(v: &Value<'_>) -> &'static [u8] {
    match v.kind() {
        Kind::Null => b"null",
        Kind::Bool => b"boolean",
        Kind::Int => b"integer",
        Kind::Float => b"number",
        Kind::Text => b"string",
        Kind::Array => b"array",
        Kind::Object => b"object",
    }
}

// ------------------------------------------------------------- the array family

/// Which of `JSON.ARRLEN`, `JSON.OBJLEN`, `JSON.STRLEN` and `JSON.OBJKEYS` is
/// being run.
///
/// The four are one command with four sets of answers, and the answers do not
/// follow a pattern. A legacy path that matched nothing is an error for
/// `JSON.ARRLEN` and `JSON.STRLEN` and a nil for the other two. A legacy path
/// that matched the wrong kind of value is an `ERR` for `JSON.ARRLEN` and
/// `JSON.OBJKEYS` and a `WRONGTYPE` for the other two. A JSONPath against a key
/// that is not there is `could not perform this operation on a key that doesn't
/// exist` for three of them and `Path does not exist or not an object` for
/// `JSON.OBJLEN`, which is a line about a path being sent for a missing key.
///
/// None of that is a simplification of RedisJSON and none of it is a guess. It
/// was read off a running module one command at a time, because a client that
/// branches on the error text is the entire reason this file exists.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Asked {
    ArrayLen,
    ObjectLen,
    TextLen,
    ObjectKeys,
}

impl Asked {
    /// The kind of value the path is supposed to name.
    fn kind(self) -> Kind {
        match self {
            Asked::ArrayLen => Kind::Array,
            Asked::ObjectLen | Asked::ObjectKeys => Kind::Object,
            Asked::TextLen => Kind::Text,
        }
    }

    /// Whether a legacy path that matched nothing answers nil rather than an
    /// error.
    fn quiet(self) -> bool {
        matches!(self, Asked::ObjectLen | Asked::ObjectKeys)
    }

    /// What a legacy path that matched the wrong kind of value gets.
    fn wrong(self) -> Error {
        match self {
            Asked::ArrayLen => Error::new(Code::Invalid, "Path does not exist or not an array"),
            Asked::ObjectKeys => Error::new(Code::Invalid, "Path does not exist or not an object"),
            Asked::ObjectLen => Error::new(
                Code::WrongType,
                "wrong type of path value - expected object",
            ),
            Asked::TextLen => Error::new(
                Code::WrongType,
                "wrong type of path value - expected string",
            ),
        }
    }

    /// What a JSONPath against a key that is not there gets.
    fn no_key(self) -> Error {
        match self {
            Asked::ObjectLen => Error::new(Code::Invalid, "Path does not exist or not an object"),
            _ => no_key(),
        }
    }

    /// The answer for one match, which is a count for three of them and the
    /// keys themselves for the fourth.
    ///
    /// A document written through this file never has its keys interned, since
    /// interning is a collection's table and there is no collection here, so
    /// [`Value::key_at`] always answers. The fallback is there because the type
    /// allows the other case rather than because anything reaches it.
    fn one(self, v: &Value<'_>, out: &mut Out) {
        match self {
            Asked::TextLen => out.int(v.text_bytes().unwrap_or_default().len() as i64),
            Asked::ArrayLen | Asked::ObjectLen => out.int(v.len() as i64),
            Asked::ObjectKeys => {
                out.array(v.len());
                for i in 0..v.len() {
                    out.bulk(v.key_at(i).unwrap_or_default());
                }
            }
        }
    }
}

/// `JSON.ARRLEN`, `JSON.OBJLEN`, `JSON.STRLEN` and `JSON.OBJKEYS`.
fn sized(db: &mut Keyspace, args: Args<'_>, out: &mut Out, asked: Asked) -> Result<()> {
    let key = args.get(1);
    let raw = args.opt(2).unwrap_or(ROOT);
    let path = path_of(raw)?;
    let body = match doc(db, key, out)? {
        Doc::Wrong => return Ok(()),
        // A legacy path against a missing key is a nil and a JSONPath against
        // one is an error, which is the opposite way round from everywhere else
        // in this file and is what the module does.
        Doc::Gone if path.legacy() => {
            out.nil();
            return Ok(());
        }
        Doc::Gone => return Err(asked.no_key()),
        Doc::Here(body) => body,
    };
    let root = readable(&body.doc)?;
    let mut hits = Vec::new();
    path.select(&root, &mut hits);
    if path.legacy() {
        let Some(v) = hits.first() else {
            if asked.quiet() {
                out.nil();
                return Ok(());
            }
            return Err(missing());
        };
        if v.kind() != asked.kind() {
            return Err(asked.wrong());
        }
        asked.one(v, out);
        return Ok(());
    }
    out.array(hits.len());
    for v in &hits {
        if v.kind() == asked.kind() {
            asked.one(v, out);
        } else {
            out.nil();
        }
    }
    Ok(())
}

/// What every array command says when a legacy path did not name an array,
/// whether because it matched nothing or because it matched something else.
///
/// One line for both, which is the same shape as `JSON.TOGGLE`'s and for the
/// same reason: a legacy path answers at most one value, so there is nothing
/// for the message to say about which of the two happened.
fn not_an_array() -> Error {
    Error::new(Code::Invalid, "Path does not exist or not an array")
}

/// The arrays a path matched, with their offsets, and nothing else.
///
/// Every array command needs the same three things and treats a match that is
/// not an array the same way, so the walk is written once. The answer is one
/// entry per match, `None` for a match that is not an array, which is the hole
/// a JSONPath reply shows as a nil.
fn arrays<'r>(root: &Value<'r>, path: &Path<'_>) -> Result<Vec<Option<(usize, Value<'r>)>>> {
    let mut hits = Vec::new();
    path.select(root, &mut hits);
    let mut out = Vec::with_capacity(hits.len());
    for v in hits {
        if v.kind() == Kind::Array {
            out.push(Some((offset(root, &v)?, v)));
        } else {
            out.push(None);
        }
    }
    Ok(out)
}

/// That a legacy path named at least one array, or the error that says it did
/// not.
///
/// A legacy path can match more than one value, `.a[*]` being the short way to
/// get there, and a write applies to every match it can rather than to the
/// first one. So this refuses only when there was nothing it could do at all: a
/// path that matched a string and then two arrays appends to the two arrays and
/// answers, and a path that matched two strings is the error. Checked on 8.10.1
/// on `{"a":["x",[1],[1,2]]}`, which answers three.
fn any_array(found: &[Option<(usize, Value<'_>)>]) -> Result<()> {
    if found.iter().any(Option::is_some) {
        return Ok(());
    }
    Err(not_an_array())
}

/// Which of the matches a legacy path answers with.
///
/// A legacy path answers one value and a write touches every match, so there is
/// a choice here, and RedisJSON does not make the same one twice. The readers
/// answer the first match. The writes answer whichever match they wrote last,
/// and they do not all walk the matches the same way round: `JSON.ARRAPPEND`,
/// the number family, `JSON.STRAPPEND` and `JSON.TOGGLE` walk forwards and so
/// answer the last, while `JSON.ARRINSERT`, `JSON.ARRTRIM` and `JSON.ARRPOP`
/// walk backwards and so answer the first. It reads like an accident of how
/// each one was written, and it is what a client sees, so it is copied rather
/// than tidied up. Read off 8.10.1 with three arrays of one, two and three
/// elements, which tells the two apart in one command.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Pick {
    First,
    Last,
}

impl Pick {
    /// The answer a legacy path gets, out of one answer per match.
    ///
    /// A `None` is a match this command did not touch because it was the wrong
    /// kind of value, and those are skipped rather than counted, which is why a
    /// path that matched a string and then an array answers about the array.
    fn of<T>(self, all: &[Option<T>]) -> Option<&T> {
        let mut kept = all.iter().flatten();
        match self {
            Pick::First => kept.next(),
            Pick::Last => kept.next_back(),
        }
    }
}

/// Answer with the new length of every array the command touched, in whichever
/// of the two shapes the path asked for.
fn lengths(path: &Path<'_>, lens: &[Option<usize>], pick: Pick, out: &mut Out) {
    if path.legacy() {
        match pick.of(lens) {
            Some(n) => out.int(*n as i64),
            None => out.nil(),
        }
        return;
    }
    out.array(lens.len());
    for n in lens {
        match n {
            Some(n) => out.int(*n as i64),
            None => out.nil(),
        }
    }
}

/// The values a command takes from `from` onwards, encoded.
///
/// Parsed before the key is touched, so a value that is not JSON changes
/// nothing, which is the rule `JSON.SET` follows too.
fn values(args: Args<'_>, from: usize) -> Result<Vec<Vec<u8>>> {
    (from..args.len())
        .map(|i| yo_doc::from_json(args.get(i)))
        .collect()
}

/// `JSON.ARRAPPEND key path value [value ...]`.
fn arrappend(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let (key, raw) = (args.get(1), args.get(2));
    if args.len() < 4 {
        return Err(args::wrong_arity("json.arrappend"));
    }
    let path = path_of(raw)?;
    let added = match values(args, 3) {
        Ok(added) => added,
        Err(e) => {
            unprefixed(&e, out);
            return Ok(());
        }
    };
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(no_key()),
        Doc::Here(body) => body,
    };
    let put: Vec<&[u8]> = added.iter().map(Vec::as_slice).collect();
    let (after, lens) = {
        let root = readable(&body.doc)?;
        let found = arrays(&root, &path)?;
        if path.legacy() {
            any_array(&found)?;
        }
        let mut at = Vec::new();
        let mut lens = Vec::with_capacity(found.len());
        for f in &found {
            match f {
                Some((off, v)) => {
                    at.push((
                        *off,
                        Edit::Splice {
                            at: v.len(),
                            take: 0,
                            put: &put,
                        },
                    ));
                    lens.push(Some(v.len() + put.len()));
                }
                None => lens.push(None),
            }
        }
        (edit(&root, &at)?, lens)
    };
    body.doc = after;
    lengths(&path, &lens, Pick::Last, out);
    Ok(())
}

/// `JSON.ARRINSERT key path index value [value ...]`.
fn arrinsert(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let (key, raw) = (args.get(1), args.get(2));
    if args.len() < 5 {
        return Err(args::wrong_arity("json.arrinsert"));
    }
    let path = path_of(raw)?;
    let want = args.int(3)?;
    let added = match values(args, 4) {
        Ok(added) => added,
        Err(e) => {
            unprefixed(&e, out);
            return Ok(());
        }
    };
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(no_key()),
        Doc::Here(body) => body,
    };
    let put: Vec<&[u8]> = added.iter().map(Vec::as_slice).collect();
    let (after, lens) = {
        let root = readable(&body.doc)?;
        let found = arrays(&root, &path)?;
        if path.legacy() {
            any_array(&found)?;
        }
        let mut at = Vec::new();
        let mut lens = Vec::with_capacity(found.len());
        for f in &found {
            match f {
                Some((off, v)) => {
                    // An index the array does not reach is refused rather than
                    // clamped, which is what makes this different from every
                    // other index here. The end itself is allowed, so an insert
                    // at the length is an append.
                    let Some(i) = place(want, v.len()) else {
                        return Err(Error::new(Code::Invalid, "index out of bounds"));
                    };
                    at.push((
                        *off,
                        Edit::Splice {
                            at: i,
                            take: 0,
                            put: &put,
                        },
                    ));
                    lens.push(Some(v.len() + put.len()));
                }
                None => lens.push(None),
            }
        }
        (edit(&root, &at)?, lens)
    };
    body.doc = after;
    lengths(&path, &lens, Pick::First, out);
    Ok(())
}

/// Where an insert index lands in an array of `len`, or nothing if it does not
/// land in it at all.
///
/// Negative counts back from the end, so `-1` is before the last element and
/// `-len` is the front. The end is a place and one past it is not.
fn place(want: i64, len: usize) -> Option<usize> {
    let at = if want < 0 { len as i64 + want } else { want };
    if at < 0 || at > len as i64 {
        return None;
    }
    Some(at as usize)
}

/// `JSON.ARRTRIM key path start stop`.
fn arrtrim(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let (key, raw) = (args.get(1), args.get(2));
    if args.len() != 5 {
        return Err(args::wrong_arity("json.arrtrim"));
    }
    let path = path_of(raw)?;
    let (from, to) = (args.int(3)?, args.int(4)?);
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(no_key()),
        Doc::Here(body) => body,
    };
    let (after, lens) = {
        let root = readable(&body.doc)?;
        let found = arrays(&root, &path)?;
        if path.legacy() {
            any_array(&found)?;
        }
        // The kept elements are the bytes already in the document, spliced back
        // over the whole array. Held out here because the edits below borrow
        // them and the borrow has to outlive the loop that builds them.
        let mut kept: Vec<Vec<&[u8]>> = Vec::with_capacity(found.len());
        for f in &found {
            let mut keep = Vec::new();
            if let Some((_, v)) = f {
                let (start, stop) = span(from, to, v.len());
                for i in start..stop {
                    if let Some(e) = v.at(i).and_then(|e| e.as_bytes()) {
                        keep.push(e);
                    }
                }
            }
            kept.push(keep);
        }
        let mut at = Vec::new();
        let mut lens = Vec::with_capacity(found.len());
        for (f, keep) in found.iter().zip(&kept) {
            match f {
                Some((off, v)) => {
                    at.push((
                        *off,
                        Edit::Splice {
                            at: 0,
                            take: v.len(),
                            put: keep,
                        },
                    ));
                    lens.push(Some(keep.len()));
                }
                None => lens.push(None),
            }
        }
        (edit(&root, &at)?, lens)
    };
    body.doc = after;
    lengths(&path, &lens, Pick::First, out);
    Ok(())
}

/// The half open run `JSON.ARRTRIM` keeps out of an array of `len`.
///
/// Both ends are inclusive on the wire and both count back from the end when
/// negative, and both clamp rather than refuse. A start past the end or a stop
/// before the start leaves nothing, which is an empty array and not an error.
fn span(from: i64, to: i64, len: usize) -> (usize, usize) {
    let n = len as i64;
    let start = if from < 0 {
        (n + from).max(0)
    } else {
        from.min(n)
    };
    let stop = if to < 0 {
        (n + to).max(0)
    } else {
        to.min(n - 1)
    };
    if start > stop || len == 0 {
        return (0, 0);
    }
    (start as usize, stop as usize + 1)
}

/// `JSON.ARRPOP key [path [index]]`.
fn arrpop(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let key = args.get(1);
    let raw = args.opt(2).unwrap_or(ROOT);
    if args.len() > 4 {
        return Err(args::wrong_arity("json.arrpop"));
    }
    let path = path_of(raw)?;
    let want = if args.len() == 4 { args.int(3)? } else { -1 };
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(no_key()),
        Doc::Here(body) => body,
    };
    let (after, gone) = {
        let root = readable(&body.doc)?;
        let found = arrays(&root, &path)?;
        if path.legacy() {
            any_array(&found)?;
        }
        let mut at = Vec::new();
        // Two levels of nothing, and they are not the same nothing. The outer
        // one is a match this command did not touch at all because it was not
        // an array, which [`Pick`] skips over. The inner one is an array it did
        // look at and found nothing in, which is an answer and is the answer a
        // legacy path gets if that array came first.
        let mut gone: Vec<Option<Option<Vec<u8>>>> = Vec::with_capacity(found.len());
        for f in &found {
            match f {
                // An empty array pops nothing and is not an error, on either
                // syntax, which is the one case where a legacy path that named
                // a real array still answers nil.
                Some((_, v)) if v.is_empty() => gone.push(Some(None)),
                Some((off, v)) => {
                    let i = reach(want, v.len());
                    let mut text = Vec::new();
                    match v.at(i) {
                        Some(e) => e.write_json(&mut text)?,
                        None => return Err(missing()),
                    }
                    at.push((
                        *off,
                        Edit::Splice {
                            at: i,
                            take: 1,
                            put: &[],
                        },
                    ));
                    gone.push(Some(Some(text)));
                }
                None => gone.push(None),
            }
        }
        (edit(&root, &at)?, gone)
    };
    body.doc = after;
    if path.legacy() {
        match Pick::First.of(&gone) {
            Some(Some(text)) => out.bulk(text),
            _ => out.nil(),
        }
        return Ok(());
    }
    out.array(gone.len());
    for g in &gone {
        match g {
            Some(Some(text)) => out.bulk(text),
            _ => out.nil(),
        }
    }
    Ok(())
}

/// Which element of an array of `len` an index reaches, clamped to one that is
/// there.
///
/// Unlike the insert index this never refuses. A pop past the end takes the
/// last element and a pop before the front takes the first, and `len` is never
/// zero here because an empty array is handled before this is called.
fn reach(want: i64, len: usize) -> usize {
    let n = len as i64;
    let at = if want < 0 { n + want } else { want };
    at.clamp(0, n - 1) as usize
}

/// `JSON.ARRINDEX key path value [start [stop]]`.
///
/// The one array command whose errors do not match the others. A legacy path
/// that matched nothing is `Path does not exist` and one that matched something
/// that is not an array is a `WRONGTYPE`, where every other command here says
/// `Path does not exist or not an array` for both.
fn arrindex(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let (key, raw, text) = (args.get(1), args.get(2), args.get(3));
    if args.len() < 4 || args.len() > 6 {
        return Err(args::wrong_arity("json.arrindex"));
    }
    let path = path_of(raw)?;
    let looking = yo_doc::from_json(text)?;
    let from = if args.len() > 4 { args.int(4)? } else { 0 };
    // Zero is the end of the array rather than the front of it, so the default
    // searches everything and a stop that was left off means the same thing as
    // one that was given as zero.
    let to = if args.len() > 5 { args.int(5)? } else { 0 };
    let body = match doc(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(missing()),
        Doc::Here(body) => body,
    };
    let root = readable(&body.doc)?;
    let looking = readable(&looking)?;
    let mut hits = Vec::new();
    path.select(&root, &mut hits);
    if path.legacy() {
        let Some(v) = hits.first() else {
            return Err(missing());
        };
        if v.kind() != Kind::Array {
            return Err(Error::new(
                Code::WrongType,
                "wrong type of path value - expected array",
            ));
        }
        out.int(seek(v, &looking, from, to));
        return Ok(());
    }
    out.array(hits.len());
    for v in &hits {
        if v.kind() == Kind::Array {
            out.int(seek(v, &looking, from, to));
        } else {
            out.nil();
        }
    }
    Ok(())
}

/// Where `looking` first sits in `v` between `from` and `to`, or `-1`.
///
/// `to` is exclusive, which is not what the rest of Redis does with a stop and
/// is what RedisJSON does here, and zero means the end rather than the front.
///
/// `from` counts back from the end when it is negative and is then clamped to
/// the last element rather than to one past it, so a start of five into an array
/// of four still looks at the fourth. That reads like a mistake and it is what
/// RedisJSON does, checked on `[1,2,3,1]` where a start of four, five or minus
/// one all answer three. An empty array answers minus one whatever the start is,
/// which falls out of the stop being zero.
fn seek(v: &Value<'_>, looking: &Value<'_>, from: i64, to: i64) -> i64 {
    let n = v.len() as i64;
    let start = if from < 0 { n + from } else { from }.clamp(0, (n - 1).max(0));
    let stop = if to == 0 {
        n
    } else if to < 0 {
        (n + to).max(0)
    } else {
        to.min(n)
    };
    let mut i = start;
    while i < stop {
        if let Some(e) = v.at(i as usize)
            && same(&e, looking)
        {
            return i;
        }
        i += 1;
    }
    -1
}

/// Whether two values are the same value.
///
/// Structural rather than a comparison of the encoded bytes, because an object
/// inside a stored document may hold its keys as intern table ids where the one
/// parsed off the wire holds them as bytes, and those two encode differently
/// while meaning the same thing.
fn same(a: &Value<'_>, b: &Value<'_>) -> bool {
    if a.kind() != b.kind() {
        return false;
    }
    match a.kind() {
        Kind::Null => true,
        Kind::Bool => a.as_bool() == b.as_bool(),
        Kind::Int => a.as_int() == b.as_int(),
        Kind::Float => a.as_float() == b.as_float(),
        Kind::Text => a.text_bytes() == b.text_bytes(),
        Kind::Array => {
            a.len() == b.len()
                && (0..a.len()).all(|i| match (a.at(i), b.at(i)) {
                    (Some(x), Some(y)) => same(&x, &y),
                    _ => false,
                })
        }
        Kind::Object => {
            a.len() == b.len()
                && (0..a.len()).all(|i| {
                    let key = a.key_at(i);
                    match (key, key.and_then(|k| b.get(k)), a.at(i)) {
                        (Some(_), Some(y), Some(x)) => same(&x, &y),
                        _ => false,
                    }
                })
        }
    }
}

// ------------------------------------------------- the numbers and the strings

/// Which of `JSON.NUMINCRBY`, `JSON.NUMMULTBY` and `JSON.NUMPOWBY` is being
/// run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Arith {
    Add,
    Mul,
    Pow,
}

/// A JSON number, which is an integer until something makes it a double.
///
/// This is the whole reason the number family is not one line. JSON has one
/// number type and every implementation that cares about round tripping keeps
/// two, so `7 + 2` has to answer `9` and `7 + 2.0` has to answer `9.0`, and the
/// document has to hold what the reply said.
#[derive(Debug, Clone, Copy)]
enum Num {
    Int(i64),
    Float(f64),
}

impl Num {
    /// The number a value holds, if it holds one.
    fn of(v: &Value<'_>) -> Option<Num> {
        match v.kind() {
            Kind::Int => v.as_int().map(Num::Int),
            Kind::Float => v.as_float().map(Num::Float),
            _ => None,
        }
    }

    /// The same number as a double, which is what mixed arithmetic works in.
    fn as_f64(self) -> f64 {
        match self {
            Num::Int(i) => i as f64,
            Num::Float(f) => f,
        }
    }

    /// The number as a document value.
    fn encode(self) -> Result<Vec<u8>> {
        let mut b = Builder::new();
        match self {
            Num::Int(i) => b.int(i)?,
            Num::Float(f) => b.float(f)?,
        }
        Ok(b.finish()?.to_vec())
    }
}

impl Arith {
    /// `a` combined with `b`, in integers when both of them are integers.
    ///
    /// Two integers are checked and overflow is refused rather than promoted to
    /// a double, so `JSON.NUMINCRBY` on the largest integer there is answers an
    /// error and leaves the document alone. A power with a negative exponent
    /// falls into the same error even though nothing overflowed, because the
    /// exponent has to be a count and there is no integer answer to two to the
    /// minus one. Both of those are RedisJSON's, read off 8.10.1.
    ///
    /// Anything with a double in it is done in doubles, and a result that is not
    /// finite is refused, which covers a product that overflowed and the square
    /// root of a negative number in one test.
    fn apply(self, a: Num, b: Num) -> Result<Num> {
        if let (Num::Int(x), Num::Int(y)) = (a, b) {
            let done = match self {
                Arith::Add => x.checked_add(y),
                Arith::Mul => x.checked_mul(y),
                Arith::Pow => u32::try_from(y).ok().and_then(|y| x.checked_pow(y)),
            };
            return done.map(Num::Int).ok_or_else(overflowed);
        }
        let (x, y) = (a.as_f64(), b.as_f64());
        let done = match self {
            Arith::Add => x + y,
            Arith::Mul => x * y,
            Arith::Pow => x.powf(y),
        };
        if !done.is_finite() {
            return Err(not_a_number());
        }
        Ok(Num::Float(done))
    }
}

/// `JSON.NUMINCRBY`, `JSON.NUMMULTBY` and `JSON.NUMPOWBY`, which are one
/// command with three operators.
///
/// The reply is a bulk string holding JSON text rather than a number, on both
/// syntaxes: a legacy path answers the new value as text and a JSONPath answers
/// a JSON array of the new values with a `null` for every match that was not a
/// number. A client that wants the number back has to parse it, which is odd
/// and is what every RedisJSON client already does.
fn arith(db: &mut Keyspace, args: Args<'_>, out: &mut Out, how: Arith) -> Result<()> {
    let (key, raw, operand) = (args.get(1), args.get(2), args.get(3));
    let path = path_of(raw)?;
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(no_key()),
        Doc::Here(body) => body,
    };
    // The new values, encoded, held out here because the edits below borrow
    // them. One entry per match, `None` for a match that was not a number.
    let mut made: Vec<Option<Vec<u8>>> = Vec::new();
    let after = {
        let root = readable(&body.doc)?;
        let mut hits = Vec::new();
        path.select(&root, &mut hits);
        let was: Vec<Option<Num>> = hits.iter().map(Num::of).collect();
        // The operand is looked at only once a match turns out to be a number.
        // `JSON.NUMINCRBY key $.a_string "x"` answers `[null]` on the path and
        // never says anything about the `"x"`, which is RedisJSON's order and
        // matters because the two answers are nothing alike.
        let holder;
        let by = if was.iter().any(Option::is_some) {
            // Parsed as JSON rather than as a number, so ` 3 ` is a three and
            // `1 2` is a parse error rather than a one.
            holder = yo_doc::from_json(operand)?;
            match Num::of(&readable(&holder)?) {
                Some(by) => Some(by),
                None => {
                    // Valid JSON that is not a number, which is `true` or `"3"`
                    // or a whole object. Unprefixed, like the other two lines
                    // this file writes itself.
                    out.error(b"bad input number");
                    return Ok(());
                }
            }
        } else if path.legacy() {
            return Err(no_number());
        } else {
            None
        };
        for w in &was {
            match (w, by) {
                (Some(w), Some(by)) => made.push(Some(how.apply(*w, by)?.encode()?)),
                _ => made.push(None),
            }
        }
        let mut at = Vec::new();
        for (v, m) in hits.iter().zip(&made) {
            if let Some(bytes) = m {
                at.push((offset(&root, v)?, Edit::Set(bytes)));
            }
        }
        edit(&root, &at)?
    };
    body.doc = after;

    // The text is written off the encoded value rather than off the number, so
    // what the client reads back is the same bytes `JSON.GET` will answer with.
    let mut text = Vec::new();
    if path.legacy() {
        if let Some(bytes) = Pick::Last.of(&made) {
            readable(bytes)?.write_json(&mut text)?;
        }
        out.bulk(&text);
        return Ok(());
    }
    text.push(b'[');
    for (i, m) in made.iter().enumerate() {
        if i > 0 {
            text.push(b',');
        }
        match m {
            Some(bytes) => readable(bytes)?.write_json(&mut text)?,
            None => text.extend_from_slice(b"null"),
        }
    }
    text.push(b']');
    out.bulk(&text);
    Ok(())
}

/// `JSON.STRAPPEND key [path] value`.
///
/// The path is optional and sits in the middle, which no other command here
/// does, so the shape has to be read off the count: three arguments means the
/// value is the last one and the path is the root. Anything past the value is
/// ignored rather than refused, which is RedisJSON's and is why the arity is
/// open ended.
fn strappend(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let key = args.get(1);
    let (raw, text) = if args.len() == 3 {
        (ROOT, args.get(2))
    } else {
        (args.get(2), args.get(3))
    };
    let path = path_of(raw)?;
    let body = match doc_mut(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => return Err(no_key()),
        Doc::Here(body) => body,
    };
    let mut made: Vec<Option<Vec<u8>>> = Vec::new();
    let after = {
        let root = readable(&body.doc)?;
        let mut hits = Vec::new();
        path.select(&root, &mut hits);
        let was: Vec<Option<&[u8]>> = hits.iter().map(Value::text_bytes).collect();
        // The value is looked at only once a match turns out to be a string,
        // the same order the number family follows.
        let holder;
        let more = if was.iter().any(Option::is_some) {
            holder = yo_doc::from_json(text)?;
            // The value is JSON and has to be a JSON string, so a client
            // appends `"a"` and not `a`. A value that is a number is a
            // `WRONGTYPE` about a path value even though it was the value and
            // not the path that was wrong, which reads like the check was
            // written in the wrong place and is what the module answers.
            match readable(&holder)?.text_bytes() {
                Some(more) => Some(more),
                None => {
                    return Err(Error::new(
                        Code::WrongType,
                        "wrong type of path value - expected string",
                    ));
                }
            }
        } else if path.legacy() {
            return Err(not_a_string());
        } else {
            None
        };
        for w in &was {
            match (w, more) {
                (Some(was), Some(more)) => {
                    let mut joined = Vec::with_capacity(was.len() + more.len());
                    joined.extend_from_slice(was);
                    joined.extend_from_slice(more);
                    let mut b = Builder::new();
                    b.text_bytes(&joined)?;
                    made.push(Some(b.finish()?.to_vec()));
                }
                _ => made.push(None),
            }
        }
        let mut at = Vec::new();
        for (v, m) in hits.iter().zip(&made) {
            if let Some(bytes) = m {
                at.push((offset(&root, v)?, Edit::Set(bytes)));
            }
        }
        edit(&root, &at)?
    };
    body.doc = after;

    // The length is in bytes and not in characters, so appending one `é` to a
    // two byte string answers four.
    let lens: Vec<Option<usize>> = made
        .iter()
        .map(|m| {
            m.as_ref()
                .and_then(|b| Value::new(b))
                .and_then(|v| v.text_bytes())
                .map(<[u8]>::len)
        })
        .collect();
    lengths(&path, &lens, Pick::Last, out);
    Ok(())
}

// ------------------------------------------------------- the two odd commands

/// `JSON.RESP key [path]`, the document as RESP types rather than as JSON text.
///
/// An object is an array whose first element is the simple string `{` followed
/// by its members flattened into key, value, key, value. An array is an array
/// whose first element is the simple string `[`. An integer is an integer, a
/// double is a bulk string holding its JSON text, a string is a bulk string, a
/// boolean is the simple string `true` or `false` and a null is a nil. The
/// reason it exists is that a client can walk the answer without a JSON parser,
/// and the reason the two containers carry a marker element is that an empty
/// array and an empty object would otherwise both be an empty array.
fn resp(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    let key = args.get(1);
    // Anything past the path is ignored, the same as `JSON.DEL`.
    let raw = args.opt(2).unwrap_or(ROOT);
    let path = Path::parse(raw)?;
    let body = match doc(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => {
            out.nil();
            return Ok(());
        }
        Doc::Here(body) => body,
    };
    let root = readable(&body.doc)?;
    if path.is_projection() {
        // A projection answers a flat array of what it worked out, and it does
        // that for a legacy path too, since there is no place in the document
        // for the legacy shape to be about.
        let got = path.project(&root);
        out.array(got.len());
        for v in &got {
            computed(v, out)?;
        }
        return Ok(());
    }
    let mut hits = Vec::new();
    path.select(&root, &mut hits);
    if path.legacy() {
        let Some(v) = hits.first() else {
            return Err(missing());
        };
        return shape(v, out);
    }
    out.array(hits.len());
    for v in &hits {
        shape(v, out)?;
    }
    Ok(())
}

/// One value a projection worked out, in the shapes `JSON.RESP` uses for the
/// same kinds: a whole number is an integer and anything else is a string.
fn computed(v: &Computed<'_>, out: &mut Out) -> Result<()> {
    match v {
        Computed::Value(v) => shape(v, out),
        Computed::Name(k) => {
            out.bulk(k);
            Ok(())
        }
        Computed::Int(i) => {
            out.int(*i);
            Ok(())
        }
        Computed::Float(x) => {
            let mut text = Vec::new();
            write_resp_float(*x, &mut text);
            out.bulk(&text);
            Ok(())
        }
    }
}

/// One value, as the RESP `JSON.RESP` answers with.
fn shape(v: &Value<'_>, out: &mut Out) -> Result<()> {
    match v.kind() {
        Kind::Null => out.nil(),
        Kind::Bool => out.simple(if v.as_bool() == Some(true) {
            b"true"
        } else {
            b"false"
        }),
        Kind::Int => out.int(v.as_int().ok_or_else(damaged)?),
        Kind::Float => {
            // A double goes out as text and not as a RESP double, so the reply
            // is the same on both protocols. The text is not the JSON writer's,
            // because Redis has its own number to text routine outside JSON and
            // this reply goes through that one.
            let mut text = Vec::new();
            write_resp_float(v.as_float().ok_or_else(damaged)?, &mut text);
            out.bulk(&text);
        }
        Kind::Text => out.bulk(v.text_bytes().ok_or_else(damaged)?),
        Kind::Array => {
            out.array(v.len() + 1);
            out.simple(b"[");
            for e in v.iter() {
                shape(&e, out)?;
            }
        }
        Kind::Object => {
            out.array(v.len() * 2 + 1);
            out.simple(b"{");
            for i in 0..v.len() {
                out.bulk(v.key_at(i).ok_or_else(damaged)?);
                shape(&v.at(i).ok_or_else(damaged)?, out)?;
            }
        }
    }
    Ok(())
}

/// `JSON.DEBUG MEMORY key [path]` and `JSON.DEBUG HELP`.
///
/// The byte counts are this encoding's and not RedisJSON's, which is D-42: the
/// two servers do not store a document the same way, so there is no arrangement
/// under which the same document would answer the same number twice.
fn debug(db: &mut Keyspace, args: Args<'_>, out: &mut Out) -> Result<()> {
    if args::is(args.get(1), b"help") {
        // Two lines, spaced the way the module spaces them.
        out.array(2);
        out.bulk(b"MEMORY <key> [path] - reports memory usage");
        out.bulk(b"HELP                - this message");
        return Ok(());
    }
    if !args::is(args.get(1), b"memory") {
        return Err(Error::new(
            Code::Invalid,
            "unknown subcommand - try `JSON.DEBUG HELP`",
        ));
    }
    if args.len() < 3 {
        return Err(args::wrong_arity("json.debug"));
    }
    let key = args.get(2);
    let raw = args.opt(3).unwrap_or(ROOT);
    let path = path_of(raw)?;
    let body = match doc(db, key, out)? {
        Doc::Wrong => return Ok(()),
        Doc::Gone => {
            // A key that is not there is a zero on a legacy path and an empty
            // set on a JSONPath, which is the one reader here that does not
            // answer a nil for it.
            if path.legacy() {
                out.int(0);
            } else {
                out.array(0);
            }
            return Ok(());
        }
        Doc::Here(body) => body,
    };
    let root = readable(&body.doc)?;
    let mut hits = Vec::new();
    path.select(&root, &mut hits);
    if path.legacy() {
        let Some(v) = hits.first() else {
            return Err(missing());
        };
        out.int(i64::try_from(v.encoded_len().ok_or_else(damaged)?).unwrap_or(i64::MAX));
        return Ok(());
    }
    out.array(hits.len());
    for v in &hits {
        out.int(i64::try_from(v.encoded_len().ok_or_else(damaged)?).unwrap_or(i64::MAX));
    }
    Ok(())
}

// ----------------------------------------------------------------- the plumbing

/// What a lookup of a document key found.
///
/// Three answers and not two, because the wrong type is a line this file writes
/// itself rather than an error it returns, and the caller has to know that the
/// reply is already written.
enum Doc<B> {
    /// The key holds a document.
    Here(B),
    /// The key is not there, which every command answers differently.
    Gone,
    /// The key holds something else and the error line is already out.
    Wrong,
}

/// The body under `key` for writing.
fn doc_mut<'d>(db: &'d mut Keyspace, key: &[u8], out: &mut Out) -> Result<Doc<&'d mut JsonBody>> {
    match db.foreign_mut(key) {
        Ok(Some(body)) => match body.downcast_mut::<JsonBody>() {
            Some(body) => Ok(Doc::Here(body)),
            None => {
                out.error(WRONG_TYPE);
                Ok(Doc::Wrong)
            }
        },
        Ok(None) => Ok(Doc::Gone),
        Err(e) if e.code() == Code::WrongType => {
            out.error(WRONG_TYPE);
            Ok(Doc::Wrong)
        }
        Err(e) => Err(e),
    }
}

/// The same, for reading.
fn doc<'d>(db: &'d mut Keyspace, key: &[u8], out: &mut Out) -> Result<Doc<&'d JsonBody>> {
    match db.foreign(key) {
        Ok(Some(body)) => match body.downcast_ref::<JsonBody>() {
            Some(body) => Ok(Doc::Here(body)),
            None => {
                out.error(WRONG_TYPE);
                Ok(Doc::Wrong)
            }
        },
        Ok(None) => Ok(Doc::Gone),
        Err(e) if e.code() == Code::WrongType => {
            out.error(WRONG_TYPE);
            Ok(Doc::Wrong)
        }
        Err(e) => Err(e),
    }
}

/// `INDENT`, `NEWLINE` and `SPACE` in any order, and where the paths start.
///
/// Redis takes these three as strings rather than as counts, so a client can
/// ask for tabs, and the default for all three is empty, which is one line.
fn format(args: Args<'_>, from: usize) -> Result<(Format<'_>, usize)> {
    let mut f = Format::default();
    let mut i = from;
    while i + 1 < args.len() {
        let arg = args.get(i);
        if args::is(arg, b"indent") {
            f.indent = args.get(i + 1);
        } else if args::is(arg, b"newline") {
            f.newline = args.get(i + 1);
        } else if args::is(arg, b"space") {
            f.space = args.get(i + 1);
        } else {
            break;
        }
        i += 2;
    }
    Ok((f, i))
}

/// A path as a JSON string, for the key of the object more than one path
/// answers.
fn quote(raw: &[u8], out: &mut Vec<u8>) {
    let mut b = Builder::new();
    // A path is bytes off the wire and could hold anything, so it goes through
    // the same escaping every other string does rather than being copied
    // between two quotes.
    if b.text_bytes(raw).is_ok()
        && let Ok(bytes) = b.finish()
        && let Some(v) = Value::new(bytes)
        && v.write_json(out).is_ok()
    {
        return;
    }
    out.extend_from_slice(b"\"\"");
}

/// The offsets of a set of matches inside the document they came out of.
fn offsets(root: &Value<'_>, hits: &[Value<'_>]) -> Result<Vec<usize>> {
    hits.iter().map(|v| offset(root, v)).collect()
}

/// The offset of one match.
fn offset(root: &Value<'_>, v: &Value<'_>) -> Result<usize> {
    v.offset_in(root).ok_or_else(|| {
        Error::new(
            Code::Invalid,
            "a path matched a value that is not in the document it was matched against",
        )
    })
}

/// The document under a key, as a value.
fn readable(doc: &[u8]) -> Result<Value<'_>> {
    Value::new(doc).ok_or_else(damaged)
}

/// What a walk that ran into bytes it could not read gets.
///
/// Only reachable if a stored document is damaged, since every document here is
/// built by this server. It is an error rather than a panic because a client
/// asking for a key it has no way of knowing is broken should get a line back
/// and not a dropped connection.
fn damaged() -> Error {
    Error::new(Code::Invalid, "the document stored here is damaged")
}

/// Send an error with no `ERR` in front of it, which is how RedisJSON answers a
/// value that is not JSON on `JSON.SET`, `JSON.ARRAPPEND`, `JSON.ARRINSERT`,
/// `JSON.MERGE` and `JSON.MSET`.
///
/// It does prefix the same error on `JSON.ARRINDEX`, `JSON.STRAPPEND` and the
/// number family, so this is not a rule about parse errors, it is a rule about
/// which command the client sent. The wording is ours either way and is D-37.
fn unprefixed(e: &Error, out: &mut Out) {
    out.error_line(b"", e.message().as_bytes());
}

/// What a legacy path that named nothing gets.
///
/// The path is not in it. RedisJSON quotes the path in some of its other
/// messages and not in this one, and this is the one clients see.
fn missing() -> Error {
    Error::new(Code::Invalid, "Path does not exist")
}

/// What a legacy `JSON.TOGGLE` gets for a path that matched nothing and for a
/// path that matched something that is not a boolean, which is one message.
fn not_a_bool() -> Error {
    Error::new(Code::Invalid, "Path does not exist or not a bool")
}

/// What a legacy number command gets when no match was a number.
///
/// The wording is RedisJSON's, typo and all, and it is one line for a path that
/// matched nothing and for one that matched a string.
fn no_number() -> Error {
    Error::new(
        Code::Invalid,
        "Path does not exist or does not contains a number",
    )
}

/// What a legacy `JSON.STRAPPEND` gets when no match was a string.
fn not_a_string() -> Error {
    Error::new(Code::Invalid, "Path does not exist or not a string")
}

/// What arithmetic that ran off the end of an integer gets.
fn overflowed() -> Error {
    Error::new(Code::Invalid, "numeric overflow")
}

/// What arithmetic that left the real numbers gets.
fn not_a_number() -> Error {
    Error::new(Code::Invalid, "result is not a number")
}

/// What the commands that need a document get when there is not one.
fn no_key() -> Error {
    Error::new(
        Code::Invalid,
        "could not perform this operation on a key that doesn't exist",
    )
}