Skip to content

Commit eb199f1

Browse files
janosnonsense
authored andcommitted
swarm: localstore hasser (#19230)
1 parent d45f8d1 commit eb199f1

5 files changed

Lines changed: 152 additions & 1 deletion

File tree

‎swarm/shed/db.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ func (db *DB) Get(key []byte) (value []byte, err error) {
123123
return value, nil
124124
}
125125

126+
// Has wraps LevelDB Has method to increment metrics counter.
127+
func (db *DB) Has(key []byte) (yes bool, err error) {
128+
yes, err = db.ldb.Has(key, nil)
129+
if err != nil {
130+
metrics.GetOrRegisterCounter("DB.hasFail", nil).Inc(1)
131+
return false, err
132+
}
133+
metrics.GetOrRegisterCounter("DB.has", nil).Inc(1)
134+
return yes, nil
135+
}
136+
126137
// Delete wraps LevelDB Delete method to increment metrics counter.
127138
func (db *DB) Delete(key []byte) (err error) {
128139
err = db.ldb.Delete(key, nil)

‎swarm/shed/index.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ func (f Index) Get(keyFields Item) (out Item, err error) {
145145
return out.Merge(keyFields), nil
146146
}
147147

148+
// Has accepts key fields represented as Item to check
149+
// if there this Item's encoded key is stored in
150+
// the index.
151+
func (f Index) Has(keyFields Item) (bool, error) {
152+
key, err := f.encodeKeyFunc(keyFields)
153+
if err != nil {
154+
return false, err
155+
}
156+
return f.db.Has(key)
157+
}
158+
148159
// Put accepts Item to encode information from it
149160
// and save it to the database.
150161
func (f Index) Put(i Item) (err error) {

‎swarm/shed/index_test.go‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var retrievalIndexFuncs = IndexFuncs{
4949
},
5050
}
5151

52-
// TestIndex validates put, get and delete functions of the Index implementation.
52+
// TestIndex validates put, get, has and delete functions of the Index implementation.
5353
func TestIndex(t *testing.T) {
5454
db, cleanupFunc := newTestDB(t)
5555
defer cleanupFunc()
@@ -177,6 +177,41 @@ func TestIndex(t *testing.T) {
177177
checkItem(t, got, want)
178178
})
179179

180+
t.Run("has", func(t *testing.T) {
181+
want := Item{
182+
Address: []byte("has-hash"),
183+
Data: []byte("DATA"),
184+
StoreTimestamp: time.Now().UTC().UnixNano(),
185+
}
186+
187+
dontWant := Item{
188+
Address: []byte("do-not-has-hash"),
189+
Data: []byte("DATA"),
190+
StoreTimestamp: time.Now().UTC().UnixNano(),
191+
}
192+
193+
err := index.Put(want)
194+
if err != nil {
195+
t.Fatal(err)
196+
}
197+
198+
has, err := index.Has(want)
199+
if err != nil {
200+
t.Fatal(err)
201+
}
202+
if !has {
203+
t.Error("item is not found")
204+
}
205+
206+
has, err = index.Has(dontWant)
207+
if err != nil {
208+
t.Fatal(err)
209+
}
210+
if has {
211+
t.Error("unwanted item is found")
212+
}
213+
})
214+
180215
t.Run("delete", func(t *testing.T) {
181216
want := Item{
182217
Address: []byte("delete-hash"),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package localstore
18+
19+
import (
20+
"github.com/ethereum/go-ethereum/swarm/chunk"
21+
)
22+
23+
// Hasser provides Has method to retrieve Chunks
24+
// from database.
25+
type Hasser struct {
26+
db *DB
27+
}
28+
29+
// NewHasser returns a new Hasser on database.
30+
func (db *DB) NewHasser() *Hasser {
31+
return &Hasser{
32+
db: db,
33+
}
34+
}
35+
36+
// Has returns true if the chunk is stored in database.
37+
func (h *Hasser) Has(addr chunk.Address) (bool, error) {
38+
return h.db.retrievalDataIndex.Has(addressToItem(addr))
39+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package localstore
18+
19+
import (
20+
"testing"
21+
)
22+
23+
// TestHas validates that Hasser is returning true for
24+
// the stored chunk and false for one that is not stored.
25+
func TestHas(t *testing.T) {
26+
db, cleanupFunc := newTestDB(t, nil)
27+
defer cleanupFunc()
28+
29+
chunk := generateTestRandomChunk()
30+
31+
err := db.NewPutter(ModePutUpload).Put(chunk)
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
hasser := db.NewHasser()
37+
38+
has, err := hasser.Has(chunk.Address())
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
if !has {
43+
t.Error("chunk not found")
44+
}
45+
46+
missingChunk := generateTestRandomChunk()
47+
48+
has, err = hasser.Has(missingChunk.Address())
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
if has {
53+
t.Error("unexpected chunk is found")
54+
}
55+
}

0 commit comments

Comments
 (0)