-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base skeleton for rocksdb bindings
- Loading branch information
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |