Commit f6788b64 authored by Huachao Huang's avatar Huachao Huang Committed by zhangjinpeng1987

Add ExternalSstFileInfo (#158)

parent c5c0b09e
......@@ -82,6 +82,7 @@ using rocksdb::SliceParts;
using rocksdb::SliceTransform;
using rocksdb::Snapshot;
using rocksdb::SstFileWriter;
using rocksdb::ExternalSstFileInfo;
using rocksdb::Status;
using rocksdb::WritableFile;
using rocksdb::WriteBatch;
......@@ -145,6 +146,7 @@ struct crocksdb_column_family_handle_t { ColumnFamilyHandle* rep; };
struct crocksdb_envoptions_t { EnvOptions rep; };
struct crocksdb_ingestexternalfileoptions_t { IngestExternalFileOptions rep; };
struct crocksdb_sstfilewriter_t { SstFileWriter* rep; };
struct crocksdb_externalsstfileinfo_t { ExternalSstFileInfo rep; };
struct crocksdb_ratelimiter_t { RateLimiter* rep; };
struct crocksdb_histogramdata_t { HistogramData rep; };
struct crocksdb_pinnableslice_t { PinnableSlice rep; };
......@@ -2905,8 +2907,9 @@ void crocksdb_sstfilewriter_delete(crocksdb_sstfilewriter_t *writer,
}
void crocksdb_sstfilewriter_finish(crocksdb_sstfilewriter_t* writer,
char** errptr) {
SaveError(errptr, writer->rep->Finish(NULL));
crocksdb_externalsstfileinfo_t* info,
char** errptr) {
SaveError(errptr, writer->rep->Finish(&info->rep));
}
uint64_t crocksdb_sstfilewriter_file_size(crocksdb_sstfilewriter_t* writer) {
......@@ -2918,6 +2921,47 @@ void crocksdb_sstfilewriter_destroy(crocksdb_sstfilewriter_t* writer) {
delete writer;
}
crocksdb_externalsstfileinfo_t* crocksdb_externalsstfileinfo_create() {
return new crocksdb_externalsstfileinfo_t;
};
void crocksdb_externalsstfileinfo_destroy(crocksdb_externalsstfileinfo_t* info) {
delete info;
}
const char* crocksdb_externalsstfileinfo_file_path(
crocksdb_externalsstfileinfo_t* info, size_t* size) {
*size = info->rep.file_path.size();
return info->rep.file_path.c_str();
}
const char* crocksdb_externalsstfileinfo_smallest_key(
crocksdb_externalsstfileinfo_t* info, size_t* size) {
*size = info->rep.smallest_key.size();
return info->rep.smallest_key.c_str();
}
const char* crocksdb_externalsstfileinfo_largest_key(
crocksdb_externalsstfileinfo_t* info, size_t* size) {
*size = info->rep.largest_key.size();
return info->rep.largest_key.c_str();
}
uint64_t crocksdb_externalsstfileinfo_sequence_number(
crocksdb_externalsstfileinfo_t* info) {
return info->rep.sequence_number;
}
uint64_t crocksdb_externalsstfileinfo_file_size(
crocksdb_externalsstfileinfo_t* info) {
return info->rep.file_size;
}
uint64_t crocksdb_externalsstfileinfo_num_entries(
crocksdb_externalsstfileinfo_t* info) {
return info->rep.num_entries;
}
crocksdb_ingestexternalfileoptions_t*
crocksdb_ingestexternalfileoptions_create() {
crocksdb_ingestexternalfileoptions_t* opt =
......
......@@ -109,6 +109,7 @@ typedef struct crocksdb_column_family_handle_t crocksdb_column_family_handle_t;
typedef struct crocksdb_envoptions_t crocksdb_envoptions_t;
typedef struct crocksdb_ingestexternalfileoptions_t crocksdb_ingestexternalfileoptions_t;
typedef struct crocksdb_sstfilewriter_t crocksdb_sstfilewriter_t;
typedef struct crocksdb_externalsstfileinfo_t crocksdb_externalsstfileinfo_t;
typedef struct crocksdb_ratelimiter_t crocksdb_ratelimiter_t;
typedef struct crocksdb_pinnableslice_t crocksdb_pinnableslice_t;
typedef struct crocksdb_user_collected_properties_t
......@@ -1219,12 +1220,31 @@ extern C_ROCKSDB_LIBRARY_API void
crocksdb_sstfilewriter_delete(crocksdb_sstfilewriter_t *writer, const char *key,
size_t keylen, char **errptr);
extern C_ROCKSDB_LIBRARY_API void crocksdb_sstfilewriter_finish(
crocksdb_sstfilewriter_t* writer, char** errptr);
crocksdb_sstfilewriter_t* writer, crocksdb_externalsstfileinfo_t* info, char** errptr);
extern C_ROCKSDB_LIBRARY_API uint64_t crocksdb_sstfilewriter_file_size(
crocksdb_sstfilewriter_t* writer);
extern C_ROCKSDB_LIBRARY_API void crocksdb_sstfilewriter_destroy(
crocksdb_sstfilewriter_t* writer);
/* ExternalSstFileInfo */
extern C_ROCKSDB_LIBRARY_API crocksdb_externalsstfileinfo_t*
crocksdb_externalsstfileinfo_create();
extern C_ROCKSDB_LIBRARY_API void
crocksdb_externalsstfileinfo_destroy(crocksdb_externalsstfileinfo_t*);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_externalsstfileinfo_file_path(crocksdb_externalsstfileinfo_t*, size_t*);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_externalsstfileinfo_smallest_key(crocksdb_externalsstfileinfo_t*, size_t*);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_externalsstfileinfo_largest_key(crocksdb_externalsstfileinfo_t*, size_t*);
extern C_ROCKSDB_LIBRARY_API uint64_t
crocksdb_externalsstfileinfo_sequence_number(crocksdb_externalsstfileinfo_t*);
extern C_ROCKSDB_LIBRARY_API uint64_t
crocksdb_externalsstfileinfo_file_size(crocksdb_externalsstfileinfo_t*);
extern C_ROCKSDB_LIBRARY_API uint64_t
crocksdb_externalsstfileinfo_num_entries(crocksdb_externalsstfileinfo_t*);
extern C_ROCKSDB_LIBRARY_API crocksdb_ingestexternalfileoptions_t*
crocksdb_ingestexternalfileoptions_create();
extern C_ROCKSDB_LIBRARY_API void
......
......@@ -37,6 +37,7 @@ pub enum DBFlushOptions {}
pub enum DBCompactionFilter {}
pub enum EnvOptions {}
pub enum SstFileWriter {}
pub enum ExternalSstFileInfo {}
pub enum IngestExternalFileOptions {}
pub enum DBBackupEngine {}
pub enum DBRestoreOptions {}
......@@ -1118,10 +1119,34 @@ extern "C" {
key_len: size_t,
err: *mut *mut c_char,
);
pub fn crocksdb_sstfilewriter_finish(writer: *mut SstFileWriter, err: *mut *mut c_char);
pub fn crocksdb_sstfilewriter_finish(
writer: *mut SstFileWriter,
info: *mut ExternalSstFileInfo,
err: *mut *mut c_char,
);
pub fn crocksdb_sstfilewriter_file_size(writer: *mut SstFileWriter) -> uint64_t;
pub fn crocksdb_sstfilewriter_destroy(writer: *mut SstFileWriter);
// ExternalSstFileInfo
pub fn crocksdb_externalsstfileinfo_create() -> *mut ExternalSstFileInfo;
pub fn crocksdb_externalsstfileinfo_destroy(info: *mut ExternalSstFileInfo);
pub fn crocksdb_externalsstfileinfo_file_path(
info: *mut ExternalSstFileInfo,
size: *mut size_t,
) -> *const uint8_t;
pub fn crocksdb_externalsstfileinfo_smallest_key(
info: *mut ExternalSstFileInfo,
size: *mut size_t,
) -> *const uint8_t;
pub fn crocksdb_externalsstfileinfo_largest_key(
info: *mut ExternalSstFileInfo,
size: *mut size_t,
) -> *const uint8_t;
pub fn crocksdb_externalsstfileinfo_sequence_number(info: *mut ExternalSstFileInfo)
-> uint64_t;
pub fn crocksdb_externalsstfileinfo_file_size(info: *mut ExternalSstFileInfo) -> uint64_t;
pub fn crocksdb_externalsstfileinfo_num_entries(info: *mut ExternalSstFileInfo) -> uint64_t;
pub fn crocksdb_ingest_external_file(
db: *mut DBInstance,
file_list: *const *const c_char,
......
......@@ -26,7 +26,7 @@ use std::collections::btree_map::Entry;
use std::ffi::{CStr, CString};
use std::fmt::{self, Debug, Formatter};
use std::ops::Deref;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::str::from_utf8;
use table_properties::TablePropertiesCollection;
......@@ -1761,11 +1761,12 @@ impl SstFileWriter {
}
/// Finalize writing to sst file and close file.
pub fn finish(&mut self) -> Result<(), String> {
pub fn finish(&mut self) -> Result<ExternalSstFileInfo, String> {
let info = ExternalSstFileInfo::new();
unsafe {
ffi_try!(crocksdb_sstfilewriter_finish(self.inner));
Ok(())
ffi_try!(crocksdb_sstfilewriter_finish(self.inner, info.inner));
}
Ok(info)
}
pub fn file_size(&mut self) -> u64 {
......@@ -1779,6 +1780,65 @@ impl Drop for SstFileWriter {
}
}
pub struct ExternalSstFileInfo {
inner: *mut crocksdb_ffi::ExternalSstFileInfo,
}
impl ExternalSstFileInfo {
pub fn new() -> ExternalSstFileInfo {
unsafe {
ExternalSstFileInfo {
inner: crocksdb_ffi::crocksdb_externalsstfileinfo_create(),
}
}
}
pub fn file_path(&self) -> PathBuf {
let mut len: size_t = 0;
unsafe {
let ptr = crocksdb_ffi::crocksdb_externalsstfileinfo_file_path(self.inner, &mut len);
let bytes = slice::from_raw_parts(ptr, len as usize);
PathBuf::from(String::from_utf8(bytes.to_owned()).unwrap())
}
}
pub fn smallest_key(&self) -> &[u8] {
let mut len: size_t = 0;
unsafe {
let ptr = crocksdb_ffi::crocksdb_externalsstfileinfo_smallest_key(self.inner, &mut len);
slice::from_raw_parts(ptr, len as usize)
}
}
pub fn largest_key(&self) -> &[u8] {
let mut len: size_t = 0;
unsafe {
let ptr = crocksdb_ffi::crocksdb_externalsstfileinfo_largest_key(self.inner, &mut len);
slice::from_raw_parts(ptr, len as usize)
}
}
pub fn sequence_number(&self) -> u64 {
unsafe { crocksdb_ffi::crocksdb_externalsstfileinfo_sequence_number(self.inner) as u64 }
}
pub fn file_size(&self) -> u64 {
unsafe { crocksdb_ffi::crocksdb_externalsstfileinfo_file_size(self.inner) as u64 }
}
pub fn num_entries(&self) -> u64 {
unsafe { crocksdb_ffi::crocksdb_externalsstfileinfo_num_entries(self.inner) as u64 }
}
}
impl Drop for ExternalSstFileInfo {
fn drop(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_externalsstfileinfo_destroy(self.inner);
}
}
}
pub fn supported_compression() -> Vec<DBCompressionType> {
unsafe {
let size = crocksdb_ffi::crocksdb_get_supported_compression_number() as usize;
......
......@@ -281,7 +281,15 @@ fn gen_sst_from_cf(opt: ColumnFamilyOptions, db: &DB, cf: &CFHandle, path: &str)
writer.put(iter.key(), iter.value()).unwrap();
iter.next();
}
writer.finish().unwrap();
let info = writer.finish().unwrap();
assert_eq!(info.file_path().to_str().unwrap(), path);
iter.seek(SeekKey::Start);
assert_eq!(info.smallest_key(), iter.key());
iter.seek(SeekKey::End);
assert_eq!(info.largest_key(), iter.key());
assert_eq!(info.sequence_number(), 0);
assert!(info.file_size() > 0);
assert!(info.num_entries() > 0);
}
fn create_default_database(path: &TempDir) -> DB {
......
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