From 0a06ebac9db50de066a06e304a7cf600af26e3c3 Mon Sep 17 00:00:00 2001 From: Linh Tran Tuan Date: Tue, 16 Feb 2021 12:25:20 +0900 Subject: [PATCH] NO-ISSUE Adding exists func to pinnable slice (#26) --- backup_test.go | 15 +++++++++++---- db_test.go | 17 +++++++++++++---- slice.go | 35 ++++++++++++++++++----------------- slice_test.go | 8 -------- 4 files changed, 42 insertions(+), 33 deletions(-) delete mode 100644 slice_test.go diff --git a/backup_test.go b/backup_test.go index ee8b093..3fd88fb 100644 --- a/backup_test.go +++ b/backup_test.go @@ -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() + } }) } diff --git a/db_test.go b/db_test.go index 61df886..fe0137b 100755 --- a/db_test.go +++ b/db_test.go @@ -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)) diff --git a/slice.go b/slice.go index 778081f..3ebb6a8 100644 --- a/slice.go +++ b/slice.go @@ -27,17 +27,19 @@ 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. @@ -45,11 +47,6 @@ 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 { @@ -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. diff --git a/slice_test.go b/slice_test.go deleted file mode 100644 index 8e58124..0000000 --- a/slice_test.go +++ /dev/null @@ -1,8 +0,0 @@ -package grocksdb - -import "testing" - -func TestStringToSlice(t *testing.T) { - slice := StringToSlice("asdf") - defer slice.Free() -}