dynomite-engine 0.0.2

Embeddable Dynamo-style distributed replication engine: token-ring partitioning, gossip cluster, hinted handoff, anti-entropy, RediSearch FT.* surface.
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
//! Memcached text-protocol parser.
//!
//! The parser is a single byte-driven state machine for requests
//! and another for responses, faithfully reproducing the state set
//! and transitions of `memcache_parse_req` and `memcache_parse_rsp`
//! in the reference engine. It mutates a [`Msg`] in place: the
//! command tag, key list, value-length accumulator, and parser
//! cursor are written back so the streaming caller can resume the
//! machine on more bytes without re-allocating state.
//!
//! The parsers MUST NOT panic on any input. Invalid bytes are
//! reported via [`MsgParseResult::Error`].

// The parser truncates ASCII-decimal accumulators into fixed-width
// counters that match the reference engine (`uint32_t` for vlen,
// `usize` for cursor offsets). The allowance keeps the Rust port
// faithful to the C casts.
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::match_same_arms)]
#![allow(clippy::needless_continue)]
// The state machine deliberately keeps the C `if (token == NULL)`
// guard pattern; rewriting as `let-else` collapses two branches
// the reference engine treats independently.
#![allow(clippy::manual_let_else)]
#![allow(clippy::redundant_else)]

use super::commands::{
    memcache_arithmetic, memcache_cas, memcache_delete, memcache_retrieval, memcache_storage,
    memcache_touch,
};
use crate::msg::{KeyPos, Msg, MsgParseResult, MsgType};

/// Maximum allowed Memcached key length in bytes.
pub const MEMCACHE_MAX_KEY_LENGTH: usize = 250;

/// Optional hash tag delimiters. When set, parsed keys carry the
/// inner range between the delimiters as the routing tag.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct HashTag {
    /// Opening byte of the hash tag.
    pub open: u8,
    /// Closing byte of the hash tag.
    pub close: u8,
}

/// State alphabet for [`memcache_parse_req`].
///
/// The numeric values match the reference engine's request state
/// indices so external parity tooling can compare them directly.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
#[repr(u32)]
pub enum ReqState {
    /// Initial state.
    #[default]
    Start = 0,
    /// Reading the command keyword.
    ReqType = 1,
    /// Skipping spaces before the first key.
    SpacesBeforeKey = 2,
    /// Reading a key.
    Key = 3,
    /// Skipping spaces between keys (`get`/`gets`).
    SpacesBeforeKeys = 4,
    /// Skipping spaces before the storage flags field.
    SpacesBeforeFlags = 5,
    /// Reading the storage flags field.
    Flags = 6,
    /// Skipping spaces before the storage expiry field.
    SpacesBeforeExpiry = 7,
    /// Reading the storage expiry field.
    Expiry = 8,
    /// Skipping spaces before the value-length field.
    SpacesBeforeVlen = 9,
    /// Reading the value-length field.
    Vlen = 10,
    /// Skipping spaces before the CAS unique field.
    SpacesBeforeCas = 11,
    /// Reading the CAS unique field.
    Cas = 12,
    /// Awaiting LF before the value bytes.
    RuntoVal = 13,
    /// Consuming the value bytes.
    Val = 14,
    /// Skipping spaces before the arithmetic numeric argument.
    SpacesBeforeNum = 15,
    /// Reading the arithmetic numeric argument.
    Num = 16,
    /// Eating optional trailing bytes up to CR.
    RuntoCrlf = 17,
    /// Awaiting trailing CR.
    Crlf = 18,
    /// Reading the optional `noreply` token.
    Noreply = 19,
    /// State after consuming `noreply`.
    AfterNoreply = 20,
    /// Awaiting the trailing LF that terminates the request.
    AlmostDone = 21,
}

impl ReqState {
    fn from_u32(v: u32) -> Self {
        match v {
            1 => Self::ReqType,
            2 => Self::SpacesBeforeKey,
            3 => Self::Key,
            4 => Self::SpacesBeforeKeys,
            5 => Self::SpacesBeforeFlags,
            6 => Self::Flags,
            7 => Self::SpacesBeforeExpiry,
            8 => Self::Expiry,
            9 => Self::SpacesBeforeVlen,
            10 => Self::Vlen,
            11 => Self::SpacesBeforeCas,
            12 => Self::Cas,
            13 => Self::RuntoVal,
            14 => Self::Val,
            15 => Self::SpacesBeforeNum,
            16 => Self::Num,
            17 => Self::RuntoCrlf,
            18 => Self::Crlf,
            19 => Self::Noreply,
            20 => Self::AfterNoreply,
            21 => Self::AlmostDone,
            _ => Self::Start,
        }
    }
}

/// State alphabet for [`memcache_parse_rsp`].
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
#[repr(u32)]
pub enum RspState {
    /// Initial state.
    #[default]
    Start = 0,
    /// Reading a numeric response (`incr`/`decr` reply).
    RspNum = 1,
    /// Reading a textual response keyword.
    RspStr = 2,
    /// Skipping spaces before the key in a `VALUE` reply.
    SpacesBeforeKey = 3,
    /// Reading the key portion of a `VALUE` reply.
    Key = 4,
    /// Skipping spaces before the flags field.
    SpacesBeforeFlags = 5,
    /// Reading the flags field.
    Flags = 6,
    /// Skipping spaces before the value-length field.
    SpacesBeforeVlen = 7,
    /// Reading the value-length field.
    Vlen = 8,
    /// Awaiting LF before the value bytes.
    RuntoVal = 9,
    /// Consuming the value bytes.
    Val = 10,
    /// Awaiting LF after the value.
    ValLf = 11,
    /// Reading the trailing `END` token.
    End = 12,
    /// Eating optional trailing bytes up to CR.
    RuntoCrlf = 13,
    /// Awaiting trailing CR.
    Crlf = 14,
    /// Awaiting the trailing LF that terminates the response.
    AlmostDone = 15,
}

impl RspState {
    fn from_u32(v: u32) -> Self {
        match v {
            1 => Self::RspNum,
            2 => Self::RspStr,
            3 => Self::SpacesBeforeKey,
            4 => Self::Key,
            5 => Self::SpacesBeforeFlags,
            6 => Self::Flags,
            7 => Self::SpacesBeforeVlen,
            8 => Self::Vlen,
            9 => Self::RuntoVal,
            10 => Self::Val,
            11 => Self::ValLf,
            12 => Self::End,
            13 => Self::RuntoCrlf,
            14 => Self::Crlf,
            15 => Self::AlmostDone,
            _ => Self::Start,
        }
    }
}

const CR: u8 = b'\r';
const LF: u8 = b'\n';

fn classify_command(token: &[u8]) -> MsgType {
    match token {
        b"get" => MsgType::ReqMcGet,
        b"gets" => MsgType::ReqMcGets,
        b"set" => MsgType::ReqMcSet,
        b"add" => MsgType::ReqMcAdd,
        b"cas" => MsgType::ReqMcCas,
        b"incr" => MsgType::ReqMcIncr,
        b"decr" => MsgType::ReqMcDecr,
        b"quit" => MsgType::ReqMcQuit,
        b"touch" => MsgType::ReqMcTouch,
        b"append" => MsgType::ReqMcAppend,
        b"delete" => MsgType::ReqMcDelete,
        b"prepend" => MsgType::ReqMcPrepend,
        b"replace" => MsgType::ReqMcReplace,
        _ => MsgType::Unknown,
    }
}

fn classify_response(token: &[u8]) -> MsgType {
    match token {
        b"END" => MsgType::RspMcEnd,
        b"VALUE" => MsgType::RspMcValue,
        b"ERROR" => MsgType::RspMcError,
        b"STORED" => MsgType::RspMcStored,
        b"EXISTS" => MsgType::RspMcExists,
        b"DELETED" => MsgType::RspMcDeleted,
        b"TOUCHED" => MsgType::RspMcTouched,
        b"NOT_FOUND" => MsgType::RspMcNotFound,
        b"NOT_STORED" => MsgType::RspMcNotStored,
        b"CLIENT_ERROR" => MsgType::RspMcClientError,
        b"SERVER_ERROR" => MsgType::RspMcServerError,
        _ => MsgType::Unknown,
    }
}

fn make_keypos(input: &[u8], start: usize, end: usize, hash_tag: Option<HashTag>) -> KeyPos {
    let bytes = input[start..end].to_vec();
    if let Some(tag) = hash_tag {
        if let Some(open_idx) = bytes.iter().position(|&b| b == tag.open) {
            if let Some(close_offset) = bytes[open_idx + 1..].iter().position(|&b| b == tag.close) {
                let tag_start = open_idx + 1;
                let tag_end = open_idx + 1 + close_offset;
                return KeyPos::new(bytes, tag_start..tag_end);
            }
        }
    }
    KeyPos::without_tag(bytes)
}

/// Parse a Memcached request from `input` and update `r` in place.
///
/// The function reproduces the reference engine's
/// `memcache_parse_req` state machine. On success, `r.ty()` is set
/// to the recognised command, parsed keys are appended to
/// [`Msg::keys`], and the parser cursor (`parser_pos`) advances
/// just past the trailing LF. On truncated input the function
/// returns [`MsgParseResult::Again`] and stores the partial state
/// on `r` for resumption. Invalid bytes return
/// [`MsgParseResult::Error`].
///
/// `hash_tag`, when set, configures the routing-tag delimiters used
/// when populating [`KeyPos::tag`].
///
/// # Examples
///
/// ```
/// use dynomite::msg::{Msg, MsgParseResult, MsgType};
/// use dynomite::proto::memcache::memcache_parse_req;
///
/// let mut r = Msg::new(0, MsgType::Unknown, true);
/// let res = memcache_parse_req(&mut r, b"set foo 0 0 3\r\nbar\r\n");
/// assert_eq!(res, MsgParseResult::Ok);
/// assert_eq!(r.ty(), MsgType::ReqMcSet);
/// assert_eq!(r.keys()[0].key(), b"foo");
/// assert_eq!(r.vlen(), 3);
/// ```
///
/// The state machine intentionally lives in a single function to
/// match the reference engine's parser shape.
#[allow(clippy::too_many_lines)]
pub fn memcache_parse_req(r: &mut Msg, input: &[u8]) -> MsgParseResult {
    memcache_parse_req_tagged(r, input, None)
}

/// Variant of [`memcache_parse_req`] that accepts an explicit
/// hash-tag configuration.
///
/// # Examples
///
/// ```
/// use dynomite::msg::{Msg, MsgType};
/// use dynomite::proto::memcache::parser::{memcache_parse_req_tagged, HashTag};
///
/// let mut r = Msg::new(0, MsgType::Unknown, true);
/// let tag = Some(HashTag { open: b'{', close: b'}' });
/// let _ = memcache_parse_req_tagged(&mut r, b"get {abc}xyz\r\n", tag);
/// assert_eq!(r.keys()[0].tag_bytes(), b"abc");
/// ```
#[allow(clippy::too_many_lines)]
pub fn memcache_parse_req_tagged(
    r: &mut Msg,
    input: &[u8],
    hash_tag: Option<HashTag>,
) -> MsgParseResult {
    if !r.is_request() {
        r.set_parse_result(MsgParseResult::Error);
        return MsgParseResult::Error;
    }
    let mut state = ReqState::from_u32(r.parser_state());
    let mut p = r.parser_pos();
    let mut token: Option<usize> = r.parser_token();
    let mut vlen = r.vlen();
    let mut ty = r.ty();
    let mut is_read = r.flags().is_read;
    let mut quit = r.flags().quit;
    let mut expect_reply = r.flags().expect_datastore_reply;
    let mut ntokens = r.ntokens();

    'machine: while p < input.len() {
        let ch = input[p];
        match state {
            ReqState::Start => {
                if ch == b' ' {
                    p += 1;
                    continue;
                }
                if !ch.is_ascii_lowercase() {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
                token = Some(p);
                state = ReqState::ReqType;
                // Do not advance; re-enter ReqType on the same byte.
            }
            ReqState::ReqType => {
                if ch == b' ' || ch == CR {
                    let start = match token {
                        Some(s) => s,
                        None => {
                            return finish_error(
                                r, state, p, token, vlen, ty, is_read, quit, ntokens,
                            );
                        }
                    };
                    let cmd = &input[start..p];
                    token = None;
                    ty = classify_command(cmd);
                    ntokens = ntokens.saturating_add(1);
                    is_read = matches!(
                        ty,
                        MsgType::ReqMcGet | MsgType::ReqMcGets | MsgType::ReqMcQuit
                    );
                    if matches!(ty, MsgType::ReqMcQuit) {
                        quit = true;
                        // The C parser sets state to SW_CRLF and steps p back by one.
                        state = ReqState::Crlf;
                        // Do not advance; re-enter on this same byte.
                        continue;
                    }
                    match ty {
                        MsgType::ReqMcGet
                        | MsgType::ReqMcGets
                        | MsgType::ReqMcDelete
                        | MsgType::ReqMcCas
                        | MsgType::ReqMcSet
                        | MsgType::ReqMcAdd
                        | MsgType::ReqMcReplace
                        | MsgType::ReqMcAppend
                        | MsgType::ReqMcPrepend
                        | MsgType::ReqMcIncr
                        | MsgType::ReqMcDecr
                        | MsgType::ReqMcTouch => {
                            if ch == CR {
                                return finish_error(
                                    r, state, p, token, vlen, ty, is_read, quit, ntokens,
                                );
                            }
                            state = ReqState::SpacesBeforeKey;
                            p += 1;
                            continue;
                        }
                        MsgType::Unknown => {
                            return finish_error(
                                r, state, p, token, vlen, ty, is_read, quit, ntokens,
                            );
                        }
                        _ => {
                            return finish_error(
                                r, state, p, token, vlen, ty, is_read, quit, ntokens,
                            );
                        }
                    }
                } else if !ch.is_ascii_lowercase() {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                } else {
                    p += 1;
                }
            }
            ReqState::SpacesBeforeKey => {
                if ch == b' ' {
                    p += 1;
                } else {
                    token = None;
                    state = ReqState::Key;
                    // Do not advance; re-process this byte under Key state.
                }
            }
            ReqState::Key => {
                if token.is_none() {
                    token = Some(p);
                }
                if ch == b' ' || ch == CR {
                    let start = token.expect("token recorded");
                    let keylen = p - start;
                    if keylen == 0 || keylen > MEMCACHE_MAX_KEY_LENGTH {
                        return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                    }
                    let kp = make_keypos(input, start, p, hash_tag);
                    r.push_key(kp);
                    ntokens = ntokens.saturating_add(1);
                    token = None;
                    let storage = memcache_storage(ty);
                    let arithmetic = memcache_arithmetic(ty);
                    let touch = memcache_touch(ty);
                    let delete = memcache_delete(ty);
                    let retrieval = memcache_retrieval(ty);
                    if storage {
                        state = ReqState::SpacesBeforeFlags;
                    } else if arithmetic || touch {
                        state = ReqState::SpacesBeforeNum;
                    } else if delete {
                        state = ReqState::RuntoCrlf;
                    } else if retrieval {
                        state = ReqState::SpacesBeforeKeys;
                    } else {
                        state = ReqState::RuntoCrlf;
                    }
                    if ch == CR {
                        if storage || arithmetic {
                            return finish_error(
                                r, state, p, token, vlen, ty, is_read, quit, ntokens,
                            );
                        }
                        // Re-enter on the CR byte (do not advance).
                    } else {
                        p += 1;
                    }
                } else {
                    p += 1;
                }
            }
            ReqState::SpacesBeforeKeys => {
                match ch {
                    b' ' => {
                        p += 1;
                    }
                    CR => {
                        state = ReqState::AlmostDone;
                        p += 1;
                    }
                    _ => {
                        token = None;
                        state = ReqState::Key;
                        // Do not advance.
                    }
                }
            }
            ReqState::SpacesBeforeFlags => {
                if ch == b' ' {
                    p += 1;
                } else if ch.is_ascii_digit() {
                    token = Some(p);
                    state = ReqState::Flags;
                    p += 1;
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::Flags => {
                if ch.is_ascii_digit() {
                    p += 1;
                } else if ch == b' ' {
                    token = None;
                    state = ReqState::SpacesBeforeExpiry;
                    p += 1;
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::SpacesBeforeExpiry => {
                if ch == b' ' {
                    p += 1;
                } else if ch.is_ascii_digit() {
                    token = Some(p);
                    state = ReqState::Expiry;
                    p += 1;
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::Expiry => {
                if ch.is_ascii_digit() {
                    p += 1;
                } else if ch == b' ' {
                    token = None;
                    state = ReqState::SpacesBeforeVlen;
                    p += 1;
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::SpacesBeforeVlen => {
                if ch == b' ' {
                    p += 1;
                } else if ch.is_ascii_digit() {
                    vlen = u32::from(ch - b'0');
                    state = ReqState::Vlen;
                    p += 1;
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::Vlen => {
                if ch.is_ascii_digit() {
                    vlen = vlen.saturating_mul(10).saturating_add(u32::from(ch - b'0'));
                    p += 1;
                } else if memcache_cas(ty) {
                    if ch != b' ' {
                        return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                    }
                    token = None;
                    state = ReqState::SpacesBeforeCas;
                    // Do not advance; re-enter on the same byte.
                } else if ch == b' ' || ch == CR {
                    token = None;
                    state = ReqState::RuntoCrlf;
                    // Do not advance.
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::SpacesBeforeCas => {
                if ch == b' ' {
                    p += 1;
                } else if ch.is_ascii_digit() {
                    token = Some(p);
                    state = ReqState::Cas;
                    p += 1;
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::Cas => {
                if ch.is_ascii_digit() {
                    p += 1;
                } else if ch == b' ' || ch == CR {
                    token = None;
                    state = ReqState::RuntoCrlf;
                    // Do not advance.
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::RuntoVal => match ch {
                LF => {
                    state = ReqState::Val;
                    p += 1;
                }
                _ => {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            },
            ReqState::Val => {
                let m = p.saturating_add(vlen as usize);
                if m >= input.len() {
                    let consumed = input.len() - p;
                    vlen = vlen.saturating_sub(consumed as u32);
                    p = input.len();
                    break 'machine;
                }
                if input[m] != CR {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
                p = m + 1;
                state = ReqState::AlmostDone;
            }
            ReqState::SpacesBeforeNum => {
                if ch == b' ' {
                    p += 1;
                } else if ch.is_ascii_digit() || ch == b'-' {
                    token = Some(p);
                    state = ReqState::Num;
                    p += 1;
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::Num => {
                if ch.is_ascii_digit() {
                    p += 1;
                } else if ch == b' ' || ch == CR {
                    token = None;
                    state = ReqState::RuntoCrlf;
                    // Do not advance.
                } else {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            }
            ReqState::RuntoCrlf => match ch {
                b' ' => {
                    p += 1;
                }
                b'n' => {
                    if memcache_storage(ty)
                        || memcache_arithmetic(ty)
                        || memcache_delete(ty)
                        || memcache_touch(ty)
                    {
                        token = Some(p);
                        state = ReqState::Noreply;
                        p += 1;
                    } else {
                        return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                    }
                }
                CR => {
                    if memcache_storage(ty) {
                        state = ReqState::RuntoVal;
                    } else {
                        state = ReqState::AlmostDone;
                    }
                    p += 1;
                }
                _ => {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            },
            ReqState::Noreply => match ch {
                b' ' | CR => {
                    let start = match token {
                        Some(s) => s,
                        None => {
                            return finish_error(
                                r, state, p, token, vlen, ty, is_read, quit, ntokens,
                            );
                        }
                    };
                    if p - start == 7 && &input[start..p] == b"noreply" {
                        token = None;
                        expect_reply = false;
                        state = ReqState::AfterNoreply;
                        // Do not advance.
                    } else {
                        return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                    }
                }
                _ => {
                    p += 1;
                }
            },
            ReqState::AfterNoreply => match ch {
                b' ' => {
                    p += 1;
                }
                CR => {
                    if memcache_storage(ty) {
                        state = ReqState::RuntoVal;
                    } else {
                        state = ReqState::AlmostDone;
                    }
                    p += 1;
                }
                _ => {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            },
            ReqState::Crlf => match ch {
                b' ' => {
                    p += 1;
                }
                CR => {
                    state = ReqState::AlmostDone;
                    p += 1;
                }
                _ => {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            },
            ReqState::AlmostDone => match ch {
                LF => {
                    return finish_done(r, p + 1, ty, is_read, quit, expect_reply, ntokens, vlen);
                }
                _ => {
                    return finish_error(r, state, p, token, vlen, ty, is_read, quit, ntokens);
                }
            },
        }
    }

    // Reached end of input without completing.
    r.set_parser_state(state as u32);
    r.set_parser_pos(p);
    r.set_parser_token(token);
    r.set_vlen(vlen);
    r.set_ntokens(ntokens);
    if ty != MsgType::Unknown {
        r.set_type(ty);
    }
    r.flags_mut().is_read = is_read;
    r.flags_mut().quit = quit;
    r.flags_mut().expect_datastore_reply = expect_reply;
    r.set_parse_result(MsgParseResult::Again);
    MsgParseResult::Again
}

#[allow(clippy::too_many_arguments)]
fn finish_done(
    r: &mut Msg,
    next_pos: usize,
    ty: MsgType,
    is_read: bool,
    quit: bool,
    expect_reply: bool,
    ntokens: u32,
    vlen: u32,
) -> MsgParseResult {
    r.set_type(ty);
    r.flags_mut().is_read = is_read;
    r.flags_mut().quit = quit;
    r.flags_mut().expect_datastore_reply = expect_reply;
    r.set_ntokens(ntokens);
    r.set_vlen(vlen);
    r.set_parser_state(ReqState::Start as u32);
    r.set_parser_pos(next_pos);
    r.set_parser_token(None);
    r.set_parse_result(MsgParseResult::Ok);
    MsgParseResult::Ok
}

#[allow(clippy::too_many_arguments)]
fn finish_error(
    r: &mut Msg,
    state: ReqState,
    pos: usize,
    token: Option<usize>,
    vlen: u32,
    ty: MsgType,
    is_read: bool,
    quit: bool,
    ntokens: u32,
) -> MsgParseResult {
    r.set_parser_state(state as u32);
    r.set_parser_pos(pos);
    r.set_parser_token(token);
    r.set_vlen(vlen);
    r.set_ntokens(ntokens);
    if ty != MsgType::Unknown {
        r.set_type(ty);
    }
    r.flags_mut().is_read = is_read;
    r.flags_mut().quit = quit;
    r.set_parse_result(MsgParseResult::Error);
    MsgParseResult::Error
}

/// Parse a Memcached response from `input` and update `r` in place.
///
/// On success the response type is recorded and the parser cursor
/// advances just past the trailing LF. The function never panics
/// on any byte sequence.
///
/// # Examples
///
/// ```
/// use dynomite::msg::{Msg, MsgParseResult, MsgType};
/// use dynomite::proto::memcache::memcache_parse_rsp;
///
/// let mut r = Msg::new(0, MsgType::Unknown, false);
/// let res = memcache_parse_rsp(&mut r, b"STORED\r\n");
/// assert_eq!(res, MsgParseResult::Ok);
/// assert_eq!(r.ty(), MsgType::RspMcStored);
/// ```
///
/// The state machine intentionally lives in a single function to
/// match the reference engine.
#[allow(clippy::too_many_lines)]
pub fn memcache_parse_rsp(r: &mut Msg, input: &[u8]) -> MsgParseResult {
    if r.is_request() {
        r.set_parse_result(MsgParseResult::Error);
        return MsgParseResult::Error;
    }
    let mut state = RspState::from_u32(r.parser_state());
    let mut p = r.parser_pos();
    let mut token: Option<usize> = r.parser_token();
    let mut vlen = r.vlen();
    let mut ty = r.ty();
    let mut end_marker = r.end_marker();

    while p < input.len() {
        let ch = input[p];
        match state {
            RspState::Start => {
                if ch.is_ascii_digit() {
                    state = RspState::RspNum;
                } else {
                    state = RspState::RspStr;
                }
                // Do not advance; re-enter under the new state.
            }
            RspState::RspNum => {
                if token.is_none() {
                    token = Some(p);
                }
                if ch.is_ascii_digit() {
                    p += 1;
                } else if ch == b' ' || ch == CR {
                    token = None;
                    ty = MsgType::RspMcNum;
                    state = RspState::Crlf;
                    // Do not advance.
                } else {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            }
            RspState::RspStr => {
                if token.is_none() {
                    token = Some(p);
                }
                if ch == b' ' || ch == CR {
                    let start = token.expect("token recorded");
                    let key_bytes = &input[start..p];
                    ty = classify_response(key_bytes);
                    if ty == MsgType::RspMcEnd {
                        end_marker = Some(start);
                    }
                    match ty {
                        MsgType::Unknown => {
                            return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                        }
                        MsgType::RspMcStored
                        | MsgType::RspMcNotStored
                        | MsgType::RspMcExists
                        | MsgType::RspMcNotFound
                        | MsgType::RspMcDeleted
                        | MsgType::RspMcTouched
                        | MsgType::RspMcEnd
                        | MsgType::RspMcError => {
                            state = RspState::Crlf;
                        }
                        MsgType::RspMcValue => {
                            state = RspState::SpacesBeforeKey;
                        }
                        MsgType::RspMcClientError | MsgType::RspMcServerError => {
                            state = RspState::RuntoCrlf;
                        }
                        _ => {
                            return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                        }
                    }
                    // Do not advance; re-enter on the same byte.
                } else {
                    p += 1;
                }
            }
            RspState::SpacesBeforeKey => {
                if ch == b' ' {
                    p += 1;
                } else {
                    state = RspState::Key;
                    // Do not advance.
                }
            }
            RspState::Key => {
                if ch == b' ' {
                    state = RspState::SpacesBeforeFlags;
                }
                p += 1;
            }
            RspState::SpacesBeforeFlags => {
                if ch == b' ' {
                    p += 1;
                } else if ch.is_ascii_digit() {
                    state = RspState::Flags;
                    // Do not advance.
                } else {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            }
            RspState::Flags => {
                if ch.is_ascii_digit() {
                    p += 1;
                } else if ch == b' ' {
                    state = RspState::SpacesBeforeVlen;
                    p += 1;
                } else {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            }
            RspState::SpacesBeforeVlen => {
                if ch == b' ' {
                    p += 1;
                } else if ch.is_ascii_digit() {
                    state = RspState::Vlen;
                    vlen = 0;
                    // Do not advance.
                } else {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            }
            RspState::Vlen => {
                if ch.is_ascii_digit() {
                    vlen = vlen.saturating_mul(10).saturating_add(u32::from(ch - b'0'));
                    p += 1;
                } else if ch == b' ' || ch == CR {
                    state = RspState::RuntoCrlf;
                    // Do not advance.
                } else {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            }
            RspState::RuntoVal => match ch {
                LF => {
                    state = RspState::Val;
                    token = None;
                    p += 1;
                }
                _ => {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            },
            RspState::Val => {
                let m = p.saturating_add(vlen as usize);
                if m >= input.len() {
                    let consumed = input.len() - p;
                    vlen = vlen.saturating_sub(consumed as u32);
                    p = input.len();
                    break;
                }
                if input[m] != CR {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
                p = m + 1;
                state = RspState::ValLf;
            }
            RspState::ValLf => match ch {
                LF => {
                    state = RspState::RspStr;
                    p += 1;
                }
                _ => {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            },
            RspState::End => {
                if token.is_none() {
                    if ch != b'E' {
                        return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                    }
                    token = Some(p);
                    p += 1;
                } else if ch == CR {
                    let start = token.expect("token recorded");
                    if p - start == 3 && &input[start..p] == b"END" {
                        end_marker = Some(start);
                        state = RspState::AlmostDone;
                        token = None;
                        p += 1;
                    } else {
                        return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                    }
                } else {
                    p += 1;
                }
            }
            RspState::RuntoCrlf => match ch {
                CR => {
                    if ty == MsgType::RspMcValue {
                        state = RspState::RuntoVal;
                    } else {
                        state = RspState::AlmostDone;
                    }
                    p += 1;
                }
                _ => {
                    p += 1;
                }
            },
            RspState::Crlf => match ch {
                b' ' => {
                    p += 1;
                }
                CR => {
                    state = RspState::AlmostDone;
                    p += 1;
                }
                _ => {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            },
            RspState::AlmostDone => match ch {
                LF => {
                    r.set_type(ty);
                    r.set_vlen(vlen);
                    r.set_end_marker(end_marker);
                    r.set_parser_state(RspState::Start as u32);
                    r.set_parser_pos(p + 1);
                    r.set_parser_token(None);
                    r.set_parse_result(MsgParseResult::Ok);
                    return MsgParseResult::Ok;
                }
                _ => {
                    return finish_error_rsp(r, state, p, token, vlen, ty, end_marker);
                }
            },
        }
    }

    r.set_parser_state(state as u32);
    r.set_parser_pos(p);
    r.set_parser_token(token);
    r.set_vlen(vlen);
    r.set_end_marker(end_marker);
    if ty != MsgType::Unknown {
        r.set_type(ty);
    }
    r.set_parse_result(MsgParseResult::Again);
    MsgParseResult::Again
}

fn finish_error_rsp(
    r: &mut Msg,
    state: RspState,
    pos: usize,
    token: Option<usize>,
    vlen: u32,
    ty: MsgType,
    end_marker: Option<usize>,
) -> MsgParseResult {
    r.set_parser_state(state as u32);
    r.set_parser_pos(pos);
    r.set_parser_token(token);
    r.set_vlen(vlen);
    r.set_end_marker(end_marker);
    if ty != MsgType::Unknown {
        r.set_type(ty);
    }
    r.set_parse_result(MsgParseResult::Error);
    MsgParseResult::Error
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_req(input: &[u8]) -> Msg {
        let mut m = Msg::new(0, MsgType::Unknown, true);
        let _ = memcache_parse_req(&mut m, input);
        m
    }

    fn parse_rsp(input: &[u8]) -> Msg {
        let mut m = Msg::new(0, MsgType::Unknown, false);
        let _ = memcache_parse_rsp(&mut m, input);
        m
    }

    #[test]
    fn parse_get() {
        let m = parse_req(b"get key1\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::ReqMcGet);
        assert_eq!(m.keys()[0].key(), b"key1");
        assert!(m.flags().is_read);
    }

    #[test]
    fn parse_set() {
        let m = parse_req(b"set key1 0 0 3\r\nval\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::ReqMcSet);
        assert_eq!(m.keys()[0].key(), b"key1");
        assert_eq!(m.vlen(), 3);
    }

    #[test]
    fn parse_set_noreply() {
        let m = parse_req(b"set key1 0 0 3 noreply\r\nval\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert!(!m.flags().expect_datastore_reply);
    }

    #[test]
    fn parse_delete() {
        let m = parse_req(b"delete key1\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::ReqMcDelete);
    }

    #[test]
    fn parse_incr() {
        let m = parse_req(b"incr counter 1\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::ReqMcIncr);
    }

    #[test]
    fn parse_quit() {
        let m = parse_req(b"quit\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::ReqMcQuit);
        assert!(m.flags().quit);
    }

    #[test]
    fn parse_get_multikey() {
        let m = parse_req(b"get a b c\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        let keys: Vec<&[u8]> = m.keys().iter().map(crate::msg::KeyPos::key).collect();
        assert_eq!(keys, vec![&b"a"[..], b"b", b"c"]);
    }

    #[test]
    fn parse_cas() {
        let m = parse_req(b"cas key1 0 0 3 7\r\nval\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::ReqMcCas);
    }

    #[test]
    fn parse_too_long_key_errors() {
        let mut input = b"get ".to_vec();
        input.extend(std::iter::repeat_n(b'k', MEMCACHE_MAX_KEY_LENGTH + 1));
        input.extend_from_slice(b"\r\n");
        let m = parse_req(&input);
        assert_eq!(m.parse_result(), MsgParseResult::Error);
    }

    #[test]
    fn parse_empty_key_errors() {
        let m = parse_req(b"get \r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Error);
    }

    #[test]
    fn parse_truncated_returns_again() {
        let m = parse_req(b"get key");
        assert_eq!(m.parse_result(), MsgParseResult::Again);
    }

    #[test]
    fn parse_stored_response() {
        let m = parse_rsp(b"STORED\r\n");
        assert_eq!(m.ty(), MsgType::RspMcStored);
    }

    #[test]
    fn parse_value_response() {
        let m = parse_rsp(b"VALUE key 0 3\r\nval\r\nEND\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::RspMcEnd);
    }

    #[test]
    fn parse_numeric_response() {
        let m = parse_rsp(b"42\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::RspMcNum);
    }

    #[test]
    fn parse_server_error_response() {
        let m = parse_rsp(b"SERVER_ERROR oops\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Ok);
        assert_eq!(m.ty(), MsgType::RspMcServerError);
    }

    #[test]
    fn parse_response_unknown_keyword_errors() {
        let m = parse_rsp(b"BOGUS\r\n");
        assert_eq!(m.parse_result(), MsgParseResult::Error);
    }
}