-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from spacejam/tyler_comparator
initial comparator
- Loading branch information
Showing
4 changed files
with
123 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright 2014 Tyler Neely | ||
Licensed 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 | ||
http://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. | ||
*/ | ||
extern crate libc; | ||
use self::libc::{c_char, c_int, c_void, size_t}; | ||
use std::ffi::CString; | ||
use std::mem; | ||
use std::ptr; | ||
use std::slice; | ||
|
||
use rocksdb_options::RocksDBOptions; | ||
use rocksdb::RocksDB; | ||
|
||
pub struct ComparatorCallback { | ||
pub name: CString, | ||
pub f: fn (&[u8], &[u8]) -> i32, | ||
} | ||
|
||
pub extern "C" fn destructor_callback(raw_cb: *mut c_void) { | ||
// turn this back into a local variable so rust will reclaim it | ||
let _: Box<ComparatorCallback> = unsafe {mem::transmute(raw_cb)}; | ||
} | ||
|
||
pub extern "C" fn name_callback(raw_cb: *mut c_void) -> *const c_char { | ||
unsafe { | ||
let cb: &mut ComparatorCallback = | ||
&mut *(raw_cb as *mut ComparatorCallback); | ||
let ptr = cb.name.as_ptr(); | ||
ptr as *const c_char | ||
} | ||
} | ||
|
||
pub extern "C" fn compare_callback( | ||
raw_cb: *mut c_void, | ||
a_raw: *const c_char, a_len: size_t, | ||
b_raw: *const c_char, b_len: size_t) -> c_int { | ||
unsafe { | ||
let cb: &mut ComparatorCallback = | ||
&mut *(raw_cb as *mut ComparatorCallback); | ||
let a: &[u8] = slice::from_raw_parts(a_raw as *const u8, a_len as usize); | ||
let b: &[u8] = slice::from_raw_parts(b_raw as *const u8, b_len as usize); | ||
(cb.f)(a, b) | ||
} | ||
} | ||
|
||
fn test_reverse_compare(a: &[u8], b: &[u8]) -> c_int { | ||
if a < b { | ||
1 | ||
} else if a > b { | ||
-1 | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[test] | ||
fn compare_works() { | ||
let path = "_rust_rocksdb_comparetest"; | ||
let opts = RocksDBOptions::new(); | ||
opts.create_if_missing(true); | ||
opts.add_comparator("test comparator", test_reverse_compare); | ||
let db = RocksDB::open(opts, path).unwrap(); | ||
// TODO add interesting test | ||
db.close(); | ||
assert!(RocksDB::destroy(opts, path).is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters