github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitower_writer.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 bitalosdb
    16  
    17  import (
    18  	"github.com/zuoyebang/bitalosdb/bitree"
    19  	"github.com/zuoyebang/bitalosdb/internal/base"
    20  	"github.com/zuoyebang/bitalosdb/internal/bitask"
    21  	"github.com/zuoyebang/bitalosdb/internal/consts"
    22  	"github.com/zuoyebang/bitalosdb/internal/utils"
    23  )
    24  
    25  type flushBitowerWriter struct {
    26  	s      *Bitower
    27  	writer *bitree.BitreeWriter
    28  	err    error
    29  }
    30  
    31  func (w *flushBitowerWriter) Set(key base.InternalKey, val []byte) error {
    32  	uk := key.UserKey
    33  	if len(uk) <= 0 {
    34  		return nil
    35  	}
    36  
    37  	if w.s.db.cache != nil {
    38  		cacheKeyHash := w.s.db.cache.GetKeyHash(key.UserKey)
    39  		switch key.Kind() {
    40  		case base.InternalKeyKindSet:
    41  			_ = w.s.db.cache.Set(key.UserKey, val, cacheKeyHash)
    42  		case base.InternalKeyKindDelete:
    43  			_ = w.s.db.cache.ExistAndDelete(key.UserKey, cacheKeyHash)
    44  		}
    45  	}
    46  
    47  	if err := w.writer.Apply(key.Clone(), val); err != nil {
    48  		w.err = err
    49  		return err
    50  	}
    51  	return nil
    52  }
    53  
    54  func (w *flushBitowerWriter) Finish() error {
    55  	if w.err != nil {
    56  		w.writer.UnrefBdbTx()
    57  		return w.err
    58  	}
    59  	return w.writer.Finish()
    60  }
    61  
    62  type flushWriter struct {
    63  	db      *DB
    64  	writers [consts.DefaultBitowerNum]*flushBitowerWriter
    65  }
    66  
    67  func (w *flushWriter) Set(key base.InternalKey, val []byte) error {
    68  	uk := key.UserKey
    69  	if len(uk) <= 0 {
    70  		return nil
    71  	}
    72  
    73  	index := w.db.getBitowerIndexByKey(uk)
    74  	return w.writers[index].Set(key, val)
    75  }
    76  
    77  func (w *flushWriter) Finish() error {
    78  	var err error
    79  
    80  	for i := range w.writers {
    81  		err = utils.FirstError(err, w.writers[i].Finish())
    82  	}
    83  
    84  	return err
    85  }
    86  
    87  func (d *DB) newFlushWriter() (*flushWriter, error) {
    88  	var err error
    89  	w := &flushWriter{
    90  		db: d,
    91  	}
    92  
    93  	for i, bitower := range d.bitowers {
    94  		w.writers[i], err = bitower.newFlushWriter()
    95  		if err != nil {
    96  			return nil, err
    97  		}
    98  	}
    99  
   100  	return w, nil
   101  }
   102  
   103  func (d *DB) doMemFlushTask(task *bitask.MemFlushTaskData) {
   104  	d.bitowers[task.Index].flush(task.NeedReport)
   105  }
   106  
   107  func (d *DB) doBitpageTask(task *bitask.BitpageTaskData) {
   108  	d.bitowers[task.Index].btree.DoBitpageTask(task)
   109  }