From 37cd43dd32828fa06909cba35fdd0c5649c647d6 Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 21 Mar 2023 10:07:52 +0800 Subject: [PATCH] =?UTF-8?q?coding=20get=5Fvarint32=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=94=B1&mut=20Slice=20=E6=94=B9=E4=B8=BA=20=20&Slice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/db_format.rs | 7 +++++- src/db/version_edit.rs | 46 +++++++++++++++++++------------------ src/db/version_edit_test.rs | 19 +++++++++++---- src/traits/coding_trait.rs | 4 ++-- src/util/coding.rs | 4 ++-- src/util/slice.rs | 2 +- src/util/slice_test.rs | 2 +- 7 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/db/db_format.rs b/src/db/db_format.rs index 49e7194..d8d0bf2 100644 --- a/src/db/db_format.rs +++ b/src/db/db_format.rs @@ -183,10 +183,15 @@ impl InternalKey { todo!() } - fn encode(self) -> Slice { + pub fn encode(self) -> Slice { self.rep_ } + /// 取得 Slice的长度 + pub fn encode_len(&self) -> usize { + self.rep_.size() + } + fn user_key(self) -> Slice { ParsedInternalKey::extract_user_key(self.rep_) } diff --git a/src/db/version_edit.rs b/src/db/version_edit.rs index aa6bea4..c779677 100644 --- a/src/db/version_edit.rs +++ b/src/db/version_edit.rs @@ -236,7 +236,7 @@ impl VersionEdit { position += Coding::put_varint32(target, position, Tag::kCompactPointer.get_value() as u32); position += Coding::put_varint32(target, position, self.compact_pointers_[i].0); position += Coding::put_length_prefixed_slice(target, position, - self.compact_pointers_[i].1.encode_len()); + self.compact_pointers_[i].1.encode_len()); } for i in 0..self.deleted_files_.len() { @@ -270,30 +270,32 @@ impl VersionEdit { /// ``` /// /// ``` - pub fn decode_from(&mut self, source: &mut Slice) -> Status { + pub fn decode_from(&mut self, source: &Slice) -> Status { self.clear(); + let version_edit = VersionEdit::new(); + let msg : Option = Option::None; - // while msg == None && Coding::get_varint32(source) != 0_u32 { - // let tag_value = Coding::get_varint32(source); - // let tag = Tag::from_value(tag_value); - // - // if tag.is_none() { - // return LevelError::corruption_string("VersionEdit", "unknown tag"); - // } - // - // // match tag { - // // Tag::k_comparator => 1, - // // Tag::kLogNumber => 2, - // // Tag::kNextFileNumber => 3, - // // Tag::kLastSequence => 4, - // // Tag::kCompactPointer => 5, - // // Tag::kDeletedFile => 6, - // // Tag::kNewFile => 7, - // // Tag::kPrevLogNumber => 9, - // // _ => 0 - // // }; - // } + while msg.is_none() && Coding::get_varint32(source) != 0_u32 { + let tag_value = Coding::get_varint32(source); + let tag = Tag::from_value(tag_value); + + if tag.is_none() { + return LevelError::corruption_string("VersionEdit", "unknown tag"); + } + + // match tag { + // Tag::k_comparator => 1, + // Tag::kLogNumber => 2, + // Tag::kNextFileNumber => 3, + // Tag::kLastSequence => 4, + // Tag::kCompactPointer => 5, + // Tag::kDeletedFile => 6, + // Tag::kNewFile => 7, + // Tag::kPrevLogNumber => 9, + // _ => 0 + // }; + } todo!() } diff --git a/src/db/version_edit_test.rs b/src/db/version_edit_test.rs index ffec818..2c2efec 100644 --- a/src/db/version_edit_test.rs +++ b/src/db/version_edit_test.rs @@ -2,6 +2,7 @@ mod test { use crate::db::version_edit; use crate::db::version_edit::{Tag, VersionEdit}; + use crate::util::slice::Slice; #[test] fn test_Tag() { @@ -11,17 +12,27 @@ mod test { let tag1 = Tag::k_comparator; let v = tag1.get_value(); assert_eq!(v, 1); - - } #[test] - fn test_Version_edit() { + fn test_version_edit_encode_to() { let mut target: Vec = vec![]; let version_edit = VersionEdit::new_with_log_number(6); version_edit.encode_to(&mut target); println!("target: {}.", &target.len()); - assert_eq!(target.len(), 2); + // todo + // assert_eq!(target.len(), 2); + } + + #[test] + fn test_version_edit_decode_from() { + let source = Slice::from("a"); + + let mut version_edit = VersionEdit::new(); + let status = version_edit.decode_from(&source); + println!("status: {}.", status.get_error()); + // todo + // assert_eq!(target.len(), 2); } } \ No newline at end of file diff --git a/src/traits/coding_trait.rs b/src/traits/coding_trait.rs index ff9362d..fd49882 100644 --- a/src/traits/coding_trait.rs +++ b/src/traits/coding_trait.rs @@ -94,7 +94,7 @@ pub trait CodingTrait { /// ``` /// /// ``` - fn get_varint32(input: &mut Slice) -> u32; + fn get_varint32(input: &Slice) -> u32; /// 从slice的开头解码一个64位的变长整数, 并将slice的索引置于解码后的位置 /// /// # Arguments @@ -108,7 +108,7 @@ pub trait CodingTrait { /// ``` /// /// ``` - fn get_varint64(input: &mut Slice) -> u64; + fn get_varint64(input: &Slice) -> u64; /// 从slice数据中读取长度 返回长度的Slice /// /// # Arguments diff --git a/src/util/coding.rs b/src/util/coding.rs index a2617c1..421ea97 100644 --- a/src/util/coding.rs +++ b/src/util/coding.rs @@ -90,7 +90,7 @@ impl CodingTrait for Coding { offset } - fn get_varint32(input: &mut Slice) -> u32 { + fn get_varint32(input: &Slice) -> u32 { let cow = input.borrow_data(); let bytes = cow.as_bytes(); let mut result = 0_u32; @@ -110,7 +110,7 @@ impl CodingTrait for Coding { result } - fn get_varint64(input: &mut Slice) -> u64 { + fn get_varint64(input: &Slice) -> u64 { let cow = input.borrow_data(); let bytes = cow.as_bytes(); let mut result = 0_u64; diff --git a/src/util/slice.rs b/src/util/slice.rs index f172c83..1bfc316 100644 --- a/src/util/slice.rs +++ b/src/util/slice.rs @@ -112,7 +112,7 @@ impl Slice { impl<'a> Slice { /// 借取 Slice 中的数据, 调用方只拥有读权限 - pub fn borrow_data(&mut self) -> Cow<'a, String> { + pub fn borrow_data(&self) -> Cow<'a, String> { unsafe { // String & Vec has the same layout let s: &String = mem::transmute(&self.data); diff --git a/src/util/slice_test.rs b/src/util/slice_test.rs index 9baf1d1..c56f7a4 100644 --- a/src/util/slice_test.rs +++ b/src/util/slice_test.rs @@ -40,7 +40,7 @@ mod test { #[test] fn test_borrow_data() { - let mut a0 = Slice::from("123"); + let a0 = Slice::from("123"); let borrowed = a0.borrow_data(); assert_eq!(3, borrowed.len()); let owned = borrowed.to_owned(); -- Gitee