Skip to content

Commit

Permalink
Fix Get to handle bitcask.ErrKeyNotFound errors for non-existent keys
Browse files Browse the repository at this point in the history
  • Loading branch information
prologic committed Oct 23, 2023
1 parent 297eb96 commit 6c2c73d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
16 changes: 13 additions & 3 deletions examples/custom-store/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package main
import (
"log"

"github.com/dgraph-io/badger/v4"
c "github.com/ostafen/clover/v2"
badgerstore "github.com/ostafen/clover/v2/store/badger"
bitcaskstore "github.com/ostafen/clover/v2/store/bitcask"
)

func main() {
store, err := badgerstore.OpenWithOptions(badger.DefaultOptions("").WithInMemory(true))
store, err := bitcaskstore.Open("test.db")
if err != nil {
log.Fatal(err)
}
Expand All @@ -19,4 +18,15 @@ func main() {
log.Fatal(err)
}
defer db.Close()

// Check if collection already exists
collectionExists, err := db.HasCollection("todos")
if err != nil {
log.Panicf("Failed to check collection: %v", err)
}

if !collectionExists {
// Create a collection named 'todos'
db.CreateCollection("todos")
}
}
15 changes: 7 additions & 8 deletions store/bitcask/bitcask.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
package bitcask

import (
"path/filepath"

"git.mills.io/prologic/bitcask"
"github.com/ostafen/clover/v2/store"
)
Expand All @@ -12,13 +10,9 @@ type bitcaskStore struct {
db *bitcask.Bitcask
}

const (
dbFileName = "data.db"
)

// Open ...
func Open(dir string) (store.Store, error) {
db, err := bitcask.Open(filepath.Join(dir, dbFileName))
db, err := bitcask.Open(dir)
if err != nil {
return nil, err
}
Expand All @@ -42,7 +36,12 @@ func (tx *bitcaskTx) Set(key, value []byte) error {
}

func (tx *bitcaskTx) Get(key []byte) ([]byte, error) {
return tx.Bitcask.Get(key)
value, err := tx.Bitcask.Get(key)
// XXX: Clover assumes non-nil errors even for "Key Not Found" (which Bitcask considers an error)
if err == bitcask.ErrKeyExpired {
return nil, nil
}
return value, nil
}

func (tx *bitcaskTx) Delete(key []byte) error {
Expand Down

0 comments on commit 6c2c73d

Please sign in to comment.