Line data Source code
1 : // Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 : // of this source code is governed by a BSD-style license that can be found in 3 : // the LICENSE file. 4 : 5 : package objstorageprovider 6 : 7 : import ( 8 : "io" 9 : 10 : "github.com/cockroachdb/pebble/objstorage" 11 : ) 12 : 13 : // NewRemoteWritable creates an objstorage.Writable out of an io.WriteCloser. 14 1 : func NewRemoteWritable(obj io.WriteCloser) objstorage.Writable { 15 1 : return &sharedWritable{storageWriter: obj} 16 1 : } 17 : 18 : // sharedWritable is a very simple implementation of Writable on top of the 19 : // WriteCloser returned by remote.Storage.CreateObject. 20 : type sharedWritable struct { 21 : // Either both p and meta must be unset / zero values, or both must be set. 22 : // The case where both are unset is true only in tests. 23 : p *provider 24 : meta objstorage.ObjectMetadata 25 : storageWriter io.WriteCloser 26 : } 27 : 28 : var _ objstorage.Writable = (*sharedWritable)(nil) 29 : 30 : // Write is part of the Writable interface. 31 2 : func (w *sharedWritable) Write(p []byte) error { 32 2 : _, err := w.storageWriter.Write(p) 33 2 : return err 34 2 : } 35 : 36 : // Finish is part of the Writable interface. 37 2 : func (w *sharedWritable) Finish() error { 38 2 : err := w.storageWriter.Close() 39 2 : w.storageWriter = nil 40 2 : if err != nil { 41 0 : w.Abort() 42 0 : return err 43 0 : } 44 : 45 : // Create the marker object. 46 2 : if w.p != nil { 47 2 : if err := w.p.sharedCreateRef(w.meta); err != nil { 48 0 : w.Abort() 49 0 : return err 50 0 : } 51 : } 52 2 : return nil 53 : } 54 : 55 : // Abort is part of the Writable interface. 56 0 : func (w *sharedWritable) Abort() { 57 0 : if w.storageWriter != nil { 58 0 : _ = w.storageWriter.Close() 59 0 : w.storageWriter = nil 60 0 : } 61 0 : if w.p != nil { 62 0 : w.p.removeMetadata(w.meta.DiskFileNum) 63 0 : } 64 : // TODO(radu): delete the object if it was created. 65 : }