10467: Use BlockReader and BlockWriter interfaces instead of passing methods to get...
authorTom Clegg <tom@curoverse.com>
Fri, 30 Dec 2016 19:58:00 +0000 (14:58 -0500)
committerTom Clegg <tom@curoverse.com>
Fri, 30 Dec 2016 19:58:00 +0000 (14:58 -0500)
services/keepstore/pipe_adapters.go
services/keepstore/volume.go
services/keepstore/volume_unix.go

index 0b3999c1966d77605975b349900ce8427de1cc70..3cb01f14a128d0b88e6696aa32886a8dbac3380d 100644 (file)
@@ -10,10 +10,10 @@ import (
 // getWithPipe invokes getter and copies the resulting data into
 // buf. If ctx is done before all data is copied, getWithPipe closes
 // the pipe with an error, and returns early with an error.
-func getWithPipe(ctx context.Context, loc string, buf []byte, getter func(context.Context, string, io.Writer) error) (int, error) {
+func getWithPipe(ctx context.Context, loc string, buf []byte, br BlockReader) (int, error) {
        piper, pipew := io.Pipe()
        go func() {
-               pipew.CloseWithError(getter(ctx, loc, pipew))
+               pipew.CloseWithError(br.ReadBlock(ctx, loc, pipew))
        }()
        done := make(chan struct{})
        var size int
@@ -39,7 +39,7 @@ func getWithPipe(ctx context.Context, loc string, buf []byte, getter func(contex
 // from buf into the pipe. If ctx is done before all data is copied,
 // putWithPipe closes the pipe with an error, and returns early with
 // an error.
-func putWithPipe(ctx context.Context, loc string, buf []byte, putter func(context.Context, string, io.Reader) error) error {
+func putWithPipe(ctx context.Context, loc string, buf []byte, bw BlockWriter) error {
        piper, pipew := io.Pipe()
        copyErr := make(chan error)
        go func() {
@@ -50,7 +50,7 @@ func putWithPipe(ctx context.Context, loc string, buf []byte, putter func(contex
 
        putErr := make(chan error, 1)
        go func() {
-               putErr <- putter(ctx, loc, piper)
+               putErr <- bw.WriteBlock(ctx, loc, piper)
                close(putErr)
        }()
 
index b72258d51a5e6358c190227db08d5bf8c419dfe4..778f27fcde87cbc324a246aec571b3fe7a5c2b8a 100644 (file)
@@ -7,6 +7,18 @@ import (
        "time"
 )
 
+type BlockWriter interface {
+       // WriteBlock reads all data from r, writes it to a backing
+       // store as "loc", and returns the number of bytes written.
+       WriteBlock(ctx context.Context, loc string, r io.Reader) error
+}
+
+type BlockReader interface {
+       // ReadBlock retrieves data previously stored as "loc" and
+       // writes it to w.
+       ReadBlock(ctx context.Context, loc string, w io.Writer) error
+}
+
 // A Volume is an interface representing a Keep back-end storage unit:
 // for example, a single mounted disk, a RAID array, an Amazon S3 volume,
 // etc.
index 52fdad34ab3da282dc763a7b816d17678dcc3a2f..234eec1d05a1a3babf3cd3c76a5a0841e7803b26 100644 (file)
@@ -213,10 +213,11 @@ func (v *UnixVolume) stat(path string) (os.FileInfo, error) {
 // Get retrieves a block, copies it to the given slice, and returns
 // the number of bytes copied.
 func (v *UnixVolume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
-       return getWithPipe(ctx, loc, buf, v.get)
+       return getWithPipe(ctx, loc, buf, v)
 }
 
-func (v *UnixVolume) get(ctx context.Context, loc string, w io.Writer) error {
+// ReadBlock implements BlockReader.
+func (v *UnixVolume) ReadBlock(ctx context.Context, loc string, w io.Writer) error {
        path := v.blockPath(loc)
        stat, err := v.stat(path)
        if err != nil {
@@ -249,10 +250,11 @@ func (v *UnixVolume) Compare(ctx context.Context, loc string, expect []byte) err
 // returns a FullError.  If the write fails due to some other error,
 // that error is returned.
 func (v *UnixVolume) Put(ctx context.Context, loc string, block []byte) error {
-       return putWithPipe(ctx, loc, block, v.put)
+       return putWithPipe(ctx, loc, block, v)
 }
 
-func (v *UnixVolume) put(ctx context.Context, loc string, rdr io.Reader) error {
+// ReadBlock implements BlockWriter.
+func (v *UnixVolume) WriteBlock(ctx context.Context, loc string, rdr io.Reader) error {
        if v.ReadOnly {
                return MethodDisabledError
        }