Skip to content
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

simplify impl of read table properties #668

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions librocksdb_sys/crocksdb/c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4368,11 +4368,10 @@ crocksdb_iterator_t* crocksdb_sstfilereader_new_iterator(
return it;
}

void crocksdb_sstfilereader_read_table_properties(
const crocksdb_sstfilereader_t* reader, void* ctx,
void (*cb)(void*, const crocksdb_table_properties_t*)) {
const crocksdb_table_properties_t* crocksdb_sstfilereader_read_table_properties(
const crocksdb_sstfilereader_t* reader) {
auto props = reader->rep->GetTableProperties();
cb(ctx, reinterpret_cast<const crocksdb_table_properties_t*>(props.get()));
return reinterpret_cast<const crocksdb_table_properties_t*>(props.get());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the pointer still valid when props is dropped?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's a shared ptr from TableReader. In the context of read_table_properties, TableReader is promised not to be dropped.

}

void crocksdb_sstfilereader_verify_checksum(crocksdb_sstfilereader_t* reader,
Expand Down
6 changes: 3 additions & 3 deletions librocksdb_sys/crocksdb/crocksdb/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1724,9 +1724,9 @@ extern C_ROCKSDB_LIBRARY_API crocksdb_iterator_t*
crocksdb_sstfilereader_new_iterator(crocksdb_sstfilereader_t* reader,
const crocksdb_readoptions_t* options);

extern C_ROCKSDB_LIBRARY_API void crocksdb_sstfilereader_read_table_properties(
const crocksdb_sstfilereader_t* reader, void* ctx,
void (*cb)(void*, const crocksdb_table_properties_t*));
extern C_ROCKSDB_LIBRARY_API const crocksdb_table_properties_t*
crocksdb_sstfilereader_read_table_properties(
const crocksdb_sstfilereader_t* reader);

extern C_ROCKSDB_LIBRARY_API void crocksdb_sstfilereader_verify_checksum(
crocksdb_sstfilereader_t* reader, char** errptr);
Expand Down
4 changes: 1 addition & 3 deletions librocksdb_sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1802,9 +1802,7 @@ extern "C" {

pub fn crocksdb_sstfilereader_read_table_properties(
reader: *const SstFileReader,
ctx: *mut c_void,
callback: extern "C" fn(*mut c_void, *const DBTableProperties),
);
) -> *const DBTableProperties;

pub fn crocksdb_sstfilereader_verify_checksum(
reader: *mut SstFileReader,
Expand Down
21 changes: 3 additions & 18 deletions src/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use std::collections::BTreeMap;
use std::ffi::{CStr, CString};
use std::fmt::{self, Debug, Formatter};
use std::io;
use std::mem;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::rc::Rc;
Expand Down Expand Up @@ -2326,24 +2325,10 @@ impl SstFileReader {
}
}

pub fn read_table_properties<F: FnOnce(&TableProperties)>(&self, mut action: F) {
extern "C" fn callback<F: FnOnce(&TableProperties)>(
ctx: *mut c_void,
ptr: *const crocksdb_ffi::DBTableProperties,
) {
unsafe {
let caller = ptr::read(ctx as *mut F);
caller(TableProperties::from_ptr(ptr));
}
}

pub fn read_table_properties<F: FnOnce(&TableProperties)>(&self, action: F) {
unsafe {
crocksdb_ffi::crocksdb_sstfilereader_read_table_properties(
self.inner,
&mut action as *mut F as *mut c_void,
callback::<F>,
);
mem::forget(action);
let ptr = crocksdb_ffi::crocksdb_sstfilereader_read_table_properties(self.inner);
action(TableProperties::from_ptr(ptr));
}
}

Expand Down