github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/buffer.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package objectio 16 17 import ( 18 "bytes" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/fileservice" 22 ) 23 24 // ObjectBuffer is the buffer prepared before writing to 25 // the object file, all data written to the object needs 26 // to be filled in it, and then written to the object 27 // file at one time 28 type ObjectBuffer struct { 29 buf *bytes.Buffer 30 vector fileservice.IOVector 31 } 32 33 func NewObjectBuffer(name string) *ObjectBuffer { 34 buffer := &ObjectBuffer{ 35 buf: new(bytes.Buffer), 36 vector: fileservice.IOVector{ 37 FilePath: name, 38 }, 39 } 40 buffer.vector.Entries = make([]fileservice.IOEntry, 0) 41 return buffer 42 } 43 44 func (b *ObjectBuffer) Write(buf []byte, items ...WriteOptions) (int, int) { 45 offset := int64(0) 46 le := len(b.vector.Entries) 47 if len(b.vector.Entries) > 0 { 48 offset = b.vector.Entries[le-1].Offset + 49 b.vector.Entries[le-1].Size 50 } 51 entry := fileservice.IOEntry{ 52 Offset: offset, 53 Size: int64(len(buf)), 54 Data: buf, 55 } 56 b.vector.Entries = append(b.vector.Entries, entry) 57 return int(offset), len(buf) 58 } 59 60 func (b *ObjectBuffer) Length() int { 61 return b.buf.Len() 62 } 63 64 func (b *ObjectBuffer) GetData() fileservice.IOVector { 65 return b.vector 66 } 67 68 func (b *ObjectBuffer) SetDataOptions(items ...WriteOptions) { 69 if len(items) == 0 { 70 return 71 } 72 for _, item := range items { 73 switch item.Type { 74 case WriteTS: 75 ts := item.Val.(time.Time) 76 b.vector.ExpireAt = ts 77 default: 78 continue 79 } 80 } 81 }