lotus/chain/store/splitstore/liveset.go

68 lines
1.1 KiB
Go
Raw Normal View History

2020-11-26 14:53:16 +00:00
package splitstore
import (
2020-11-26 15:49:47 +00:00
"github.com/bmatsuo/lmdb-go/lmdb"
2020-11-26 14:53:16 +00:00
cid "github.com/ipfs/go-cid"
)
type LiveSet interface {
Mark(cid.Cid) error
Has(cid.Cid) (bool, error)
Close() error
}
2020-11-26 15:49:47 +00:00
type liveSet struct {
env *lmdb.Env
db lmdb.DBI
}
2020-11-26 16:58:03 +00:00
var markBytes = []byte{}
2020-11-26 15:49:47 +00:00
func NewLiveSet(env *lmdb.Env, name string) (LiveSet, error) {
var db lmdb.DBI
err := env.Update(func(txn *lmdb.Txn) (err error) {
db, err = txn.CreateDBI(name)
return
})
if err != nil {
return nil, err
}
return &liveSet{env: env, db: db}, nil
}
func (s *liveSet) Mark(cid cid.Cid) error {
return s.env.Update(func(txn *lmdb.Txn) error {
return txn.Put(s.db, cid.Hash(), markBytes, 0)
})
}
func (s *liveSet) Has(cid cid.Cid) (has bool, err error) {
err = s.env.View(func(txn *lmdb.Txn) error {
2020-11-26 18:37:02 +00:00
txn.RawRead = true
2020-11-26 15:49:47 +00:00
_, err := txn.Get(s.db, cid.Hash())
if err != nil {
if lmdb.IsNotFound(err) {
has = false
return nil
}
return err
}
has = true
return nil
})
return
}
func (s *liveSet) Close() error {
return s.env.Update(func(txn *lmdb.Txn) error {
return txn.Drop(s.db, true)
})
}