From eb22951d1ec2eed9f48fbfc6e23f1b7bce9d30a0 Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Mon, 23 Aug 2021 17:20:07 +0800 Subject: [PATCH] feat: Implement IoCallback support (#23) * feat: Implement IoCallback support Signed-off-by: Xuanwo * Fix build Signed-off-by: Xuanwo --- go.sum | 1 + storage.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/go.sum b/go.sum index 817cccf..842c233 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,7 @@ github.com/dave/rebecca v0.9.1/go.mod h1:N6XYdMD/OKw3lkF3ywh8Z6wPGuwNFDNtWYEMFWE github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/google/pprof v0.0.0-20181127221834-b4f47329b966/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= diff --git a/storage.go b/storage.go index 124765a..66e96c2 100644 --- a/storage.go +++ b/storage.go @@ -5,6 +5,7 @@ import ( "io" "strings" + "github.com/beyondstorage/go-storage/v4/pkg/iowrap" "github.com/beyondstorage/go-storage/v4/services" . "github.com/beyondstorage/go-storage/v4/types" ) @@ -153,6 +154,9 @@ func (s *Storage) read(ctx context.Context, path string, w io.Writer, opt pairSt if opt.HasOffset { offset = opt.Offset } + if opt.HasIoCallback { + w = iowrap.CallbackWriter(w, opt.IoCallback) + } written, err := w.Write(o.data[offset:]) if err != nil { @@ -184,6 +188,10 @@ func (s *Storage) write(ctx context.Context, path string, r io.Reader, size int6 o.mode = ModeRead o.data = make([]byte, size) + if opt.HasIoCallback { + r = iowrap.CallbackReader(r, opt.IoCallback) + } + // TODO: we need to add integration tests for this case. read, err := r.Read(o.data) // Update o.length even after read met error. @@ -205,6 +213,7 @@ func (s *Storage) writeAppend(ctx context.Context, o *Object, r io.Reader, size return 0, services.ErrObjectModeInvalid } } + buf := make([]byte, size) read, err := r.Read(buf) ro.data = append(ro.data, buf[:read]...)