Skip to content

Commit

Permalink
Merge pull request tecbot#110 from kapitan-k/orig
Browse files Browse the repository at this point in the history
added dbpath
  • Loading branch information
tecbot authored Nov 1, 2017
2 parents b9cb0d3 + 7b1a909 commit d5fe1e4
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
76 changes: 76 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gorocksdb

import (
"io/ioutil"
"strconv"
"testing"

"github.com/facebookgo/ensure"
Expand Down Expand Up @@ -47,6 +48,49 @@ func TestDBCRUD(t *testing.T) {
ensure.True(t, v3.Data() == nil)
}

func TestDBCRUDDBPaths(t *testing.T) {
names := make([]string, 4)
target_sizes := make([]uint64, len(names))

for i := range names {
names[i] = "TestDBGet_" + strconv.FormatInt(int64(i), 10)
target_sizes[i] = uint64(1024 * 1024 * (i + 1))
}

db := newTestDBPathNames(t, "TestDBGet", names, target_sizes, nil)
defer db.Close()

var (
givenKey = []byte("hello")
givenVal1 = []byte("world1")
givenVal2 = []byte("world2")
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
)

// create
ensure.Nil(t, db.Put(wo, givenKey, givenVal1))

// retrieve
v1, err := db.Get(ro, givenKey)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), givenVal1)

// update
ensure.Nil(t, db.Put(wo, givenKey, givenVal2))
v2, err := db.Get(ro, givenKey)
defer v2.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v2.Data(), givenVal2)

// delete
ensure.Nil(t, db.Delete(wo, givenKey))
v3, err := db.Get(ro, givenKey)
ensure.Nil(t, err)
ensure.True(t, v3.Data() == nil)
}

func newTestDB(t *testing.T, name string, applyOpts func(opts *Options)) *DB {
dir, err := ioutil.TempDir("", "gorocksdb-"+name)
ensure.Nil(t, err)
Expand All @@ -64,3 +108,35 @@ func newTestDB(t *testing.T, name string, applyOpts func(opts *Options)) *DB {

return db
}

func newTestDBPathNames(t *testing.T, name string, names []string, target_sizes []uint64, applyOpts func(opts *Options)) *DB {
ensure.DeepEqual(t, len(target_sizes), len(names))
ensure.NotDeepEqual(t, len(names), 0)

dir, err := ioutil.TempDir("", "gorocksdb-"+name)
ensure.Nil(t, err)

paths := make([]string, len(names))
for i, name := range names {
dir, err := ioutil.TempDir("", "gorocksdb-"+name)
ensure.Nil(t, err)
paths[i] = dir
}

dbpaths := NewDBPathsFromData(paths, target_sizes)
defer DestroyDBPaths(dbpaths)

opts := NewDefaultOptions()
opts.SetDBPaths(dbpaths)
// test the ratelimiter
rateLimiter := NewRateLimiter(1024, 100*1000, 10)
opts.SetRateLimiter(rateLimiter)
opts.SetCreateIfMissing(true)
if applyOpts != nil {
applyOpts(opts)
}
db, err := OpenDb(opts, dir)
ensure.Nil(t, err)

return db
}
48 changes: 48 additions & 0 deletions dbpath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gorocksdb

// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import "unsafe"

// DBPath represents options for a dbpath.
type DBPath struct {
c *C.rocksdb_dbpath_t
}

// NewDBPath creates a DBPath object
// with the given path and target_size.
func NewDBPath(path string, target_size uint64) *DBPath {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
return NewNativeDBPath(C.rocksdb_dbpath_create(cpath, C.uint64_t(target_size)))
}

// NewNativeDBPath creates a DBPath object.
func NewNativeDBPath(c *C.rocksdb_dbpath_t) *DBPath {
return &DBPath{c}
}

// Destroy deallocates the DBPath object.
func (dbpath *DBPath) Destroy() {
C.rocksdb_dbpath_destroy(dbpath.c)
}

// NewDBPathsFromData creates a slice with allocated DBPath objects
// from paths and target_sizes.
func NewDBPathsFromData(paths []string, target_sizes []uint64) []*DBPath {
dbpaths := make([]*DBPath, len(paths))
for i, path := range paths {
targetSize := target_sizes[i]
dbpaths[i] = NewDBPath(path, targetSize)
}

return dbpaths
}

// DestroyDBPaths deallocates all DBPath objects in dbpaths.
func DestroyDBPaths(dbpaths []*DBPath) {
for _, dbpath := range dbpaths {
dbpath.Destroy()
}
}
36 changes: 36 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,42 @@ func (opts *Options) SetParanoidChecks(value bool) {
C.rocksdb_options_set_paranoid_checks(opts.c, boolToChar(value))
}

// SetDBPaths sets the DBPaths of the options.
//
// A list of paths where SST files can be put into, with its target size.
// Newer data is placed into paths specified earlier in the vector while
// older data gradually moves to paths specified later in the vector.
//
// For example, you have a flash device with 10GB allocated for the DB,
// as well as a hard drive of 2TB, you should config it to be:
// [{"/flash_path", 10GB}, {"/hard_drive", 2TB}]
//
// The system will try to guarantee data under each path is close to but
// not larger than the target size. But current and future file sizes used
// by determining where to place a file are based on best-effort estimation,
// which means there is a chance that the actual size under the directory
// is slightly more than target size under some workloads. User should give
// some buffer room for those cases.
//
// If none of the paths has sufficient room to place a file, the file will
// be placed to the last path anyway, despite to the target size.
//
// Placing newer data to earlier paths is also best-efforts. User should
// expect user files to be placed in higher levels in some extreme cases.
//
// If left empty, only one path will be used, which is db_name passed when
// opening the DB.
// Default: empty
func (opts *Options) SetDBPaths(dbpaths []*DBPath) {
l := len(dbpaths)
cDbpaths := make([]*C.rocksdb_dbpath_t, l)
for i, v := range dbpaths {
cDbpaths[i] = v.c
}

C.rocksdb_options_set_db_paths(opts.c, &cDbpaths[0], C.size_t(l))
}

// SetEnv sets the specified object to interact with the environment,
// e.g. to read/write files, schedule background work, etc.
// Default: DefaultEnv
Expand Down

0 comments on commit d5fe1e4

Please sign in to comment.