github.com/nutsdb/nutsdb@v1.0.4/rwmanger_mmap.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  	"errors"
    19  
    20  	mmap "github.com/xujiajun/mmap-go"
    21  )
    22  
    23  // MMapRWManager represents the RWManager which using mmap.
    24  type MMapRWManager struct {
    25  	path        string
    26  	fdm         *fdManager
    27  	m           mmap.MMap
    28  	segmentSize int64
    29  }
    30  
    31  var (
    32  	// ErrUnmappedMemory is returned when a function is called on unmapped memory
    33  	ErrUnmappedMemory = errors.New("unmapped memory")
    34  
    35  	// ErrIndexOutOfBound is returned when given offset out of mapped region
    36  	ErrIndexOutOfBound = errors.New("offset out of mapped region")
    37  )
    38  
    39  // WriteAt copies data to mapped region from the b slice starting at
    40  // given off and returns number of bytes copied to the mapped region.
    41  func (mm *MMapRWManager) WriteAt(b []byte, off int64) (n int, err error) {
    42  	if mm.m == nil {
    43  		return 0, ErrUnmappedMemory
    44  	} else if off >= int64(len(mm.m)) || off < 0 {
    45  		return 0, ErrIndexOutOfBound
    46  	}
    47  
    48  	return copy(mm.m[off:], b), nil
    49  }
    50  
    51  // ReadAt copies data to b slice from mapped region starting at
    52  // given off and returns number of bytes copied to the b slice.
    53  func (mm *MMapRWManager) ReadAt(b []byte, off int64) (n int, err error) {
    54  	if mm.m == nil {
    55  		return 0, ErrUnmappedMemory
    56  	} else if off >= int64(len(mm.m)) || off < 0 {
    57  		return 0, ErrIndexOutOfBound
    58  	}
    59  
    60  	return copy(b, mm.m[off:]), nil
    61  }
    62  
    63  // Sync synchronizes the mapping's contents to the file's contents on disk.
    64  func (mm *MMapRWManager) Sync() (err error) {
    65  	return mm.m.Flush()
    66  }
    67  
    68  // Release deletes the memory mapped region, flushes any remaining changes
    69  func (mm *MMapRWManager) Release() (err error) {
    70  	mm.fdm.reduceUsing(mm.path)
    71  	return mm.m.Unmap()
    72  }
    73  
    74  func (mm *MMapRWManager) Size() int64 {
    75  	return mm.segmentSize
    76  }
    77  
    78  // Close will remove the cache in the fdm of the specified path, and call the close method of the os of the file
    79  func (mm *MMapRWManager) Close() (err error) {
    80  	return mm.fdm.closeByPath(mm.path)
    81  }