Skip to content

Commit

Permalink
Implement VersionEdit (#8)
Browse files Browse the repository at this point in the history
This implements VersionEdit, which holds metadata for a Version in the
DB. A version in LevelDB is like a snapshot which roughly is a
collection of sstable files. This change is a prerequisite towards it.
  • Loading branch information
sunchao authored Jan 10, 2018
1 parent b413e03 commit ccd0615
Show file tree
Hide file tree
Showing 5 changed files with 518 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

pub const NUM_LEVELS: i32 = 7;

/// Level-0 compaction is triggered when we hit this many files.
pub const L0_COMPACTION_TRIGGER: i32 = 4;

/// Soft limit on number of level-0 files. We slow down writes at this point.
pub const L0_SLOWDOWN_WRITES_TRIGGER: i32 = 8;

/// Maximum number of level-0 files. We stop writes at this point.
pub const L0_STOP_WRITES_TRIGGER: i32 = 12;
48 changes: 48 additions & 0 deletions src/dbformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::cmp::Ordering;
use std::convert::TryFrom;
use std::fmt::{Debug, Display, Formatter, Result as DebugResult};
use std::rc::Rc;

use slice::Slice;
Expand All @@ -35,6 +36,15 @@ pub enum ValueType {
VALUE = 0x1
}

impl Display for ValueType {
fn fmt(&self, f: &mut Formatter) -> DebugResult {
match self {
&ValueType::DELETION => write!(f, "ValueType::Deletion"),
&ValueType::VALUE => write!(f, "ValueType::Value")
}
}
}

impl TryFrom<u8> for ValueType {
type Error = super::result::Error;
fn try_from(v: u8) -> Result<ValueType> {
Expand Down Expand Up @@ -213,6 +223,29 @@ impl InternalKey {
}
}

impl Debug for InternalKey {
fn fmt(&self, f: &mut Formatter) -> DebugResult {
if let Ok(parsed) = ParsedInternalKey::try_from(&self.rep) {
write!(f, "{:?}", parsed)
} else {
let s = unsafe {
::std::str::from_utf8_unchecked(&self.rep[..])
};
write!(f, "(bad){}", s)
}
}
}

impl<'a> From<&'a Slice> for InternalKey {
fn from(s: &'a Slice) -> InternalKey {
let mut v = Vec::new();
v.extend_from_slice(s.data());
InternalKey {
rep: v
}
}
}

fn append_internal_key(result: &mut Vec<u8>, key: &ParsedInternalKey) {
result.extend_from_slice(&key.user_key.data());
let key_len = result.len();
Expand Down Expand Up @@ -250,6 +283,13 @@ impl ParsedInternalKey {
}
}

impl Debug for ParsedInternalKey {
fn fmt(&self, f: &mut Formatter) -> DebugResult {
write!(f, "'{}' @ {} : {}", self.user_key.as_str(), self.seqno, self.value_type)
}
}


impl<'a> TryFrom<&'a Slice> for ParsedInternalKey {
type Error = super::result::Error;

Expand All @@ -266,6 +306,14 @@ impl<'a> TryFrom<&'a Slice> for ParsedInternalKey {
}
}

impl<'a> TryFrom<&'a Vec<u8>> for ParsedInternalKey {
type Error = super::result::Error;

fn try_from(s: &Vec<u8>) -> Result<ParsedInternalKey> {
ParsedInternalKey::try_from(&Slice::from(s))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ pub mod result;
pub mod slice;
pub mod comparator;
pub mod dbformat;
pub mod config;
pub mod skiplist;
pub mod iterator;
pub mod memtable;
pub mod write_batch;
pub mod log_format;
pub mod log_writer;
pub mod log_reader;
pub mod version_edit;
7 changes: 7 additions & 0 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ impl<'a> From<&'a [u8]> for Slice {
}
}

impl<'a> From<&'a Vec<u8>> for Slice {
#[inline]
fn from(v: &'a Vec<u8>) -> Self {
Slice::new(v[..].as_ptr(), v.len())
}
}

impl<'a> From<&'a str> for Slice {
#[inline]
fn from(s: &'a str) -> Self {
Expand Down
Loading

0 comments on commit ccd0615

Please sign in to comment.