github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logstore/driver/batchstoredriver/appender.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 batchstoredriver
    16  
    17  type fileAppender struct {
    18  	rfile         *rotateFile
    19  	capacity      int
    20  	size          int
    21  	tempPos       int
    22  	rollbackState *vFileState
    23  	syncWaited    *vFile
    24  	info          any //for address
    25  }
    26  
    27  func newFileAppender(rfile *rotateFile) *fileAppender {
    28  	appender := &fileAppender{
    29  		rfile: rfile,
    30  	}
    31  	return appender
    32  }
    33  
    34  func (appender *fileAppender) Prepare(size int, lsn uint64) (any, error) {
    35  	var err error
    36  	appender.capacity = size
    37  	appender.rfile.Lock()
    38  	defer appender.rfile.Unlock()
    39  	if appender.syncWaited, appender.rollbackState, err = appender.rfile.makeSpace(size); err != nil {
    40  		return nil, err
    41  	}
    42  	appender.tempPos = appender.rollbackState.bufPos
    43  	addr := &VFileAddress{
    44  		LSN:     lsn,
    45  		Version: appender.rollbackState.file.version,
    46  		Offset:  appender.rollbackState.pos,
    47  	}
    48  	appender.info = addr
    49  	// logutil.Infof("log %d-%d at %d-%d",v.Group,v.GroupLSN,appender.rollbackState.file.version,appender.rollbackState.pos)
    50  	// appender.activeId = appender.rfile.idAlloc.Alloc()
    51  	return addr, err
    52  }
    53  
    54  func (appender *fileAppender) Write(data []byte) (int, error) {
    55  	appender.size += len(data)
    56  	if appender.size > appender.capacity {
    57  		panic("write logic error")
    58  	}
    59  	// n := copy(appender.rollbackState.file.buf[appender.tempPos:], data)
    60  	// logutil.Infof("%p|write in buf[%v,%v]\n", appender, appender.tempPos, appender.tempPos+n)
    61  	// vf := appender.rollbackState.file
    62  	// logutil.Infof("%p|write vf in buf [%v,%v]\n", vf, vf.syncpos+appender.tempPos, vf.syncpos+appender.tempPos+n)
    63  	n, err := appender.rollbackState.file.WriteAt(data,
    64  		int64(appender.size-len(data)+appender.rollbackState.pos))
    65  	appender.tempPos += n
    66  	return n, err
    67  }
    68  
    69  func (appender *fileAppender) Commit() error {
    70  	err := appender.rollbackState.file.Log(appender.info)
    71  	// appender.rollbackState.file.bufpos = appender.tempPos
    72  	if err != nil {
    73  		return err
    74  	}
    75  	if appender.info == nil {
    76  		return nil
    77  	}
    78  	appender.rollbackState.file.FinishWrite()
    79  	return nil
    80  }