Skip to content

Commit

Permalink
Added listFailedPushesAsString (#149)
Browse files Browse the repository at this point in the history
* Added listFailedPushesAsString

* added a cehck to make sure i.links is not nil
ehsan6sha authored Jul 10, 2023
1 parent 56da8f9 commit 5ddf929
Showing 3 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mobile/client.go
Original file line number Diff line number Diff line change
@@ -194,6 +194,14 @@ func (c *Client) ListFailedPushes() (*LinkIterator, error) {
return &LinkIterator{links: links}, nil
}

func (c *Client) ListFailedPushesAsString() (*StringIterator, error) {
links, err := c.listFailedPushesAsString(context.TODO())
if err != nil {
return nil, err
}
return &StringIterator{links: links}, nil
}

// RetryFailedPushes retries pushing all links that failed to push.
// The retry is disrupted as soon as a failure occurs.
// See ListFailedPushes.
24 changes: 24 additions & 0 deletions mobile/link_iter.go
Original file line number Diff line number Diff line change
@@ -27,3 +27,27 @@ func (i *LinkIterator) Next() ([]byte, error) {
}
return next.(cidlink.Link).Bytes(), nil
}

type StringIterator struct {
links []string
offset int
}

func (i *StringIterator) HasNext() bool {
if i.links == nil {
return false
}
return i.offset < len(i.links)
}

func (i *StringIterator) Next() (string, error) {
if i.links == nil || !i.HasNext() {
return "", errors.New("no more items")
}
next := i.links[i.offset]
if next == "" {
return "", errors.New("no more items")
}
i.offset++
return next, nil
}
28 changes: 28 additions & 0 deletions mobile/store.go
Original file line number Diff line number Diff line change
@@ -49,3 +49,31 @@ func (c *Client) listFailedPushes(ctx context.Context) ([]ipld.Link, error) {
}
return links, nil
}

func (c *Client) listFailedPushesAsString(ctx context.Context) ([]string, error) {
q := query.Query{
KeysOnly: true,
Prefix: failedPushKeyPrefix.String(),
}
results, err := c.ds.Query(ctx, q)
if err != nil {
return nil, err
}
defer results.Close()
var links []string
for r := range results.Next() {
if ctx.Err() != nil {
return nil, ctx.Err()
}
if r.Error != nil {
return nil, r.Error
}
key := datastore.RawKey(r.Key)
c, err := cid.Decode(key.BaseNamespace())
if err != nil {
return nil, err
}
links = append(links, c.String())
}
return links, nil
}

0 comments on commit 5ddf929

Please sign in to comment.