10 changed files with 122 additions and 120 deletions
-
2weed/storage/backend/memory_map/memory_map.go
-
60weed/storage/backend/memory_map/memory_map_backend.go
-
60weed/storage/needle/needle_read_write.go
-
5weed/storage/volume_create.go
-
5weed/storage/volume_create_linux.go
-
32weed/storage/volume_create_windows.go
-
6weed/storage/volume_loading.go
-
7weed/storage/volume_read_write.go
-
60weed/storage/volume_super_block.go
-
5weed/storage/volume_vacuum.go
@ -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() |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue