mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-10-22 01:47:24 +08:00
refactor memory mapped file into backend storage
This commit is contained in:
@@ -21,8 +21,6 @@ type MemoryMap struct {
|
||||
End_of_file int64
|
||||
}
|
||||
|
||||
var FileMemoryMap = make(map[string]*MemoryMap)
|
||||
|
||||
func ReadMemoryMapMaxSizeMb(memoryMapMaxSizeMbString string) (uint32, error) {
|
||||
if memoryMapMaxSizeMbString == "" {
|
||||
return 0, nil
|
||||
|
60
weed/storage/backend/memory_map/memory_map_backend.go
Normal file
60
weed/storage/backend/memory_map/memory_map_backend.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package memory_map
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/backend"
|
||||
)
|
||||
|
||||
var (
|
||||
_ backend.DataStorageBackend = &MemoryMappedFile{}
|
||||
)
|
||||
|
||||
type MemoryMappedFile struct {
|
||||
mm *MemoryMap
|
||||
}
|
||||
|
||||
func NewMemoryMappedFile(f *os.File, memoryMapSizeMB uint32) *MemoryMappedFile {
|
||||
mmf := &MemoryMappedFile{
|
||||
mm : new(MemoryMap),
|
||||
}
|
||||
mmf.mm.CreateMemoryMap(f, 1024*1024*uint64(memoryMapSizeMB))
|
||||
return mmf
|
||||
}
|
||||
|
||||
func (mmf *MemoryMappedFile) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
readBytes, e := mmf.mm.ReadMemory(uint64(off), uint64(len(p)))
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
// TODO avoid the extra copy
|
||||
copy(p, readBytes)
|
||||
return len(readBytes), nil
|
||||
}
|
||||
|
||||
func (mmf *MemoryMappedFile) WriteAt(p []byte, off int64) (n int, err error) {
|
||||
mmf.mm.WriteMemory(uint64(off), uint64(len(p)), p)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (mmf *MemoryMappedFile) Truncate(off int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mmf *MemoryMappedFile) Close() error {
|
||||
mmf.mm.DeleteFileAndMemoryMap()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mmf *MemoryMappedFile) GetStat() (datSize int64, modTime time.Time, err error) {
|
||||
stat, e := mmf.mm.File.Stat()
|
||||
if e == nil {
|
||||
return stat.Size(), stat.ModTime(), nil
|
||||
}
|
||||
return 0, time.Time{}, err
|
||||
}
|
||||
|
||||
func (mmf *MemoryMappedFile) String() string {
|
||||
return mmf.mm.File.Name()
|
||||
}
|
Reference in New Issue
Block a user