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
51a1588b
Commit
51a1588b
authored
Mar 05, 2016
by
Jay Lee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use test mod instead
parent
47329cae
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
197 additions
and
173 deletions
+197
-173
ffi.rs
src/ffi.rs
+53
-47
main.rs
src/main.rs
+1
-0
merge_operator.rs
src/merge_operator.rs
+59
-54
rocksdb.rs
src/rocksdb.rs
+80
-72
test_column_family.rs
test/test_column_family.rs
+4
-0
No files found.
src/ffi.rs
View file @
51a1588b
...
@@ -411,59 +411,65 @@ extern "C" {
...
@@ -411,59 +411,65 @@ extern "C" {
}
}
#[test]
#[cfg(test)]
fn
internal
()
{
mod
test
{
unsafe
{
use
super
::
*
;
use
std
::
ffi
::
CString
;
use
libc
::
*
;
let
opts
=
rocksdb_options_create
();
use
std
::
ffi
::
CString
;
assert
!
(
!
opts
.
0
.is_null
());
rocksdb_options_increase_parallelism
(
opts
,
0
);
#[test]
rocksdb_options_optimize_level_style_compaction
(
opts
,
0
);
fn
internal
()
{
rocksdb_options_set_create_if_missing
(
opts
,
true
);
unsafe
{
let
opts
=
rocksdb_options_create
();
assert
!
(
!
opts
.
0
.is_null
());
let
rustpath
=
"_rust_rocksdb_internaltest"
;
rocksdb_options_increase_parallelism
(
opts
,
0
)
;
let
cpath
=
CString
::
new
(
rustpath
)
.unwrap
(
);
rocksdb_options_optimize_level_style_compaction
(
opts
,
0
);
let
cpath_ptr
=
cpath
.as_ptr
(
);
rocksdb_options_set_create_if_missing
(
opts
,
true
);
let
mut
err
:
*
const
i8
=
0
as
*
const
i8
;
let
rustpath
=
"_rust_rocksdb_internaltest"
;
let
err_ptr
:
*
mut
*
const
i8
=
&
mut
err
;
let
cpath
=
CString
::
new
(
rustpath
)
.unwrap
();
let
db
=
rocksdb_open
(
opts
,
cpath_ptr
,
err_ptr
);
let
cpath_ptr
=
cpath
.as_ptr
();
if
!
err
.is_null
()
{
println!
(
"failed to open rocksdb: {}"
,
error_message
(
err
));
}
assert
!
(
err
.is_null
());
let
writeopts
=
rocksdb_writeoptions_create
();
let
mut
err
:
*
const
i8
=
0
as
*
const
i8
;
assert
!
(
!
writeopts
.
0
.is_null
());
let
err_ptr
:
*
mut
*
const
i8
=
&
mut
err
;
let
db
=
rocksdb_open
(
opts
,
cpath_ptr
,
err_ptr
);
if
!
err
.is_null
()
{
println!
(
"failed to open rocksdb: {}"
,
error_message
(
err
));
}
assert
!
(
err
.is_null
());
let
key
=
b
"name
\x00
"
;
let
writeopts
=
rocksdb_writeoptions_create
();
let
val
=
b
"spacejam
\x00
"
;
assert
!
(
!
writeopts
.
0
.is_null
());
rocksdb_put
(
db
,
writeopts
.clone
(),
key
.as_ptr
(),
4
,
val
.as_ptr
(),
8
,
err_ptr
);
rocksdb_writeoptions_destroy
(
writeopts
);
assert
!
(
err
.is_null
());
let
readopts
=
rocksdb_readoptions_create
();
let
key
=
b
"name
\x00
"
;
assert
!
(
!
readopts
.
0
.is_null
());
let
val
=
b
"spacejam
\x00
"
;
rocksdb_put
(
db
,
writeopts
.clone
(),
key
.as_ptr
(),
4
,
val
.as_ptr
(),
8
,
err_ptr
);
rocksdb_writeoptions_destroy
(
writeopts
);
assert
!
(
err
.is_null
());
let
val_len
:
size_t
=
0
;
let
readopts
=
rocksdb_readoptions_create
();
let
val_len_ptr
=
&
val_len
as
*
const
size_t
;
assert
!
(
!
readopts
.
0
.is_null
());
rocksdb_get
(
db
,
readopts
.clone
(),
let
val_len
:
size_t
=
0
;
key
.as_ptr
(),
let
val_len_ptr
=
&
val_len
as
*
const
size_t
;
4
,
rocksdb_get
(
db
,
val_len_ptr
,
readopts
.clone
(),
err_ptr
);
key
.as_ptr
(),
rocksdb_readoptions_destroy
(
readopts
);
4
,
assert
!
(
err
.is_null
());
val_len_ptr
,
rocksdb_close
(
db
);
err_ptr
);
rocksdb_destroy_db
(
opts
,
cpath_ptr
,
err_ptr
);
rocksdb_readoptions_destroy
(
readopts
);
assert
!
(
err
.is_null
());
assert
!
(
err
.is_null
());
rocksdb_close
(
db
);
rocksdb_destroy_db
(
opts
,
cpath_ptr
,
err_ptr
);
assert
!
(
err
.is_null
());
}
}
}
}
}
src/main.rs
View file @
51a1588b
...
@@ -146,6 +146,7 @@ mod tests {
...
@@ -146,6 +146,7 @@ mod tests {
use
rocksdb
::{
BlockBasedOptions
,
DB
,
Options
};
use
rocksdb
::{
BlockBasedOptions
,
DB
,
Options
};
use
rocksdb
::
DBCompactionStyle
::
DBUniversalCompaction
;
use
rocksdb
::
DBCompactionStyle
::
DBUniversalCompaction
;
#[allow(dead_code)]
fn
tuned_for_somebody_elses_disk
(
path
:
&
str
,
fn
tuned_for_somebody_elses_disk
(
path
:
&
str
,
opts
:
&
mut
Options
,
opts
:
&
mut
Options
,
blockopts
:
&
mut
BlockBasedOptions
)
blockopts
:
&
mut
BlockBasedOptions
)
...
...
src/merge_operator.rs
View file @
51a1588b
...
@@ -151,67 +151,72 @@ impl<'a> Iterator for &'a mut MergeOperands {
...
@@ -151,67 +151,72 @@ impl<'a> Iterator for &'a mut MergeOperands {
}
}
}
}
#[allow(unused_variables)]
#[cfg(test)]
#[allow(dead_code)]
mod
test
{
fn
test_provided_merge
(
new_key
:
&
[
u8
],
use
super
::
*
;
existing_val
:
Option
<&
[
u8
]
>
,
use
rocksdb_options
::
Options
;
operands
:
&
mut
MergeOperands
)
use
rocksdb
::{
DB
,
DBVector
,
Writable
};
->
Vec
<
u8
>
{
let
nops
=
operands
.size_hint
()
.
0
;
#[allow(unused_variables)]
let
mut
result
:
Vec
<
u8
>
=
Vec
::
with_capacity
(
nops
);
#[allow(dead_code)]
match
existing_val
{
fn
test_provided_merge
(
new_key
:
&
[
u8
],
Some
(
v
)
=>
{
existing_val
:
Option
<&
[
u8
]
>
,
for
e
in
v
{
operands
:
&
mut
MergeOperands
)
result
.push
(
*
e
);
->
Vec
<
u8
>
{
let
nops
=
operands
.size_hint
()
.
0
;
let
mut
result
:
Vec
<
u8
>
=
Vec
::
with_capacity
(
nops
);
match
existing_val
{
Some
(
v
)
=>
{
for
e
in
v
{
result
.push
(
*
e
);
}
}
}
None
=>
(),
}
}
None
=>
(),
for
op
in
operands
{
}
for
e
in
op
{
for
op
in
operands
{
result
.push
(
*
e
);
for
e
in
op
{
}
result
.push
(
*
e
);
}
}
result
}
}
result
}
#[allow(dead_code)]
#[test]
fn
mergetest
()
{
#[allow(dead_code)]
use
rocksdb_options
::
Options
;
#[test]
use
rocksdb
::{
DB
,
DBVector
,
Writable
};
fn
mergetest
()
{
let
path
=
"_rust_rocksdb_mergetest"
;
let
path
=
"_rust_rocksdb_mergetest"
;
let
mut
opts
=
Options
::
new
();
let
mut
opts
=
Options
::
new
();
opts
.create_if_missing
(
true
);
opts
.create_if_missing
(
true
);
opts
.add_merge_operator
(
"test operator"
,
test_provided_merge
);
opts
.add_merge_operator
(
"test operator"
,
test_provided_merge
);
{
{
let
db
=
DB
::
open
(
&
opts
,
path
)
.unwrap
();
let
db
=
DB
::
open
(
&
opts
,
path
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"a"
);
let
p
=
db
.put
(
b
"k1"
,
b
"a"
);
assert
!
(
p
.is_ok
());
assert
!
(
p
.is_ok
());
let
_
=
db
.merge
(
b
"k1"
,
b
"b"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"b"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"c"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"c"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"d"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"d"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"efg"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"efg"
);
let
m
=
db
.merge
(
b
"k1"
,
b
"h"
);
let
m
=
db
.merge
(
b
"k1"
,
b
"h"
);
assert
!
(
m
.is_ok
());
assert
!
(
m
.is_ok
());
match
db
.get
(
b
"k1"
)
{
match
db
.get
(
b
"k1"
)
{
Ok
(
Some
(
value
))
=>
{
Ok
(
Some
(
value
))
=>
{
match
value
.to_utf8
()
{
match
value
.to_utf8
()
{
Some
(
v
)
=>
println!
(
"retrieved utf8 value: {}"
,
v
),
Some
(
v
)
=>
println!
(
"retrieved utf8 value: {}"
,
v
),
None
=>
{
None
=>
println!
(
"did not read valid utf-8 out of the db"
),
println!
(
"did not read valid utf-8 out of the db"
)
}
}
}
}
Err
(
e
)
=>
println!
(
"error reading value {:?}"
,
e
),
_
=>
panic!
(
"value not present"
),
}
}
Err
(
e
)
=>
println!
(
"error reading value"
),
_
=>
panic!
(
"value not present"
),
}
assert
!
(
m
.is_ok
());
assert
!
(
m
.is_ok
());
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"abcdefgh"
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"abcdefgh"
);
assert
!
(
db
.delete
(
b
"k1"
)
.is_ok
());
assert
!
(
db
.delete
(
b
"k1"
)
.is_ok
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
}
assert
!
(
DB
::
destroy
(
&
opts
,
path
)
.is_ok
());
}
}
assert
!
(
DB
::
destroy
(
&
opts
,
path
)
.is_ok
());
}
}
src/rocksdb.rs
View file @
51a1588b
...
@@ -863,86 +863,94 @@ impl DBVector {
...
@@ -863,86 +863,94 @@ impl DBVector {
}
}
}
}
#[test]
#[cfg(test)]
fn
external
()
{
mod
test
{
let
path
=
"_rust_rocksdb_externaltest"
;
use
super
::
*
;
{
use
rocksdb_options
::
*
;
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
use
std
::
str
;
let
p
=
db
.put
(
b
"k1"
,
b
"v1111"
);
assert
!
(
p
.is_ok
());
#[test]
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
fn
external
()
{
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"v1111"
);
let
path
=
"_rust_rocksdb_externaltest"
;
assert
!
(
db
.delete
(
b
"k1"
)
.is_ok
());
{
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
}
let
p
=
db
.put
(
b
"k1"
,
b
"v1111"
);
let
opts
=
Options
::
new
();
assert
!
(
p
.is_ok
());
let
result
=
DB
::
destroy
(
&
opts
,
path
);
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
assert
!
(
result
.is_ok
());
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"v1111"
);
}
assert
!
(
db
.delete
(
b
"k1"
)
.is_ok
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
#[test]
fn
errors_do_stuff
()
{
let
path
=
"_rust_rocksdb_error"
;
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
opts
=
Options
::
new
();
// The DB will still be open when we try to destroy and the lock should fail
match
DB
::
destroy
(
&
opts
,
path
)
{
Err
(
ref
s
)
=>
{
assert
!
(
s
==
"IO error: lock _rust_rocksdb_error/LOCK: No locks
\
available"
)
}
}
Ok
(
_
)
=>
panic!
(
"should fail"
),
let
opts
=
Options
::
new
();
let
result
=
DB
::
destroy
(
&
opts
,
path
);
assert
!
(
result
.is_ok
());
}
}
}
#[test
]
#[allow(unused_variables)
]
fn
writebatch_works
()
{
#[test]
let
path
=
"_rust_rocksdb_writebacktest"
;
fn
errors_do_stuff
()
{
{
let
path
=
"_rust_rocksdb_error"
;
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
opts
=
Options
::
new
();
// The DB will still be open when we try to destroy and the lock should fail
match
DB
::
destroy
(
&
opts
,
path
)
{
Err
(
ref
s
)
=>
{
assert
!
(
s
==
"IO error: lock _rust_rocksdb_error/LOCK: No locks
\
available"
)
}
Ok
(
_
)
=>
panic!
(
"should fail"
),
}
}
#[test]
fn
writebatch_works
()
{
let
path
=
"_rust_rocksdb_writebacktest"
;
{
{
// test put
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
batch
=
WriteBatch
::
new
();
{
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
// test put
let
_
=
batch
.put
(
b
"k1"
,
b
"v1111"
);
let
batch
=
WriteBatch
::
new
();
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
let
p
=
db
.write
(
batch
);
let
_
=
batch
.put
(
b
"k1"
,
b
"v1111"
);
assert
!
(
p
.is_ok
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
let
p
=
db
.write
(
batch
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"v1111"
);
assert
!
(
p
.is_ok
());
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"v1111"
);
}
{
// test delete
let
batch
=
WriteBatch
::
new
();
let
_
=
batch
.delete
(
b
"k1"
);
let
p
=
db
.write
(
batch
);
assert
!
(
p
.is_ok
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
}
}
}
let
opts
=
Options
::
new
();
assert
!
(
DB
::
destroy
(
&
opts
,
path
)
.is_ok
());
}
#[test]
fn
iterator_test
()
{
let
path
=
"_rust_rocksdb_iteratortest"
;
{
{
// test delete
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
batch
=
WriteBatch
::
new
();
let
p
=
db
.put
(
b
"k1"
,
b
"v1111"
);
let
_
=
batch
.delete
(
b
"k1"
);
let
p
=
db
.write
(
batch
);
assert
!
(
p
.is_ok
());
assert
!
(
p
.is_ok
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
let
p
=
db
.put
(
b
"k2"
,
b
"v2222"
);
assert
!
(
p
.is_ok
());
let
p
=
db
.put
(
b
"k3"
,
b
"v3333"
);
assert
!
(
p
.is_ok
());
let
iter
=
db
.iterator
(
IteratorMode
::
Start
);
for
(
k
,
v
)
in
iter
{
println!
(
"Hello {}: {}"
,
str
::
from_utf8
(
&*
k
)
.unwrap
(),
str
::
from_utf8
(
&*
v
)
.unwrap
());
}
}
}
let
opts
=
Options
::
new
();
assert
!
(
DB
::
destroy
(
&
opts
,
path
)
.is_ok
());
}
}
let
opts
=
Options
::
new
();
assert
!
(
DB
::
destroy
(
&
opts
,
path
)
.is_ok
());
}
#[test]
fn
iterator_test
()
{
let
path
=
"_rust_rocksdb_iteratortest"
;
{
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"v1111"
);
assert
!
(
p
.is_ok
());
let
p
=
db
.put
(
b
"k2"
,
b
"v2222"
);
assert
!
(
p
.is_ok
());
let
p
=
db
.put
(
b
"k3"
,
b
"v3333"
);
assert
!
(
p
.is_ok
());
let
iter
=
db
.iterator
(
IteratorMode
::
Start
);
for
(
k
,
v
)
in
iter
{
println!
(
"Hello {}: {}"
,
from_utf8
(
&*
k
)
.unwrap
(),
from_utf8
(
&*
v
)
.unwrap
());
}
}
let
opts
=
Options
::
new
();
assert
!
(
DB
::
destroy
(
&
opts
,
path
)
.is_ok
());
}
}
test/test_column_family.rs
View file @
51a1588b
...
@@ -76,6 +76,9 @@ pub fn test_column_family() {
...
@@ -76,6 +76,9 @@ pub fn test_column_family() {
"v1"
);
"v1"
);
let
p
=
db
.put_cf
(
cf1
,
b
"k1"
,
b
"a"
);
let
p
=
db
.put_cf
(
cf1
,
b
"k1"
,
b
"a"
);
assert
!
(
p
.is_ok
());
assert
!
(
p
.is_ok
());
/*
// TODO support family merge operator
// have not finished yet, following codes won't work.
db.merge_cf(cf1, b"k1", b"b").unwrap();
db.merge_cf(cf1, b"k1", b"b").unwrap();
db.merge_cf(cf1, b"k1", b"c").unwrap();
db.merge_cf(cf1, b"k1", b"c").unwrap();
db.merge_cf(cf1, b"k1", b"d").unwrap();
db.merge_cf(cf1, b"k1", b"d").unwrap();
...
@@ -98,6 +101,7 @@ pub fn test_column_family() {
...
@@ -98,6 +101,7 @@ pub fn test_column_family() {
// TODO assert!(r.unwrap().to_utf8().unwrap() == "abcdefgh");
// TODO assert!(r.unwrap().to_utf8().unwrap() == "abcdefgh");
assert!(db.delete(b"k1").is_ok());
assert!(db.delete(b"k1").is_ok());
assert!(db.get(b"k1").unwrap().is_none());
assert!(db.get(b"k1").unwrap().is_none());
*/
}
}
// TODO should be able to use writebatch ops with a cf
// TODO should be able to use writebatch ops with a cf
{
{
...
...
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