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
ae5118e6
Commit
ae5118e6
authored
Nov 17, 2014
by
Tyler Neely
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some initial scratchings
parent
48659acb
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
192 additions
and
1 deletion
+192
-1
Cargo.lock
Cargo.lock
+4
-0
Cargo.toml
Cargo.toml
+5
-0
README.md
README.md
+16
-1
lib.rs
src/lib.rs
+167
-0
No files found.
Cargo.lock
0 → 100644
View file @
ae5118e6
[root]
name = "rust-rocksdb"
version = "0.0.1"
Cargo.toml
0 → 100644
View file @
ae5118e6
[package]
name
=
"rust-rocksdb"
version
=
"0.0.1"
authors
=
[
"Tyler Neely <t@jujit.su>"
]
README.md
View file @
ae5118e6
rust-rocksdb
============
rust wrapper for rocksdb
*
rust wrapper for rocksdb
*
development began 11/16/14
*
status: minimal functionality with poor style and an annoying interface
### running
1.
get rocksdb
```
git clone https://github.com/facebook/rocksdb
cd rocksdb
make shared_lib
```
2.
run tests
```
LD_PRELOAD=/path/to/rocksdb/librocksdb.so cargo test
```
3.
enjoy (as much as you can with such a poor current library! stay tuned!)
src/lib.rs
0 → 100644
View file @
ae5118e6
extern
crate
libc
;
use
libc
::{
c_int
,
size_t
};
use
std
::
io
::{
IoResult
,
IoError
};
use
std
::
c_vec
::
CVec
;
#[repr(C)]
struct
RocksdbOptions
(
*
const
c_int
);
#[repr(C)]
struct
RocksdbInstance
(
*
const
c_int
);
#[repr(C)]
struct
RocksdbWriteOptions
(
*
const
c_int
);
#[repr(C)]
struct
RocksdbReadOptions
(
*
const
c_int
);
#[link(name
=
"rocksdb"
)]
extern
{
fn
rocksdb_options_create
()
->
RocksdbOptions
;
fn
rocksdb_options_increase_parallelism
(
options
:
RocksdbOptions
,
threads
:
c_int
);
fn
rocksdb_options_optimize_level_style_compaction
(
options
:
RocksdbOptions
,
memtable_memory_budget
:
c_int
);
fn
rocksdb_options_set_create_if_missing
(
options
:
RocksdbOptions
,
v
:
c_int
);
fn
rocksdb_open
(
options
:
RocksdbOptions
,
path
:
*
const
i8
,
err
:
*
mut
i8
)
->
RocksdbInstance
;
fn
rocksdb_writeoptions_create
()
->
RocksdbWriteOptions
;
fn
rocksdb_put
(
db
:
RocksdbInstance
,
writeopts
:
RocksdbWriteOptions
,
k
:
*
const
u8
,
kLen
:
size_t
,
v
:
*
const
u8
,
vLen
:
size_t
,
err
:
*
mut
i8
);
fn
rocksdb_readoptions_create
()
->
RocksdbReadOptions
;
fn
rocksdb_get
(
db
:
RocksdbInstance
,
readopts
:
RocksdbReadOptions
,
k
:
*
const
u8
,
kLen
:
size_t
,
valLen
:
*
const
size_t
,
err
:
*
mut
i8
)
->
*
mut
u8
;
fn
rocksdb_close
(
db
:
RocksdbInstance
);
}
pub
struct
Rocksdb
{
inner
:
RocksdbInstance
,
}
impl
Rocksdb
{
pub
fn
open
(
path
:
&
str
,
create_if_missing
:
bool
)
->
IoResult
<
Rocksdb
>
{
unsafe
{
let
opts
=
rocksdb_options_create
();
let
RocksdbOptions
(
opt_ptr
)
=
opts
;
if
opt_ptr
.is_null
()
{
return
Err
(
IoError
::
last_error
());
}
rocksdb_options_increase_parallelism
(
opts
,
0
);
rocksdb_options_optimize_level_style_compaction
(
opts
,
0
);
match
create_if_missing
{
true
=>
rocksdb_options_set_create_if_missing
(
opts
,
1
),
false
=>
rocksdb_options_set_create_if_missing
(
opts
,
0
),
}
let
cpath
=
path
.to_c_str
();
let
cpath_ptr
=
cpath
.as_ptr
();
let
err
=
0
as
*
mut
i8
;
let
db
=
rocksdb_open
(
opts
,
cpath_ptr
,
err
);
let
RocksdbInstance
(
db_ptr
)
=
db
;
if
err
.is_not_null
()
{
return
Err
(
IoError
::
last_error
());
}
if
db_ptr
.is_null
()
{
return
Err
(
IoError
::
last_error
());
}
Ok
(
Rocksdb
{
inner
:
db
})
}
}
pub
fn
put
(
&
self
,
key
:
&
[
u8
],
value
:
&
[
u8
])
->
IoResult
<
bool
>
{
unsafe
{
let
writeopts
=
rocksdb_writeoptions_create
();
let
err
=
0
as
*
mut
i8
;
rocksdb_put
(
self
.inner
,
writeopts
,
key
.as_ptr
(),
key
.len
()
as
size_t
,
value
.as_ptr
(),
value
.len
()
as
size_t
,
err
);
if
err
.is_not_null
()
{
return
Err
(
IoError
::
last_error
());
}
return
Ok
(
true
)
}
}
pub
fn
get
(
&
self
,
key
:
&
[
u8
])
->
IoResult
<
CVec
<
u8
>>
{
unsafe
{
let
readopts
=
rocksdb_readoptions_create
();
let
RocksdbReadOptions
(
read_opts_ptr
)
=
readopts
;
if
read_opts_ptr
.is_null
()
{
return
Err
(
IoError
::
last_error
());
}
let
mut
val_len
:
size_t
=
0
;
let
val_len_ptr
=
&
val_len
as
*
const
size_t
;
let
err
=
0
as
*
mut
i8
;
let
val
=
rocksdb_get
(
self
.inner
,
readopts
,
key
.as_ptr
(),
key
.len
()
as
size_t
,
val_len_ptr
,
err
);
if
err
.is_not_null
()
{
return
Err
(
IoError
::
last_error
());
}
return
Ok
(
CVec
::
new_with_dtor
(
val
,
val_len
as
uint
,
proc
(){
libc
::
free
(
val
as
*
mut
libc
::
c_void
);
}))
}
}
pub
fn
close
(
&
self
)
{
unsafe
{
rocksdb_close
(
self
.inner
);
}
}
}
#[test]
fn
external
()
{
let
db
=
Rocksdb
::
open
(
"testdb"
,
true
)
.unwrap
();
db
.put
(
b
"k1"
,
b
"v1111"
);
let
r
=
db
.get
(
b
"k1"
)
.unwrap
();
db
.close
();
let
v
=
r
.get
(
0
)
.unwrap
();
assert
!
(
r
.len
()
==
5
);
}
#[test]
fn
internal
()
{
unsafe
{
let
opts
=
rocksdb_options_create
();
let
RocksdbOptions
(
opt_ptr
)
=
opts
;
assert
!
(
opt_ptr
.is_not_null
());
rocksdb_options_increase_parallelism
(
opts
,
0
);
rocksdb_options_optimize_level_style_compaction
(
opts
,
0
);
rocksdb_options_set_create_if_missing
(
opts
,
1
);
let
rustpath
=
"datadir"
;
let
cpath
=
rustpath
.to_c_str
();
let
cpath_ptr
=
cpath
.as_ptr
();
//TODO this will SIGSEGV
let
err
=
0
as
*
mut
i8
;
let
db
=
rocksdb_open
(
opts
,
cpath_ptr
,
err
);
assert
!
(
err
.is_null
());
let
writeopts
=
rocksdb_writeoptions_create
();
let
RocksdbWriteOptions
(
write_opt_ptr
)
=
writeopts
;
assert
!
(
write_opt_ptr
.is_not_null
());
let
key
=
b
"name
\x00
"
;
let
val
=
b
"spacejam
\x00
"
;
rocksdb_put
(
db
,
writeopts
,
key
.as_ptr
(),
4
,
val
.as_ptr
(),
8
,
err
);
assert
!
(
err
.is_null
());
let
readopts
=
rocksdb_readoptions_create
();
let
RocksdbReadOptions
(
read_opts_ptr
)
=
readopts
;
assert
!
(
read_opts_ptr
.is_not_null
());
let
mut
val_len
:
size_t
=
0
;
let
val_len_ptr
=
&
val_len
as
*
const
size_t
;
rocksdb_get
(
db
,
readopts
,
key
.as_ptr
(),
4
,
val_len_ptr
,
err
);
assert
!
(
err
.is_null
());
rocksdb_close
(
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