diff --git a/src/db/db_format.rs b/src/db/db_format.rs index 49e71941eb429e190669de559b88e9f560b25ee9..d8d0bf2cec00c8994dc965cc75f41c833dc0de8d 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 aa6bea4c4a5bacab024c83a8063b8976f74771fd..c7796776c25467c91aad375c98eddc18ea894981 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 ffec818b1f719f325c4e90ab786022f6493cc610..2c2efec824a1ce11feaae552d53f68ba7a402da2 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 ff9362d36e751bf0f52cefcbd5b55d9ff4d26d25..fd498821991260e1d1546a55b8f04d1911029030 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 a2617c12cd3f14e02000f7f1792be4fa3d3de295..421ea97a39fb695fae2bf327210c7545e4fe48b5 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 f172c833011347023f99a8469031a3fca21330e4..1bfc31642ad7438ebc1fe2421abfcf4e0f60cee2 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 9baf1d17fbf6e1fd0adf9c14d35919821e45acaa..c56f7a4b077c51a2a01e455299b125ef58bdc9b0 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();