swarm: localstore hasser (#19230)

This commit is contained in:
Janoš Guljaš
2019-03-07 10:07:54 +01:00
committed by Anton Evangelatov
parent d45f8d1880
commit eb199f1fc2
5 changed files with 152 additions and 1 deletions

View File

@@ -123,6 +123,17 @@ func (db *DB) Get(key []byte) (value []byte, err error) {
return value, nil
}
// Has wraps LevelDB Has method to increment metrics counter.
func (db *DB) Has(key []byte) (yes bool, err error) {
yes, err = db.ldb.Has(key, nil)
if err != nil {
metrics.GetOrRegisterCounter("DB.hasFail", nil).Inc(1)
return false, err
}
metrics.GetOrRegisterCounter("DB.has", nil).Inc(1)
return yes, nil
}
// Delete wraps LevelDB Delete method to increment metrics counter.
func (db *DB) Delete(key []byte) (err error) {
err = db.ldb.Delete(key, nil)

View File

@@ -145,6 +145,17 @@ func (f Index) Get(keyFields Item) (out Item, err error) {
return out.Merge(keyFields), nil
}
// Has accepts key fields represented as Item to check
// if there this Item's encoded key is stored in
// the index.
func (f Index) Has(keyFields Item) (bool, error) {
key, err := f.encodeKeyFunc(keyFields)
if err != nil {
return false, err
}
return f.db.Has(key)
}
// Put accepts Item to encode information from it
// and save it to the database.
func (f Index) Put(i Item) (err error) {

View File

@@ -49,7 +49,7 @@ var retrievalIndexFuncs = IndexFuncs{
},
}
// TestIndex validates put, get and delete functions of the Index implementation.
// TestIndex validates put, get, has and delete functions of the Index implementation.
func TestIndex(t *testing.T) {
db, cleanupFunc := newTestDB(t)
defer cleanupFunc()
@@ -177,6 +177,41 @@ func TestIndex(t *testing.T) {
checkItem(t, got, want)
})
t.Run("has", func(t *testing.T) {
want := Item{
Address: []byte("has-hash"),
Data: []byte("DATA"),
StoreTimestamp: time.Now().UTC().UnixNano(),
}
dontWant := Item{
Address: []byte("do-not-has-hash"),
Data: []byte("DATA"),
StoreTimestamp: time.Now().UTC().UnixNano(),
}
err := index.Put(want)
if err != nil {
t.Fatal(err)
}
has, err := index.Has(want)
if err != nil {
t.Fatal(err)
}
if !has {
t.Error("item is not found")
}
has, err = index.Has(dontWant)
if err != nil {
t.Fatal(err)
}
if has {
t.Error("unwanted item is found")
}
})
t.Run("delete", func(t *testing.T) {
want := Item{
Address: []byte("delete-hash"),