Commit 5f49f9ef authored by dorianzheng's avatar dorianzheng Committed by Huachao Huang

Add blob index encoder (#272)

parent 2088c8d4
......@@ -4988,6 +4988,18 @@ void ctitandb_decode_blob_index(const char* value, size_t value_size,
index->blob_size = bi.blob_handle.size;
}
void ctitandb_encode_blob_index(const ctitandb_blob_index_t& index,
char** value, size_t* value_size) {
BlobIndex bi;
bi.file_number = index.file_number;
bi.blob_handle.offset = index.blob_offset;
bi.blob_handle.size = index.blob_size;
std::string result;
bi.EncodeTo(&result);
*value = CopyString(result);
*value_size = result.size();
}
void ctitandb_options_set_disable_background_gc(ctitandb_options_t* options,
unsigned char disable) {
options->rep.disable_background_gc = disable;
......
......@@ -1986,6 +1986,9 @@ extern C_ROCKSDB_LIBRARY_API void ctitandb_decode_blob_index(
const char* value, size_t value_size, ctitandb_blob_index_t* index,
char** errptr);
extern C_ROCKSDB_LIBRARY_API void ctitandb_encode_blob_index(
const ctitandb_blob_index_t& index, char** value, size_t* value_size);
extern C_ROCKSDB_LIBRARY_API void ctitandb_options_set_disable_background_gc(
ctitandb_options_t* options, unsigned char disable);
......
Subproject commit da05929b46cf5e5129b75c1b95feb6d0c41da27c
Subproject commit c96bfa2e2e75f8fddbfa9362ba072d07b453df20
......@@ -1822,6 +1822,11 @@ extern "C" {
index: *mut DBTitanBlobIndex,
errptr: *mut *mut c_char,
);
pub fn ctitandb_encode_blob_index(
index: &DBTitanBlobIndex,
value: *mut *mut u8,
value_size: *mut u64,
);
pub fn ctitandb_options_set_disable_background_gc(opts: *mut DBTitanDBOptions, disable: bool);
pub fn ctitandb_options_set_max_background_gc(opts: *mut DBTitanDBOptions, size: i32);
......
......@@ -2,9 +2,14 @@ use std::ffi::{CStr, CString};
use std::ops::Deref;
use crocksdb_ffi::{self, DBCompressionType, DBTitanBlobIndex, DBTitanDBOptions};
use librocksdb_sys::ctitandb_encode_blob_index;
use std::ffi::c_void;
use std::ops::DerefMut;
use std::os::raw::c_double;
use std::os::raw::c_int;
use std::os::raw::c_uchar;
use std::ptr;
use std::slice;
pub struct TitanDBOptions {
pub inner: *mut DBTitanDBOptions,
......@@ -124,7 +129,7 @@ pub struct TitanBlobIndex {
}
impl TitanBlobIndex {
pub fn decode_from(value: &[u8]) -> Result<Self, String> {
pub fn decode(value: &[u8]) -> Result<Self, String> {
let mut index = Self::default();
unsafe {
ffi_try!(ctitandb_decode_blob_index(
......@@ -135,6 +140,18 @@ impl TitanBlobIndex {
}
Ok(index)
}
pub fn encode(&self) -> Vec<u8> {
let mut value = ptr::null_mut();
let mut value_size: u64 = 0;
unsafe {
ctitandb_encode_blob_index(&self.inner, &mut value, &mut value_size);
let slice = slice::from_raw_parts(value, value_size as usize);
let vec = slice.to_vec();
libc::free(value as *mut c_void);
vec
}
}
}
impl Deref for TitanBlobIndex {
......@@ -143,3 +160,9 @@ impl Deref for TitanBlobIndex {
&self.inner
}
}
impl DerefMut for TitanBlobIndex {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
......@@ -16,6 +16,7 @@ use std::collections::HashMap;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use tempdir::TempDir;
use rand::Rng;
use rocksdb::{
ColumnFamilyOptions, DBCompressionType, DBEntryType, DBOptions, SeekKey,
TablePropertiesCollector, TablePropertiesCollectorFactory, TitanBlobIndex, TitanDBOptions,
......@@ -64,7 +65,7 @@ impl TablePropertiesCollector for TitanCollector {
self.num_entries += 1;
if let DBEntryType::BlobIndex = entry_type {
self.num_blobs += 1;
let index = TitanBlobIndex::decode_from(value).unwrap();
let index = TitanBlobIndex::decode(value).unwrap();
assert!(index.file_number > 0);
assert!(index.blob_size > 0);
}
......@@ -150,3 +151,17 @@ fn test_titandb() {
let num_entries = n as u32 * max_value_size as u32;
check_table_properties(&db, num_entries / 2, num_entries);
}
#[test]
fn test_titan_blob_index() {
let mut index = TitanBlobIndex::default();
let mut rng = rand::thread_rng();
index.file_number = rng.gen();
index.blob_size = rng.gen();
index.blob_offset = rng.gen();
let value = index.encode();
let index2 = TitanBlobIndex::decode(&value).unwrap();
assert_eq!(index2.file_number, index.file_number);
assert_eq!(index2.blob_size, index.blob_size);
assert_eq!(index2.blob_offset, index.blob_offset);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment