-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Properly Implement Retrieval Lookups Based on CIDs #57
Changes from 5 commits
39a4f78
4ca08b5
b0d3c0c
9a31d7a
9211de0
7b74974
d94a1cf
582fa86
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 |
---|---|---|
@@ -1,87 +1,126 @@ | ||
package piecestore | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/filecoin-project/go-statestore" | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-datastore/namespace" | ||
) | ||
|
||
var DSPrefix = "/storagemarket/pieces" | ||
// DSPiecePrefix is the name space for storing piece infos | ||
var DSPiecePrefix = "/storagemarket/pieces" | ||
|
||
// DSCIDPrefix is the name space for storing CID infos | ||
var DSCIDPrefix = "/storagemarket/cid-infos" | ||
|
||
// NewPieceStore returns a new piecestore based on the given datastore | ||
func NewPieceStore(ds datastore.Batching) PieceStore { | ||
return &pieceStore{ | ||
store: statestore.New(namespace.Wrap(ds, datastore.NewKey(DSPrefix))), | ||
pieces: statestore.New(namespace.Wrap(ds, datastore.NewKey(DSPiecePrefix))), | ||
cidInfos: statestore.New(namespace.Wrap(ds, datastore.NewKey(DSCIDPrefix))), | ||
} | ||
} | ||
|
||
type pieceStore struct { | ||
store *statestore.StateStore | ||
pieces *statestore.StateStore | ||
cidInfos *statestore.StateStore | ||
} | ||
|
||
func (ps *pieceStore) AddDealForPiece(pieceCID []byte, dealInfo DealInfo) error { | ||
// Do we need to de-dupe or anything here? | ||
return ps.mutatePieceInfo(pieceCID, func(pi *PieceInfo) error { | ||
for _, di := range pi.Deals { | ||
if di == dealInfo { | ||
return nil | ||
} | ||
} | ||
pi.Deals = append(pi.Deals, dealInfo) | ||
return nil | ||
}) | ||
} | ||
|
||
func (ps *pieceStore) AddBlockInfosToPiece(pieceCID []byte, blockInfos []BlockInfo) error { | ||
// Do we need to de-dupe or anything here? | ||
return ps.mutatePieceInfo(pieceCID, func(pi *PieceInfo) error { | ||
pi.Blocks = blockInfos | ||
return nil | ||
}) | ||
func (ps *pieceStore) AddPieceBlockLocations(pieceCID []byte, blockLocations map[cid.Cid]BlockLocation) error { | ||
for c, blockLocation := range blockLocations { | ||
err := ps.mutateCIDInfo(c, func(ci *CIDInfo) error { | ||
for _, pbl := range ci.PieceBlockLocations { | ||
if bytes.Equal(pbl.PieceCID, pieceCID) && pbl.BlockLocation == blockLocation { | ||
return nil | ||
} | ||
} | ||
ci.PieceBlockLocations = append(ci.PieceBlockLocations, PieceBlockLocation{blockLocation, pieceCID}) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (ps *pieceStore) HasBlockInfo(pieceCID []byte) (bool, error) { | ||
pi, err := ps.GetPieceInfo(pieceCID) | ||
if err != nil { | ||
return false, err | ||
func (ps *pieceStore) GetPieceInfo(pieceCID []byte) (PieceInfo, error) { | ||
var out PieceInfo | ||
if err := ps.pieces.Get(newKey(pieceCID)).Get(&out); err != nil { | ||
return PieceInfo{}, err | ||
} | ||
|
||
return len(pi.Blocks) > 0, err | ||
return out, nil | ||
} | ||
|
||
func (ps *pieceStore) HasDealInfo(pieceCID []byte) (bool, error) { | ||
pi, err := ps.GetPieceInfo(pieceCID) | ||
if err != nil { | ||
return false, err | ||
func (ps *pieceStore) GetCIDInfo(payloadCID cid.Cid) (CIDInfo, error) { | ||
var out CIDInfo | ||
if err := ps.cidInfos.Get(payloadCID).Get(&out); err != nil { | ||
return CIDInfo{}, err | ||
} | ||
|
||
return len(pi.Deals) > 0, nil | ||
return out, nil | ||
} | ||
|
||
func (ps *pieceStore) GetPieceInfo(pieceCID []byte) (PieceInfo, error) { | ||
var out PieceInfo | ||
if err := ps.store.Get(newKey(pieceCID)).Get(&out); err != nil { | ||
return PieceInfo{}, err | ||
func (ps *pieceStore) ensurePieceInfo(pieceCID []byte) error { | ||
has, err := ps.pieces.Has(newKey(pieceCID)) | ||
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. Maybe call it canHasPieceInfo /jk |
||
|
||
if err != nil { | ||
return err | ||
} | ||
return out, nil | ||
if has { | ||
return nil | ||
} | ||
|
||
pieceInfo := PieceInfo{PieceCID: pieceCID} | ||
return ps.pieces.Begin(newKey(pieceCID), &pieceInfo) | ||
} | ||
|
||
func (ps *pieceStore) ensurePieceInfo(pieceCID []byte) (PieceInfo, error) { | ||
pieceInfo, err := ps.GetPieceInfo(pieceCID) | ||
func (ps *pieceStore) ensureCIDInfo(c cid.Cid) error { | ||
has, err := ps.cidInfos.Has(c) | ||
|
||
if err == nil { | ||
return pieceInfo, nil | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pieceInfo = PieceInfo{PieceCID: pieceCID} | ||
err = ps.store.Begin(newKey(pieceCID), &pieceInfo) | ||
if has { | ||
return nil | ||
} | ||
|
||
return pieceInfo, err | ||
cidInfo := CIDInfo{CID: c} | ||
return ps.cidInfos.Begin(c, &cidInfo) | ||
} | ||
|
||
func (ps *pieceStore) mutatePieceInfo(pieceCID []byte, mutator interface{}) error { | ||
_, err := ps.ensurePieceInfo(pieceCID) | ||
err := ps.ensurePieceInfo(pieceCID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ps.pieces.Get(newKey(pieceCID)).Mutate(mutator) | ||
} | ||
|
||
func (ps *pieceStore) mutateCIDInfo(c cid.Cid, mutator interface{}) error { | ||
err := ps.ensureCIDInfo(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ps.store.Get(newKey(pieceCID)).Mutate(mutator) | ||
return ps.cidInfos.Get(c).Mutate(mutator) | ||
} | ||
|
||
func newKey(pieceCID []byte) fmt.Stringer { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,51 +9,164 @@ import ( | |
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/filecoin-project/go-fil-markets/piecestore" | ||
"github.com/filecoin-project/go-fil-markets/shared_testutil" | ||
) | ||
|
||
func TestStorePieceInfo(t *testing.T) { | ||
ps := piecestore.NewPieceStore(datastore.NewMapDatastore()) | ||
|
||
pieceCid := []byte{1, 2, 3, 4} | ||
initializePieceStore := func(t *testing.T) piecestore.PieceStore { | ||
ps := piecestore.NewPieceStore(datastore.NewMapDatastore()) | ||
_, err := ps.GetPieceInfo(pieceCid) | ||
assert.Error(t, err) | ||
return ps | ||
} | ||
|
||
// Add a deal info | ||
t.Run("can add deals", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
dealInfo := piecestore.DealInfo{ | ||
DealID: rand.Uint64(), | ||
SectorID: rand.Uint64(), | ||
Offset: rand.Uint64(), | ||
Length: rand.Uint64(), | ||
} | ||
err := ps.AddDealForPiece(pieceCid, dealInfo) | ||
assert.NoError(t, err) | ||
|
||
pi, err := ps.GetPieceInfo(pieceCid) | ||
assert.NoError(t, err) | ||
assert.Len(t, pi.Deals, 1) | ||
assert.Equal(t, pi.Deals[0], dealInfo) | ||
}) | ||
|
||
t.Run("adding same deal twice does not dup", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
dealInfo := piecestore.DealInfo{ | ||
DealID: rand.Uint64(), | ||
SectorID: rand.Uint64(), | ||
Offset: rand.Uint64(), | ||
Length: rand.Uint64(), | ||
} | ||
err := ps.AddDealForPiece(pieceCid, dealInfo) | ||
assert.NoError(t, err) | ||
|
||
pi, err := ps.GetPieceInfo(pieceCid) | ||
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. nit: I should think you could skip the error/value checking on the first try since it's tested above. 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. I just follow past of least resistance to get no lint-stage errors |
||
assert.NoError(t, err) | ||
assert.Len(t, pi.Deals, 1) | ||
assert.Equal(t, pi.Deals[0], dealInfo) | ||
|
||
err = ps.AddDealForPiece(pieceCid, dealInfo) | ||
assert.NoError(t, err) | ||
|
||
pi, err = ps.GetPieceInfo(pieceCid) | ||
assert.NoError(t, err) | ||
assert.Len(t, pi.Deals, 1) | ||
assert.Equal(t, pi.Deals[0], dealInfo) | ||
}) | ||
} | ||
|
||
func TestStoreCIDInfo(t *testing.T) { | ||
|
||
pieceCid1 := []byte{1, 2, 3, 4} | ||
pieceCid2 := []byte{5, 6, 7, 8} | ||
testCIDs := shared_testutil.GenerateCids(3) | ||
blockLocations := make([]piecestore.BlockLocation, 0, 3) | ||
for i := 0; i < 3; i++ { | ||
blockLocations = append(blockLocations, piecestore.BlockLocation{ | ||
RelOffset: rand.Uint64(), | ||
BlockSize: rand.Uint64(), | ||
}) | ||
} | ||
|
||
_, err := ps.GetPieceInfo(pieceCid) | ||
assert.Error(t, err) | ||
|
||
// Add a PieceInfo and some state | ||
testCid, err := cid.Decode("bafzbeigai3eoy2ccc7ybwjfz5r3rdxqrinwi4rwytly24tdbh6yk7zslrm") | ||
assert.NoError(t, err) | ||
blockInfos := []piecestore.BlockInfo{{testCid, 42, 43}} | ||
|
||
err = ps.AddBlockInfosToPiece(pieceCid, blockInfos) | ||
assert.NoError(t, err) | ||
has, err := ps.HasBlockInfo(pieceCid) | ||
assert.True(t, has) | ||
assert.NoError(t, err) | ||
has, err = ps.HasDealInfo(pieceCid) | ||
assert.False(t, has) | ||
assert.NoError(t, err) | ||
|
||
pi, err := ps.GetPieceInfo(pieceCid) | ||
assert.NoError(t, err) | ||
assert.Len(t, pi.Blocks, 1) | ||
assert.Equal(t, pi.Blocks[0], piecestore.BlockInfo{testCid, 42, 43}) | ||
|
||
dealInfo := piecestore.DealInfo{ | ||
DealID: rand.Uint64(), | ||
SectorID: rand.Uint64(), | ||
Offset: rand.Uint64(), | ||
Length: rand.Uint64(), | ||
initializePieceStore := func(t *testing.T) piecestore.PieceStore { | ||
ps := piecestore.NewPieceStore(datastore.NewMapDatastore()) | ||
_, err := ps.GetCIDInfo(testCIDs[0]) | ||
assert.Error(t, err) | ||
return ps | ||
} | ||
err = ps.AddDealForPiece(pieceCid, dealInfo) | ||
assert.NoError(t, err) | ||
|
||
has, err = ps.HasBlockInfo(pieceCid) | ||
assert.True(t, has) | ||
assert.NoError(t, err) | ||
has, err = ps.HasDealInfo(pieceCid) | ||
assert.True(t, has) | ||
assert.NoError(t, err) | ||
pi, err = ps.GetPieceInfo(pieceCid) | ||
assert.NoError(t, err) | ||
assert.Len(t, pi.Deals, 1) | ||
assert.Equal(t, pi.Deals[0], dealInfo) | ||
|
||
t.Run("can add piece block locations", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
err := ps.AddPieceBlockLocations(pieceCid1, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[0]: blockLocations[0], | ||
testCIDs[1]: blockLocations[1], | ||
testCIDs[2]: blockLocations[2], | ||
}) | ||
assert.NoError(t, err) | ||
|
||
ci, err := ps.GetCIDInfo(testCIDs[0]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[0], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[1]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[1], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[2]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[2], pieceCid1}) | ||
}) | ||
|
||
t.Run("overlapping adds", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
err := ps.AddPieceBlockLocations(pieceCid1, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[0]: blockLocations[0], | ||
testCIDs[1]: blockLocations[2], | ||
}) | ||
assert.NoError(t, err) | ||
err = ps.AddPieceBlockLocations(pieceCid2, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[1]: blockLocations[1], | ||
testCIDs[2]: blockLocations[2], | ||
}) | ||
assert.NoError(t, err) | ||
|
||
ci, err := ps.GetCIDInfo(testCIDs[0]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[0], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[1]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 2) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[2], pieceCid1}) | ||
assert.Equal(t, ci.PieceBlockLocations[1], piecestore.PieceBlockLocation{blockLocations[1], pieceCid2}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[2]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[2], pieceCid2}) | ||
}) | ||
|
||
t.Run("duplicate adds", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
err := ps.AddPieceBlockLocations(pieceCid1, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[0]: blockLocations[0], | ||
testCIDs[1]: blockLocations[1], | ||
}) | ||
assert.NoError(t, err) | ||
err = ps.AddPieceBlockLocations(pieceCid1, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[1]: blockLocations[1], | ||
testCIDs[2]: blockLocations[2], | ||
}) | ||
assert.NoError(t, err) | ||
|
||
ci, err := ps.GetCIDInfo(testCIDs[0]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[0], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[1]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[1], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[2]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[2], pieceCid1}) | ||
}) | ||
} |
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.
Guess we can remove this comment now.
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.
ok!