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
e98a85f4
Commit
e98a85f4
authored
Dec 13, 2014
by
Tyler Neely
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleanup for merge operators
parent
0f0f5910
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
21 deletions
+90
-21
README.md
README.md
+36
-0
lib.rs
src/lib.rs
+3
-0
main.rs
src/main.rs
+48
-18
rocksdb.rs
src/rocksdb.rs
+3
-3
No files found.
README.md
View file @
e98a85f4
...
@@ -32,6 +32,42 @@ fn main() {
...
@@ -32,6 +32,42 @@ fn main() {
}
}
```
```
###### Rustic Merge Operator
```
rust
extern
crate
rocksdb
;
use
rocksdb
::{
RocksDBOptions
,
RocksDB
,
MergeOperands
};
fn
concat_merge
<
'a
>
(
new_key
:
&
[
u8
],
existing_val
:
Option
<&
[
u8
]
>
,
mut
operands
:
&
mut
MergeOperands
)
->
Vec
<
u8
>
{
let
mut
result
:
Vec
<
u8
>
=
Vec
::
with_capacity
(
operands
.size_hint
()
.val0
());
match
existing_val
{
Some
(
v
)
=>
result
.push_all
(
v
),
None
=>
(),
}
for
op
in
operands
{
result
.push_all
(
op
);
}
result
}
fn
main
()
{
let
path
=
"/path/to/rocksdb"
;
let
opts
=
RocksDBOptions
::
new
();
opts
.create_if_missing
(
true
);
opts
.add_merge_operator
(
"test operator"
,
concat_merge
);
let
db
=
RocksDB
::
open
(
opts
,
path
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"a"
);
db
.merge
(
b
"k1"
,
b
"b"
);
db
.merge
(
b
"k1"
,
b
"c"
);
db
.merge
(
b
"k1"
,
b
"d"
);
db
.merge
(
b
"k1"
,
b
"efg"
);
let
r
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.to_utf8
()
.unwrap
()
==
"abcdefg"
);
db
.close
();
}
```
### status
### status
-
[
x
]
basic open/put/get/delete/close
-
[
x
]
basic open/put/get/delete/close
-
[
x
]
linux support
-
[
x
]
linux support
...
...
src/lib.rs
View file @
e98a85f4
...
@@ -5,7 +5,10 @@
...
@@ -5,7 +5,10 @@
pub
use
ffi
as
rocksdb_ffi
;
pub
use
ffi
as
rocksdb_ffi
;
pub
use
rocksdb
::{
pub
use
rocksdb
::{
RocksDB
,
RocksDB
,
MergeOperands
,
RocksDBResult
,
RocksDBResult
,
RocksDBOptions
,
RocksDBVector
,
};
};
pub
mod
rocksdb
;
pub
mod
rocksdb
;
pub
mod
ffi
;
pub
mod
ffi
;
src/main.rs
View file @
e98a85f4
extern
crate
rocksdb
;
extern
crate
rocksdb
;
extern
crate
test
;
extern
crate
test
;
use
rocksdb
::
RocksDB
;
use
rocksdb
::
{
RocksDBOptions
,
RocksDB
,
MergeOperands
}
;
use
test
::
Bencher
;
use
test
::
Bencher
;
#[allow(dead_code)]
#[allow(dead_code)]
fn
main
()
{
fn
main
()
{
match
RocksDB
::
open_default
(
"/tmp/rust-rocksdb"
)
{
let
path
=
"/tmp/rust-rocksdb"
;
Ok
(
db
)
=>
{
let
db
=
RocksDB
::
open_default
(
path
)
.unwrap
();
assert
!
(
db
.put
(
b
"my key"
,
b
"my value"
)
.is_ok
());
assert
!
(
db
.put
(
b
"my key"
,
b
"my value"
)
.is_ok
());
db
.get
(
b
"my key"
)
.map
(
|
value
|
{
match
value
.to_utf8
()
{
Some
(
v
)
=>
println!
(
"retrieved utf8 value: {}"
,
v
),
None
=>
println!
(
"did not read valid utf-8 out of the db"
),
}
})
.on_absent
(
||
{
println!
(
"value not found"
)
})
.on_error
(
|
e
|
{
println!
(
"error retrieving value: {}"
,
e
)
});
db
.get
(
b
"my key"
)
.map
(
|
value
|
{
assert
!
(
db
.delete
(
b
"my key"
)
.is_ok
());
match
value
.to_utf8
()
{
db
.close
();
Some
(
v
)
=>
println!
(
"retrieved utf8 value: {}"
,
v
),
None
=>
println!
(
"did not read valid utf-8 out of the db"
),
}
})
.on_absent
(
||
{
println!
(
"value not found"
)
})
.on_error
(
|
e
|
{
println!
(
"error retrieving value: {}"
,
e
)
});
assert
!
(
db
.delete
(
b
"my key"
)
.is_ok
());
custom_merge
();
}
db
.close
();
#[allow(dead_code)]
},
fn
concat_merge
(
new_key
:
&
[
u8
],
existing_val
:
Option
<&
[
u8
]
>
,
Err
(
e
)
=>
panic!
(
e
),
mut
operands
:
&
mut
MergeOperands
)
->
Vec
<
u8
>
{
let
mut
result
:
Vec
<
u8
>
=
Vec
::
with_capacity
(
operands
.size_hint
()
.val0
());
match
existing_val
{
Some
(
v
)
=>
result
.push_all
(
v
),
None
=>
(),
}
for
op
in
operands
{
result
.push_all
(
op
);
}
}
result
}
#[allow(dead_code)]
fn
custom_merge
()
{
let
path
=
"_rust_rocksdb_mergetest"
;
let
opts
=
RocksDBOptions
::
new
();
opts
.create_if_missing
(
true
);
opts
.add_merge_operator
(
"test operator"
,
concat_merge
);
let
db
=
RocksDB
::
open
(
opts
,
path
)
.unwrap
();
let
p
=
db
.put
(
b
"k1"
,
b
"a"
);
db
.merge
(
b
"k1"
,
b
"b"
);
db
.merge
(
b
"k1"
,
b
"c"
);
db
.merge
(
b
"k1"
,
b
"d"
);
db
.merge
(
b
"k1"
,
b
"efg"
);
let
m
=
db
.merge
(
b
"k1"
,
b
"h"
);
let
r
=
db
.get
(
b
"k1"
);
assert
!
(
r
.unwrap
()
.to_utf8
()
.unwrap
()
==
"abcdefgh"
);
db
.close
();
RocksDB
::
destroy
(
opts
,
path
)
.is_ok
();
}
}
#[allow(dead_code)]
#[allow(dead_code)]
...
...
src/rocksdb.rs
View file @
e98a85f4
...
@@ -54,7 +54,7 @@ impl RocksDBOptions {
...
@@ -54,7 +54,7 @@ impl RocksDBOptions {
}
}
}
}
pub
fn
add_merge_operator
<
'a
>
(
&
self
,
name
:
&
str
,
merge_fn
:
f
or
<
'a
>
f
n
(
&
[
u8
],
Option
<&
[
u8
]
>
,
&
mut
MergeOperands
)
->
Vec
<
u8
>
)
{
pub
fn
add_merge_operator
<
'a
>
(
&
self
,
name
:
&
str
,
merge_fn
:
fn
(
&
[
u8
],
Option
<&
[
u8
]
>
,
&
mut
MergeOperands
)
->
Vec
<
u8
>
)
{
let
cb
=
box
MergeOperatorCallback
{
let
cb
=
box
MergeOperatorCallback
{
name
:
name
.to_c_str
(),
name
:
name
.to_c_str
(),
merge_fn
:
merge_fn
,
merge_fn
:
merge_fn
,
...
@@ -418,7 +418,7 @@ impl <'a> Iterator<&'a [u8]> for &'a mut MergeOperands<'a> {
...
@@ -418,7 +418,7 @@ impl <'a> Iterator<&'a [u8]> for &'a mut MergeOperands<'a> {
struct
MergeOperatorCallback
{
struct
MergeOperatorCallback
{
name
:
CString
,
name
:
CString
,
merge_fn
:
f
or
<
'b
>
f
n
(
&
[
u8
],
Option
<&
[
u8
]
>
,
&
mut
MergeOperands
)
->
Vec
<
u8
>
,
merge_fn
:
fn
(
&
[
u8
],
Option
<&
[
u8
]
>
,
&
mut
MergeOperands
)
->
Vec
<
u8
>
,
}
}
extern
"C"
fn
destructor_callback
(
raw_cb
:
*
mut
c_void
)
{
extern
"C"
fn
destructor_callback
(
raw_cb
:
*
mut
c_void
)
{
...
@@ -479,7 +479,7 @@ extern "C" fn partial_merge_callback(
...
@@ -479,7 +479,7 @@ extern "C" fn partial_merge_callback(
}
}
}
}
fn
test_provided_merge
<
'a
>
(
new_key
:
&
[
u8
],
existing_val
:
Option
<&
[
u8
]
>
,
fn
test_provided_merge
(
new_key
:
&
[
u8
],
existing_val
:
Option
<&
[
u8
]
>
,
mut
operands
:
&
mut
MergeOperands
)
->
Vec
<
u8
>
{
mut
operands
:
&
mut
MergeOperands
)
->
Vec
<
u8
>
{
let
mut
result
:
Vec
<
u8
>
=
Vec
::
with_capacity
(
operands
.size_hint
()
.val0
());
let
mut
result
:
Vec
<
u8
>
=
Vec
::
with_capacity
(
operands
.size_hint
()
.val0
());
match
existing_val
{
match
existing_val
{
...
...
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