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
93f4208e
Commit
93f4208e
authored
Oct 26, 2016
by
goroutine
Committed by
GitHub
Oct 26, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #51 from zhangjinpeng1987/master
hold `Options` instance in `DB` instance
parents
6c32ab80
1e738758
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
75 additions
and
63 deletions
+75
-63
main.rs
src/main.rs
+4
-3
merge_operator.rs
src/merge_operator.rs
+1
-1
rocksdb.rs
src/rocksdb.rs
+58
-48
test_column_family.rs
test/test_column_family.rs
+5
-5
test_compact_range.rs
test/test_compact_range.rs
+1
-1
test_compaction_filter.rs
test/test_compaction_filter.rs
+4
-3
test_iterator.rs
test/test_iterator.rs
+1
-1
test_rocksdb_options.rs
test/test_rocksdb_options.rs
+1
-1
No files found.
src/main.rs
View file @
93f4208e
...
...
@@ -90,7 +90,7 @@ fn custom_merge() {
opts
.create_if_missing
(
true
);
opts
.add_merge_operator
(
"test operator"
,
concat_merge
);
{
let
db
=
DB
::
open
(
&
opts
,
path
)
.unwrap
();
let
db
=
DB
::
open
(
opts
,
path
)
.unwrap
();
db
.put
(
b
"k1"
,
b
"a"
)
.unwrap
();
db
.merge
(
b
"k1"
,
b
"b"
)
.unwrap
();
db
.merge
(
b
"k1"
,
b
"c"
)
.unwrap
();
...
...
@@ -108,6 +108,7 @@ fn custom_merge() {
Err
(
e
)
=>
println!
(
"error retrieving value: {}"
,
e
),
}
}
let
opts
=
Options
::
new
();
DB
::
destroy
(
&
opts
,
path
)
.is_ok
();
}
...
...
@@ -146,7 +147,7 @@ mod tests {
#[allow(dead_code)]
fn
tuned_for_somebody_elses_disk
(
path
:
&
str
,
opts
:
&
mut
Options
,
mut
opts
:
Options
,
blockopts
:
&
mut
BlockBasedOptions
)
->
DB
{
let
per_level_compression
:
[
DBCompressionType
;
7
]
=
[
DBCompressionType
::
DBNo
,
...
...
@@ -189,7 +190,7 @@ mod tests {
// let filter = new_bloom_filter(10);
// opts.set_filter(filter);
DB
::
open
(
&
opts
,
path
)
.unwrap
()
DB
::
open
(
opts
,
path
)
.unwrap
()
}
// TODO(tyler) unstable
...
...
src/merge_operator.rs
View file @
93f4208e
...
...
@@ -180,7 +180,7 @@ mod test {
let
mut
opts
=
Options
::
new
();
opts
.create_if_missing
(
true
);
opts
.add_merge_operator
(
"test operator"
,
test_provided_merge
);
let
db
=
DB
::
open
(
&
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
db
=
DB
::
open
(
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"a"
);
assert
!
(
p
.is_ok
());
let
_
=
db
.merge
(
b
"k1"
,
b
"b"
);
...
...
src/rocksdb.rs
View file @
93f4208e
...
...
@@ -46,6 +46,7 @@ pub struct DB {
inner
:
*
mut
DBInstance
,
cfs
:
BTreeMap
<
String
,
CFHandle
>
,
path
:
String
,
opts
:
Options
,
}
unsafe
impl
Send
for
DB
{}
...
...
@@ -265,14 +266,14 @@ impl DB {
pub
fn
open_default
(
path
:
&
str
)
->
Result
<
DB
,
String
>
{
let
mut
opts
=
Options
::
new
();
opts
.create_if_missing
(
true
);
DB
::
open
(
&
opts
,
path
)
DB
::
open
(
opts
,
path
)
}
pub
fn
open
(
opts
:
&
Options
,
path
:
&
str
)
->
Result
<
DB
,
String
>
{
pub
fn
open
(
opts
:
Options
,
path
:
&
str
)
->
Result
<
DB
,
String
>
{
DB
::
open_cf
(
opts
,
path
,
&
[],
&
[])
}
pub
fn
open_cf
(
opts
:
&
Options
,
pub
fn
open_cf
(
opts
:
Options
,
path
:
&
str
,
cfs
:
&
[
&
str
],
cf_opts
:
&
[
&
Options
])
...
...
@@ -294,61 +295,66 @@ impl DB {
return
Err
(
format!
(
"cfs.len() and cf_opts.len() not match."
));
}
let
mut
cfs_v
=
cfs
.to_vec
();
let
mut
cf_opts_v
=
cf_opts
.to_vec
();
// Always open the default column family
if
!
cfs_v
.contains
(
&
DEFAULT_COLUMN_FAMILY
)
{
cfs_v
.push
(
DEFAULT_COLUMN_FAMILY
);
cf_opts_v
.push
(
opts
);
}
// We need to store our CStrings in an intermediate vector
// so that their pointers remain valid.
let
c_cfs
:
Vec
<
CString
>
=
cfs_v
.iter
()
.map
(|
cf
|
CString
::
new
(
cf
.as_bytes
())
.unwrap
())
.collect
();
let
cfnames
:
Vec
<*
const
_
>
=
c_cfs
.iter
()
.map
(|
cf
|
cf
.as_ptr
())
.collect
();
// These handles will be populated by DB.
let
cfhandles
:
Vec
<
_
>
=
cfs_v
.iter
()
.map
(|
_
|
0
as
*
mut
DBCFHandle
)
.collect
();
let
(
db
,
cf_map
)
=
{
let
mut
cfs_v
=
cfs
.to_vec
();
let
mut
cf_opts_v
=
cf_opts
.to_vec
();
// Always open the default column family
if
!
cfs_v
.contains
(
&
DEFAULT_COLUMN_FAMILY
)
{
cfs_v
.push
(
DEFAULT_COLUMN_FAMILY
);
cf_opts_v
.push
(
&
opts
);
}
let
cfopts
:
Vec
<
_
>
=
cf_opts_v
.iter
()
.map
(|
x
|
x
.inner
as
*
const
rocksdb_ffi
::
DBOptions
)
.collect
();
// We need to store our CStrings in an intermediate vector
// so that their pointers remain valid.
let
c_cfs
:
Vec
<
CString
>
=
cfs_v
.iter
()
.map
(|
cf
|
CString
::
new
(
cf
.as_bytes
())
.unwrap
())
.collect
();
let
cfnames
:
Vec
<*
const
_
>
=
c_cfs
.iter
()
.map
(|
cf
|
cf
.as_ptr
())
.collect
();
// These handles will be populated by DB.
let
cfhandles
:
Vec
<
_
>
=
cfs_v
.iter
()
.map
(|
_
|
ptr
::
null_mut
())
.collect
();
let
cfopts
:
Vec
<
_
>
=
cf_opts_v
.iter
()
.map
(|
x
|
x
.inner
as
*
const
rocksdb_ffi
::
DBOptions
)
.collect
();
let
db
=
unsafe
{
ffi_try!
(
rocksdb_open_column_families
(
opts
.inner
,
cpath
.as_ptr
(),
cfs_v
.len
()
as
c_int
,
cfnames
.as_ptr
(),
cfopts
.as_ptr
(),
cfhandles
.as_ptr
()))
};
let
db
=
unsafe
{
ffi_try!
(
rocksdb_open_column_families
(
opts
.inner
,
cpath
.as_ptr
(),
cfs_v
.len
()
as
c_int
,
cfnames
.as_ptr
(),
cfopts
.as_ptr
(),
cfhandles
.as_ptr
()))
};
for
handle
in
&
cfhandles
{
if
handle
.is_null
()
{
return
Err
(
"Received null column family handle from DB."
.to_owned
());
}
}
for
handle
in
&
cfhandles
{
if
handle
.is_null
(
)
{
return
Err
(
"Received null column family handle from DB."
.to_owned
()
);
let
mut
cf_map
=
BTreeMap
::
new
();
for
(
n
,
h
)
in
cfs_v
.iter
()
.zip
(
cfhandles
)
{
cf_map
.insert
((
*
n
)
.to_owned
(),
CFHandle
{
inner
:
h
}
);
}
}
let
mut
cf_map
=
BTreeMap
::
new
();
for
(
n
,
h
)
in
cfs_v
.iter
()
.zip
(
cfhandles
)
{
cf_map
.insert
((
*
n
)
.to_owned
(),
CFHandle
{
inner
:
h
});
}
if
db
.is_null
()
{
return
Err
(
"Could not initialize database."
.to_owned
());
}
if
db
.is_null
()
{
return
Err
(
"Could not initialize database."
.to_owned
());
}
(
db
,
cf_map
)
};
Ok
(
DB
{
inner
:
db
,
cfs
:
cf_map
,
path
:
path
.to_owned
(),
opts
:
opts
,
})
}
...
...
@@ -796,6 +802,10 @@ impl DB {
None
}
pub
fn
get_statistics
(
&
self
)
->
Option
<
String
>
{
self
.opts
.get_statistics
()
}
}
impl
Writable
for
DB
{
...
...
@@ -1088,7 +1098,7 @@ mod test {
let
mut
opts
=
Options
::
new
();
opts
.create_if_missing
(
true
);
let
mut
db
=
DB
::
open
(
&
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
mut
db
=
DB
::
open
(
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
for
(
&
cf
,
&
cf_opts
)
in
cfs
.iter
()
.zip
(
&
cfs_ref_opts
)
{
if
cf
==
"default"
{
continue
;
...
...
test/test_column_family.rs
View file @
93f4208e
...
...
@@ -26,7 +26,7 @@ pub fn test_column_family() {
let
mut
opts
=
Options
::
new
();
opts
.create_if_missing
(
true
);
opts
.add_merge_operator
(
"test operator"
,
test_provided_merge
);
let
mut
db
=
DB
::
open
(
&
opts
,
path_str
)
.unwrap
();
let
mut
db
=
DB
::
open
(
opts
,
path_str
)
.unwrap
();
let
opts
=
Options
::
new
();
match
db
.create_cf
(
"cf1"
,
&
opts
)
{
Ok
(
_
)
=>
println!
(
"cf1 created successfully"
),
...
...
@@ -41,7 +41,7 @@ pub fn test_column_family() {
{
let
mut
opts
=
Options
::
new
();
opts
.add_merge_operator
(
"test operator"
,
test_provided_merge
);
match
DB
::
open
(
&
opts
,
path_str
)
{
match
DB
::
open
(
opts
,
path_str
)
{
Ok
(
_
)
=>
{
panic!
(
"should not have opened DB successfully without
\
specifying column
...
...
@@ -58,7 +58,7 @@ pub fn test_column_family() {
{
let
mut
opts
=
Options
::
new
();
opts
.add_merge_operator
(
"test operator"
,
test_provided_merge
);
match
DB
::
open_cf
(
&
opts
,
path_str
,
&
[
"cf1"
],
&
[
&
opts
])
{
match
DB
::
open_cf
(
Options
::
new
()
,
path_str
,
&
[
"cf1"
],
&
[
&
opts
])
{
Ok
(
_
)
=>
println!
(
"successfully opened db with column family"
),
Err
(
e
)
=>
panic!
(
"failed to open db with column family: {}"
,
e
),
}
...
...
@@ -67,7 +67,7 @@ pub fn test_column_family() {
{
let
mut
opts
=
Options
::
new
();
opts
.add_merge_operator
(
"test operator"
,
test_provided_merge
);
let
db
=
match
DB
::
open_cf
(
&
opts
,
path_str
,
&
[
"cf1"
],
&
[
&
opts
])
{
let
db
=
match
DB
::
open_cf
(
Options
::
new
()
,
path_str
,
&
[
"cf1"
],
&
[
&
opts
])
{
Ok
(
db
)
=>
{
println!
(
"successfully opened db with column family"
);
db
...
...
@@ -115,7 +115,7 @@ pub fn test_column_family() {
}
// should b able to drop a cf
{
let
mut
db
=
DB
::
open_cf
(
&
Options
::
new
(),
path_str
,
&
[
"cf1"
],
&
[
&
Options
::
new
()])
.unwrap
();
let
mut
db
=
DB
::
open_cf
(
Options
::
new
(),
path_str
,
&
[
"cf1"
],
&
[
&
Options
::
new
()])
.unwrap
();
match
db
.drop_cf
(
"cf1"
)
{
Ok
(
_
)
=>
println!
(
"cf1 successfully dropped."
),
Err
(
e
)
=>
panic!
(
"failed to drop column family: {}"
,
e
),
...
...
test/test_compact_range.rs
View file @
93f4208e
...
...
@@ -7,7 +7,7 @@ fn test_compact_range() {
let
path
=
TempDir
::
new
(
"_rust_rocksdb_test_compact_range"
)
.expect
(
""
);
let
mut
opts
=
Options
::
new
();
opts
.create_if_missing
(
true
);
let
db
=
DB
::
open
(
&
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
db
=
DB
::
open
(
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
samples
=
vec!
[
(
b
"k1"
.to_vec
(),
b
"value--------1"
.to_vec
()),
(
b
"k2"
.to_vec
(),
b
"value--------2"
.to_vec
()),
...
...
test/test_compaction_filter.rs
View file @
93f4208e
...
...
@@ -37,7 +37,7 @@ fn test_compaction_filter() {
}))
.unwrap
();
opts
.create_if_missing
(
true
);
let
db
=
DB
::
open
(
&
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
db
=
DB
::
open
(
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
samples
=
vec!
[
(
b
"key1"
.to_vec
(),
b
"value1"
.to_vec
()),
(
b
"key2"
.to_vec
(),
b
"value2"
.to_vec
()),
...
...
@@ -58,7 +58,9 @@ fn test_compaction_filter() {
}
drop
(
db
);
// reregister with ignore_snapshots set to true
let
mut
opts
=
Options
::
new
();
opts
.set_compaction_filter
(
"test"
,
true
,
Box
::
new
(
Filter
{
...
...
@@ -69,7 +71,7 @@ fn test_compaction_filter() {
assert
!
(
drop_called
.load
(
Ordering
::
Relaxed
));
drop_called
.store
(
false
,
Ordering
::
Relaxed
);
{
let
db
=
DB
::
open
(
&
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
db
=
DB
::
open
(
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
_snap
=
db
.snapshot
();
// Because ignore_snapshots is true, so all the keys will be compacted.
db
.compact_range
(
Some
(
b
"key1"
),
Some
(
b
"key3"
));
...
...
@@ -79,6 +81,5 @@ fn test_compaction_filter() {
assert_eq!
(
*
filtered_kvs
.read
()
.unwrap
(),
samples
);
}
drop
(
opts
);
assert
!
(
drop_called
.load
(
Ordering
::
Relaxed
));
}
test/test_iterator.rs
View file @
93f4208e
...
...
@@ -118,7 +118,7 @@ fn read_with_upper_bound() {
let
mut
opts
=
Options
::
new
();
opts
.create_if_missing
(
true
);
{
let
db
=
DB
::
open
(
&
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
db
=
DB
::
open
(
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
writeopts
=
WriteOptions
::
new
();
db
.put_opt
(
b
"k1-0"
,
b
"a"
,
&
writeopts
)
.unwrap
();
db
.put_opt
(
b
"k1-1"
,
b
"b"
,
&
writeopts
)
.unwrap
();
...
...
test/test_rocksdb_options.rs
View file @
93f4208e
...
...
@@ -8,6 +8,6 @@ fn test_set_num_levels() {
let
mut
opts
=
Options
::
new
();
opts
.create_if_missing
(
true
);
opts
.set_num_levels
(
2
);
let
db
=
DB
::
open
(
&
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
let
db
=
DB
::
open
(
opts
,
path
.path
()
.to_str
()
.unwrap
())
.unwrap
();
drop
(
db
);
}
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