Skip to content

Commit

Permalink
add some basic db test
Browse files Browse the repository at this point in the history
Signed-off-by: Fullstop000 <[email protected]>
  • Loading branch information
Fullstop000 committed Jan 18, 2020
1 parent 29784a0 commit 5be8b47
Show file tree
Hide file tree
Showing 13 changed files with 763 additions and 223 deletions.
37 changes: 21 additions & 16 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ pub const HEADER_SIZE: usize = 12;
/// non-const method, all threads accessing the same WriteBatch must use
/// external synchronization.
///
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct WriteBatch {
pub(super) contents: Vec<u8>,
}

impl WriteBatch {
pub fn new() -> Self {
impl Default for WriteBatch {
fn default() -> Self {
let contents = vec![0; HEADER_SIZE];
Self { contents }
}
}

impl WriteBatch {
#[inline]
pub fn data(&self) -> &[u8] {
self.contents.as_slice()
Expand Down Expand Up @@ -168,30 +170,33 @@ impl WriteBatch {
self.contents.clear();
self.contents.append(src);
}

/// Returns the number of entires included in this entry
#[inline]
pub fn get_count(&self) -> u32 {
decode_fixed_32(&self.contents.as_slice()[8..])
decode_fixed_32(&self.contents[8..])
}

#[inline]
pub(crate) fn set_count(&mut self, count: u32) {
let s = self.contents.as_mut_slice();
encode_fixed_32(&mut s[8..], count)
encode_fixed_32(&mut self.contents[8..], count)
}

#[inline]
pub(crate) fn set_sequence(&mut self, seq: u64) {
encode_fixed_64(self.contents.as_mut_slice(), seq)
encode_fixed_64(&mut self.contents, seq)
}

/// Returns the seq number of this batch
#[inline]
pub fn get_sequence(&self) -> u64 {
decode_fixed_64(self.contents.as_slice())
decode_fixed_64(&self.contents)
}

/// Returns false when this batch contains no entries to be written
#[inline]
pub fn is_empty(&self) -> bool {
self.contents.is_empty()
self.get_count() == 0
}
}

Expand Down Expand Up @@ -244,14 +249,14 @@ mod tests {

#[test]
fn test_empty_batch() {
let b = WriteBatch::new();
let b = WriteBatch::default();
assert_eq!("", print_contents(&b).as_str());
assert_eq!(0, b.get_count());
assert!(b.is_empty());
}

#[test]
fn test_multiple_records() {
let mut b = WriteBatch::new();
let mut b = WriteBatch::default();
b.put("foo".as_bytes(), "bar".as_bytes());
b.delete("box".as_bytes());
b.put("baz".as_bytes(), "boo".as_bytes());
Expand All @@ -266,7 +271,7 @@ mod tests {

#[test]
fn test_corrupted_batch() {
let mut b = WriteBatch::new();
let mut b = WriteBatch::default();
b.put("foo".as_bytes(), "bar".as_bytes());
b.delete("box".as_bytes());
b.set_sequence(200);
Expand All @@ -279,8 +284,8 @@ mod tests {

#[test]
fn test_append_batch() {
let mut b1 = WriteBatch::new();
let mut b2 = WriteBatch::new();
let mut b1 = WriteBatch::default();
let mut b2 = WriteBatch::default();
b1.set_sequence(200);
b2.set_sequence(300);
b1.append(b2.clone());
Expand All @@ -302,7 +307,7 @@ mod tests {

#[test]
fn test_approximate_size() {
let mut b = WriteBatch::new();
let mut b = WriteBatch::default();
let empty_size = b.approximate_size();
b.put("foo".as_bytes(), "bar".as_bytes());
let one_key_size = b.approximate_size();
Expand Down
1 change: 0 additions & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ pub trait Cache<T> {

/// Release a mapping returned by a previous `look_up()`.
/// REQUIRES: handle must not have been released yet.
/// REQUIRES: handle must have been returned by a method on *this.
fn release(&self, handle: HandleRef<T>);

/// If the cache contains entry for key, erase it. Note that the
Expand Down
13 changes: 12 additions & 1 deletion src/compaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,27 @@ use crate::version::version_edit::{FileMetaData, VersionEdit};
use crate::version::version_set::{total_file_size, FileIterFactory};
use crate::version::{LevelFileNumIterator, Version};
use std::cmp::Ordering as CmpOrdering;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

/// Information for a manual compaction
#[derive(Clone)]
pub struct ManualCompaction {
pub level: usize,
pub done: bool,
pub done: Arc<AtomicBool>,
pub begin: Option<InternalKey>, // None means beginning of key range
pub end: Option<InternalKey>, // None means end of key range
}

impl PartialEq for ManualCompaction {
fn eq(&self, other: &ManualCompaction) -> bool {
Arc::ptr_eq(&self.done, &other.done)
&& self.level == other.level
&& self.begin == other.begin
&& self.end == other.end
}
}

/// A helper enum describing relations between the indexes of `inputs` in `Compaction`
// TODO: use const instead
pub enum CompactionInputsRelation {
Expand Down
2 changes: 1 addition & 1 deletion src/db/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl InternalKey {
#[inline]
pub fn user_key(&self) -> &[u8] {
let length = self.data.len();
&self.data.as_slice()[..length - INTERNAL_KEY_TAIL]
&self.data[..length - INTERNAL_KEY_TAIL]
}

/// Returns a `ParsedInternalKey`
Expand Down
Loading

0 comments on commit 5be8b47

Please sign in to comment.