Skip to content

Commit

Permalink
Add base skeleton for rocksdb bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda authored and gdiazlo committed Mar 14, 2019
1 parent 9a6e508 commit 5af4c89
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
59 changes: 59 additions & 0 deletions rocksdb/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rocksdb

// #cgo LDFLAGS: -L../c-deps/rocksdb/
// #cgo LDFLAGS: -lrocksdb
// #include "../c-deps/rocksdb/include/rocksdb/c.h"
// #include <stdlib.h>
import "C"
import (
"errors"
"unsafe"
)

// DB is a reusable handler to a RocksDB database on disk, created by Open.
type DB struct {
db *C.rocksdb_t
opts *Options
}

func Open(path string, opts *Options) (*DB, error) {
var cErr *C.char
var cPath = C.CString(path)
defer C.free(unsafe.Pointer(cPath))

db := C.rocksdb_open(opts.opts, cPath, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}

return &DB{
db: db,
opts: opts,
}, nil
}

func (db *DB) Close() error {
if db.db != nil {
C.rocksdb_close(db.db)
db.db = nil
}
db.opts.Destroy()
return nil
}
45 changes: 45 additions & 0 deletions rocksdb/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rocksdb

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
)

func TestOpenDB(t *testing.T) {
db := newTestDB(t, "TestOpenDB", nil)
defer db.Close()
}

func newTestDB(t *testing.T, name string, applyOpts func(opts *Options)) *DB {
dir, err := ioutil.TempDir("", "rocksdb-"+name)
require.NoError(t, err)

opts := NewDefaultOptions()
opts.SetCreateIfMissing(true)
if applyOpts != nil {
applyOpts(opts)
}

db, err := Open(dir, opts)
require.NoError(t, err)

return db
}
55 changes: 55 additions & 0 deletions rocksdb/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rocksdb

// #cgo LDFLAGS: -L../c-deps/rocksdb/
// #cgo LDFLAGS: -lrocksdb
// #include "../c-deps/rocksdb/include/rocksdb/c.h"
import "C"

type CompressionType uint

// Compression types
const (
NoCompression = CompressionType(C.rocksdb_no_compression)
SnappyCompression = CompressionType(C.rocksdb_snappy_compression)
)

// Options represent all of the available options when opening a database with Open.
type Options struct {
opts *C.rocksdb_options_t
}

func NewDefaultOptions() *Options {
return NewNativeOptions(C.rocksdb_options_create())
}

func NewNativeOptions(opts *C.rocksdb_options_t) *Options {
return &Options{opts: opts}
}

// SetCreateIfMissing specifies whether the database
// should be created if it is missing.
// Default: false
func (o *Options) SetCreateIfMissing(value bool) {
C.rocksdb_options_set_create_if_missing(o.opts, boolToUchar(value))
}

func (o *Options) Destroy() {
C.rocksdb_options_destroy(o.opts)
o.opts = nil
}
28 changes: 28 additions & 0 deletions rocksdb/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rocksdb

// #include <stdlib.h>
import "C"

// boolToUchar converts a bool value to C.uchar.
func boolToUchar(b bool) C.uchar {
if b {
return C.uchar(1)
}
return C.uchar(0)
}

0 comments on commit 5af4c89

Please sign in to comment.