|
| 1 | +// Copyright 2019 The Swarm 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 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/metrics" |
| 25 | + "github.com/ethersphere/swarm/chunk" |
| 26 | + "github.com/ethersphere/swarm/shed" |
| 27 | + "github.com/syndtr/goleveldb/leveldb" |
| 28 | +) |
| 29 | + |
| 30 | +// GetMulti returns chunks from the database. If one of the chunks is not found |
| 31 | +// chunk.ErrChunkNotFound will be returned. All required indexes will be updated |
| 32 | +// required by the Getter Mode. GetMulti is required to implement chunk.Store |
| 33 | +// interface. |
| 34 | +func (db *DB) GetMulti(ctx context.Context, mode chunk.ModeGet, addrs ...chunk.Address) (chunks []chunk.Chunk, err error) { |
| 35 | + metricName := fmt.Sprintf("localstore.GetMulti.%s", mode) |
| 36 | + |
| 37 | + metrics.GetOrRegisterCounter(metricName, nil).Inc(1) |
| 38 | + defer totalTimeMetric(metricName, time.Now()) |
| 39 | + |
| 40 | + defer func() { |
| 41 | + if err != nil { |
| 42 | + metrics.GetOrRegisterCounter(metricName+".error", nil).Inc(1) |
| 43 | + } |
| 44 | + }() |
| 45 | + |
| 46 | + out, err := db.getMulti(mode, addrs...) |
| 47 | + if err != nil { |
| 48 | + if err == leveldb.ErrNotFound { |
| 49 | + return nil, chunk.ErrChunkNotFound |
| 50 | + } |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + chunks = make([]chunk.Chunk, len(out)) |
| 54 | + for i, ch := range out { |
| 55 | + chunks[i] = chunk.NewChunk(ch.Address, ch.Data).WithPinCounter(ch.PinCounter) |
| 56 | + } |
| 57 | + return chunks, nil |
| 58 | +} |
| 59 | + |
| 60 | +// getMulti returns Items from the retrieval index |
| 61 | +// and updates other indexes. |
| 62 | +func (db *DB) getMulti(mode chunk.ModeGet, addrs ...chunk.Address) (out []shed.Item, err error) { |
| 63 | + out = make([]shed.Item, len(addrs)) |
| 64 | + for i, addr := range addrs { |
| 65 | + out[i].Address = addr |
| 66 | + } |
| 67 | + |
| 68 | + err = db.retrievalDataIndex.Fill(out) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + switch mode { |
| 74 | + // update the access timestamp and gc index |
| 75 | + case chunk.ModeGetRequest: |
| 76 | + db.updateGCItems(out...) |
| 77 | + |
| 78 | + case chunk.ModeGetPin: |
| 79 | + err := db.pinIndex.Fill(out) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + // no updates to indexes |
| 85 | + case chunk.ModeGetSync: |
| 86 | + case chunk.ModeGetLookup: |
| 87 | + default: |
| 88 | + return out, ErrInvalidMode |
| 89 | + } |
| 90 | + return out, nil |
| 91 | +} |
0 commit comments