2960: Buffer reads when serialize enabled on unix volume.
[arvados.git] / services / keepstore / streamwriterat.go
index 365b55f233febb040f8faeb969fd0d257c804860..3426dadc1ffea0a6f2e8a0c850d29c1275dbd88a 100644 (file)
@@ -19,10 +19,16 @@ import (
 // streamWriterAt writes the data to the provided io.Writer in
 // sequential order.
 //
+// streamWriterAt can also be used as an asynchronous buffer: the
+// caller can use the io.Writer interface to write into a memory
+// buffer and return without waiting for the wrapped writer to catch
+// up.
+//
 // Close returns when all data has been written through.
 type streamWriterAt struct {
        writer     io.Writer
        buf        []byte
+       writepos   int         // target offset if Write is called
        partsize   int         // size of each part written through to writer
        endpos     int         // portion of buf actually used, judging by WriteAt calls so far
        partfilled []int       // number of bytes written to each part so far
@@ -81,6 +87,13 @@ func (swa *streamWriterAt) writeToWriter() {
        }
 }
 
+// Write implements io.Writer.
+func (swa *streamWriterAt) Write(p []byte) (int, error) {
+       n, err := swa.WriteAt(p, int64(swa.writepos))
+       swa.writepos += n
+       return n, err
+}
+
 // WriteAt implements io.WriterAt.
 func (swa *streamWriterAt) WriteAt(p []byte, offset int64) (int, error) {
        pos := int(offset)