implement bolt-backed liveset

This commit is contained in:
vyzo 2021-02-27 12:44:31 +02:00
parent cb1789ea6e
commit 27a9b974db
4 changed files with 83 additions and 6 deletions

View File

@ -1,7 +1,6 @@
package splitstore package splitstore
import ( import (
"fmt"
"path/filepath" "path/filepath"
cid "github.com/ipfs/go-cid" cid "github.com/ipfs/go-cid"
@ -25,5 +24,5 @@ func NewLiveSetEnv(path string, useLMDB bool) (LiveSetEnv, error) {
return NewLMDBLiveSetEnv(filepath.Join(path, "sweep.lmdb")) return NewLMDBLiveSetEnv(filepath.Join(path, "sweep.lmdb"))
} }
return nil, fmt.Errorf("TODO: non-lmdb livesets") return NewBoltLiveSetEnv(filepath.Join(path, "sweep.bolt"))
} }

View File

@ -0,0 +1,80 @@
package splitstore
import (
"time"
"golang.org/x/xerrors"
cid "github.com/ipfs/go-cid"
bolt "go.etcd.io/bbolt"
)
type BoltLiveSetEnv struct {
db *bolt.DB
}
var _ LiveSetEnv = (*BoltLiveSetEnv)(nil)
type BoltLiveSet struct {
db *bolt.DB
bucketId []byte
}
var _ LiveSet = (*BoltLiveSet)(nil)
func NewBoltLiveSetEnv(path string) (*BoltLiveSetEnv, error) {
db, err := bolt.Open(path, 0644,
&bolt.Options{
Timeout: 1 * time.Second,
})
if err != nil {
return nil, err
}
return &BoltLiveSetEnv{db: db}, nil
}
func (e *BoltLiveSetEnv) NewLiveSet(name string) (LiveSet, error) {
bucketId := []byte(name)
err := e.db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(bucketId)
if err != nil {
return xerrors.Errorf("error creating bolt db bucket %s: %w", name, err)
}
return nil
})
if err != nil {
return nil, err
}
return &BoltLiveSet{db: e.db, bucketId: bucketId}, nil
}
func (e *BoltLiveSetEnv) Close() error {
return e.db.Close()
}
func (s *BoltLiveSet) Mark(cid cid.Cid) error {
return s.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(s.bucketId)
return b.Put(cid.Hash(), markBytes)
})
}
func (s *BoltLiveSet) Has(cid cid.Cid) (result bool, err error) {
err = s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(s.bucketId)
v := b.Get(cid.Hash())
result = v != nil
return nil
})
return result, err
}
func (s *BoltLiveSet) Close() error {
return s.db.Update(func(tx *bolt.Tx) error {
return tx.DeleteBucket(s.bucketId)
})
}

View File

@ -5,9 +5,8 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/ledgerwatch/lmdb-go/lmdb"
cid "github.com/ipfs/go-cid" cid "github.com/ipfs/go-cid"
"github.com/ledgerwatch/lmdb-go/lmdb"
) )
var LMDBLiveSetMapSize int64 = 1 << 34 // 16G; TODO grow the map dynamically var LMDBLiveSetMapSize int64 = 1 << 34 // 16G; TODO grow the map dynamically

View File

@ -5,9 +5,8 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/ledgerwatch/lmdb-go/lmdb"
cid "github.com/ipfs/go-cid" cid "github.com/ipfs/go-cid"
"github.com/ledgerwatch/lmdb-go/lmdb"
"github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/abi"
) )