diff options
author | Leon Yang <lnyng@fb.com> | 2022-02-08 10:58:45 -0800 |
---|---|---|
committer | Facebook GitHub Bot <facebook-github-bot@users.noreply.github.com> | 2022-02-08 11:00:54 -0800 |
commit | d3a9a98741377e58411bc7c63765ac0ecb48d01e (patch) | |
tree | 43a75d6f926d494a28270d4e9b97f2deb32a64ef | |
parent | 36564e62e3b25287ddfe912c264df0c7744567e8 (diff) |
Rename SerializedFrame enums
Summary:
SerializedFrame is very similar to `std::borrow::Cow<'a, [u8]>`, except that its owned type is Bytes instead of Vec. As a result we can't use Cow for our purpose. Byte is similar to Vec instead of it's ref counted, which is easier to use in our case.
Rename SerializedFrame enum from Bytes to Owned and Slice to Borrowed to clarify ownship model.
Reviewed By: boyuni
Differential Revision: D34061719
fbshipit-source-id: b86c019dd5e0c1ae50d6004ef787f1a618d20492
-rw-r--r-- | below/store/src/cursor.rs | 10 | ||||
-rw-r--r-- | below/store/src/lib.rs | 20 |
2 files changed, 16 insertions, 14 deletions
diff --git a/below/store/src/cursor.rs b/below/store/src/cursor.rs index 48f03bd0..911b6a61 100644 --- a/below/store/src/cursor.rs +++ b/below/store/src/cursor.rs @@ -379,14 +379,14 @@ impl StoreCursor { decompressor: &mut Option<Decompressor<(u64, usize)>>, ) -> Result<SerializedFrame<'a>> { let serialized_frame = if compressed { - SerializedFrame::Bytes( + SerializedFrame::Owned( decompressor .get_or_insert_with(Decompressor::new) .decompress_with_dict_reset(data_slice) .context("Failed to decompress data frame")?, ) } else { - SerializedFrame::Slice(data_slice) + SerializedFrame::Borrowed(data_slice) }; Ok(serialized_frame) } @@ -425,7 +425,7 @@ impl StoreCursor { ) .context("Failed to get serialized dict key frame")?; let d = decompressor.get_or_insert_with(Decompressor::new); - d.load_dict(dict_key_frame.bytes(), dict_key) + d.load_dict(dict_key_frame.into_owned(), dict_key) .context("Failed to set decompressor dict")?; d } @@ -440,7 +440,7 @@ impl StoreCursor { .decompress_with_loaded_dict(data_slice) .context("Failed to decompress data frame with dictionary")? }; - Ok(SerializedFrame::Bytes(bytes)) + Ok(SerializedFrame::Owned(bytes)) } /// Get index entry at offset and it's corresponding data slice. @@ -584,7 +584,7 @@ impl Cursor for StoreCursor { }; let ts = std::time::UNIX_EPOCH + std::time::Duration::from_secs(index_entry.timestamp); - match deserialize_frame(serialized_data.data(), format) { + match deserialize_frame(serialized_data.as_ref(), format) { Ok(df) => Some((ts, df)), Err(e) => { warn!(self.logger, "Failed to deserialize data frame: {}", e); diff --git a/below/store/src/lib.rs b/below/store/src/lib.rs index b880522c..d435c8c0 100644 --- a/below/store/src/lib.rs +++ b/below/store/src/lib.rs @@ -213,22 +213,24 @@ fn get_index_files(path: &Path) -> Result<Vec<String>> { } enum SerializedFrame<'a> { - Bytes(bytes::Bytes), - Slice(&'a [u8]), + Owned(bytes::Bytes), + Borrowed(&'a [u8]), } -impl<'a> SerializedFrame<'a> { - fn data(&self) -> &[u8] { +impl AsRef<[u8]> for SerializedFrame<'_> { + fn as_ref(&self) -> &[u8] { match self { - SerializedFrame::Bytes(b) => &b, - SerializedFrame::Slice(s) => s, + SerializedFrame::Owned(b) => b.as_ref(), + SerializedFrame::Borrowed(s) => s, } } +} - fn bytes(self) -> bytes::Bytes { +impl SerializedFrame<'_> { + fn into_owned(self) -> bytes::Bytes { match self { - SerializedFrame::Bytes(b) => b, - SerializedFrame::Slice(s) => bytes::Bytes::copy_from_slice(s), + SerializedFrame::Owned(b) => b, + SerializedFrame::Borrowed(s) => bytes::Bytes::copy_from_slice(s), } } } |