-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
agate, util: fix varint decode and add header #29
Changes from 4 commits
24c8aa5
a83c3a3
7395bcc
4507fbb
aa86c44
d4d67f0
e04b17c
da3b0f9
080d02d
038e7e0
4b10f45
8310dd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,60 @@ | ||
use super::Result; | ||
use crate::util::{decode_varint_uncheck, encode_varint_uncheck, varint_len}; | ||
use bytes::{BufMut, Bytes, BytesMut}; | ||
use std::fs::{File, OpenOptions}; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Default, Debug, PartialEq)] | ||
struct Header { | ||
key_len: usize, | ||
value_len: usize, | ||
key_len: u32, | ||
value_len: u32, | ||
expires_at: u64, | ||
meta: u8, | ||
user_meta: u8, | ||
} | ||
|
||
impl Header { | ||
pub fn encoded_len(&self) -> usize { | ||
skyzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1 + 1 | ||
+ varint_len(self.expires_at as usize) | ||
+ varint_len(self.key_len as usize) | ||
+ varint_len(self.value_len as usize) | ||
} | ||
|
||
pub fn encode(&self, bytes: &mut BytesMut) { | ||
let encoded_len = self.encoded_len(); | ||
bytes.reserve(encoded_len); | ||
let read = unsafe { | ||
let buf = bytes.bytes_mut(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps adding an if check before entering There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the check |
||
*(*buf.get_unchecked_mut(0)).as_mut_ptr() = self.meta; | ||
*(*buf.get_unchecked_mut(1)).as_mut_ptr() = self.user_meta; | ||
let mut read = 2; | ||
read += encode_varint_uncheck(buf.get_unchecked_mut(read..), self.key_len as u64); | ||
read += encode_varint_uncheck(buf.get_unchecked_mut(read..), self.value_len as u64); | ||
read += encode_varint_uncheck(buf.get_unchecked_mut(read..), self.expires_at); | ||
read | ||
}; | ||
assert_eq!(read, encoded_len); | ||
skyzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsafe { | ||
bytes.advance_mut(read); | ||
} | ||
} | ||
|
||
pub fn decode(&mut self, bytes: &mut Bytes) -> usize { | ||
self.meta = bytes[0]; | ||
self.user_meta = bytes[1]; | ||
let mut index = 2; | ||
let (key_len, count) = unsafe { decode_varint_uncheck(&bytes[index..]) }; | ||
self.key_len = key_len as u32; | ||
index += count; | ||
let (value_len, count) = unsafe { decode_varint_uncheck(&bytes[index..]) }; | ||
self.value_len = value_len as u32; | ||
index += count; | ||
let (expires_at, count) = unsafe { decode_varint_uncheck(&bytes[index..]) }; | ||
self.expires_at = expires_at; | ||
index += count; | ||
index | ||
} | ||
} | ||
|
||
pub struct Wal { | ||
|
@@ -18,3 +68,27 @@ impl Wal { | |
Ok(Wal { f, path }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_header_encode() { | ||
let header = Header { | ||
key_len: 233333, | ||
value_len: 2333, | ||
expires_at: std::u64::MAX - 2333333, | ||
user_meta: b'A', | ||
meta: b'B', | ||
}; | ||
|
||
let mut buf = BytesMut::new(); | ||
header.encode(&mut buf); | ||
let mut buf = buf.freeze(); | ||
|
||
let mut new_header = Header::default(); | ||
new_header.decode(&mut buf); | ||
assert_eq!(new_header, header); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can write it in safe rust by using for loop.
https://github.com/BusyJay/pecan/blob/d4a17231253cd6b304a15d9d0f65edd5a0bcceab/utils/src/codec.rs#L54-L84 is an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied
codec.rs
and modified input parameter asBytesMut
, plus adapting error type to agatedb.