Skip to content

Commit

Permalink
make has faster
Browse files Browse the repository at this point in the history
We don't actually need to *load* the value to see if we have it.
  • Loading branch information
Stebalien committed Oct 4, 2018
1 parent aa6f265 commit 812b6af
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ func (d *Datastore) Has(key ds.Key) (bool, error) {
return txn.Has(key)
}

func (d *Datastore) GetSize(key ds.Key) (int, error) {
txn := d.newImplicitTransaction(true)
defer txn.Discard()

return txn.Has(key)
}

func (d *Datastore) Delete(key ds.Key) error {
txn := d.newImplicitTransaction(false)
defer txn.Discard()
Expand Down Expand Up @@ -242,15 +249,15 @@ func (t *txn) Get(key ds.Key) ([]byte, error) {
}

func (t *txn) Has(key ds.Key) (bool, error) {
_, err := t.Get(key)

if err == nil {
return true, nil
} else if err == ds.ErrNotFound {
_, err := t.txn.Get(key.Bytes())
switch err {
case badger.ErrKeyNotFound:
return false, nil
case nil:
return true, nil
default:
return false, err
}

return false, err
}

func (t *txn) Delete(key ds.Key) error {
Expand Down

0 comments on commit 812b6af

Please # to comment.