Skip to content

Commit

Permalink
make delete idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Aug 21, 2019
1 parent 824440f commit e9032eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module github.com/ipfs/go-ds-s3

require (
github.com/aws/aws-sdk-go v1.15.60
github.com/ipfs/go-datastore v0.0.5
github.com/ipfs/go-datastore v0.1.0
github.com/ipfs/go-ipfs v0.4.22
google.golang.org/appengine v1.4.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ github.com/ipfs/go-datastore v0.0.3 h1:/eP3nMDmLzMJNoWSSYvEkmMTTrm9FFCN+JraP9Ndl
github.com/ipfs/go-datastore v0.0.3/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE=
github.com/ipfs/go-datastore v0.0.5 h1:q3OfiOZV5rlsK1H5V8benjeUApRfMGs4Mrhmr6NriQo=
github.com/ipfs/go-datastore v0.0.5/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE=
github.com/ipfs/go-datastore v0.1.0 h1:TOxI04l8CmO4zGtesENhzm4PwkFwJXY3rKiYaaMf9fI=
github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE=
github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk=
github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps=
github.com/ipfs/go-ds-badger v0.0.2 h1:7ToQt7QByBhOTuZF2USMv+PGlMcBC7FW7FdgQ4FCsoo=
Expand Down
19 changes: 16 additions & 3 deletions s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ func (s *S3Bucket) Delete(k ds.Key) error {
Bucket: aws.String(s.Bucket),
Key: aws.String(s.s3Path(k.String())),
})
return parseError(err)
if isNotFound(err) {
// delete is idempotent
err = nil
}
return err
}

func (s *S3Bucket) Query(q dsq.Query) (dsq.Results, error) {
Expand Down Expand Up @@ -223,8 +227,13 @@ func (s *S3Bucket) s3Path(p string) string {
return path.Join(s.RootDirectory, p)
}

func isNotFound(err error) bool {
s3Err, ok := err.(awserr.Error)
return ok && s3Err.Code() == s3.ErrCodeNoSuchKey
}

func parseError(err error) error {
if s3Err, ok := err.(awserr.Error); ok && s3Err.Code() == s3.ErrCodeNoSuchKey {
if isNotFound(err) {
return ds.ErrNotFound
}
return err
Expand Down Expand Up @@ -336,12 +345,16 @@ func (b *s3Batch) newDeleteJob(objs []*s3.ObjectIdentifier) func() error {
Objects: objs,
},
})
if err != nil {
if err != nil && !isNotFound(err) {
return err
}

var errs []string
for _, err := range resp.Errors {
if err.Code != nil && *err.Code == s3.ErrCodeNoSuchKey {
// idempotent
continue
}
errs = append(errs, err.String())
}

Expand Down

0 comments on commit e9032eb

Please sign in to comment.