From eb199f1fc2fc91e40431c5acce61227a95d476bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jano=C5=A1=20Gulja=C5=A1?= Date: Thu, 7 Mar 2019 10:07:54 +0100 Subject: [PATCH] swarm: localstore hasser (#19230) --- swarm/shed/db.go | 11 +++++ swarm/shed/index.go | 11 +++++ swarm/shed/index_test.go | 37 ++++++++++++++- swarm/storage/localstore/mode_has.go | 39 ++++++++++++++++ swarm/storage/localstore/mode_has_test.go | 55 +++++++++++++++++++++++ 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 swarm/storage/localstore/mode_has.go create mode 100644 swarm/storage/localstore/mode_has_test.go diff --git a/swarm/shed/db.go b/swarm/shed/db.go index 6f6b16450..8c11bf48b 100644 --- a/swarm/shed/db.go +++ b/swarm/shed/db.go @@ -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) diff --git a/swarm/shed/index.go b/swarm/shed/index.go index d02bf1a00..6be018d20 100644 --- a/swarm/shed/index.go +++ b/swarm/shed/index.go @@ -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) { diff --git a/swarm/shed/index_test.go b/swarm/shed/index_test.go index 489f001bd..de181fa41 100644 --- a/swarm/shed/index_test.go +++ b/swarm/shed/index_test.go @@ -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"), diff --git a/swarm/storage/localstore/mode_has.go b/swarm/storage/localstore/mode_has.go new file mode 100644 index 000000000..90feaceef --- /dev/null +++ b/swarm/storage/localstore/mode_has.go @@ -0,0 +1,39 @@ +// Copyright 2019 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package localstore + +import ( + "github.com/ethereum/go-ethereum/swarm/chunk" +) + +// Hasser provides Has method to retrieve Chunks +// from database. +type Hasser struct { + db *DB +} + +// NewHasser returns a new Hasser on database. +func (db *DB) NewHasser() *Hasser { + return &Hasser{ + db: db, + } +} + +// Has returns true if the chunk is stored in database. +func (h *Hasser) Has(addr chunk.Address) (bool, error) { + return h.db.retrievalDataIndex.Has(addressToItem(addr)) +} diff --git a/swarm/storage/localstore/mode_has_test.go b/swarm/storage/localstore/mode_has_test.go new file mode 100644 index 000000000..332616ca2 --- /dev/null +++ b/swarm/storage/localstore/mode_has_test.go @@ -0,0 +1,55 @@ +// Copyright 2019 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package localstore + +import ( + "testing" +) + +// TestHas validates that Hasser is returning true for +// the stored chunk and false for one that is not stored. +func TestHas(t *testing.T) { + db, cleanupFunc := newTestDB(t, nil) + defer cleanupFunc() + + chunk := generateTestRandomChunk() + + err := db.NewPutter(ModePutUpload).Put(chunk) + if err != nil { + t.Fatal(err) + } + + hasser := db.NewHasser() + + has, err := hasser.Has(chunk.Address()) + if err != nil { + t.Fatal(err) + } + if !has { + t.Error("chunk not found") + } + + missingChunk := generateTestRandomChunk() + + has, err = hasser.Has(missingChunk.Address()) + if err != nil { + t.Fatal(err) + } + if has { + t.Error("unexpected chunk is found") + } +}