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" {
}
#[test]
fn
internal
()
{
unsafe
{
use
std
::
ffi
::
CString
;
let
opts
=
rocksdb_options_create
();
assert
!
(
!
opts
.
0
.is_null
());
#[cfg(test)]
mod
test
{
use
super
::
*
;
use
libc
::
*
;
use
std
::
ffi
::
CString
;
rocksdb_options_increase_parallelism
(
opts
,
0
);
rocksdb_options_optimize_level_style_compaction
(
opts
,
0
);
rocksdb_options_set_create_if_missing
(
opts
,
true
);
#[test]
fn
internal
()
{
unsafe
{
let
opts
=
rocksdb_options_create
();
assert
!
(
!
opts
.
0
.is_null
());
let
rustpath
=
"_rust_rocksdb_internaltest"
;
let
cpath
=
CString
::
new
(
rustpath
)
.unwrap
(
);
let
cpath_ptr
=
cpath
.as_ptr
(
);
rocksdb_options_increase_parallelism
(
opts
,
0
)
;
rocksdb_options_optimize_level_style_compaction
(
opts
,
0
);
rocksdb_options_set_create_if_missing
(
opts
,
true
);
let
mut
err
:
*
const
i8
=
0
as
*
const
i8
;
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
rustpath
=
"_rust_rocksdb_internaltest"
;
let
cpath
=
CString
::
new
(
rustpath
)
.unwrap
();
let
cpath_ptr
=
cpath
.as_ptr
();
let
writeopts
=
rocksdb_writeoptions_create
();
assert
!
(
!
writeopts
.
0
.is_null
());
let
mut
err
:
*
const
i8
=
0
as
*
const
i8
;
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
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
writeopts
=
rocksdb_writeoptions_create
();
assert
!
(
!
writeopts
.
0
.is_null
());
let
readopts
=
rocksdb_readoptions_create
();
assert
!
(
!
readopts
.
0
.is_null
());
let
key
=
b
"name
\x00
"
;
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
val_len_ptr
=
&
val_len
as
*
const
size_t
;
rocksdb_get
(
db
,
readopts
.clone
(),
key
.as_ptr
(),
4
,
val_len_ptr
,
err_ptr
);
rocksdb_readoptions_destroy
(
readopts
);
assert
!
(
err
.is_null
());
rocksdb_close
(
db
);
rocksdb_destroy_db
(
opts
,
cpath_ptr
,
err_ptr
);
assert
!
(
err
.is_null
());
let
readopts
=
rocksdb_readoptions_create
();
assert
!
(
!
readopts
.
0
.is_null
());
let
val_len
:
size_t
=
0
;
let
val_len_ptr
=
&
val_len
as
*
const
size_t
;
rocksdb_get
(
db
,
readopts
.clone
(),
key
.as_ptr
(),
4
,
val_len_ptr
,
err_ptr
);
rocksdb_readoptions_destroy
(
readopts
);
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 {
use
rocksdb
::{
BlockBasedOptions
,
DB
,
Options
};
use
rocksdb
::
DBCompactionStyle
::
DBUniversalCompaction
;
#[allow(dead_code)]
fn
tuned_for_somebody_elses_disk
(
path
:
&
str
,
opts
:
&
mut
Options
,
blockopts
:
&
mut
BlockBasedOptions
)
...
...
src/merge_operator.rs
View file @
51a1588b
...
...
@@ -151,67 +151,72 @@ impl<'a> Iterator for &'a mut MergeOperands {
}
}
#[allow(unused_variables)]
#[allow(dead_code)]
fn
test_provided_merge
(
new_key
:
&
[
u8
],
existing_val
:
Option
<&
[
u8
]
>
,
operands
:
&
mut
MergeOperands
)
->
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
);
#[cfg(test)]
mod
test
{
use
super
::
*
;
use
rocksdb_options
::
Options
;
use
rocksdb
::{
DB
,
DBVector
,
Writable
};
#[allow(unused_variables)]
#[allow(dead_code)]
fn
test_provided_merge
(
new_key
:
&
[
u8
],
existing_val
:
Option
<&
[
u8
]
>
,
operands
:
&
mut
MergeOperands
)
->
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
{
result
.push
(
*
e
);
for
op
in
operands
{
for
e
in
op
{
result
.push
(
*
e
);
}
}
result
}
result
}
#[allow(dead_code)]
#[test]
fn
mergetest
()
{
use
rocksdb_options
::
Options
;
use
rocksdb
::{
DB
,
DBVector
,
Writable
};
let
path
=
"_rust_rocksdb_mergetest"
;
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
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"a"
);
assert
!
(
p
.is_ok
());
let
_
=
db
.merge
(
b
"k1"
,
b
"b"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"c"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"d"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"efg"
);
let
m
=
db
.merge
(
b
"k1"
,
b
"h"
);
assert
!
(
m
.is_ok
());
match
db
.get
(
b
"k1"
)
{
Ok
(
Some
(
value
))
=>
{
match
value
.to_utf8
()
{
Some
(
v
)
=>
println!
(
"retrieved utf8 value: {}"
,
v
),
None
=>
println!
(
"did not read valid utf-8 out of the db"
),
#[allow(dead_code)]
#[test]
fn
mergetest
()
{
let
path
=
"_rust_rocksdb_mergetest"
;
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
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"a"
);
assert
!
(
p
.is_ok
());
let
_
=
db
.merge
(
b
"k1"
,
b
"b"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"c"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"d"
);
let
_
=
db
.merge
(
b
"k1"
,
b
"efg"
);
let
m
=
db
.merge
(
b
"k1"
,
b
"h"
);
assert
!
(
m
.is_ok
());
match
db
.get
(
b
"k1"
)
{
Ok
(
Some
(
value
))
=>
{
match
value
.to_utf8
()
{
Some
(
v
)
=>
println!
(
"retrieved utf8 value: {}"
,
v
),
None
=>
{
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
());
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"abcdefgh"
);
assert
!
(
db
.delete
(
b
"k1"
)
.is_ok
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
assert
!
(
m
.is_ok
());
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"abcdefgh"
);
assert
!
(
db
.delete
(
b
"k1"
)
.is_ok
());
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 {
}
}
#[test]
fn
external
()
{
let
path
=
"_rust_rocksdb_externaltest"
;
{
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"v1111"
);
assert
!
(
p
.is_ok
());
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"v1111"
);
assert
!
(
db
.delete
(
b
"k1"
)
.is_ok
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
}
let
opts
=
Options
::
new
();
let
result
=
DB
::
destroy
(
&
opts
,
path
);
assert
!
(
result
.is_ok
());
}
#[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"
)
#[cfg(test)]
mod
test
{
use
super
::
*
;
use
rocksdb_options
::
*
;
use
std
::
str
;
#[test]
fn
external
()
{
let
path
=
"_rust_rocksdb_externaltest"
;
{
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"v1111"
);
assert
!
(
p
.is_ok
());
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"v1111"
);
assert
!
(
db
.delete
(
b
"k1"
)
.is_ok
());
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
}
Ok
(
_
)
=>
panic!
(
"should fail"
),
let
opts
=
Options
::
new
();
let
result
=
DB
::
destroy
(
&
opts
,
path
);
assert
!
(
result
.is_ok
());
}
}
#[test
]
fn
writebatch_works
()
{
let
path
=
"_rust_rocksdb_writebacktest"
;
{
#[allow(unused_variables)
]
#[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"
),
}
}
#[test]
fn
writebatch_works
()
{
let
path
=
"_rust_rocksdb_writebacktest"
;
{
// test put
let
batch
=
WriteBatch
::
new
();
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
let
_
=
batch
.put
(
b
"k1"
,
b
"v1111"
);
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
let
p
=
db
.write
(
batch
);
assert
!
(
p
.is_ok
());
let
r
:
Result
<
Option
<
DBVector
>
,
String
>
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.unwrap
()
.to_utf8
()
.unwrap
()
==
"v1111"
);
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
{
// test put
let
batch
=
WriteBatch
::
new
();
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
let
_
=
batch
.put
(
b
"k1"
,
b
"v1111"
);
assert
!
(
db
.get
(
b
"k1"
)
.unwrap
()
.is_none
());
let
p
=
db
.write
(
batch
);
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
batch
=
WriteBatch
::
new
();
let
_
=
batch
.delete
(
b
"k1"
);
let
p
=
db
.write
(
batch
);
let
db
=
DB
::
open_default
(
path
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"v1111"
);
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() {
"v1"
);
let
p
=
db
.put_cf
(
cf1
,
b
"k1"
,
b
"a"
);
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"c").unwrap();
db.merge_cf(cf1, b"k1", b"d").unwrap();
...
...
@@ -98,6 +101,7 @@ pub fn test_column_family() {
// TODO assert!(r.unwrap().to_utf8().unwrap() == "abcdefgh");
assert!(db.delete(b"k1").is_ok());
assert!(db.get(b"k1").unwrap().is_none());
*/
}
// 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