Skip to content

Commit

Permalink
NO-ISSUE Adding exists func to pinnable slice (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
linxGnu authored Feb 16, 2021
1 parent c661356 commit 0a06eba
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
15 changes: 11 additions & 4 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,16 @@ func TestBackupEngine(t *testing.T) {
r := NewDefaultReadOptions()
defer r.Destroy()

v3, err := backupDB.GetPinned(r, givenKey)
defer v3.Destroy()
require.Nil(t, err)
require.EqualValues(t, v3.Data(), givenVal2)
for i := 0; i < 1000; i++ {
v3, err := backupDB.GetPinned(r, givenKey)
require.Nil(t, err)
require.EqualValues(t, v3.Data(), givenVal2)
v3.Destroy()

v4, err := backupDB.GetPinned(r, []byte("justFake"))
require.Nil(t, err)
require.False(t, v4.Exists())
v4.Destroy()
}
})
}
17 changes: 13 additions & 4 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@ func TestDBCRUD(t *testing.T) {
require.EqualValues(t, v2.Data(), givenVal2)

// retrieve pinned
v3, err := db.GetPinned(ro, givenKey)
defer v3.Destroy()
require.Nil(t, err)
require.EqualValues(t, v3.Data(), givenVal2)
for i := 0; i < 1000; i++ {
v3, err := db.GetPinned(ro, givenKey)
require.Nil(t, err)
require.EqualValues(t, v3.Data(), givenVal2)
v3.Destroy()
v3.Destroy()

v3NE, err := db.GetPinned(ro, []byte("justFake"))
require.Nil(t, err)
require.False(t, v3NE.Exists())
v3NE.Destroy()
v3NE.Destroy()
}

// delete
require.Nil(t, db.Delete(wo, givenKey))
Expand Down
35 changes: 18 additions & 17 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,26 @@ func NewSlice(data *C.char, size C.size_t) *Slice {
return &Slice{data, size, false}
}

// StringToSlice is similar to NewSlice, but can be called with
// a Go string type. This exists to make testing integration
// with Gorocksdb easier.
func StringToSlice(data string) *Slice {
return NewSlice(C.CString(data), C.size_t(len(data)))
// Exists returns if underlying data exists.
func (s *Slice) Exists() bool {
return s.data != nil
}

// Data returns the data of the slice. If the key doesn't exist this will be a
// nil slice.
func (s *Slice) Data() []byte {
return charToByte(s.data, s.size)
if s.Exists() {
return charToByte(s.data, s.size)
}

return nil
}

// Size returns the size of the data.
func (s *Slice) Size() int {
return int(s.size)
}

// Exists returns if the key exists
func (s *Slice) Exists() bool {
return s.data != nil
}

// Free frees the slice data.
func (s *Slice) Free() {
if !s.freed {
Expand All @@ -69,16 +66,20 @@ func NewNativePinnableSliceHandle(c *C.rocksdb_pinnableslice_t) *PinnableSliceHa
return &PinnableSliceHandle{c}
}

// Exists returns if underlying data exists.
func (h *PinnableSliceHandle) Exists() bool {
return h.c != nil
}

// Data returns the data of the slice.
func (h *PinnableSliceHandle) Data() []byte {
if h.c == nil {
return nil
if h.Exists() {
var cValLen C.size_t
cValue := C.rocksdb_pinnableslice_value(h.c, &cValLen)
return charToByte(cValue, cValLen)
}

var cValLen C.size_t
cValue := C.rocksdb_pinnableslice_value(h.c, &cValLen)

return charToByte(cValue, cValLen)
return nil
}

// Destroy calls the destructor of the underlying pinnable slice handle.
Expand Down
8 changes: 0 additions & 8 deletions slice_test.go

This file was deleted.

0 comments on commit 0a06eba

Please sign in to comment.