Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
R
rust-rocksdb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fangzongwu
rust-rocksdb
Commits
ea46eb52
Commit
ea46eb52
authored
Jul 02, 2017
by
Huachao Huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
address comment
parent
d7acfb70
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
63 additions
and
37 deletions
+63
-37
table_properties.rs
src/table_properties.rs
+3
-2
table_properties_collector.rs
src/table_properties_collector.rs
+29
-14
table_properties_collector_factory.rs
src/table_properties_collector_factory.rs
+25
-10
test_table_properties.rs
tests/test_table_properties.rs
+6
-11
No files found.
src/table_properties.rs
View file @
ea46eb52
...
...
@@ -18,6 +18,7 @@ use libc::size_t;
use
std
::
collections
::
HashMap
;
use
std
::
marker
::
PhantomData
;
use
std
::
slice
;
use
std
::
str
;
pub
struct
TablePropertiesCollection
{
handle
:
TablePropertiesCollectionHandle
,
...
...
@@ -90,13 +91,13 @@ impl<'a> TablePropertiesCollectionIter<'a> {
}
}
pub
fn
key
(
&
self
)
->
String
{
pub
fn
key
(
&
self
)
->
&
str
{
unsafe
{
let
mut
klen
:
size_t
=
0
;
let
k
=
crocksdb_ffi
::
crocksdb_table_properties_collection_iter_key
(
self
.inner
,
&
mut
klen
);
let
bytes
=
slice
::
from_raw_parts
(
k
,
klen
);
String
::
from_utf8
(
bytes
.to_owned
()
)
.unwrap
()
str
::
from_utf8
(
bytes
)
.unwrap
()
}
}
...
...
src/table_properties_collector.rs
View file @
ea46eb52
...
...
@@ -26,7 +26,7 @@ use std::slice;
/// TablePropertiesCollector object per table and then call it sequentially
pub
trait
TablePropertiesCollector
{
/// The name of the properties collector.
fn
name
(
&
self
)
->
&
CString
;
fn
name
(
&
self
)
->
&
str
;
/// Will be called when a new key/value pair is inserted into the table.
fn
add_userkey
(
&
mut
self
,
...
...
@@ -41,21 +41,35 @@ pub trait TablePropertiesCollector {
fn
finish
(
&
mut
self
)
->
HashMap
<
Vec
<
u8
>
,
Vec
<
u8
>>
;
}
extern
"C"
fn
name
(
collector
:
*
mut
c_void
)
->
*
const
c_char
{
struct
TablePropertiesCollectorHandle
{
name
:
CString
,
rep
:
Box
<
TablePropertiesCollector
>
,
}
impl
TablePropertiesCollectorHandle
{
fn
new
(
collector
:
Box
<
TablePropertiesCollector
>
)
->
TablePropertiesCollectorHandle
{
TablePropertiesCollectorHandle
{
name
:
CString
::
new
(
collector
.name
())
.unwrap
(),
rep
:
collector
,
}
}
}
extern
"C"
fn
name
(
handle
:
*
mut
c_void
)
->
*
const
c_char
{
unsafe
{
let
collector
=
&
mut
*
(
collector
as
*
mut
Box
<
TablePropertiesCollector
>
);
collector
.name
()
.as_ptr
()
let
handle
=
&
mut
*
(
handle
as
*
mut
TablePropertiesCollectorHandle
);
handle
.name
.as_ptr
()
}
}
extern
"C"
fn
destruct
(
collector
:
*
mut
c_void
)
{
extern
"C"
fn
destruct
(
handle
:
*
mut
c_void
)
{
unsafe
{
let
collector
=
&
mut
*
(
collector
as
*
mut
Box
<
TablePropertiesCollector
>
);
Box
::
from_raw
(
collector
);
let
handle
=
&
mut
*
(
handle
as
*
mut
TablePropertiesCollectorHandle
);
Box
::
from_raw
(
handle
);
}
}
pub
extern
"C"
fn
add_userkey
(
collector
:
*
mut
c_void
,
pub
extern
"C"
fn
add_userkey
(
handle
:
*
mut
c_void
,
key
:
*
const
uint8_t
,
key_len
:
size_t
,
value
:
*
const
uint8_t
,
...
...
@@ -64,17 +78,17 @@ pub extern "C" fn add_userkey(collector: *mut c_void,
seq
:
uint64_t
,
file_size
:
uint64_t
)
{
unsafe
{
let
collector
=
&
mut
*
(
collector
as
*
mut
Box
<
TablePropertiesCollector
>
);
let
handle
=
&
mut
*
(
handle
as
*
mut
TablePropertiesCollectorHandle
);
let
key
=
slice
::
from_raw_parts
(
key
,
key_len
);
let
value
=
slice
::
from_raw_parts
(
value
,
value_len
);
collector
.add_userkey
(
key
,
value
,
mem
::
transmute
(
entry_type
),
seq
,
file_size
);
handle
.rep
.add_userkey
(
key
,
value
,
mem
::
transmute
(
entry_type
),
seq
,
file_size
);
}
}
pub
extern
"C"
fn
finish
(
collector
:
*
mut
c_void
,
props
:
*
mut
DBUserCollectedProperties
)
{
pub
extern
"C"
fn
finish
(
handle
:
*
mut
c_void
,
props
:
*
mut
DBUserCollectedProperties
)
{
unsafe
{
let
collector
=
&
mut
*
(
collector
as
*
mut
Box
<
TablePropertiesCollector
>
);
for
(
key
,
value
)
in
collector
.finish
()
{
let
handle
=
&
mut
*
(
handle
as
*
mut
TablePropertiesCollectorHandle
);
for
(
key
,
value
)
in
handle
.rep
.finish
()
{
crocksdb_ffi
::
crocksdb_user_collected_properties_add
(
props
,
key
.as_ptr
(),
key
.len
(),
...
...
@@ -86,8 +100,9 @@ pub extern "C" fn finish(collector: *mut c_void, props: *mut DBUserCollectedProp
pub
unsafe
fn
new_table_properties_collector
(
collector
:
Box
<
TablePropertiesCollector
>
)
->
*
mut
DBTablePropertiesCollector
{
let
handle
=
TablePropertiesCollectorHandle
::
new
(
collector
);
crocksdb_ffi
::
crocksdb_table_properties_collector_create
(
Box
::
into_raw
(
Box
::
new
(
collector
))
as
*
mut
c_void
,
Box
::
into_raw
(
Box
::
new
(
handle
))
as
*
mut
c_void
,
name
,
destruct
,
add_userkey
,
...
...
src/table_properties_collector_factory.rs
View file @
ea46eb52
...
...
@@ -20,30 +20,44 @@ use table_properties_collector::{TablePropertiesCollector, new_table_properties_
/// Internals create a new `TablePropertiesCollector` for each new table.
pub
trait
TablePropertiesCollectorFactory
{
/// The name of the properties collector factory.
fn
name
(
&
self
)
->
&
CString
;
fn
name
(
&
self
)
->
&
str
;
/// Has to be thread-safe.
fn
create_table_properties_collector
(
&
mut
self
,
cf
:
u32
)
->
Box
<
TablePropertiesCollector
>
;
}
extern
"C"
fn
name
(
factory
:
*
mut
c_void
)
->
*
const
c_char
{
struct
TablePropertiesCollectorFactoryHandle
{
name
:
CString
,
rep
:
Box
<
TablePropertiesCollectorFactory
>
,
}
impl
TablePropertiesCollectorFactoryHandle
{
fn
new
(
rep
:
Box
<
TablePropertiesCollectorFactory
>
)
->
TablePropertiesCollectorFactoryHandle
{
TablePropertiesCollectorFactoryHandle
{
name
:
CString
::
new
(
rep
.name
())
.unwrap
(),
rep
:
rep
,
}
}
}
extern
"C"
fn
name
(
handle
:
*
mut
c_void
)
->
*
const
c_char
{
unsafe
{
let
factory
=
&
mut
*
(
factory
as
*
mut
Box
<
TablePropertiesCollectorFactory
>
);
factory
.name
()
.as_ptr
()
let
handle
=
&
mut
*
(
handle
as
*
mut
TablePropertiesCollectorFactoryHandle
);
handle
.name
.as_ptr
()
}
}
extern
"C"
fn
destruct
(
factory
:
*
mut
c_void
)
{
extern
"C"
fn
destruct
(
handle
:
*
mut
c_void
)
{
unsafe
{
Box
::
from_raw
(
factory
as
*
mut
Box
<
TablePropertiesCollectorFactory
>
);
Box
::
from_raw
(
handle
as
*
mut
TablePropertiesCollectorFactoryHandle
);
}
}
extern
"C"
fn
create_table_properties_collector
(
factory
:
*
mut
c_void
,
extern
"C"
fn
create_table_properties_collector
(
handle
:
*
mut
c_void
,
cf
:
uint32_t
)
->
*
mut
DBTablePropertiesCollector
{
unsafe
{
let
factory
=
&
mut
*
(
factory
as
*
mut
Box
<
TablePropertiesCollectorFactory
>
);
let
collector
=
factory
.create_table_properties_collector
(
cf
);
let
handle
=
&
mut
*
(
handle
as
*
mut
TablePropertiesCollectorFactoryHandle
);
let
collector
=
handle
.rep
.create_table_properties_collector
(
cf
);
new_table_properties_collector
(
collector
)
}
}
...
...
@@ -51,8 +65,9 @@ extern "C" fn create_table_properties_collector(factory: *mut c_void,
pub
unsafe
fn
new_table_properties_collector_factory
(
factory
:
Box
<
TablePropertiesCollectorFactory
>
)
->
*
mut
DBTablePropertiesCollectorFactory
{
let
handle
=
TablePropertiesCollectorFactoryHandle
::
new
(
factory
);
crocksdb_ffi
::
crocksdb_table_properties_collector_factory_create
(
Box
::
into_raw
(
Box
::
new
(
factory
))
as
*
mut
c_void
,
Box
::
into_raw
(
Box
::
new
(
handle
))
as
*
mut
c_void
,
name
,
destruct
,
create_table_properties_collector
,
...
...
tests/test_table_properties.rs
View file @
ea46eb52
...
...
@@ -16,7 +16,6 @@ use rocksdb::{DB, Range, Options, Writable, DBEntryType, TablePropertiesCollecti
TablePropertiesCollector
,
TablePropertiesCollectorFactory
};
use
std
::
cmp
::
Ordering
;
use
std
::
collections
::
HashMap
;
use
std
::
ffi
::
CString
;
use
std
::
fmt
;
use
std
::
io
::
Cursor
;
use
tempdir
::
TempDir
;
...
...
@@ -40,7 +39,6 @@ fn decode_u32(x: &[u8]) -> u32 {
}
struct
ExampleCollector
{
name
:
CString
,
num_keys
:
u32
,
num_puts
:
u32
,
num_merges
:
u32
,
...
...
@@ -51,7 +49,6 @@ struct ExampleCollector {
impl
ExampleCollector
{
fn
new
()
->
ExampleCollector
{
ExampleCollector
{
name
:
CString
::
new
(
"example-collector"
)
.unwrap
(),
num_keys
:
0
,
num_puts
:
0
,
num_merges
:
0
,
...
...
@@ -98,8 +95,8 @@ impl fmt::Display for ExampleCollector {
}
impl
TablePropertiesCollector
for
ExampleCollector
{
fn
name
(
&
self
)
->
&
CString
{
&
self
.name
fn
name
(
&
self
)
->
&
str
{
"example-collector"
}
fn
add_userkey
(
&
mut
self
,
key
:
&
[
u8
],
_
:
&
[
u8
],
entry_type
:
DBEntryType
,
_
:
u64
,
_
:
u64
)
{
...
...
@@ -122,19 +119,17 @@ impl TablePropertiesCollector for ExampleCollector {
}
}
struct
ExampleFactory
{
name
:
CString
,
}
struct
ExampleFactory
{}
impl
ExampleFactory
{
fn
new
()
->
ExampleFactory
{
ExampleFactory
{
name
:
CString
::
new
(
"example-factory"
)
.unwrap
()
}
ExampleFactory
{}
}
}
impl
TablePropertiesCollectorFactory
for
ExampleFactory
{
fn
name
(
&
self
)
->
&
CString
{
&
self
.name
fn
name
(
&
self
)
->
&
str
{
"example-factory"
}
fn
create_table_properties_collector
(
&
mut
self
,
_
:
u32
)
->
Box
<
TablePropertiesCollector
>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment