github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/meta_set.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  	"sync/atomic"
    19  
    20  	"github.com/zuoyebang/bitalosdb/internal/base"
    21  	"github.com/zuoyebang/bitalosdb/internal/manifest"
    22  )
    23  
    24  type metaEdit = manifest.MetaEditor
    25  type bitowerMetaEditor = manifest.BitowerMetaEditor
    26  
    27  type metaSet struct {
    28  	atomic struct {
    29  		logSeqNum     uint64
    30  		visibleSeqNum uint64
    31  	}
    32  
    33  	dirname string
    34  	opts    *Options
    35  	meta    *manifest.Metadata
    36  }
    37  
    38  func (ms *metaSet) init(dirname string, opts *Options) error {
    39  	manifestPath := base.MakeFilepath(opts.FS, dirname, fileTypeMeta, 0)
    40  	meta, err := manifest.NewMetadata(manifestPath, opts.FS)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	ms.dirname = dirname
    46  	ms.opts = opts
    47  	ms.meta = meta
    48  	ms.atomic.logSeqNum = 1
    49  	if meta.LastSeqNum != 0 {
    50  		ms.atomic.logSeqNum = meta.LastSeqNum + 1
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func (ms *metaSet) apply(sme *bitowerMetaEditor) error {
    57  	me := &metaEdit{}
    58  	logSeqNum := atomic.LoadUint64(&ms.atomic.logSeqNum)
    59  	me.LastSeqNum = logSeqNum - 1
    60  
    61  	ms.meta.Write(me, sme)
    62  
    63  	return nil
    64  }
    65  
    66  func (ms *metaSet) close() error {
    67  	err := ms.meta.Close()
    68  	return err
    69  }