Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gorocks] add a call to open a database with TTL #19

Merged
merged 2 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[gorocks] add function call for creating column family with TTL
  • Loading branch information
dforciea committed Nov 15, 2021
commit 96abbfa2c3e86e424569209e6507da6fd7780d05
20 changes: 20 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func OpenDbColumnFamiliesWithTTL(
return nil, nil, errors.New("must provide the same number of column family names and options")
}

if numColumnFamilies != len(cfTtls) {
return nil, nil, errors.New("must provide the same number of column family names and ttls")
}

cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

Expand Down Expand Up @@ -725,6 +729,22 @@ func (db *DB) CreateColumnFamily(opts *Options, name string) (*ColumnFamilyHandl
return NewNativeColumnFamilyHandle(cHandle), nil
}

// CreateColumnFamilyWithTTL creates a new column family with a TTL.
func (db *DB) CreateColumnFamilyWithTTL(opts *Options, name string, ttl int) (*ColumnFamilyHandle, error) {
var (
cErr *C.char
cName = C.CString(name)
cTtl = C.int(ttl)
)
defer C.free(unsafe.Pointer(cName))
cHandle := C.rocksdb_create_column_family_with_ttl(db.c, opts.c, cName, cTtl, &cErr)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewNativeColumnFamilyHandle(cHandle), nil
}

// DropColumnFamily drops a column family.
func (db *DB) DropColumnFamily(c *ColumnFamilyHandle) error {
var cErr *C.char
Expand Down
43 changes: 42 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,35 @@ func TestOpenDbColumnFamiliesWithTTL(t *testing.T) {
opts.SetCreateIfMissing(true)
opts.SetCreateIfMissingColumnFamilies(true)

db, _, err := OpenDbColumnFamiliesWithTTL(opts, dir, []string{"default", "mycf"}, []*Options{opts, opts}, []int{3600})
db, _, err := OpenDbColumnFamiliesWithTTL(opts, dir, []string{"default", "mycf"}, []*Options{opts, opts}, []int{3600, 3600})
defer db.Close()

ensure.Nil(t, err)
}

func TestCreateColumnFamilyWithTTL(t *testing.T) {
db := newTestDBWithTTL(t, "TestCreateColumnFamilyWithTTL", nil)
defer db.Close()

var (
givenKey = []byte("hello")
givenVal = []byte("world")
o = NewDefaultOptions()
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
)

cf, err := db.CreateColumnFamilyWithTTL(o, "cf", 3600)
ensure.Nil(t, err)

ensure.Nil(t, db.PutCF(wo, cf, givenKey, givenVal))

v, err := db.GetCF(ro, cf, givenKey)
defer v.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v.Data(), givenVal)
}

func TestDBCRUD(t *testing.T) {
db := newTestDB(t, "TestDBGet", nil)
defer db.Close()
Expand Down Expand Up @@ -158,6 +181,24 @@ func newTestDB(t *testing.T, name string, applyOpts func(opts *Options)) *DB {
return db
}

func newTestDBWithTTL(t *testing.T, name string, applyOpts func(opts *Options)) *DB {
dir, err := ioutil.TempDir("", "gorocksdb-"+name)
ensure.Nil(t, err)

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

return db
}

func newSecondaryTestDB(t *testing.T, name string) *DB {
secondaryDir := name + "-secondary"

Expand Down