Skip to content

Commit

Permalink
fix: pebble.Open no longer accept relative path / FormatVersion var r…
Browse files Browse the repository at this point in the history
…emoved / BlockFilter & BlockCollector interface changed
  • Loading branch information
marino39 committed Mar 22, 2024
1 parent 225fc0d commit bad71c9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
23 changes: 13 additions & 10 deletions bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/fs"
"math"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -145,17 +144,14 @@ func Open(dirname string, opts *Options) (DB, error) {
opts.PebbleOptions = DefaultPebbleOptions()
}

if !strings.HasPrefix(dirname, "/") {
wd, err := os.Getwd()
if err != nil {
return nil, err
}

dirname = path.Clean(path.Join(wd, dirname))
// expand the path if it is not absolute
dirname, err := utils.PathExpand(dirname)
if err != nil {
return nil, err
}

bondPath := filepath.Join(dirname, "bond")
_, err := os.Stat(bondPath)
_, err = os.Stat(bondPath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
Expand Down Expand Up @@ -528,7 +524,7 @@ func iteratorToSST(itr Iterator, path string) error {
return err
}
opts := sstable.WriterOptions{
TableFormat: sstable.TableFormatRocksDBv2, Parallelism: true, Comparer: DefaultKeyComparer(),
TableFormat: sstable.TableFormatPebblev3, Parallelism: true, Comparer: DefaultKeyComparer(),
}
writer := sstable.NewWriter(objstorageprovider.NewFileWritable(file), opts)

Expand Down Expand Up @@ -580,6 +576,13 @@ func PebbleFormatVersion(dir string) (uint64, error) {
func MigratePebbleFormatVersion(dir string, upgradeVersion uint64) error {
opt := DefaultPebbleOptions()
opt.FormatMajorVersion = pebble.FormatMajorVersion(upgradeVersion)

// expand the path if it is not absolute
dir, err := utils.PathExpand(dir)
if err != nil {
return err
}

db, err := pebble.Open(dir, opt)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/sstable"
"github.com/go-bond/bond"
"github.com/go-bond/bond/utils"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -112,6 +113,11 @@ func (b BlockIndexFilter) Intersects(prop []byte) (bool, error) {
return tr, nil
}

func (b *BlockIndexFilter) SyntheticSuffixIntersects(prop []byte, suffix []byte) (bool, error) {
//TODO implement me
panic("implement me")
}

func TestBlockFilter_EqualDist(t *testing.T) {
opt := bond.DefaultPebbleOptions()

Expand All @@ -124,7 +130,10 @@ func TestBlockFilter_EqualDist(t *testing.T) {
},
}

db, err := pebble.Open(".db", opt)
dir := ".db"
dir, _ = utils.PathExpand(dir)

db, err := pebble.Open(dir, opt)
require.NoError(t, err)

dummyData := []byte("dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, ")
Expand Down Expand Up @@ -266,7 +275,10 @@ func TestBlockFilter_SequDist(t *testing.T) {
},
}

db, err := pebble.Open(".db", opt)
dir := ".db"
dir, _ = utils.PathExpand(dir)

db, err := pebble.Open(dir, opt)
require.NoError(t, err)

dummyData := []byte("dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, dummy data, ")
Expand Down Expand Up @@ -503,6 +515,11 @@ func (b BlockIndexValueFilter) Intersects(props []byte) (bool, error) {
return exist, nil
}

func (b BlockIndexValueFilter) SyntheticSuffixIntersects(prop []byte, suffix []byte) (bool, error) {
//TODO implement me
panic("implement me")
}

func TestDeleteSurface(t *testing.T) {
opt := bond.DefaultPebbleOptions()
opt.Comparer = pebble.DefaultComparer
Expand All @@ -511,9 +528,12 @@ func TestDeleteSurface(t *testing.T) {
return &BlockIndexValueCollector{}
}}
opt.DisableWAL = true
opt.FormatMajorVersion = 13 // pebble.FormatBlockPropertyCollector

dir := ".db"
dir, _ = utils.PathExpand(dir)

opt.FormatMajorVersion = pebble.FormatBlockPropertyCollector
pdb, err := pebble.Open(".db", opt)
pdb, err := pebble.Open(dir, opt)
require.NoError(t, err)
for i := 0; i < 100; i++ {
pdb.Set([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%d", i)), pebble.NoSync)
Expand All @@ -525,7 +545,7 @@ func TestDeleteSurface(t *testing.T) {
}
pdb.Flush()
pdb.Close()
pdb, err = pebble.Open(".db", opt)
pdb, err = pebble.Open(dir, opt)
require.NoError(t, err)
itr, err := pdb.NewIter(&pebble.IterOptions{
PointKeyFilters: []pebble.BlockPropertyFilter{
Expand Down
18 changes: 18 additions & 0 deletions utils/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"os"
"path"
"strings"
)

func PathExpand(p string) (string, error) {
if !strings.HasPrefix(p, "/") {
wd, err := os.Getwd()
if err != nil {
return "", err
}
p = path.Clean(path.Join(wd, p))
}
return p, nil
}

0 comments on commit bad71c9

Please sign in to comment.