1use std::fmt::{Display, Formatter};
16use std::ops::Bound;
17
18use api::v1::meta::{
19 BatchDeleteRequest as PbBatchDeleteRequest, BatchDeleteResponse as PbBatchDeleteResponse,
20 BatchGetRequest as PbBatchGetRequest, BatchGetResponse as PbBatchGetResponse,
21 BatchPutRequest as PbBatchPutRequest, BatchPutResponse as PbBatchPutResponse,
22 CompareAndPutRequest as PbCompareAndPutRequest,
23 CompareAndPutResponse as PbCompareAndPutResponse, DeleteRangeRequest as PbDeleteRangeRequest,
24 DeleteRangeResponse as PbDeleteRangeResponse, PutRequest as PbPutRequest,
25 PutResponse as PbPutResponse, RangeRequest as PbRangeRequest, RangeResponse as PbRangeResponse,
26 ResponseHeader as PbResponseHeader,
27};
28
29use crate::error::Result;
30use crate::rpc::KeyValue;
31use crate::{error, util};
32
33pub fn to_range(key: Vec<u8>, range_end: Vec<u8>) -> (Bound<Vec<u8>>, Bound<Vec<u8>>) {
34 match (&key[..], &range_end[..]) {
35 (_, []) => (Bound::Included(key.clone()), Bound::Included(key)),
36 ([0], [0]) => (Bound::Unbounded, Bound::Unbounded),
38 (_, [0]) => (Bound::Included(key), Bound::Unbounded),
40 (_, _) => (Bound::Included(key), Bound::Excluded(range_end)),
41 }
42}
43
44#[derive(Debug, Clone, Default)]
45pub struct RangeRequest {
46 pub key: Vec<u8>,
49 pub range_end: Vec<u8>,
56 pub limit: i64,
59 pub keys_only: bool,
61}
62
63impl From<RangeRequest> for PbRangeRequest {
64 fn from(req: RangeRequest) -> Self {
65 Self {
66 header: None,
67 key: req.key,
68 range_end: req.range_end,
69 limit: req.limit,
70 keys_only: req.keys_only,
71 }
72 }
73}
74
75impl From<PbRangeRequest> for RangeRequest {
76 fn from(value: PbRangeRequest) -> Self {
77 Self {
78 key: value.key,
79 range_end: value.range_end,
80 limit: value.limit,
81 keys_only: value.keys_only,
82 }
83 }
84}
85
86impl Display for RangeRequest {
87 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
88 write!(
89 f,
90 "RangeRequest{{key: '{}', range_end: '{}', limit: {}, keys_only: {}}}",
91 String::from_utf8_lossy(&self.key),
92 String::from_utf8_lossy(&self.range_end),
93 self.limit,
94 self.keys_only
95 )
96 }
97}
98
99impl RangeRequest {
100 pub fn new() -> Self {
101 Self {
102 key: vec![],
103 range_end: vec![],
104 limit: 0,
105 keys_only: false,
106 }
107 }
108
109 pub fn range(&self) -> (Bound<Vec<u8>>, Bound<Vec<u8>>) {
111 to_range(self.key.clone(), self.range_end.clone())
112 }
113
114 pub fn with_key(mut self, key: impl Into<Vec<u8>>) -> Self {
117 self.key = key.into();
118 self
119 }
120
121 pub fn with_range(mut self, key: impl Into<Vec<u8>>, range_end: impl Into<Vec<u8>>) -> Self {
131 self.key = key.into();
132 self.range_end = range_end.into();
133 self
134 }
135
136 pub fn with_prefix(mut self, key: impl Into<Vec<u8>>) -> Self {
139 self.key = key.into();
140 self.range_end = util::get_prefix_end_key(&self.key);
141 self
142 }
143
144 pub fn with_limit(mut self, limit: i64) -> Self {
147 self.limit = limit;
148 self
149 }
150
151 pub fn with_keys_only(mut self) -> Self {
153 self.keys_only = true;
154 self
155 }
156}
157
158#[derive(Debug, Clone, PartialEq)]
159pub struct RangeResponse {
160 pub kvs: Vec<KeyValue>,
161 pub more: bool,
162}
163
164impl Display for RangeResponse {
165 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
166 write!(
167 f,
168 "RangeResponse{{kvs: [{}], more: {}}}",
169 self.kvs
170 .iter()
171 .map(|kv| kv.to_string())
172 .collect::<Vec<_>>()
173 .join(", "),
174 self.more
175 )
176 }
177}
178
179impl TryFrom<PbRangeResponse> for RangeResponse {
180 type Error = error::Error;
181
182 fn try_from(pb: PbRangeResponse) -> Result<Self> {
183 util::check_response_header(pb.header.as_ref())?;
184
185 Ok(Self {
186 kvs: pb.kvs.into_iter().map(KeyValue::new).collect(),
187 more: pb.more,
188 })
189 }
190}
191
192impl RangeResponse {
193 pub fn to_proto_resp(self, header: PbResponseHeader) -> PbRangeResponse {
194 PbRangeResponse {
195 header: Some(header),
196 kvs: self.kvs.into_iter().map(Into::into).collect(),
197 more: self.more,
198 }
199 }
200
201 pub fn take_kvs(&mut self) -> Vec<KeyValue> {
202 self.kvs.drain(..).collect()
203 }
204}
205
206#[derive(Debug, Clone, Default)]
207pub struct PutRequest {
208 pub key: Vec<u8>,
210 pub value: Vec<u8>,
213 pub prev_kv: bool,
216}
217
218impl From<PutRequest> for PbPutRequest {
219 fn from(req: PutRequest) -> Self {
220 Self {
221 header: None,
222 key: req.key,
223 value: req.value,
224 prev_kv: req.prev_kv,
225 }
226 }
227}
228
229impl From<PbPutRequest> for PutRequest {
230 fn from(value: PbPutRequest) -> Self {
231 Self {
232 key: value.key,
233 value: value.value,
234 prev_kv: value.prev_kv,
235 }
236 }
237}
238
239impl PutRequest {
240 pub fn new() -> Self {
241 Self {
242 key: vec![],
243 value: vec![],
244 prev_kv: false,
245 }
246 }
247
248 pub fn with_key(mut self, key: impl Into<Vec<u8>>) -> Self {
250 self.key = key.into();
251 self
252 }
253
254 pub fn with_value(mut self, value: impl Into<Vec<u8>>) -> Self {
257 self.value = value.into();
258 self
259 }
260
261 pub fn with_prev_kv(mut self) -> Self {
264 self.prev_kv = true;
265 self
266 }
267}
268
269#[derive(Debug, Clone, PartialEq, Default)]
270pub struct PutResponse {
271 pub prev_kv: Option<KeyValue>,
272}
273
274impl TryFrom<PbPutResponse> for PutResponse {
275 type Error = error::Error;
276
277 fn try_from(pb: PbPutResponse) -> Result<Self> {
278 util::check_response_header(pb.header.as_ref())?;
279
280 Ok(Self {
281 prev_kv: pb.prev_kv.map(KeyValue::new),
282 })
283 }
284}
285
286impl PutResponse {
287 pub fn to_proto_resp(self, header: PbResponseHeader) -> PbPutResponse {
288 PbPutResponse {
289 header: Some(header),
290 prev_kv: self.prev_kv.map(Into::into),
291 }
292 }
293}
294
295#[derive(Clone)]
296pub struct BatchGetRequest {
297 pub keys: Vec<Vec<u8>>,
298}
299
300impl From<BatchGetRequest> for PbBatchGetRequest {
301 fn from(req: BatchGetRequest) -> Self {
302 Self {
303 header: None,
304 keys: req.keys,
305 }
306 }
307}
308
309impl From<PbBatchGetRequest> for BatchGetRequest {
310 fn from(value: PbBatchGetRequest) -> Self {
311 Self { keys: value.keys }
312 }
313}
314
315impl Default for BatchGetRequest {
316 fn default() -> Self {
317 Self::new()
318 }
319}
320
321impl BatchGetRequest {
322 pub fn new() -> Self {
323 Self { keys: vec![] }
324 }
325
326 pub fn with_keys(mut self, keys: Vec<Vec<u8>>) -> Self {
327 self.keys = keys;
328 self
329 }
330
331 pub fn add_key(mut self, key: impl Into<Vec<u8>>) -> Self {
332 self.keys.push(key.into());
333 self
334 }
335}
336
337#[derive(Debug, Clone)]
338pub struct BatchGetResponse {
339 pub kvs: Vec<KeyValue>,
340}
341
342impl Display for BatchGetResponse {
343 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
344 write!(
345 f,
346 "[{}]",
347 self.kvs
348 .iter()
349 .map(|kv| kv.to_string())
350 .collect::<Vec<_>>()
351 .join(", "),
352 )
353 }
354}
355
356impl TryFrom<PbBatchGetResponse> for BatchGetResponse {
357 type Error = error::Error;
358
359 fn try_from(pb: PbBatchGetResponse) -> Result<Self> {
360 util::check_response_header(pb.header.as_ref())?;
361
362 Ok(Self {
363 kvs: pb.kvs.into_iter().map(KeyValue::new).collect(),
364 })
365 }
366}
367
368impl BatchGetResponse {
369 pub fn to_proto_resp(self, header: PbResponseHeader) -> PbBatchGetResponse {
370 PbBatchGetResponse {
371 header: Some(header),
372 kvs: self.kvs.into_iter().map(Into::into).collect(),
373 }
374 }
375}
376
377#[derive(Debug, Clone, Default)]
378pub struct BatchPutRequest {
379 pub kvs: Vec<KeyValue>,
380 pub prev_kv: bool,
383}
384
385impl From<BatchPutRequest> for PbBatchPutRequest {
386 fn from(req: BatchPutRequest) -> Self {
387 Self {
388 header: None,
389 kvs: req.kvs.into_iter().map(Into::into).collect(),
390 prev_kv: req.prev_kv,
391 }
392 }
393}
394
395impl From<PbBatchPutRequest> for BatchPutRequest {
396 fn from(value: PbBatchPutRequest) -> Self {
397 Self {
398 kvs: value.kvs.into_iter().map(KeyValue::new).collect(),
399 prev_kv: value.prev_kv,
400 }
401 }
402}
403
404impl BatchPutRequest {
405 pub fn new() -> Self {
406 Self {
407 kvs: vec![],
408 prev_kv: false,
409 }
410 }
411
412 pub fn add_kv(mut self, key: impl Into<Vec<u8>>, value: impl Into<Vec<u8>>) -> Self {
413 self.kvs.push(KeyValue {
414 key: key.into(),
415 value: value.into(),
416 });
417 self
418 }
419
420 pub fn with_prev_kv(mut self) -> Self {
423 self.prev_kv = true;
424 self
425 }
426}
427
428#[derive(Debug, Clone, Default)]
429pub struct BatchPutResponse {
430 pub prev_kvs: Vec<KeyValue>,
431}
432
433impl TryFrom<PbBatchPutResponse> for BatchPutResponse {
434 type Error = error::Error;
435
436 fn try_from(pb: PbBatchPutResponse) -> Result<Self> {
437 util::check_response_header(pb.header.as_ref())?;
438
439 Ok(Self {
440 prev_kvs: pb.prev_kvs.into_iter().map(KeyValue::new).collect(),
441 })
442 }
443}
444
445impl BatchPutResponse {
446 pub fn to_proto_resp(self, header: PbResponseHeader) -> PbBatchPutResponse {
447 PbBatchPutResponse {
448 header: Some(header),
449 prev_kvs: self.prev_kvs.into_iter().map(Into::into).collect(),
450 }
451 }
452
453 pub fn take_prev_kvs(&mut self) -> Vec<KeyValue> {
454 self.prev_kvs.drain(..).collect()
455 }
456}
457
458#[derive(Debug, Clone, Default)]
459pub struct BatchDeleteRequest {
460 pub keys: Vec<Vec<u8>>,
461 pub prev_kv: bool,
464}
465
466impl From<BatchDeleteRequest> for PbBatchDeleteRequest {
467 fn from(req: BatchDeleteRequest) -> Self {
468 Self {
469 header: None,
470 keys: req.keys,
471 prev_kv: req.prev_kv,
472 }
473 }
474}
475
476impl From<PbBatchDeleteRequest> for BatchDeleteRequest {
477 fn from(value: PbBatchDeleteRequest) -> Self {
478 Self {
479 keys: value.keys,
480 prev_kv: value.prev_kv,
481 }
482 }
483}
484
485impl BatchDeleteRequest {
486 pub fn new() -> Self {
487 Self {
488 keys: vec![],
489 prev_kv: false,
490 }
491 }
492
493 pub fn with_keys(mut self, keys: Vec<Vec<u8>>) -> Self {
495 self.keys = keys;
496 self
497 }
498
499 pub fn add_key(mut self, key: impl Into<Vec<u8>>) -> Self {
500 self.keys.push(key.into());
501 self
502 }
503
504 pub fn with_prev_kv(mut self) -> Self {
507 self.prev_kv = true;
508 self
509 }
510}
511
512#[derive(Debug, Clone, Default)]
513pub struct BatchDeleteResponse {
514 pub prev_kvs: Vec<KeyValue>,
515}
516
517impl TryFrom<PbBatchDeleteResponse> for BatchDeleteResponse {
518 type Error = error::Error;
519
520 fn try_from(pb: PbBatchDeleteResponse) -> Result<Self> {
521 util::check_response_header(pb.header.as_ref())?;
522
523 Ok(Self {
524 prev_kvs: pb.prev_kvs.into_iter().map(KeyValue::new).collect(),
525 })
526 }
527}
528
529impl BatchDeleteResponse {
530 pub fn to_proto_resp(self, header: PbResponseHeader) -> PbBatchDeleteResponse {
531 PbBatchDeleteResponse {
532 header: Some(header),
533 prev_kvs: self.prev_kvs.into_iter().map(Into::into).collect(),
534 }
535 }
536}
537
538#[derive(Debug, Clone, Default)]
539pub struct CompareAndPutRequest {
540 pub key: Vec<u8>,
542 pub expect: Vec<u8>,
543 pub value: Vec<u8>,
546}
547
548impl From<CompareAndPutRequest> for PbCompareAndPutRequest {
549 fn from(req: CompareAndPutRequest) -> Self {
550 Self {
551 header: None,
552 key: req.key,
553 expect: req.expect,
554 value: req.value,
555 }
556 }
557}
558
559impl From<PbCompareAndPutRequest> for CompareAndPutRequest {
560 fn from(value: PbCompareAndPutRequest) -> Self {
561 Self {
562 key: value.key,
563 expect: value.expect,
564 value: value.value,
565 }
566 }
567}
568
569impl CompareAndPutRequest {
570 pub fn new() -> Self {
571 Self {
572 key: vec![],
573 expect: vec![],
574 value: vec![],
575 }
576 }
577
578 pub fn with_key(mut self, key: impl Into<Vec<u8>>) -> Self {
580 self.key = key.into();
581 self
582 }
583
584 pub fn with_expect(mut self, expect: impl Into<Vec<u8>>) -> Self {
586 self.expect = expect.into();
587 self
588 }
589
590 pub fn with_value(mut self, value: impl Into<Vec<u8>>) -> Self {
593 self.value = value.into();
594 self
595 }
596}
597
598#[derive(Debug, Clone, Default)]
599pub struct CompareAndPutResponse {
600 pub success: bool,
601 pub prev_kv: Option<KeyValue>,
602}
603
604impl TryFrom<PbCompareAndPutResponse> for CompareAndPutResponse {
605 type Error = error::Error;
606
607 fn try_from(pb: PbCompareAndPutResponse) -> Result<Self> {
608 util::check_response_header(pb.header.as_ref())?;
609
610 Ok(Self {
611 success: pb.success,
612 prev_kv: pb.prev_kv.map(KeyValue::new),
613 })
614 }
615}
616
617impl CompareAndPutResponse {
618 pub fn handle<R, E, F>(self, f: F) -> std::result::Result<R, E>
619 where
620 F: FnOnce(Self) -> std::result::Result<R, E>,
621 {
622 f(self)
623 }
624
625 pub fn to_proto_resp(self, header: PbResponseHeader) -> PbCompareAndPutResponse {
626 PbCompareAndPutResponse {
627 header: Some(header),
628 success: self.success,
629 prev_kv: self.prev_kv.map(Into::into),
630 }
631 }
632
633 pub fn is_success(&self) -> bool {
634 self.success
635 }
636
637 pub fn take_prev_kv(&mut self) -> Option<KeyValue> {
638 self.prev_kv.take()
639 }
640}
641
642#[derive(Debug, Clone, Default)]
643pub struct DeleteRangeRequest {
644 pub key: Vec<u8>,
646 pub range_end: Vec<u8>,
655 pub prev_kv: bool,
658 }
662
663impl From<DeleteRangeRequest> for PbDeleteRangeRequest {
664 fn from(req: DeleteRangeRequest) -> Self {
665 Self {
666 header: None,
667 key: req.key,
668 range_end: req.range_end,
669 prev_kv: req.prev_kv,
670 }
671 }
672}
673
674impl From<PbDeleteRangeRequest> for DeleteRangeRequest {
675 fn from(value: PbDeleteRangeRequest) -> Self {
676 Self {
677 key: value.key,
678 range_end: value.range_end,
679 prev_kv: value.prev_kv,
680 }
681 }
682}
683
684impl DeleteRangeRequest {
685 pub fn new() -> Self {
686 Self {
687 key: vec![],
688 range_end: vec![],
689 prev_kv: false,
690 }
691 }
692
693 pub fn range(&self) -> (Bound<Vec<u8>>, Bound<Vec<u8>>) {
695 to_range(self.key.clone(), self.range_end.clone())
696 }
697
698 pub fn with_key(mut self, key: impl Into<Vec<u8>>) -> Self {
701 self.key = key.into();
702 self
703 }
704
705 pub fn with_range(mut self, key: impl Into<Vec<u8>>, range_end: impl Into<Vec<u8>>) -> Self {
716 self.key = key.into();
717 self.range_end = range_end.into();
718 self
719 }
720
721 pub fn with_prefix(mut self, key: impl Into<Vec<u8>>) -> Self {
724 self.key = key.into();
725 self.range_end = util::get_prefix_end_key(&self.key);
726 self
727 }
728
729 pub fn with_prev_kv(mut self) -> Self {
732 self.prev_kv = true;
733 self
734 }
735}
736
737#[derive(Debug, Clone, PartialEq)]
738pub struct DeleteRangeResponse {
739 pub deleted: i64,
740 pub prev_kvs: Vec<KeyValue>,
741}
742
743impl TryFrom<PbDeleteRangeResponse> for DeleteRangeResponse {
744 type Error = error::Error;
745
746 fn try_from(pb: PbDeleteRangeResponse) -> Result<Self> {
747 util::check_response_header(pb.header.as_ref())?;
748
749 Ok(Self {
750 deleted: pb.deleted,
751 prev_kvs: pb.prev_kvs.into_iter().map(KeyValue::new).collect(),
752 })
753 }
754}
755
756impl DeleteRangeResponse {
757 pub fn new(deleted: i64) -> Self {
759 Self {
760 deleted,
761 prev_kvs: vec![],
762 }
763 }
764
765 pub fn with_prev_kvs(&mut self, prev_kvs: Vec<KeyValue>) {
767 self.prev_kvs = prev_kvs;
768 }
769
770 pub fn to_proto_resp(self, header: PbResponseHeader) -> PbDeleteRangeResponse {
771 PbDeleteRangeResponse {
772 header: Some(header),
773 deleted: self.deleted,
774 prev_kvs: self.prev_kvs.into_iter().map(Into::into).collect(),
775 }
776 }
777
778 pub fn deleted(&self) -> i64 {
779 self.deleted
780 }
781
782 pub fn take_prev_kvs(&mut self) -> Vec<KeyValue> {
783 self.prev_kvs.drain(..).collect()
784 }
785}
786
787#[cfg(test)]
788mod tests {
789 use api::v1::meta::{
790 BatchPutRequest as PbBatchPutRequest, BatchPutResponse as PbBatchPutResponse,
791 CompareAndPutRequest as PbCompareAndPutRequest,
792 CompareAndPutResponse as PbCompareAndPutResponse,
793 DeleteRangeRequest as PbDeleteRangeRequest, DeleteRangeResponse as PbDeleteRangeResponse,
794 KeyValue as PbKeyValue, PutRequest as PbPutRequest, PutResponse as PbPutResponse,
795 RangeRequest as PbRangeRequest, RangeResponse as PbRangeResponse,
796 };
797
798 use super::*;
799
800 #[test]
801 fn test_range_request_trans() {
802 let (key, range_end, limit) = (b"test_key1".to_vec(), b"test_range_end1".to_vec(), 1);
803
804 let req = RangeRequest::new()
805 .with_range(key.clone(), range_end.clone())
806 .with_limit(limit)
807 .with_keys_only();
808
809 let into_req: PbRangeRequest = req.into();
810 assert!(into_req.header.is_none());
811 assert_eq!(key, into_req.key);
812 assert_eq!(range_end, into_req.range_end);
813 assert_eq!(limit, into_req.limit);
814 assert!(into_req.keys_only);
815 }
816
817 #[test]
818 fn test_prefix_request_trans() {
819 let (key, limit) = (b"test_key1".to_vec(), 1);
820
821 let req = RangeRequest::new()
822 .with_prefix(key.clone())
823 .with_limit(limit)
824 .with_keys_only();
825
826 let into_req: PbRangeRequest = req.into();
827 assert!(into_req.header.is_none());
828 assert_eq!(key, into_req.key);
829 assert_eq!(b"test_key2".to_vec(), into_req.range_end);
830 assert_eq!(limit, into_req.limit);
831 assert!(into_req.keys_only);
832 }
833
834 #[test]
835 fn test_range_response_trans() {
836 let pb_res = PbRangeResponse {
837 header: None,
838 kvs: vec![
839 PbKeyValue {
840 key: b"k1".to_vec(),
841 value: b"v1".to_vec(),
842 },
843 PbKeyValue {
844 key: b"k2".to_vec(),
845 value: b"v2".to_vec(),
846 },
847 ],
848 more: true,
849 };
850
851 let mut res: RangeResponse = pb_res.try_into().unwrap();
852 assert!(res.more);
853 let mut kvs = res.take_kvs();
854 let kv0 = kvs.get_mut(0).unwrap();
855 assert_eq!(b"k1".to_vec(), kv0.key().to_vec());
856 assert_eq!(b"k1".to_vec(), kv0.take_key());
857 assert_eq!(b"v1".to_vec(), kv0.value().to_vec());
858 assert_eq!(b"v1".to_vec(), kv0.take_value());
859
860 let kv1 = kvs.get_mut(1).unwrap();
861 assert_eq!(b"k2".to_vec(), kv1.key().to_vec());
862 assert_eq!(b"k2".to_vec(), kv1.take_key());
863 assert_eq!(b"v2".to_vec(), kv1.value().to_vec());
864 assert_eq!(b"v2".to_vec(), kv1.take_value());
865 }
866
867 #[test]
868 fn test_put_request_trans() {
869 let (key, value) = (b"test_key1".to_vec(), b"test_value1".to_vec());
870
871 let req = PutRequest::new()
872 .with_key(key.clone())
873 .with_value(value.clone())
874 .with_prev_kv();
875
876 let into_req: PbPutRequest = req.into();
877 assert!(into_req.header.is_none());
878 assert_eq!(key, into_req.key);
879 assert_eq!(value, into_req.value);
880 }
881
882 #[test]
883 fn test_put_response_trans() {
884 let pb_res = PbPutResponse {
885 header: None,
886 prev_kv: Some(PbKeyValue {
887 key: b"k1".to_vec(),
888 value: b"v1".to_vec(),
889 }),
890 };
891
892 let res: PutResponse = pb_res.try_into().unwrap();
893 let mut kv = res.prev_kv.unwrap();
894 assert_eq!(b"k1".to_vec(), kv.key().to_vec());
895 assert_eq!(b"k1".to_vec(), kv.take_key());
896 assert_eq!(b"v1".to_vec(), kv.value().to_vec());
897 assert_eq!(b"v1".to_vec(), kv.take_value());
898 }
899
900 #[test]
901 fn test_batch_get_request_trans() {
902 let req = BatchGetRequest::default()
903 .add_key(b"test_key1".to_vec())
904 .add_key(b"test_key2".to_vec())
905 .add_key(b"test_key3".to_vec());
906
907 let into_req: PbBatchGetRequest = req.into();
908
909 assert!(into_req.header.is_none());
910 assert_eq!(b"test_key1".as_slice(), into_req.keys.first().unwrap());
911 assert_eq!(b"test_key2".as_slice(), into_req.keys.get(1).unwrap());
912 assert_eq!(b"test_key3".as_slice(), into_req.keys.get(2).unwrap());
913 }
914
915 #[test]
916 fn test_batch_get_response_trans() {
917 let pb_res = PbBatchGetResponse {
918 header: None,
919 kvs: vec![PbKeyValue {
920 key: b"test_key1".to_vec(),
921 value: b"test_value1".to_vec(),
922 }],
923 };
924 let res: BatchGetResponse = pb_res.try_into().unwrap();
925 let kvs = res.kvs;
926 assert_eq!(b"test_key1".as_slice(), kvs[0].key());
927 assert_eq!(b"test_value1".as_slice(), kvs[0].value());
928 }
929
930 #[test]
931 fn test_batch_put_request_trans() {
932 let req = BatchPutRequest::new()
933 .add_kv(b"test_key1".to_vec(), b"test_value1".to_vec())
934 .add_kv(b"test_key2".to_vec(), b"test_value2".to_vec())
935 .add_kv(b"test_key3".to_vec(), b"test_value3".to_vec())
936 .with_prev_kv();
937
938 let into_req: PbBatchPutRequest = req.into();
939 assert!(into_req.header.is_none());
940 assert_eq!(b"test_key1".to_vec(), into_req.kvs.first().unwrap().key);
941 assert_eq!(b"test_key2".to_vec(), into_req.kvs.get(1).unwrap().key);
942 assert_eq!(b"test_key3".to_vec(), into_req.kvs.get(2).unwrap().key);
943 assert_eq!(b"test_value1".to_vec(), into_req.kvs.first().unwrap().value);
944 assert_eq!(b"test_value2".to_vec(), into_req.kvs.get(1).unwrap().value);
945 assert_eq!(b"test_value3".to_vec(), into_req.kvs.get(2).unwrap().value);
946 assert!(into_req.prev_kv);
947 }
948
949 #[test]
950 fn test_batch_put_response_trans() {
951 let pb_res = PbBatchPutResponse {
952 header: None,
953 prev_kvs: vec![PbKeyValue {
954 key: b"k1".to_vec(),
955 value: b"v1".to_vec(),
956 }],
957 };
958
959 let mut res: BatchPutResponse = pb_res.try_into().unwrap();
960 let kvs = res.take_prev_kvs();
961 assert_eq!(b"k1".to_vec(), kvs[0].key().to_vec());
962 assert_eq!(b"v1".to_vec(), kvs[0].value().to_vec());
963 }
964
965 #[test]
966 fn test_batch_delete_request_trans() {
967 let req = BatchDeleteRequest::new()
968 .add_key(b"test_key1".to_vec())
969 .add_key(b"test_key2".to_vec())
970 .add_key(b"test_key3".to_vec())
971 .with_prev_kv();
972
973 let into_req: PbBatchDeleteRequest = req.into();
974 assert!(into_req.header.is_none());
975 assert_eq!(&b"test_key1".to_vec(), into_req.keys.first().unwrap());
976 assert_eq!(&b"test_key2".to_vec(), into_req.keys.get(1).unwrap());
977 assert_eq!(&b"test_key3".to_vec(), into_req.keys.get(2).unwrap());
978 assert!(into_req.prev_kv);
979 }
980
981 #[test]
982 fn test_batch_delete_response_trans() {
983 let pb_res = PbBatchDeleteResponse {
984 header: None,
985 prev_kvs: vec![PbKeyValue {
986 key: b"k1".to_vec(),
987 value: b"v1".to_vec(),
988 }],
989 };
990
991 let res: BatchDeleteResponse = pb_res.try_into().unwrap();
992 let kvs = res.prev_kvs;
993 assert_eq!(b"k1".to_vec(), kvs[0].key().to_vec());
994 assert_eq!(b"v1".to_vec(), kvs[0].value().to_vec());
995 }
996
997 #[test]
998 fn test_compare_and_put_request_trans() {
999 let (key, expect, value) = (
1000 b"test_key1".to_vec(),
1001 b"test_expect1".to_vec(),
1002 b"test_value1".to_vec(),
1003 );
1004
1005 let req = CompareAndPutRequest::new()
1006 .with_key(key.clone())
1007 .with_expect(expect.clone())
1008 .with_value(value.clone());
1009
1010 let into_req: PbCompareAndPutRequest = req.into();
1011 assert!(into_req.header.is_none());
1012 assert_eq!(key, into_req.key);
1013 assert_eq!(expect, into_req.expect);
1014 assert_eq!(value, into_req.value);
1015 }
1016
1017 #[test]
1018 fn test_compare_and_put_response_trans() {
1019 let pb_res = PbCompareAndPutResponse {
1020 header: None,
1021 success: true,
1022 prev_kv: Some(PbKeyValue {
1023 key: b"k1".to_vec(),
1024 value: b"v1".to_vec(),
1025 }),
1026 };
1027
1028 let mut res: CompareAndPutResponse = pb_res.try_into().unwrap();
1029 let mut kv = res.take_prev_kv().unwrap();
1030 assert_eq!(b"k1".to_vec(), kv.key().to_vec());
1031 assert_eq!(b"k1".to_vec(), kv.take_key());
1032 assert_eq!(b"v1".to_vec(), kv.value().to_vec());
1033 assert_eq!(b"v1".to_vec(), kv.take_value());
1034 }
1035
1036 #[test]
1037 fn test_delete_range_request_trans() {
1038 let (key, range_end) = (b"test_key1".to_vec(), b"test_range_end1".to_vec());
1039
1040 let req = DeleteRangeRequest::new()
1041 .with_range(key.clone(), range_end.clone())
1042 .with_prev_kv();
1043
1044 let into_req: PbDeleteRangeRequest = req.into();
1045 assert!(into_req.header.is_none());
1046 assert_eq!(key, into_req.key);
1047 assert_eq!(range_end, into_req.range_end);
1048 assert!(into_req.prev_kv);
1049 }
1050
1051 #[test]
1052 fn test_delete_prefix_request_trans() {
1053 let key = b"test_key1".to_vec();
1054
1055 let req = DeleteRangeRequest::new()
1056 .with_prefix(key.clone())
1057 .with_prev_kv();
1058
1059 let into_req: PbDeleteRangeRequest = req.into();
1060 assert!(into_req.header.is_none());
1061 assert_eq!(key, into_req.key);
1062 assert_eq!(b"test_key2".to_vec(), into_req.range_end);
1063 assert!(into_req.prev_kv);
1064 }
1065
1066 #[test]
1067 fn test_delete_range_response_trans() {
1068 let pb_res = PbDeleteRangeResponse {
1069 header: None,
1070 deleted: 2,
1071 prev_kvs: vec![
1072 PbKeyValue {
1073 key: b"k1".to_vec(),
1074 value: b"v1".to_vec(),
1075 },
1076 PbKeyValue {
1077 key: b"k2".to_vec(),
1078 value: b"v2".to_vec(),
1079 },
1080 ],
1081 };
1082
1083 let mut res: DeleteRangeResponse = pb_res.try_into().unwrap();
1084 assert_eq!(2, res.deleted());
1085 let mut kvs = res.take_prev_kvs();
1086 let kv0 = kvs.get_mut(0).unwrap();
1087 assert_eq!(b"k1".to_vec(), kv0.key().to_vec());
1088 assert_eq!(b"k1".to_vec(), kv0.take_key());
1089 assert_eq!(b"v1".to_vec(), kv0.value().to_vec());
1090 assert_eq!(b"v1".to_vec(), kv0.take_value());
1091
1092 let kv1 = kvs.get_mut(1).unwrap();
1093 assert_eq!(b"k2".to_vec(), kv1.key().to_vec());
1094 assert_eq!(b"k2".to_vec(), kv1.take_key());
1095 assert_eq!(b"v2".to_vec(), kv1.value().to_vec());
1096 assert_eq!(b"v2".to_vec(), kv1.take_value());
1097 }
1098}