github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/objstorage/objstorageprovider/shared_writable.go (about) 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 func NewRemoteWritable(obj io.WriteCloser) objstorage.Writable { 15 return &sharedWritable{storageWriter: obj} 16 } 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 func (w *sharedWritable) Write(p []byte) error { 32 _, err := w.storageWriter.Write(p) 33 return err 34 } 35 36 // Finish is part of the Writable interface. 37 func (w *sharedWritable) Finish() error { 38 err := w.storageWriter.Close() 39 w.storageWriter = nil 40 if err != nil { 41 w.Abort() 42 return err 43 } 44 45 // Create the marker object. 46 if w.p != nil { 47 if err := w.p.sharedCreateRef(w.meta); err != nil { 48 w.Abort() 49 return err 50 } 51 } 52 return nil 53 } 54 55 // Abort is part of the Writable interface. 56 func (w *sharedWritable) Abort() { 57 if w.storageWriter != nil { 58 _ = w.storageWriter.Close() 59 w.storageWriter = nil 60 } 61 if w.p != nil { 62 w.p.removeMetadata(w.meta.DiskFileNum) 63 } 64 // TODO(radu): delete the object if it was created. 65 }