pravega_wire_protocol/
commands.rs

1//
2// Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10
11use super::error::CommandError;
12use super::error::InvalidData;
13use super::error::Io;
14use bincode2::Config;
15use bincode2::LengthOption;
16use byteorder::{BigEndian, ByteOrder, ReadBytesExt};
17use lazy_static::*;
18use serde::{Deserialize, Serialize};
19use snafu::ResultExt;
20use std::fmt;
21use std::hash::{Hash, Hasher};
22use std::i64;
23use std::io::Cursor;
24use std::io::{Read, Write};
25
26pub const WIRE_VERSION: i32 = 15;
27pub const OLDEST_COMPATIBLE_VERSION: i32 = 5;
28pub const TYPE_SIZE: u32 = 4;
29pub const TYPE_PLUS_LENGTH_SIZE: u32 = 8;
30pub const MAX_WIRECOMMAND_SIZE: u32 = 0x00FF_FFFF; // 16MB-1
31pub const NULL_ATTRIBUTE_VALUE: i64 = std::i64::MIN;
32/**
33 * trait for Command.
34 */
35pub trait Command {
36    const TYPE_CODE: i32;
37    fn write_fields(&self) -> Result<Vec<u8>, CommandError>;
38    fn read_from(input: &[u8]) -> Result<Self, CommandError>
39    where
40        Self: Sized;
41}
42
43/**
44 * trait for Request
45 */
46pub trait Request {
47    fn get_request_id(&self) -> i64;
48    fn must_log(&self) -> bool {
49        true
50    }
51}
52
53/**
54 * trait for Reply
55 */
56pub trait Reply {
57    fn get_request_id(&self) -> i64;
58    fn is_failure(&self) -> bool {
59        false
60    }
61}
62
63/*
64 * bincode serialize and deserialize config
65 */
66lazy_static! {
67    static ref CONFIG: Config = {
68        let mut config = bincode2::config();
69        config.big_endian();
70        config.limit(MAX_WIRECOMMAND_SIZE.into());
71        config.array_length(LengthOption::U32);
72        config.string_length(LengthOption::U16);
73        config
74    };
75}
76
77/**
78 * 1. Hello Command
79 */
80#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
81pub struct HelloCommand {
82    pub high_version: i32,
83    pub low_version: i32,
84}
85
86impl Command for HelloCommand {
87    const TYPE_CODE: i32 = -127;
88    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
89        let encoded = CONFIG.serialize(&self).context(InvalidData {
90            command_type: Self::TYPE_CODE,
91        })?;
92        Ok(encoded)
93    }
94
95    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
96        let decoded: HelloCommand = CONFIG.deserialize(input).context(InvalidData {
97            command_type: Self::TYPE_CODE,
98        })?;
99        Ok(decoded)
100    }
101}
102
103impl Request for HelloCommand {
104    fn get_request_id(&self) -> i64 {
105        0
106    }
107}
108
109impl Reply for HelloCommand {
110    fn get_request_id(&self) -> i64 {
111        0
112    }
113}
114
115/**
116 * 2. WrongHost Command
117 */
118#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
119pub struct WrongHostCommand {
120    pub request_id: i64,
121    pub segment: String,
122    pub correct_host: String,
123    pub server_stack_trace: String,
124}
125
126impl Command for WrongHostCommand {
127    const TYPE_CODE: i32 = 50;
128    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
129        let encoded = CONFIG.serialize(&self).context(InvalidData {
130            command_type: Self::TYPE_CODE,
131        })?;
132        Ok(encoded)
133    }
134    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
135        let decoded: WrongHostCommand = CONFIG.deserialize(input).context(InvalidData {
136            command_type: Self::TYPE_CODE,
137        })?;
138        Ok(decoded)
139    }
140}
141
142impl Reply for WrongHostCommand {
143    fn get_request_id(&self) -> i64 {
144        self.request_id
145    }
146    fn is_failure(&self) -> bool {
147        true
148    }
149}
150
151/**
152 * 3. SegmentIsSealed Command
153 */
154#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
155pub struct SegmentIsSealedCommand {
156    pub request_id: i64,
157    pub segment: String,
158    pub server_stack_trace: String,
159    pub offset: i64,
160}
161
162impl Command for SegmentIsSealedCommand {
163    const TYPE_CODE: i32 = 51;
164    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
165        let encoded = CONFIG.serialize(&self).context(InvalidData {
166            command_type: Self::TYPE_CODE,
167        })?;
168        Ok(encoded)
169    }
170
171    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
172        let decoded: SegmentIsSealedCommand = CONFIG.deserialize(input).context(InvalidData {
173            command_type: Self::TYPE_CODE,
174        })?;
175        Ok(decoded)
176    }
177}
178
179impl Reply for SegmentIsSealedCommand {
180    fn get_request_id(&self) -> i64 {
181        self.request_id
182    }
183    fn is_failure(&self) -> bool {
184        true
185    }
186}
187
188/**
189 * 4. SegmentIsTruncated Command
190 */
191#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
192pub struct SegmentIsTruncatedCommand {
193    pub request_id: i64,
194    pub segment: String,
195    pub start_offset: i64,
196    pub server_stack_trace: String,
197    pub offset: i64,
198}
199
200impl Command for SegmentIsTruncatedCommand {
201    const TYPE_CODE: i32 = 56;
202    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
203        let encoded = CONFIG.serialize(&self).context(InvalidData {
204            command_type: Self::TYPE_CODE,
205        })?;
206        Ok(encoded)
207    }
208
209    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
210        let decoded: SegmentIsTruncatedCommand = CONFIG.deserialize(input).context(InvalidData {
211            command_type: Self::TYPE_CODE,
212        })?;
213        Ok(decoded)
214    }
215}
216
217impl Reply for SegmentIsTruncatedCommand {
218    fn get_request_id(&self) -> i64 {
219        self.request_id
220    }
221    fn is_failure(&self) -> bool {
222        true
223    }
224}
225
226/**
227 * 5. SegmentAlreadyExists Command
228 */
229#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
230pub struct SegmentAlreadyExistsCommand {
231    pub request_id: i64,
232    pub segment: String,
233    pub server_stack_trace: String,
234}
235
236impl Command for SegmentAlreadyExistsCommand {
237    const TYPE_CODE: i32 = 52;
238    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
239        let encoded = CONFIG.serialize(&self).context(InvalidData {
240            command_type: Self::TYPE_CODE,
241        })?;
242        Ok(encoded)
243    }
244
245    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
246        let decoded: SegmentAlreadyExistsCommand = CONFIG.deserialize(input).context(InvalidData {
247            command_type: Self::TYPE_CODE,
248        })?;
249        Ok(decoded)
250    }
251}
252
253impl Reply for SegmentAlreadyExistsCommand {
254    fn get_request_id(&self) -> i64 {
255        self.request_id
256    }
257    fn is_failure(&self) -> bool {
258        true
259    }
260}
261
262impl fmt::Display for SegmentAlreadyExistsCommand {
263    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
264        write!(f, "Segment already exists: {}", self.segment)
265    }
266}
267
268/**
269 * 6. NoSuchSegment Command
270 */
271#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
272pub struct NoSuchSegmentCommand {
273    pub request_id: i64,
274    pub segment: String,
275    pub server_stack_trace: String,
276    pub offset: i64,
277}
278
279impl Command for NoSuchSegmentCommand {
280    const TYPE_CODE: i32 = 53;
281    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
282        let encoded = CONFIG.serialize(&self).context(InvalidData {
283            command_type: Self::TYPE_CODE,
284        })?;
285        Ok(encoded)
286    }
287
288    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
289        let decoded: NoSuchSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
290            command_type: Self::TYPE_CODE,
291        })?;
292        Ok(decoded)
293    }
294}
295
296impl Reply for NoSuchSegmentCommand {
297    fn get_request_id(&self) -> i64 {
298        self.request_id
299    }
300    fn is_failure(&self) -> bool {
301        true
302    }
303}
304
305impl fmt::Display for NoSuchSegmentCommand {
306    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
307        write!(f, "No such segment: {}", self.segment)
308    }
309}
310
311/**
312 * 7. TableSegmentNotEmpty Command
313 */
314#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
315pub struct TableSegmentNotEmptyCommand {
316    pub request_id: i64,
317    pub segment: String,
318    pub server_stack_trace: String,
319}
320
321impl Command for TableSegmentNotEmptyCommand {
322    const TYPE_CODE: i32 = 80;
323    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
324        let encoded = CONFIG.serialize(&self).context(InvalidData {
325            command_type: Self::TYPE_CODE,
326        })?;
327        Ok(encoded)
328    }
329
330    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
331        let decoded: TableSegmentNotEmptyCommand = CONFIG.deserialize(input).context(InvalidData {
332            command_type: Self::TYPE_CODE,
333        })?;
334        Ok(decoded)
335    }
336}
337
338impl Reply for TableSegmentNotEmptyCommand {
339    fn get_request_id(&self) -> i64 {
340        self.request_id
341    }
342    fn is_failure(&self) -> bool {
343        true
344    }
345}
346
347impl fmt::Display for TableSegmentNotEmptyCommand {
348    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
349        write!(f, "Table Segment is not empty: {}", self.segment)
350    }
351}
352
353/**
354 * 8. InvalidEventNumber Command
355 */
356#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
357pub struct InvalidEventNumberCommand {
358    pub writer_id: u128,
359    pub event_number: i64,
360    pub server_stack_trace: String,
361}
362
363impl Command for InvalidEventNumberCommand {
364    const TYPE_CODE: i32 = 55;
365    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
366        let encoded = CONFIG.serialize(&self).context(InvalidData {
367            command_type: Self::TYPE_CODE,
368        })?;
369        Ok(encoded)
370    }
371
372    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
373        let decoded: InvalidEventNumberCommand = CONFIG.deserialize(input).context(InvalidData {
374            command_type: Self::TYPE_CODE,
375        })?;
376        Ok(decoded)
377    }
378}
379
380impl Reply for InvalidEventNumberCommand {
381    fn get_request_id(&self) -> i64 {
382        self.event_number
383    }
384    fn is_failure(&self) -> bool {
385        true
386    }
387}
388
389impl fmt::Display for InvalidEventNumberCommand {
390    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
391        write!(
392            f,
393            "Invalid event number: {} for writer: {}",
394            self.event_number, self.writer_id,
395        )
396    }
397}
398
399/**
400 * 9. OperationUnsupported Command
401 */
402#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
403pub struct OperationUnsupportedCommand {
404    pub request_id: i64,
405    pub operation_name: String,
406    pub server_stack_trace: String,
407}
408
409impl Command for OperationUnsupportedCommand {
410    const TYPE_CODE: i32 = 57;
411    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
412        let encoded = CONFIG.serialize(&self).context(InvalidData {
413            command_type: Self::TYPE_CODE,
414        })?;
415        Ok(encoded)
416    }
417
418    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
419        let decoded: OperationUnsupportedCommand = CONFIG.deserialize(input).context(InvalidData {
420            command_type: Self::TYPE_CODE,
421        })?;
422        Ok(decoded)
423    }
424}
425
426impl Reply for OperationUnsupportedCommand {
427    fn get_request_id(&self) -> i64 {
428        self.request_id
429    }
430    fn is_failure(&self) -> bool {
431        true
432    }
433}
434
435/**
436 * 10. Padding Command
437 */
438#[derive(PartialEq, Debug, Clone)]
439pub struct PaddingCommand {
440    pub length: i32,
441}
442
443impl Command for PaddingCommand {
444    const TYPE_CODE: i32 = -1;
445
446    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
447        let res = vec![0; self.length as usize];
448        Ok(res)
449    }
450
451    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
452        //FIXME: In java we use skipBytes to remove these padding bytes.
453        //FIXME: I think we don't need to do in rust.
454        Ok(PaddingCommand {
455            length: input.len() as i32,
456        })
457    }
458}
459
460/**
461 * 11. PartialEvent Command
462 */
463#[derive(PartialEq, Debug, Clone)]
464pub struct PartialEventCommand {
465    pub data: Vec<u8>,
466}
467
468impl Command for PartialEventCommand {
469    const TYPE_CODE: i32 = -2;
470    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
471        //FIXME: In java, we use data.getBytes(data.readerIndex(), (OutputStream) out, data.readableBytes());
472        // which means the result would not contain the prefix length;
473        // so in rust we can directly return data.
474        Ok(self.data.clone())
475    }
476
477    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
478        Ok(PartialEventCommand { data: input.to_vec() })
479    }
480}
481
482/**
483 * 12. Event Command
484 */
485#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
486pub struct EventCommand {
487    #[serde(with = "serde_bytes")]
488    pub data: Vec<u8>,
489}
490
491impl Command for EventCommand {
492    const TYPE_CODE: i32 = 0;
493    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
494        let mut res = Vec::with_capacity(self.data.len() + 4);
495        res.extend_from_slice(&EventCommand::TYPE_CODE.to_be_bytes());
496        CONFIG.serialize_into(&mut res, &self).context(InvalidData {
497            command_type: Self::TYPE_CODE,
498        })?;
499        Ok(res)
500    }
501
502    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
503        //read the type_code.
504        let _type_code = BigEndian::read_i32(input);
505        let decoded: EventCommand = CONFIG.deserialize(&input[4..]).context(InvalidData {
506            command_type: Self::TYPE_CODE,
507        })?;
508        Ok(decoded)
509    }
510}
511
512/**
513 * 13. SetupAppend Command
514 */
515#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
516pub struct SetupAppendCommand {
517    pub request_id: i64,
518    pub writer_id: u128,
519    pub segment: String,
520    pub delegation_token: String,
521}
522
523impl Command for SetupAppendCommand {
524    const TYPE_CODE: i32 = 1;
525    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
526        let encoded = CONFIG.serialize(&self).context(InvalidData {
527            command_type: Self::TYPE_CODE,
528        })?;
529        Ok(encoded)
530    }
531
532    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
533        let decoded: SetupAppendCommand = CONFIG.deserialize(input).context(InvalidData {
534            command_type: Self::TYPE_CODE,
535        })?;
536        Ok(decoded)
537    }
538}
539
540impl Request for SetupAppendCommand {
541    fn get_request_id(&self) -> i64 {
542        self.request_id
543    }
544}
545
546/**
547 * 14. AppendBlock Command
548 */
549#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
550pub struct AppendBlockCommand {
551    pub writer_id: u128,
552    #[serde(with = "serde_bytes")]
553    pub data: Vec<u8>,
554}
555
556impl Command for AppendBlockCommand {
557    const TYPE_CODE: i32 = 3;
558    //FIXME: The serialize and deserialize method need to customize;
559    // In JAVA, it doesn't write data(because it'empty), but here it will write the prefix length(0).
560    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
561        let encoded = CONFIG.serialize(&self).context(InvalidData {
562            command_type: Self::TYPE_CODE,
563        })?;
564        Ok(encoded)
565    }
566
567    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
568        let decoded: AppendBlockCommand = CONFIG.deserialize(input).context(InvalidData {
569            command_type: Self::TYPE_CODE,
570        })?;
571        Ok(decoded)
572    }
573}
574
575/**
576 * 15. AppendBlockEnd Command
577 */
578#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
579pub struct AppendBlockEndCommand {
580    pub writer_id: u128,
581    pub size_of_whole_events: i32,
582    #[serde(with = "serde_bytes")]
583    pub data: Vec<u8>,
584    pub num_event: i32,
585    pub last_event_number: i64,
586    pub request_id: i64,
587}
588
589impl Command for AppendBlockEndCommand {
590    const TYPE_CODE: i32 = 4;
591
592    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
593        let encoded = CONFIG.serialize(&self).context(InvalidData {
594            command_type: Self::TYPE_CODE,
595        })?;
596        Ok(encoded)
597    }
598
599    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
600        let decoded: AppendBlockEndCommand = CONFIG.deserialize(input).context(InvalidData {
601            command_type: Self::TYPE_CODE,
602        })?;
603        Ok(decoded)
604    }
605}
606
607/**
608 * 16.ConditionalAppend Command
609 */
610#[derive(PartialEq, Debug, Clone)]
611pub struct ConditionalAppendCommand {
612    pub writer_id: u128,
613    pub event_number: i64,
614    pub expected_offset: i64,
615    pub event: EventCommand,
616    pub request_id: i64,
617}
618
619impl ConditionalAppendCommand {
620    fn read_event(rdr: &mut Cursor<&[u8]>) -> Result<EventCommand, std::io::Error> {
621        let _type_code = rdr.read_i32::<BigEndian>()?;
622        let event_length = rdr.read_u32::<BigEndian>()?;
623        // read the data in event
624        let mut msg: Vec<u8> = vec![0; event_length as usize];
625        rdr.read_exact(&mut msg)?;
626        Ok(EventCommand { data: msg })
627    }
628}
629
630impl Command for ConditionalAppendCommand {
631    const TYPE_CODE: i32 = 5;
632    // Customize the serialize and deserialize method.
633    // Because in ConditionalAppend the event should be serialize as |type_code|length|data|
634    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
635        let mut res = Vec::new();
636        res.extend_from_slice(&self.writer_id.to_be_bytes());
637        res.extend_from_slice(&self.event_number.to_be_bytes());
638        res.extend_from_slice(&self.expected_offset.to_be_bytes());
639        res.write_all(&self.event.write_fields()?).context(Io {
640            command_type: Self::TYPE_CODE,
641        })?;
642        res.extend_from_slice(&self.request_id.to_be_bytes());
643        Ok(res)
644    }
645
646    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
647        let mut rdr = Cursor::new(input);
648        let ctx = Io {
649            command_type: Self::TYPE_CODE,
650        };
651        let writer_id = rdr.read_u128::<BigEndian>().context(ctx)?;
652        let event_number = rdr.read_i64::<BigEndian>().context(ctx)?;
653        let expected_offset = rdr.read_i64::<BigEndian>().context(ctx)?;
654        let event = ConditionalAppendCommand::read_event(&mut rdr).context(ctx)?;
655        let request_id = rdr.read_i64::<BigEndian>().context(ctx)?;
656        Ok(ConditionalAppendCommand {
657            writer_id,
658            event_number,
659            expected_offset,
660            event,
661            request_id,
662        })
663    }
664}
665
666impl Request for ConditionalAppendCommand {
667    fn get_request_id(&self) -> i64 {
668        self.request_id
669    }
670}
671
672/**
673 *  17. AppendSetup Command.
674 */
675#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
676pub struct AppendSetupCommand {
677    pub request_id: i64,
678    pub segment: String,
679    pub writer_id: u128,
680    pub last_event_number: i64,
681}
682
683impl Command for AppendSetupCommand {
684    const TYPE_CODE: i32 = 2;
685
686    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
687        let encoded = CONFIG.serialize(&self).context(InvalidData {
688            command_type: Self::TYPE_CODE,
689        })?;
690        Ok(encoded)
691    }
692
693    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
694        let decoded: AppendSetupCommand = CONFIG.deserialize(input).context(InvalidData {
695            command_type: Self::TYPE_CODE,
696        })?;
697        Ok(decoded)
698    }
699}
700
701impl Reply for AppendSetupCommand {
702    fn get_request_id(&self) -> i64 {
703        self.request_id
704    }
705}
706
707/**
708 * 18. DataAppended Command
709 */
710#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
711pub struct DataAppendedCommand {
712    pub writer_id: u128,
713    pub event_number: i64,
714    pub previous_event_number: i64,
715    pub request_id: i64,
716    pub current_segment_write_offset: i64,
717}
718
719impl Command for DataAppendedCommand {
720    const TYPE_CODE: i32 = 7;
721
722    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
723        let encoded = CONFIG.serialize(&self).context(InvalidData {
724            command_type: Self::TYPE_CODE,
725        })?;
726        Ok(encoded)
727    }
728
729    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
730        let decoded: DataAppendedCommand = CONFIG.deserialize(input).context(InvalidData {
731            command_type: Self::TYPE_CODE,
732        })?;
733        Ok(decoded)
734    }
735}
736
737impl Reply for DataAppendedCommand {
738    fn get_request_id(&self) -> i64 {
739        self.request_id
740    }
741}
742
743/**
744 * 19. ConditionalCheckFailed Command
745 */
746#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
747pub struct ConditionalCheckFailedCommand {
748    pub writer_id: u128,
749    pub event_number: i64,
750    pub request_id: i64,
751}
752
753impl Command for ConditionalCheckFailedCommand {
754    const TYPE_CODE: i32 = 8;
755
756    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
757        let encoded = CONFIG.serialize(&self).context(InvalidData {
758            command_type: Self::TYPE_CODE,
759        })?;
760        Ok(encoded)
761    }
762
763    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
764        let decoded: ConditionalCheckFailedCommand = CONFIG.deserialize(input).context(InvalidData {
765            command_type: Self::TYPE_CODE,
766        })?;
767        Ok(decoded)
768    }
769}
770
771impl Reply for ConditionalCheckFailedCommand {
772    fn get_request_id(&self) -> i64 {
773        self.request_id
774    }
775}
776
777/**
778 * 20. ReadSegment Command
779 */
780#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
781pub struct ReadSegmentCommand {
782    pub segment: String,
783    pub offset: i64,
784    pub suggested_length: i32,
785    pub delegation_token: String,
786    pub request_id: i64,
787}
788
789impl Command for ReadSegmentCommand {
790    const TYPE_CODE: i32 = 9;
791
792    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
793        let encoded = CONFIG.serialize(&self).context(InvalidData {
794            command_type: Self::TYPE_CODE,
795        })?;
796        Ok(encoded)
797    }
798
799    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
800        let decoded: ReadSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
801            command_type: Self::TYPE_CODE,
802        })?;
803        Ok(decoded)
804    }
805}
806
807impl Request for ReadSegmentCommand {
808    fn get_request_id(&self) -> i64 {
809        self.request_id
810    }
811}
812
813/**
814 * 21. SegmentRead Command
815 */
816#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
817pub struct SegmentReadCommand {
818    pub segment: String,
819    pub offset: i64,
820    pub at_tail: bool,
821    pub end_of_segment: bool,
822    #[serde(with = "serde_bytes")]
823    pub data: Vec<u8>,
824    pub request_id: i64,
825}
826
827impl Command for SegmentReadCommand {
828    const TYPE_CODE: i32 = 10;
829
830    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
831        let encoded = CONFIG.serialize(&self).context(InvalidData {
832            command_type: Self::TYPE_CODE,
833        })?;
834        Ok(encoded)
835    }
836
837    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
838        let decoded: SegmentReadCommand = CONFIG.deserialize(input).context(InvalidData {
839            command_type: Self::TYPE_CODE,
840        })?;
841        Ok(decoded)
842    }
843}
844
845impl Reply for SegmentReadCommand {
846    fn get_request_id(&self) -> i64 {
847        self.request_id
848    }
849}
850
851/**
852 * 22. GetSegmentAttribute Command
853 */
854#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
855pub struct GetSegmentAttributeCommand {
856    pub request_id: i64,
857    pub segment_name: String,
858    pub attribute_id: u128,
859    pub delegation_token: String,
860}
861
862impl Command for GetSegmentAttributeCommand {
863    const TYPE_CODE: i32 = 34;
864
865    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
866        let encoded = CONFIG.serialize(&self).context(InvalidData {
867            command_type: Self::TYPE_CODE,
868        })?;
869        Ok(encoded)
870    }
871
872    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
873        let decoded: GetSegmentAttributeCommand = CONFIG.deserialize(input).context(InvalidData {
874            command_type: Self::TYPE_CODE,
875        })?;
876        Ok(decoded)
877    }
878}
879
880impl Request for GetSegmentAttributeCommand {
881    fn get_request_id(&self) -> i64 {
882        self.request_id
883    }
884}
885
886/**
887 * 23. SegmentAttribute Command
888 */
889#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
890pub struct SegmentAttributeCommand {
891    pub request_id: i64,
892    pub value: i64,
893}
894
895impl Command for SegmentAttributeCommand {
896    const TYPE_CODE: i32 = 35;
897
898    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
899        let encoded = CONFIG.serialize(&self).context(InvalidData {
900            command_type: Self::TYPE_CODE,
901        })?;
902        Ok(encoded)
903    }
904
905    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
906        let decoded: SegmentAttributeCommand = CONFIG.deserialize(input).context(InvalidData {
907            command_type: Self::TYPE_CODE,
908        })?;
909        Ok(decoded)
910    }
911}
912
913impl Reply for SegmentAttributeCommand {
914    fn get_request_id(&self) -> i64 {
915        self.request_id
916    }
917}
918
919/**
920 * 24. UpdateSegmentAttribute Command
921 */
922#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
923pub struct UpdateSegmentAttributeCommand {
924    pub request_id: i64,
925    pub segment_name: String,
926    pub attribute_id: u128,
927    pub new_value: i64,
928    pub expected_value: i64,
929    pub delegation_token: String,
930}
931
932impl Command for UpdateSegmentAttributeCommand {
933    const TYPE_CODE: i32 = 36;
934
935    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
936        let encoded = CONFIG.serialize(&self).context(InvalidData {
937            command_type: Self::TYPE_CODE,
938        })?;
939        Ok(encoded)
940    }
941
942    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
943        let decoded: UpdateSegmentAttributeCommand = CONFIG.deserialize(input).context(InvalidData {
944            command_type: Self::TYPE_CODE,
945        })?;
946        Ok(decoded)
947    }
948}
949
950impl Request for UpdateSegmentAttributeCommand {
951    fn get_request_id(&self) -> i64 {
952        self.request_id
953    }
954}
955
956/**
957 * 25. SegmentAttributeUpdated Command
958 */
959#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
960pub struct SegmentAttributeUpdatedCommand {
961    pub request_id: i64,
962    pub success: bool,
963}
964
965impl Command for SegmentAttributeUpdatedCommand {
966    const TYPE_CODE: i32 = 37;
967
968    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
969        let encoded = CONFIG.serialize(&self).context(InvalidData {
970            command_type: Self::TYPE_CODE,
971        })?;
972        Ok(encoded)
973    }
974
975    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
976        let decoded: SegmentAttributeUpdatedCommand = CONFIG.deserialize(input).context(InvalidData {
977            command_type: Self::TYPE_CODE,
978        })?;
979        Ok(decoded)
980    }
981}
982
983impl Reply for SegmentAttributeUpdatedCommand {
984    fn get_request_id(&self) -> i64 {
985        self.request_id
986    }
987}
988
989/**
990 * 26. GetStreamSegmentInfo Command
991 */
992#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
993pub struct GetStreamSegmentInfoCommand {
994    pub request_id: i64,
995    pub segment_name: String,
996    pub delegation_token: String,
997}
998
999impl Command for GetStreamSegmentInfoCommand {
1000    const TYPE_CODE: i32 = 11;
1001
1002    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1003        let encoded = CONFIG.serialize(&self).context(InvalidData {
1004            command_type: Self::TYPE_CODE,
1005        })?;
1006        Ok(encoded)
1007    }
1008
1009    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1010        let decoded: GetStreamSegmentInfoCommand = CONFIG.deserialize(input).context(InvalidData {
1011            command_type: Self::TYPE_CODE,
1012        })?;
1013        Ok(decoded)
1014    }
1015}
1016
1017impl Request for GetStreamSegmentInfoCommand {
1018    fn get_request_id(&self) -> i64 {
1019        self.request_id
1020    }
1021}
1022
1023/**
1024 * 27. StreamSegmentInfo Command
1025 */
1026#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1027pub struct StreamSegmentInfoCommand {
1028    pub request_id: i64,
1029    pub segment_name: String,
1030    pub exists: bool,
1031    pub is_sealed: bool,
1032    pub is_deleted: bool,
1033    pub last_modified: i64,
1034    pub write_offset: i64,
1035    pub start_offset: i64,
1036}
1037
1038impl Command for StreamSegmentInfoCommand {
1039    const TYPE_CODE: i32 = 12;
1040
1041    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1042        let encoded = CONFIG.serialize(&self).context(InvalidData {
1043            command_type: Self::TYPE_CODE,
1044        })?;
1045        Ok(encoded)
1046    }
1047
1048    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1049        let decoded: StreamSegmentInfoCommand = CONFIG.deserialize(input).context(InvalidData {
1050            command_type: Self::TYPE_CODE,
1051        })?;
1052        Ok(decoded)
1053    }
1054}
1055
1056impl Reply for StreamSegmentInfoCommand {
1057    fn get_request_id(&self) -> i64 {
1058        self.request_id
1059    }
1060}
1061
1062/**
1063 * 28. CreateSegment Command
1064 */
1065#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1066pub struct CreateSegmentCommand {
1067    pub request_id: i64,
1068    pub segment: String,
1069    pub target_rate: i32,
1070    pub scale_type: u8,
1071    pub delegation_token: String,
1072}
1073
1074impl Command for CreateSegmentCommand {
1075    const TYPE_CODE: i32 = 20;
1076
1077    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1078        let encoded = CONFIG.serialize(&self).context(InvalidData {
1079            command_type: Self::TYPE_CODE,
1080        })?;
1081        Ok(encoded)
1082    }
1083
1084    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1085        let decoded: CreateSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
1086            command_type: Self::TYPE_CODE,
1087        })?;
1088        Ok(decoded)
1089    }
1090}
1091
1092impl Request for CreateSegmentCommand {
1093    fn get_request_id(&self) -> i64 {
1094        self.request_id
1095    }
1096}
1097
1098/**
1099 * 29. CreateTableSegment Command
1100 */
1101#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1102pub struct CreateTableSegmentCommand {
1103    pub request_id: i64,
1104    pub segment: String,
1105    pub delegation_token: String,
1106}
1107
1108impl Command for CreateTableSegmentCommand {
1109    const TYPE_CODE: i32 = 70;
1110
1111    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1112        let encoded = CONFIG.serialize(&self).context(InvalidData {
1113            command_type: Self::TYPE_CODE,
1114        })?;
1115        Ok(encoded)
1116    }
1117
1118    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1119        let decoded: CreateTableSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
1120            command_type: Self::TYPE_CODE,
1121        })?;
1122        Ok(decoded)
1123    }
1124}
1125
1126impl Request for CreateTableSegmentCommand {
1127    fn get_request_id(&self) -> i64 {
1128        self.request_id
1129    }
1130}
1131
1132/**
1133 * 30. SegmentCreated Command
1134 */
1135#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1136pub struct SegmentCreatedCommand {
1137    pub request_id: i64,
1138    pub segment: String,
1139}
1140
1141impl Command for SegmentCreatedCommand {
1142    const TYPE_CODE: i32 = 21;
1143
1144    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1145        let encoded = CONFIG.serialize(&self).context(InvalidData {
1146            command_type: Self::TYPE_CODE,
1147        })?;
1148        Ok(encoded)
1149    }
1150
1151    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1152        let decoded: SegmentCreatedCommand = CONFIG.deserialize(input).context(InvalidData {
1153            command_type: Self::TYPE_CODE,
1154        })?;
1155        Ok(decoded)
1156    }
1157}
1158
1159impl Reply for SegmentCreatedCommand {
1160    fn get_request_id(&self) -> i64 {
1161        self.request_id
1162    }
1163}
1164
1165/**
1166 * 31. UpdateSegmentPolicy Command
1167 */
1168#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1169pub struct UpdateSegmentPolicyCommand {
1170    pub request_id: i64,
1171    pub segment: String,
1172    pub target_rate: i32,
1173    pub scale_type: u8,
1174    pub delegation_token: String,
1175}
1176
1177impl Command for UpdateSegmentPolicyCommand {
1178    const TYPE_CODE: i32 = 32;
1179
1180    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1181        let encoded = CONFIG.serialize(&self).context(InvalidData {
1182            command_type: Self::TYPE_CODE,
1183        })?;
1184        Ok(encoded)
1185    }
1186
1187    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1188        let decoded: UpdateSegmentPolicyCommand = CONFIG.deserialize(input).context(InvalidData {
1189            command_type: Self::TYPE_CODE,
1190        })?;
1191        Ok(decoded)
1192    }
1193}
1194
1195impl Request for UpdateSegmentPolicyCommand {
1196    fn get_request_id(&self) -> i64 {
1197        self.request_id
1198    }
1199}
1200
1201/**
1202 * 32. SegmentPolicyUpdated Command
1203 */
1204#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1205pub struct SegmentPolicyUpdatedCommand {
1206    pub request_id: i64,
1207    pub segment: String,
1208}
1209
1210impl Command for SegmentPolicyUpdatedCommand {
1211    const TYPE_CODE: i32 = 33;
1212
1213    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1214        let encoded = CONFIG.serialize(&self).context(InvalidData {
1215            command_type: Self::TYPE_CODE,
1216        })?;
1217        Ok(encoded)
1218    }
1219
1220    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1221        let decoded: SegmentPolicyUpdatedCommand = CONFIG.deserialize(input).context(InvalidData {
1222            command_type: Self::TYPE_CODE,
1223        })?;
1224        Ok(decoded)
1225    }
1226}
1227
1228impl Reply for SegmentPolicyUpdatedCommand {
1229    fn get_request_id(&self) -> i64 {
1230        self.request_id
1231    }
1232}
1233
1234/**
1235 * 33. MergeSegments Command
1236 */
1237#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1238pub struct MergeSegmentsCommand {
1239    pub request_id: i64,
1240    pub target: String,
1241    pub source: String,
1242    pub delegation_token: String,
1243}
1244
1245impl Command for MergeSegmentsCommand {
1246    const TYPE_CODE: i32 = 58;
1247
1248    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1249        let encoded = CONFIG.serialize(&self).context(InvalidData {
1250            command_type: Self::TYPE_CODE,
1251        })?;
1252        Ok(encoded)
1253    }
1254
1255    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1256        let decoded: MergeSegmentsCommand = CONFIG.deserialize(input).context(InvalidData {
1257            command_type: Self::TYPE_CODE,
1258        })?;
1259        Ok(decoded)
1260    }
1261}
1262
1263impl Request for MergeSegmentsCommand {
1264    fn get_request_id(&self) -> i64 {
1265        self.request_id
1266    }
1267}
1268/**
1269 * 34. MergeTableSegments Command
1270 */
1271#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1272pub struct MergeTableSegmentsCommand {
1273    pub request_id: i64,
1274    pub target: String,
1275    pub source: String,
1276    pub delegation_token: String,
1277}
1278
1279impl Command for MergeTableSegmentsCommand {
1280    const TYPE_CODE: i32 = 72;
1281
1282    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1283        let encoded = CONFIG.serialize(&self).context(InvalidData {
1284            command_type: Self::TYPE_CODE,
1285        })?;
1286        Ok(encoded)
1287    }
1288
1289    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1290        let decoded: MergeTableSegmentsCommand = CONFIG.deserialize(input).context(InvalidData {
1291            command_type: Self::TYPE_CODE,
1292        })?;
1293        Ok(decoded)
1294    }
1295}
1296
1297impl Request for MergeTableSegmentsCommand {
1298    fn get_request_id(&self) -> i64 {
1299        self.request_id
1300    }
1301}
1302
1303/**
1304 * 35. SegmentsMerged Command
1305 */
1306#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1307pub struct SegmentsMergedCommand {
1308    pub request_id: i64,
1309    pub target: String,
1310    pub source: String,
1311    pub new_target_write_offset: i64,
1312}
1313
1314impl Command for SegmentsMergedCommand {
1315    const TYPE_CODE: i32 = 59;
1316
1317    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1318        let encoded = CONFIG.serialize(&self).context(InvalidData {
1319            command_type: Self::TYPE_CODE,
1320        })?;
1321        Ok(encoded)
1322    }
1323
1324    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1325        let decoded: SegmentsMergedCommand = CONFIG.deserialize(input).context(InvalidData {
1326            command_type: Self::TYPE_CODE,
1327        })?;
1328        Ok(decoded)
1329    }
1330}
1331
1332impl Reply for SegmentsMergedCommand {
1333    fn get_request_id(&self) -> i64 {
1334        self.request_id
1335    }
1336}
1337
1338/**
1339 * 36. SealSegment Command
1340 */
1341#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1342pub struct SealSegmentCommand {
1343    pub request_id: i64,
1344    pub segment: String,
1345    pub delegation_token: String,
1346}
1347
1348impl Command for SealSegmentCommand {
1349    const TYPE_CODE: i32 = 28;
1350
1351    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1352        let encoded = CONFIG.serialize(&self).context(InvalidData {
1353            command_type: Self::TYPE_CODE,
1354        })?;
1355        Ok(encoded)
1356    }
1357
1358    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1359        let decoded: SealSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
1360            command_type: Self::TYPE_CODE,
1361        })?;
1362        Ok(decoded)
1363    }
1364}
1365
1366impl Request for SealSegmentCommand {
1367    fn get_request_id(&self) -> i64 {
1368        self.request_id
1369    }
1370}
1371
1372/**
1373 * 37. SealTableSegment Command
1374 */
1375#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1376pub struct SealTableSegmentCommand {
1377    pub request_id: i64,
1378    pub segment: String,
1379    pub delegation_token: String,
1380}
1381
1382impl Command for SealTableSegmentCommand {
1383    const TYPE_CODE: i32 = 73;
1384
1385    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1386        let encoded = CONFIG.serialize(&self).context(InvalidData {
1387            command_type: Self::TYPE_CODE,
1388        })?;
1389        Ok(encoded)
1390    }
1391
1392    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1393        let decoded: SealTableSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
1394            command_type: Self::TYPE_CODE,
1395        })?;
1396        Ok(decoded)
1397    }
1398}
1399
1400impl Request for SealTableSegmentCommand {
1401    fn get_request_id(&self) -> i64 {
1402        self.request_id
1403    }
1404}
1405
1406/**
1407 * 38. SegmentSealed Command
1408 */
1409#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1410pub struct SegmentSealedCommand {
1411    pub request_id: i64,
1412    pub segment: String,
1413}
1414
1415impl Command for SegmentSealedCommand {
1416    const TYPE_CODE: i32 = 29;
1417
1418    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1419        let encoded = CONFIG.serialize(&self).context(InvalidData {
1420            command_type: Self::TYPE_CODE,
1421        })?;
1422        Ok(encoded)
1423    }
1424
1425    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1426        let decoded: SegmentSealedCommand = CONFIG.deserialize(input).context(InvalidData {
1427            command_type: Self::TYPE_CODE,
1428        })?;
1429        Ok(decoded)
1430    }
1431}
1432
1433impl Reply for SegmentSealedCommand {
1434    fn get_request_id(&self) -> i64 {
1435        self.request_id
1436    }
1437}
1438
1439/**
1440 * 39. TruncateSegment Command
1441 */
1442#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1443pub struct TruncateSegmentCommand {
1444    pub request_id: i64,
1445    pub segment: String,
1446    pub truncation_offset: i64,
1447    pub delegation_token: String,
1448}
1449
1450impl Command for TruncateSegmentCommand {
1451    const TYPE_CODE: i32 = 38;
1452
1453    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1454        let encoded = CONFIG.serialize(&self).context(InvalidData {
1455            command_type: Self::TYPE_CODE,
1456        })?;
1457        Ok(encoded)
1458    }
1459
1460    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1461        let decoded: TruncateSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
1462            command_type: Self::TYPE_CODE,
1463        })?;
1464        Ok(decoded)
1465    }
1466}
1467
1468impl Request for TruncateSegmentCommand {
1469    fn get_request_id(&self) -> i64 {
1470        self.request_id
1471    }
1472}
1473
1474/**
1475 * 40. SegmentTruncated Command
1476 */
1477#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1478pub struct SegmentTruncatedCommand {
1479    pub request_id: i64,
1480    pub segment: String,
1481}
1482
1483impl Command for SegmentTruncatedCommand {
1484    const TYPE_CODE: i32 = 39;
1485
1486    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1487        let encoded = CONFIG.serialize(&self).context(InvalidData {
1488            command_type: Self::TYPE_CODE,
1489        })?;
1490        Ok(encoded)
1491    }
1492
1493    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1494        let decoded: SegmentTruncatedCommand = CONFIG.deserialize(input).context(InvalidData {
1495            command_type: Self::TYPE_CODE,
1496        })?;
1497        Ok(decoded)
1498    }
1499}
1500
1501impl Reply for SegmentTruncatedCommand {
1502    fn get_request_id(&self) -> i64 {
1503        self.request_id
1504    }
1505}
1506
1507/**
1508 * 41. DeleteSegment Command
1509 */
1510#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1511pub struct DeleteSegmentCommand {
1512    pub request_id: i64,
1513    pub segment: String,
1514    pub delegation_token: String,
1515}
1516
1517impl Command for DeleteSegmentCommand {
1518    const TYPE_CODE: i32 = 30;
1519
1520    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1521        let encoded = CONFIG.serialize(&self).context(InvalidData {
1522            command_type: Self::TYPE_CODE,
1523        })?;
1524        Ok(encoded)
1525    }
1526
1527    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1528        let decoded: DeleteSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
1529            command_type: Self::TYPE_CODE,
1530        })?;
1531        Ok(decoded)
1532    }
1533}
1534
1535impl Request for DeleteSegmentCommand {
1536    fn get_request_id(&self) -> i64 {
1537        self.request_id
1538    }
1539}
1540
1541/**
1542 * 42. DeleteTableSegment Command
1543 */
1544#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1545pub struct DeleteTableSegmentCommand {
1546    pub request_id: i64,
1547    pub segment: String,
1548    pub must_be_empty: bool, // If true, the Table Segment will only be deleted if it is empty (contains no keys)
1549    pub delegation_token: String,
1550}
1551
1552impl Command for DeleteTableSegmentCommand {
1553    const TYPE_CODE: i32 = 71;
1554    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1555        let encoded = CONFIG.serialize(&self).context(InvalidData {
1556            command_type: Self::TYPE_CODE,
1557        })?;
1558        Ok(encoded)
1559    }
1560
1561    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1562        let decoded: DeleteTableSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
1563            command_type: Self::TYPE_CODE,
1564        })?;
1565        Ok(decoded)
1566    }
1567}
1568
1569impl Request for DeleteTableSegmentCommand {
1570    fn get_request_id(&self) -> i64 {
1571        self.request_id
1572    }
1573}
1574
1575/**
1576 * 43. SegmentDeleted Command
1577 */
1578#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1579pub struct SegmentDeletedCommand {
1580    pub request_id: i64,
1581    pub segment: String,
1582}
1583
1584impl Command for SegmentDeletedCommand {
1585    const TYPE_CODE: i32 = 31;
1586
1587    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1588        let encoded = CONFIG.serialize(&self).context(InvalidData {
1589            command_type: Self::TYPE_CODE,
1590        })?;
1591        Ok(encoded)
1592    }
1593
1594    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1595        let decoded: SegmentDeletedCommand = CONFIG.deserialize(input).context(InvalidData {
1596            command_type: Self::TYPE_CODE,
1597        })?;
1598        Ok(decoded)
1599    }
1600}
1601
1602impl Reply for SegmentDeletedCommand {
1603    fn get_request_id(&self) -> i64 {
1604        self.request_id
1605    }
1606}
1607/**
1608 * 44. KeepAlive Command
1609 */
1610#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1611pub struct KeepAliveCommand {}
1612
1613impl Command for KeepAliveCommand {
1614    const TYPE_CODE: i32 = 100;
1615
1616    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1617        let res: Vec<u8> = Vec::new();
1618        Ok(res)
1619    }
1620
1621    fn read_from(_input: &[u8]) -> Result<Self, CommandError> {
1622        Ok(KeepAliveCommand {})
1623    }
1624}
1625
1626impl Request for KeepAliveCommand {
1627    fn get_request_id(&self) -> i64 {
1628        -1
1629    }
1630    fn must_log(&self) -> bool {
1631        false
1632    }
1633}
1634
1635impl Reply for KeepAliveCommand {
1636    fn get_request_id(&self) -> i64 {
1637        -1
1638    }
1639}
1640
1641/**
1642 * 45. AuthTokenCheckFailed Command
1643 */
1644#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1645pub struct AuthTokenCheckFailedCommand {
1646    pub request_id: i64,
1647    pub server_stack_trace: String,
1648    pub error_code: i32,
1649}
1650
1651impl AuthTokenCheckFailedCommand {
1652    pub fn is_token_expired(&self) -> bool {
1653        ErrorCode::value_of(self.error_code) == ErrorCode::TokenExpired
1654    }
1655
1656    pub fn get_error_code(&self) -> ErrorCode {
1657        ErrorCode::value_of(self.error_code)
1658    }
1659}
1660
1661impl Command for AuthTokenCheckFailedCommand {
1662    const TYPE_CODE: i32 = 60;
1663
1664    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1665        let encoded = CONFIG.serialize(&self).context(InvalidData {
1666            command_type: Self::TYPE_CODE,
1667        })?;
1668        Ok(encoded)
1669    }
1670
1671    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1672        let decoded: AuthTokenCheckFailedCommand = CONFIG.deserialize(input).context(InvalidData {
1673            command_type: Self::TYPE_CODE,
1674        })?;
1675        Ok(decoded)
1676    }
1677}
1678
1679impl Reply for AuthTokenCheckFailedCommand {
1680    fn get_request_id(&self) -> i64 {
1681        self.request_id
1682    }
1683}
1684
1685#[derive(PartialEq, Debug, Clone)]
1686pub enum ErrorCode {
1687    Unspecified,
1688    TokenCheckedFailed,
1689    TokenExpired,
1690}
1691
1692impl ErrorCode {
1693    pub fn get_code(error_code: &ErrorCode) -> i32 {
1694        match error_code {
1695            ErrorCode::Unspecified => -1,
1696            ErrorCode::TokenCheckedFailed => 0,
1697            ErrorCode::TokenExpired => 1,
1698        }
1699    }
1700    pub fn value_of(code: i32) -> ErrorCode {
1701        match code {
1702            -1 => ErrorCode::Unspecified,
1703            0 => ErrorCode::TokenCheckedFailed,
1704            1 => ErrorCode::TokenExpired,
1705            _ => panic!("Unknown value: {}", code),
1706        }
1707    }
1708}
1709
1710/**
1711 * 46. UpdateTableEntries Command
1712 */
1713#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1714pub struct UpdateTableEntriesCommand {
1715    pub request_id: i64,
1716    pub segment: String,
1717    pub delegation_token: String,
1718    pub table_entries: TableEntries,
1719    pub table_segment_offset: i64,
1720}
1721
1722impl Command for UpdateTableEntriesCommand {
1723    const TYPE_CODE: i32 = 74;
1724
1725    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1726        let encoded = CONFIG.serialize(&self).context(InvalidData {
1727            command_type: Self::TYPE_CODE,
1728        })?;
1729        Ok(encoded)
1730    }
1731
1732    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1733        let decoded: UpdateTableEntriesCommand = CONFIG.deserialize(input).context(InvalidData {
1734            command_type: Self::TYPE_CODE,
1735        })?;
1736        Ok(decoded)
1737    }
1738}
1739
1740impl Request for UpdateTableEntriesCommand {
1741    fn get_request_id(&self) -> i64 {
1742        self.request_id
1743    }
1744}
1745
1746/**
1747 * 47. TableEntriesUpdated Command
1748 */
1749#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1750pub struct TableEntriesUpdatedCommand {
1751    pub request_id: i64,
1752    pub updated_versions: Vec<i64>,
1753}
1754
1755impl Command for TableEntriesUpdatedCommand {
1756    const TYPE_CODE: i32 = 75;
1757
1758    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1759        let encoded = CONFIG.serialize(&self).context(InvalidData {
1760            command_type: Self::TYPE_CODE,
1761        })?;
1762        Ok(encoded)
1763    }
1764
1765    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1766        let decoded: TableEntriesUpdatedCommand = CONFIG.deserialize(input).context(InvalidData {
1767            command_type: Self::TYPE_CODE,
1768        })?;
1769        Ok(decoded)
1770    }
1771}
1772
1773impl Reply for TableEntriesUpdatedCommand {
1774    fn get_request_id(&self) -> i64 {
1775        self.request_id
1776    }
1777}
1778
1779/**
1780 * 48. RemoveTableKeys Command
1781 */
1782#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1783pub struct RemoveTableKeysCommand {
1784    pub request_id: i64,
1785    pub segment: String,
1786    pub delegation_token: String,
1787    pub keys: Vec<TableKey>,
1788    pub table_segment_offset: i64,
1789}
1790
1791impl Command for RemoveTableKeysCommand {
1792    const TYPE_CODE: i32 = 76;
1793
1794    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1795        let encoded = CONFIG.serialize(&self).context(InvalidData {
1796            command_type: Self::TYPE_CODE,
1797        })?;
1798        Ok(encoded)
1799    }
1800
1801    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1802        let decoded: RemoveTableKeysCommand = CONFIG.deserialize(input).context(InvalidData {
1803            command_type: Self::TYPE_CODE,
1804        })?;
1805        Ok(decoded)
1806    }
1807}
1808
1809impl Request for RemoveTableKeysCommand {
1810    fn get_request_id(&self) -> i64 {
1811        self.request_id
1812    }
1813}
1814
1815/**
1816 * 49. TableKeysRemoved Command
1817 */
1818#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1819pub struct TableKeysRemovedCommand {
1820    pub request_id: i64,
1821    pub segment: String,
1822}
1823
1824impl Command for TableKeysRemovedCommand {
1825    const TYPE_CODE: i32 = 77;
1826
1827    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1828        let encoded = CONFIG.serialize(&self).context(InvalidData {
1829            command_type: Self::TYPE_CODE,
1830        })?;
1831        Ok(encoded)
1832    }
1833
1834    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1835        let decoded: TableKeysRemovedCommand = CONFIG.deserialize(input).context(InvalidData {
1836            command_type: Self::TYPE_CODE,
1837        })?;
1838        Ok(decoded)
1839    }
1840}
1841
1842impl Reply for TableKeysRemovedCommand {
1843    fn get_request_id(&self) -> i64 {
1844        self.request_id
1845    }
1846}
1847
1848/**
1849 * 50. ReadTable Command
1850 */
1851#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1852pub struct ReadTableCommand {
1853    pub request_id: i64,
1854    pub segment: String,
1855    pub delegation_token: String,
1856    pub keys: Vec<TableKey>,
1857}
1858
1859impl Command for ReadTableCommand {
1860    const TYPE_CODE: i32 = 78;
1861
1862    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1863        let encoded = CONFIG.serialize(&self).context(InvalidData {
1864            command_type: Self::TYPE_CODE,
1865        })?;
1866        Ok(encoded)
1867    }
1868
1869    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1870        let decoded: ReadTableCommand = CONFIG.deserialize(input).context(InvalidData {
1871            command_type: Self::TYPE_CODE,
1872        })?;
1873        Ok(decoded)
1874    }
1875}
1876
1877impl Request for ReadTableCommand {
1878    fn get_request_id(&self) -> i64 {
1879        self.request_id
1880    }
1881}
1882
1883/**
1884 * 51. TableRead Command
1885 */
1886#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1887pub struct TableReadCommand {
1888    pub request_id: i64,
1889    pub segment: String,
1890    pub entries: TableEntries,
1891}
1892
1893impl Command for TableReadCommand {
1894    const TYPE_CODE: i32 = 79;
1895
1896    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1897        let encoded = CONFIG.serialize(&self).context(InvalidData {
1898            command_type: Self::TYPE_CODE,
1899        })?;
1900        Ok(encoded)
1901    }
1902
1903    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1904        let decoded: TableReadCommand = CONFIG.deserialize(input).context(InvalidData {
1905            command_type: Self::TYPE_CODE,
1906        })?;
1907        Ok(decoded)
1908    }
1909}
1910
1911impl Reply for TableReadCommand {
1912    fn get_request_id(&self) -> i64 {
1913        self.request_id
1914    }
1915}
1916
1917/**
1918 * 52. ReadTableKeys Command
1919 */
1920#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1921pub struct ReadTableKeysCommand {
1922    pub request_id: i64,
1923    pub segment: String,
1924    pub delegation_token: String,
1925    pub suggested_key_count: i32,
1926    pub continuation_token: Vec<u8>,
1927}
1928
1929impl Command for ReadTableKeysCommand {
1930    const TYPE_CODE: i32 = 83;
1931
1932    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1933        let encoded = CONFIG.serialize(&self).context(InvalidData {
1934            command_type: Self::TYPE_CODE,
1935        })?;
1936        Ok(encoded)
1937    }
1938
1939    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1940        let decoded: ReadTableKeysCommand = CONFIG.deserialize(input).context(InvalidData {
1941            command_type: Self::TYPE_CODE,
1942        })?;
1943        Ok(decoded)
1944    }
1945}
1946
1947impl Request for ReadTableKeysCommand {
1948    fn get_request_id(&self) -> i64 {
1949        self.request_id
1950    }
1951}
1952
1953/**
1954 * 53. TableKeysRead Command
1955 */
1956#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1957pub struct TableKeysReadCommand {
1958    pub request_id: i64,
1959    pub segment: String,
1960    pub keys: Vec<TableKey>,
1961    pub continuation_token: Vec<u8>, // this is used to indicate the point from which the next keys should be fetched.
1962}
1963
1964impl Command for TableKeysReadCommand {
1965    const TYPE_CODE: i32 = 84;
1966
1967    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
1968        let encoded = CONFIG.serialize(&self).context(InvalidData {
1969            command_type: Self::TYPE_CODE,
1970        })?;
1971        Ok(encoded)
1972    }
1973
1974    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
1975        let decoded: TableKeysReadCommand = CONFIG.deserialize(input).context(InvalidData {
1976            command_type: Self::TYPE_CODE,
1977        })?;
1978        Ok(decoded)
1979    }
1980}
1981
1982impl Reply for TableKeysReadCommand {
1983    fn get_request_id(&self) -> i64 {
1984        self.request_id
1985    }
1986}
1987
1988/**
1989 * 54. ReadTableEntries Command
1990 */
1991#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
1992pub struct ReadTableEntriesCommand {
1993    pub request_id: i64,
1994    pub segment: String,
1995    pub delegation_token: String,
1996    pub suggested_entry_count: i32,
1997    pub continuation_token: Vec<u8>,
1998}
1999
2000impl Command for ReadTableEntriesCommand {
2001    const TYPE_CODE: i32 = 85;
2002
2003    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2004        let encoded = CONFIG.serialize(&self).context(InvalidData {
2005            command_type: Self::TYPE_CODE,
2006        })?;
2007        Ok(encoded)
2008    }
2009
2010    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2011        let decoded: ReadTableEntriesCommand = CONFIG.deserialize(input).context(InvalidData {
2012            command_type: Self::TYPE_CODE,
2013        })?;
2014        Ok(decoded)
2015    }
2016}
2017
2018impl Request for ReadTableEntriesCommand {
2019    fn get_request_id(&self) -> i64 {
2020        self.request_id
2021    }
2022}
2023
2024/**
2025 * 55. TableEntriesRead Command
2026 */
2027#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2028pub struct TableEntriesReadCommand {
2029    pub request_id: i64,
2030    pub segment: String,
2031    pub entries: TableEntries,
2032    pub continuation_token: Vec<u8>,
2033}
2034
2035impl Command for TableEntriesReadCommand {
2036    const TYPE_CODE: i32 = 86;
2037
2038    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2039        let encoded = CONFIG.serialize(&self).context(InvalidData {
2040            command_type: Self::TYPE_CODE,
2041        })?;
2042        Ok(encoded)
2043    }
2044
2045    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2046        let decoded: TableEntriesReadCommand = CONFIG.deserialize(input).context(InvalidData {
2047            command_type: Self::TYPE_CODE,
2048        })?;
2049        Ok(decoded)
2050    }
2051}
2052
2053impl Reply for TableEntriesReadCommand {
2054    fn get_request_id(&self) -> i64 {
2055        self.request_id
2056    }
2057}
2058
2059/**
2060 * 56. TableKeyDoesNotExist Command
2061 */
2062#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2063pub struct TableKeyDoesNotExistCommand {
2064    pub request_id: i64,
2065    pub segment: String,
2066    pub server_stack_trace: String,
2067}
2068
2069impl Command for TableKeyDoesNotExistCommand {
2070    const TYPE_CODE: i32 = 81;
2071
2072    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2073        let encoded = CONFIG.serialize(&self).context(InvalidData {
2074            command_type: Self::TYPE_CODE,
2075        })?;
2076        Ok(encoded)
2077    }
2078
2079    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2080        let decoded: TableKeyDoesNotExistCommand = CONFIG.deserialize(input).context(InvalidData {
2081            command_type: Self::TYPE_CODE,
2082        })?;
2083        Ok(decoded)
2084    }
2085}
2086
2087impl Reply for TableKeyDoesNotExistCommand {
2088    fn get_request_id(&self) -> i64 {
2089        self.request_id
2090    }
2091
2092    fn is_failure(&self) -> bool {
2093        true
2094    }
2095}
2096
2097impl fmt::Display for TableKeyDoesNotExistCommand {
2098    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2099        write!(
2100            f,
2101            "Conditional table update failed since the key does not exist : {}",
2102            self.segment
2103        )
2104    }
2105}
2106
2107/**
2108 * 57. TableKeyBadVersion Command
2109 */
2110#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2111pub struct TableKeyBadVersionCommand {
2112    pub request_id: i64,
2113    pub segment: String,
2114    pub server_stack_trace: String,
2115}
2116
2117impl Command for TableKeyBadVersionCommand {
2118    const TYPE_CODE: i32 = 82;
2119
2120    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2121        let encoded = CONFIG.serialize(&self).context(InvalidData {
2122            command_type: Self::TYPE_CODE,
2123        })?;
2124        Ok(encoded)
2125    }
2126
2127    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2128        let decoded: TableKeyBadVersionCommand = CONFIG.deserialize(input).context(InvalidData {
2129            command_type: Self::TYPE_CODE,
2130        })?;
2131        Ok(decoded)
2132    }
2133}
2134
2135impl Reply for TableKeyBadVersionCommand {
2136    fn get_request_id(&self) -> i64 {
2137        self.request_id
2138    }
2139
2140    fn is_failure(&self) -> bool {
2141        true
2142    }
2143}
2144
2145impl fmt::Display for TableKeyBadVersionCommand {
2146    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2147        write!(
2148            f,
2149            "Conditional table update failed since the key version is incorrect : {}",
2150            self.segment
2151        )
2152    }
2153}
2154
2155/**
2156 * Table Key Struct.
2157 * Need to override the serialize
2158 */
2159#[derive(Serialize, Deserialize, Debug, Clone, Eq)]
2160pub struct TableKey {
2161    pub payload: i32,
2162    #[serde(with = "serde_bytes")]
2163    pub data: Vec<u8>,
2164    pub key_version: i64,
2165}
2166
2167impl TableKey {
2168    pub const KEY_NO_VERSION: i64 = i64::min_value();
2169    pub const KEY_NOT_EXISTS: i64 = -1i64;
2170    const HEADER_BYTES: i32 = 2 * 4;
2171    pub fn new(data: Vec<u8>, key_version: i64) -> TableKey {
2172        let payload = (4 + data.len() + 8) as i32;
2173        TableKey {
2174            payload,
2175            data,
2176            key_version,
2177        }
2178    }
2179}
2180
2181impl Hash for TableKey {
2182    fn hash<H>(&self, state: &mut H)
2183    where
2184        H: Hasher,
2185    {
2186        self.data.hash(state);
2187    }
2188}
2189
2190impl PartialEq for TableKey {
2191    fn eq(&self, other: &Self) -> bool {
2192        self.data == other.data
2193    }
2194}
2195
2196#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2197pub struct TableValue {
2198    pub payload: i32,
2199    #[serde(with = "serde_bytes")]
2200    pub data: Vec<u8>,
2201}
2202
2203impl TableValue {
2204    const HEADER_BYTES: i32 = 2 * 4;
2205    pub fn new(data: Vec<u8>) -> TableValue {
2206        let payload = (data.len() + 4) as i32;
2207        TableValue { payload, data }
2208    }
2209}
2210
2211/**
2212 * TableEntries Struct.
2213 */
2214#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2215pub struct TableEntries {
2216    pub entries: Vec<(TableKey, TableValue)>,
2217}
2218
2219impl TableEntries {
2220    fn get_header_byte(entry_count: i32) -> i32 {
2221        4 + entry_count * (TableKey::HEADER_BYTES + TableValue::HEADER_BYTES)
2222    }
2223
2224    pub fn size(&self) -> i32 {
2225        let mut data_bytes = 0;
2226        for x in &self.entries {
2227            data_bytes += (x.0.data.len() + 8 + x.1.data.len()) as i32
2228        }
2229
2230        data_bytes + TableEntries::get_header_byte(self.entries.len() as i32)
2231    }
2232}
2233
2234/**
2235 * 58 ReadTableEntriesDelta Command
2236 */
2237#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2238pub struct ReadTableEntriesDeltaCommand {
2239    pub request_id: i64,
2240    pub segment: String,
2241    pub delegation_token: String,
2242    pub from_position: i64,
2243    pub suggested_entry_count: i32,
2244}
2245
2246impl Command for ReadTableEntriesDeltaCommand {
2247    const TYPE_CODE: i32 = 88;
2248
2249    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2250        let encoded = CONFIG.serialize(&self).context(InvalidData {
2251            command_type: Self::TYPE_CODE,
2252        })?;
2253        Ok(encoded)
2254    }
2255
2256    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2257        let decoded: ReadTableEntriesDeltaCommand = CONFIG.deserialize(input).context(InvalidData {
2258            command_type: Self::TYPE_CODE,
2259        })?;
2260        Ok(decoded)
2261    }
2262}
2263
2264impl Request for ReadTableEntriesDeltaCommand {
2265    fn get_request_id(&self) -> i64 {
2266        self.request_id
2267    }
2268}
2269
2270/**
2271 * 59 TableEntriesDeltaRead Command
2272 */
2273#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2274pub struct TableEntriesDeltaReadCommand {
2275    pub request_id: i64,
2276    pub segment: String,
2277    pub entries: TableEntries,
2278    pub should_clear: bool,
2279    pub reached_end: bool,
2280    pub last_position: i64,
2281}
2282
2283impl Command for TableEntriesDeltaReadCommand {
2284    const TYPE_CODE: i32 = 87;
2285
2286    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2287        let encoded = CONFIG.serialize(&self).context(InvalidData {
2288            command_type: Self::TYPE_CODE,
2289        })?;
2290        Ok(encoded)
2291    }
2292
2293    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2294        let decoded: TableEntriesDeltaReadCommand = CONFIG.deserialize(input).context(InvalidData {
2295            command_type: Self::TYPE_CODE,
2296        })?;
2297        Ok(decoded)
2298    }
2299}
2300
2301impl Reply for TableEntriesDeltaReadCommand {
2302    fn get_request_id(&self) -> i64 {
2303        self.request_id
2304    }
2305}
2306
2307/**
2308 * 60.ConditionalAppendRawBytes Command
2309 */
2310#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2311pub struct ConditionalBlockEndCommand {
2312    pub writer_id: u128,
2313    pub event_number: i64,
2314    pub expected_offset: i64,
2315    #[serde(with = "serde_bytes")]
2316    pub data: Vec<u8>,
2317    pub request_id: i64,
2318}
2319
2320impl Command for ConditionalBlockEndCommand {
2321    const TYPE_CODE: i32 = 89;
2322
2323    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2324        let encoded = CONFIG.serialize(&self).context(InvalidData {
2325            command_type: Self::TYPE_CODE,
2326        })?;
2327        Ok(encoded)
2328    }
2329
2330    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2331        let decoded: ConditionalBlockEndCommand = CONFIG.deserialize(input).context(InvalidData {
2332            command_type: Self::TYPE_CODE,
2333        })?;
2334        Ok(decoded)
2335    }
2336}
2337
2338impl Request for ConditionalBlockEndCommand {
2339    fn get_request_id(&self) -> i64 {
2340        self.request_id
2341    }
2342}
2343
2344/**
2345 * 61. CreateTransientSegment Command
2346 */
2347#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2348pub struct CreateTransientSegmentCommand {
2349    pub request_id: i64,
2350    pub writer_id: u128,
2351    pub segment: String,
2352    pub delegation_token: String,
2353}
2354
2355impl Command for CreateTransientSegmentCommand {
2356    const TYPE_CODE: i32 = 40;
2357
2358    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2359        let encoded = CONFIG.serialize(&self).context(InvalidData {
2360            command_type: Self::TYPE_CODE,
2361        })?;
2362        Ok(encoded)
2363    }
2364
2365    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2366        let decoded: CreateTransientSegmentCommand = CONFIG.deserialize(input).context(InvalidData {
2367            command_type: Self::TYPE_CODE,
2368        })?;
2369        Ok(decoded)
2370    }
2371}
2372
2373impl Request for CreateTransientSegmentCommand {
2374    fn get_request_id(&self) -> i64 {
2375        self.request_id
2376    }
2377}
2378
2379/**
2380 * 62. CreateTransientSegment Command
2381 */
2382#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2383pub struct FlushToStorageCommand {
2384    pub container_id: i32,
2385    pub delegation_token: String,
2386    pub request_id: i64,
2387}
2388
2389impl Command for FlushToStorageCommand {
2390    const TYPE_CODE: i32 = -3;
2391
2392    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2393        let encoded = CONFIG.serialize(&self).context(InvalidData {
2394            command_type: Self::TYPE_CODE,
2395        })?;
2396        Ok(encoded)
2397    }
2398
2399    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2400        let decoded: FlushToStorageCommand = CONFIG.deserialize(input).context(InvalidData {
2401            command_type: Self::TYPE_CODE,
2402        })?;
2403        Ok(decoded)
2404    }
2405}
2406
2407impl Request for FlushToStorageCommand {
2408    fn get_request_id(&self) -> i64 {
2409        self.request_id
2410    }
2411}
2412
2413/**
2414 * 63. StorageFlushed Command
2415 */
2416#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2417pub struct StorageFlushedCommand {
2418    pub request_id: i64,
2419}
2420
2421impl Command for StorageFlushedCommand {
2422    const TYPE_CODE: i32 = -4;
2423
2424    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2425        let encoded = CONFIG.serialize(&self).context(InvalidData {
2426            command_type: Self::TYPE_CODE,
2427        })?;
2428        Ok(encoded)
2429    }
2430
2431    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2432        let decoded: StorageFlushedCommand = CONFIG.deserialize(input).context(InvalidData {
2433            command_type: Self::TYPE_CODE,
2434        })?;
2435        Ok(decoded)
2436    }
2437}
2438
2439impl Reply for StorageFlushedCommand {
2440    fn get_request_id(&self) -> i64 {
2441        self.request_id
2442    }
2443}
2444
2445/**
2446 * 64. ListStorageChunks Command
2447 */
2448#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2449pub struct ListStorageChunksCommand {
2450    pub segment: String,
2451    pub delegation_token: String,
2452    pub request_id: i64,
2453}
2454
2455impl Command for ListStorageChunksCommand {
2456    const TYPE_CODE: i32 = -5;
2457
2458    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2459        let encoded = CONFIG.serialize(&self).context(InvalidData {
2460            command_type: Self::TYPE_CODE,
2461        })?;
2462        Ok(encoded)
2463    }
2464
2465    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2466        let decoded: ListStorageChunksCommand = CONFIG.deserialize(input).context(InvalidData {
2467            command_type: Self::TYPE_CODE,
2468        })?;
2469        Ok(decoded)
2470    }
2471}
2472
2473impl Request for ListStorageChunksCommand {
2474    fn get_request_id(&self) -> i64 {
2475        self.request_id
2476    }
2477}
2478
2479/**
2480 * Chunkinfo Struct.
2481 * Need to override the serialize
2482 */
2483#[derive(Serialize, Deserialize, Debug, Clone, Eq)]
2484pub struct ChunkInfo {
2485    pub length_in_metadata: i64,
2486    pub length_in_storage: i64,
2487    pub start_offset: i64,
2488    pub chunck_name: String,
2489    pub exists_in_storage: bool,
2490}
2491
2492impl ChunkInfo {
2493    pub fn new(
2494        length_in_metadata: i64,
2495        length_in_storage: i64,
2496        start_offset: i64,
2497        chunck_name: String,
2498        exists_in_storage: bool,
2499    ) -> ChunkInfo {
2500        ChunkInfo {
2501            length_in_metadata,
2502            length_in_storage,
2503            start_offset,
2504            chunck_name,
2505            exists_in_storage,
2506        }
2507    }
2508}
2509
2510impl PartialEq for ChunkInfo {
2511    fn eq(&self, other: &Self) -> bool {
2512        self.chunck_name == other.chunck_name
2513    }
2514}
2515
2516/**
2517 * 65. StorageChunksListed Command
2518 */
2519#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2520pub struct StorageChunksListedCommand {
2521    pub request_id: i64,
2522    pub chunks: Vec<ChunkInfo>,
2523}
2524
2525impl Command for StorageChunksListedCommand {
2526    const TYPE_CODE: i32 = -6;
2527
2528    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2529        let encoded = CONFIG.serialize(&self).context(InvalidData {
2530            command_type: Self::TYPE_CODE,
2531        })?;
2532        Ok(encoded)
2533    }
2534
2535    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2536        let decoded: StorageChunksListedCommand = CONFIG.deserialize(input).context(InvalidData {
2537            command_type: Self::TYPE_CODE,
2538        })?;
2539        Ok(decoded)
2540    }
2541}
2542
2543impl Reply for StorageChunksListedCommand {
2544    fn get_request_id(&self) -> i64 {
2545        self.request_id
2546    }
2547}
2548
2549/**
2550 * 66. ErrorMessage Command
2551 */
2552#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2553pub struct ErrorMessageCommand {
2554    pub request_id: i64,
2555    pub segment: String,
2556    pub message: String,
2557    pub error_code: i32,
2558}
2559
2560impl Command for ErrorMessageCommand {
2561    const TYPE_CODE: i32 = 61;
2562
2563    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2564        let encoded = CONFIG.serialize(&self).context(InvalidData {
2565            command_type: Self::TYPE_CODE,
2566        })?;
2567        Ok(encoded)
2568    }
2569
2570    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2571        let decoded: ErrorMessageCommand = CONFIG.deserialize(input).context(InvalidData {
2572            command_type: Self::TYPE_CODE,
2573        })?;
2574        Ok(decoded)
2575    }
2576}
2577
2578impl Reply for ErrorMessageCommand {
2579    fn get_request_id(&self) -> i64 {
2580        self.request_id
2581    }
2582}
2583
2584/**
2585 * 67. GetTableSegmentInfo Command
2586 */
2587#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2588pub struct GetTableSegmentInfoCommand {
2589    pub request_id: i64,
2590    pub segment_name: String,
2591    pub delegation_token: String,
2592}
2593
2594impl Command for GetTableSegmentInfoCommand {
2595    const TYPE_CODE: i32 = 68;
2596
2597    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2598        let encoded = CONFIG.serialize(&self).context(InvalidData {
2599            command_type: Self::TYPE_CODE,
2600        })?;
2601        Ok(encoded)
2602    }
2603
2604    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2605        let decoded: GetTableSegmentInfoCommand = CONFIG.deserialize(input).context(InvalidData {
2606            command_type: Self::TYPE_CODE,
2607        })?;
2608        Ok(decoded)
2609    }
2610}
2611
2612impl Request for GetTableSegmentInfoCommand {
2613    fn get_request_id(&self) -> i64 {
2614        self.request_id
2615    }
2616}
2617
2618/**
2619 * 68. MergeSegmentsBatch Command
2620 */
2621#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2622pub struct MergeSegmentsBatchCommand {
2623    pub request_id: i64,
2624    pub target_segment_id: String,
2625    pub source_segment_ids: Vec<String>,
2626    pub delegation_token: String,
2627}
2628
2629impl Command for MergeSegmentsBatchCommand {
2630    const TYPE_CODE: i32 = 90;
2631
2632    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2633        let encoded = CONFIG.serialize(&self).context(InvalidData {
2634            command_type: Self::TYPE_CODE,
2635        })?;
2636        Ok(encoded)
2637    }
2638
2639    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2640        let decoded: MergeSegmentsBatchCommand = CONFIG.deserialize(input).context(InvalidData {
2641            command_type: Self::TYPE_CODE,
2642        })?;
2643        Ok(decoded)
2644    }
2645}
2646
2647impl Request for MergeSegmentsBatchCommand {
2648    fn get_request_id(&self) -> i64 {
2649        self.request_id
2650    }
2651}
2652
2653/**
2654 * 69. ErrorMessage Command
2655 */
2656#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2657pub struct TableSegmentInfoCommand {
2658    pub request_id: i64,
2659    pub segment_name: String,
2660    pub delegation_token: String,
2661}
2662
2663impl Command for TableSegmentInfoCommand {
2664    const TYPE_CODE: i32 = 69;
2665
2666    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2667        let encoded = CONFIG.serialize(&self).context(InvalidData {
2668            command_type: Self::TYPE_CODE,
2669        })?;
2670        Ok(encoded)
2671    }
2672
2673    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2674        let decoded: TableSegmentInfoCommand = CONFIG.deserialize(input).context(InvalidData {
2675            command_type: Self::TYPE_CODE,
2676        })?;
2677        Ok(decoded)
2678    }
2679}
2680
2681impl Reply for TableSegmentInfoCommand {
2682    fn get_request_id(&self) -> i64 {
2683        self.request_id
2684    }
2685}
2686
2687/**
2688 * 70. SegmentsBatchMerged Command
2689 */
2690#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2691pub struct SegmentsBatchMergedCommand {
2692    pub request_id: i64,
2693    pub target: String,
2694    pub sources: Vec<String>,
2695    pub new_target_write_offset: Vec<i64>,
2696}
2697
2698impl Command for SegmentsBatchMergedCommand {
2699    const TYPE_CODE: i32 = 91;
2700
2701    fn write_fields(&self) -> Result<Vec<u8>, CommandError> {
2702        let encoded = CONFIG.serialize(&self).context(InvalidData {
2703            command_type: Self::TYPE_CODE,
2704        })?;
2705        Ok(encoded)
2706    }
2707
2708    fn read_from(input: &[u8]) -> Result<Self, CommandError> {
2709        let decoded: SegmentsBatchMergedCommand = CONFIG.deserialize(input).context(InvalidData {
2710            command_type: Self::TYPE_CODE,
2711        })?;
2712        Ok(decoded)
2713    }
2714}
2715
2716impl Reply for SegmentsBatchMergedCommand {
2717    fn get_request_id(&self) -> i64 {
2718        self.request_id
2719    }
2720}