Skip to content

Commit

Permalink
feat: add offset to ListLocalDealsPage
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Mar 25, 2022
1 parent c16479e commit b8c9c31
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
25 changes: 17 additions & 8 deletions storagemarket/impl/provider.go
Expand Up @@ -478,8 +478,8 @@ func (p *Provider) GetLocalDeal(propCid cid.Cid) (storagemarket.MinerDeal, error
return d, err
}

func (p *Provider) ListLocalDealsPage(offsetPropCid *cid.Cid, count int) ([]storagemarket.MinerDeal, error) {
if count == 0 {
func (p *Provider) ListLocalDealsPage(startPropCid *cid.Cid, offset int, limit int) ([]storagemarket.MinerDeal, error) {
if limit == 0 {
return []storagemarket.MinerDeal{}, nil
}

Expand All @@ -495,14 +495,23 @@ func (p *Provider) ListLocalDealsPage(offsetPropCid *cid.Cid, count int) ([]stor
})

// Iterate through deals until we reach the target signed proposal cid,
// then add deals from that point up to count
page := make([]storagemarket.MinerDeal, 0, count)
for _, dl := range deals {
if offsetPropCid == nil || dl.ProposalCid == *offsetPropCid {
// find the offset from there, then add deals from that point up to limit
page := make([]storagemarket.MinerDeal, 0, limit)
startIndex := -1
if startPropCid == nil {
startIndex = 0
}
for i, dl := range deals {
// Find the deal with a proposal cid matching startPropCid
if startPropCid != nil && dl.ProposalCid == *startPropCid {
// Start adding deals from offset after the first matching deal
startIndex = i + offset
}

if startIndex >= 0 && i >= startIndex {
page = append(page, dl)
offsetPropCid = nil // add all deals from this point on
}
if len(page) == count {
if len(page) == limit {
return page, nil
}
}
Expand Down
19 changes: 13 additions & 6 deletions storagemarket/impl/provider_test.go
Expand Up @@ -179,8 +179,8 @@ func TestProvider_Migrations(t *testing.T) {
require.NoError(t, err)
require.Equal(t, len(deals), count)

// Verify get a page of deals without a nil offset proposal cid
listedDeals, err := provider.ListLocalDealsPage(nil, len(deals))
// Verify get a page of deals without a nil start proposal cid
listedDeals, err := provider.ListLocalDealsPage(nil, 0, len(deals))
require.NoError(t, err)
require.Len(t, listedDeals, len(deals))
for i, dl := range listedDeals {
Expand All @@ -194,21 +194,28 @@ func TestProvider_Migrations(t *testing.T) {
secondDeal := listedDeals[1]
thirdDeal := listedDeals[2]

// Verify get a page of deals with a nil offset proposal cid and with a limit
listedDeals, err = provider.ListLocalDealsPage(nil, 2)
// Verify get a page of deals with a nil start proposal cid and with a limit
listedDeals, err = provider.ListLocalDealsPage(nil, 0, 2)
require.NoError(t, err)
require.Len(t, listedDeals, 2)
// Verify correct deals
require.Equal(t, firstDeal.ProposalCid, listedDeals[0].ProposalCid)
require.Equal(t, secondDeal.ProposalCid, listedDeals[1].ProposalCid)

// Verify get a page of deals with an offset proposal cid and with a limit
listedDeals, err = provider.ListLocalDealsPage(&secondDeal.ProposalCid, 2)
// Verify get a page of deals with a start proposal cid and with a limit
listedDeals, err = provider.ListLocalDealsPage(&secondDeal.ProposalCid, 0, 2)
require.NoError(t, err)
require.Len(t, listedDeals, 2)
// Verify correct deals
require.Equal(t, secondDeal.ProposalCid, listedDeals[0].ProposalCid)
require.Equal(t, thirdDeal.ProposalCid, listedDeals[1].ProposalCid)

// Verify get a page of deals with a start proposal cid, and offset and a limit
listedDeals, err = provider.ListLocalDealsPage(&secondDeal.ProposalCid, 1, 1)
require.NoError(t, err)
require.Len(t, listedDeals, 1)
// Verify correct deals
require.Equal(t, thirdDeal.ProposalCid, listedDeals[0].ProposalCid)
}

func TestHandleDealStream(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions storagemarket/provider.go
Expand Up @@ -46,9 +46,9 @@ type StorageProvider interface {
ListLocalDeals() ([]MinerDeal, error)

// ListLocalDealsPage lists deals by creation time descending, starting
// at the deal with the given signed proposal cid and returning up to
// count deals
ListLocalDealsPage(offsetPropCid *cid.Cid, count int) ([]MinerDeal, error)
// at the deal with the given signed proposal cid, skipping offset deals
// and returning up to limit deals
ListLocalDealsPage(offsetPropCid *cid.Cid, offset int, limit int) ([]MinerDeal, error)

// AddStorageCollateral adds storage collateral
AddStorageCollateral(ctx context.Context, amount abi.TokenAmount) error
Expand Down

0 comments on commit b8c9c31

Please sign in to comment.