github.com/nutsdb/nutsdb@v1.0.4/rwmanger_fileio.go (about)

     1  // Copyright 2019 The nutsdb Author. All rights reserved.
     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 nutsdb
    16  
    17  import (
    18  	"os"
    19  )
    20  
    21  // FileIORWManager represents the RWManager which using standard I/O.
    22  type FileIORWManager struct {
    23  	fd          *os.File
    24  	path        string
    25  	fdm         *fdManager
    26  	segmentSize int64
    27  }
    28  
    29  // WriteAt writes len(b) bytes to the File starting at byte offset off.
    30  // `WriteAt` is a wrapper of the *File.WriteAt.
    31  func (fm *FileIORWManager) WriteAt(b []byte, off int64) (n int, err error) {
    32  	return fm.fd.WriteAt(b, off)
    33  }
    34  
    35  // ReadAt reads len(b) bytes from the File starting at byte offset off.
    36  // `ReadAt` is a wrapper of the *File.ReadAt.
    37  func (fm *FileIORWManager) ReadAt(b []byte, off int64) (n int, err error) {
    38  	return fm.fd.ReadAt(b, off)
    39  }
    40  
    41  // Sync commits the current contents of the file to stable storage.
    42  // Typically, this means flushing the file system's in-memory copy
    43  // of recently written data to disk.
    44  // `Sync` is a wrapper of the *File.Sync.
    45  func (fm *FileIORWManager) Sync() (err error) {
    46  	return fm.fd.Sync()
    47  }
    48  
    49  // Release is a wrapper around the reduceUsing method
    50  func (fm *FileIORWManager) Release() (err error) {
    51  	fm.fdm.reduceUsing(fm.path)
    52  	return nil
    53  }
    54  
    55  func (fm *FileIORWManager) Size() int64 {
    56  	return fm.segmentSize
    57  }
    58  
    59  // Close will remove the cache in the fdm of the specified path, and call the close method of the os of the file
    60  func (fm *FileIORWManager) Close() (err error) {
    61  	return fm.fdm.closeByPath(fm.path)
    62  }