-
Notifications
You must be signed in to change notification settings - Fork 110
chunk, shed, storage: chunk.Store GetMulti method #1691
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2019 The Swarm 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 <http://www.gnu.org/licenses/>. | ||
|
||
package localstore | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/metrics" | ||
"github.com/ethersphere/swarm/chunk" | ||
"github.com/ethersphere/swarm/shed" | ||
"github.com/syndtr/goleveldb/leveldb" | ||
) | ||
|
||
// GetMulti returns chunks from the database. If one of the chunks is not found | ||
// chunk.ErrChunkNotFound will be returned. All required indexes will be updated | ||
// required by the Getter Mode. GetMulti is required to implement chunk.Store | ||
// interface. | ||
func (db *DB) GetMulti(ctx context.Context, mode chunk.ModeGet, addrs ...chunk.Address) (chunks []chunk.Chunk, err error) { | ||
metricName := fmt.Sprintf("localstore.GetMulti.%s", mode) | ||
|
||
metrics.GetOrRegisterCounter(metricName, nil).Inc(1) | ||
defer totalTimeMetric(metricName, time.Now()) | ||
|
||
defer func() { | ||
if err != nil { | ||
metrics.GetOrRegisterCounter(metricName+".error", nil).Inc(1) | ||
} | ||
}() | ||
|
||
out, err := db.getMulti(mode, addrs...) | ||
if err != nil { | ||
if err == leveldb.ErrNotFound { | ||
return nil, chunk.ErrChunkNotFound | ||
} | ||
return nil, err | ||
} | ||
chunks = make([]chunk.Chunk, len(out)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there might be cases where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @acud. Actually, len(out) is equal to len(addrs) as out is constructed on line 63 in getMulti with len(addrs) as length. I like your idea of passing nils for not found chunks. I am also interested in other opinions, as this would require changing shed.Index.Fill to handle []*Item instead []Item, but Item is used with value semantics. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK I missed the fact they are of equal length :) |
||
for i, ch := range out { | ||
chunks[i] = chunk.NewChunk(ch.Address, ch.Data).WithPinCounter(ch.PinCounter) | ||
} | ||
return chunks, nil | ||
} | ||
|
||
// getMulti returns Items from the retrieval index | ||
// and updates other indexes. | ||
func (db *DB) getMulti(mode chunk.ModeGet, addrs ...chunk.Address) (out []shed.Item, err error) { | ||
out = make([]shed.Item, len(addrs)) | ||
for i, addr := range addrs { | ||
out[i].Address = addr | ||
} | ||
|
||
err = db.retrievalDataIndex.Fill(out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch mode { | ||
// update the access timestamp and gc index | ||
case chunk.ModeGetRequest: | ||
db.updateGCItems(out...) | ||
|
||
case chunk.ModeGetPin: | ||
err := db.pinIndex.Fill(out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// no updates to indexes | ||
case chunk.ModeGetSync: | ||
case chunk.ModeGetLookup: | ||
default: | ||
return out, ErrInvalidMode | ||
} | ||
return out, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this assumes that all requested chunks are in the DB. Does this account for the case where GC was run in between and
value
is not found in the db? only by returning an error right? I guess this is also a feasible approachThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if the chunk is missing the error will be returned.
As the snapshot is acquired, gc will not influence this result if it runs while this function is called. The chunk will be returned in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool 👍 let's keep it so then. I think for now it would also make error handling more clear and easy on the calling context