github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logstore/driver/entry/entry.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 entry
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/binary"
    20  	"io"
    21  	"os"
    22  	"sync"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/entry"
    25  )
    26  
    27  type Entry struct {
    28  	Entry entry.Entry
    29  	Info  *entry.Info //for wal in post append
    30  	Lsn   uint64
    31  	Ctx   any //for addr in batchstore
    32  	err   error
    33  	wg    *sync.WaitGroup
    34  }
    35  
    36  func NewEntry(e entry.Entry) *Entry {
    37  	en := &Entry{
    38  		Entry: e,
    39  		wg:    &sync.WaitGroup{},
    40  	}
    41  	en.wg.Add(1)
    42  	return en
    43  }
    44  func NewEmptyEntry() *Entry {
    45  	en := &Entry{
    46  		Entry: entry.GetBase(),
    47  		wg:    &sync.WaitGroup{},
    48  	}
    49  	en.wg.Add(1)
    50  	return en
    51  }
    52  func (e *Entry) SetInfo() {
    53  	info := e.Entry.GetInfo()
    54  	if info != nil {
    55  		e.Info = info.(*entry.Info)
    56  	}
    57  }
    58  func (e *Entry) ReadFrom(r io.Reader) (n int64, err error) {
    59  	if err = binary.Read(r, binary.BigEndian, &e.Lsn); err != nil {
    60  		return
    61  	}
    62  	_, err = e.Entry.ReadFrom(r)
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	e.Info = e.Entry.GetInfo().(*entry.Info)
    67  	return
    68  }
    69  
    70  func (e *Entry) ReadAt(r *os.File, offset int) (int, error) {
    71  	lsnbuf := make([]byte, 8)
    72  	n, err := r.ReadAt(lsnbuf, int64(offset))
    73  	if err != nil {
    74  		return n, err
    75  	}
    76  	offset += 8
    77  
    78  	bbuf := bytes.NewBuffer(lsnbuf)
    79  	if err := binary.Read(bbuf, binary.BigEndian, &e.Lsn); err != nil {
    80  		return n, err
    81  	}
    82  
    83  	n2, err := e.Entry.ReadAt(r, offset)
    84  	return n2 + n, err
    85  }
    86  
    87  func (e *Entry) WriteTo(w io.Writer) (int64, error) {
    88  	if err := binary.Write(w, binary.BigEndian, e.Lsn); err != nil {
    89  		return 0, err
    90  	}
    91  	n, err := e.Entry.WriteTo(w)
    92  	n += 8
    93  	return n, err
    94  }
    95  
    96  func (e *Entry) WaitDone() error {
    97  	e.wg.Wait()
    98  	return e.err
    99  }
   100  
   101  func (e *Entry) DoneWithErr(err error) {
   102  	e.err = err
   103  	info := e.Entry.GetInfo()
   104  	if info != nil {
   105  		e.Info = info.(*entry.Info)
   106  	}
   107  	e.wg.Done()
   108  }
   109  
   110  func (e *Entry) GetSize() int {
   111  	return e.Entry.TotalSize() + 8 //LSN
   112  }