github.com/lbryio/lbcd@v0.22.119/claimtrie/merkletrie/merkletrierepo/pebble.go (about)

     1  package merkletrierepo
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/cockroachdb/pebble"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  type Pebble struct {
    11  	db *pebble.DB
    12  }
    13  
    14  func NewPebble(path string) (*Pebble, error) {
    15  
    16  	cache := pebble.NewCache(512 << 20)
    17  	//defer cache.Unref()
    18  	//
    19  	//go func() {
    20  	//	tick := time.NewTicker(60 * time.Second)
    21  	//	for range tick.C {
    22  	//
    23  	//		m := cache.Metrics()
    24  	//		fmt.Printf("cnt: %s, objs: %s, hits: %s, miss: %s, hitrate: %.2f\n",
    25  	//			humanize.Bytes(uint64(m.Size)),
    26  	//			humanize.Comma(m.Count),
    27  	//			humanize.Comma(m.Hits),
    28  	//			humanize.Comma(m.Misses),
    29  	//			float64(m.Hits)/float64(m.Hits+m.Misses))
    30  	//
    31  	//	}
    32  	//}()
    33  
    34  	db, err := pebble.Open(path, &pebble.Options{Cache: cache, BytesPerSync: 32 << 20, MaxOpenFiles: 2000})
    35  	repo := &Pebble{db: db}
    36  
    37  	return repo, errors.Wrapf(err, "unable to open %s", path)
    38  }
    39  
    40  func (repo *Pebble) Get(key []byte) ([]byte, io.Closer, error) {
    41  	d, c, e := repo.db.Get(key)
    42  	if e == pebble.ErrNotFound {
    43  		return nil, c, nil
    44  	}
    45  	return d, c, e
    46  }
    47  
    48  func (repo *Pebble) Set(key, value []byte) error {
    49  	return repo.db.Set(key, value, pebble.NoSync)
    50  }
    51  
    52  func (repo *Pebble) Close() error {
    53  
    54  	err := repo.db.Flush()
    55  	if err != nil {
    56  		// if we fail to close are we going to try again later?
    57  		return errors.Wrap(err, "on flush")
    58  	}
    59  
    60  	err = repo.db.Close()
    61  	return errors.Wrap(err, "on close")
    62  }
    63  
    64  func (repo *Pebble) Flush() error {
    65  	_, err := repo.db.AsyncFlush()
    66  	return err
    67  }