refactoring

This commit is contained in:
Chris Lu 2020-04-11 14:27:25 -07:00
parent df9d538044
commit c8ca234773
6 changed files with 13 additions and 18 deletions

View file

@ -3,12 +3,10 @@ package memory_map
import ( import (
"os" "os"
"time" "time"
"github.com/chrislusf/seaweedfs/weed/storage/backend"
) )
var ( var (
_ backend.BackendStorageFile = &MemoryMappedFile{} // _ backend.BackendStorageFile = &MemoryMappedFile{} // remove this to break import cycle
) )
type MemoryMappedFile struct { type MemoryMappedFile struct {

View file

@ -1,15 +1,14 @@
// +build !linux,!windows // +build !linux,!windows
package storage package backend
import ( import (
"os" "os"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/storage/backend"
) )
func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32) (backend.BackendStorageFile, error) { func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32) (BackendStorageFile, error) {
file, e := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) file, e := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if e != nil { if e != nil {
return nil, e return nil, e
@ -17,5 +16,5 @@ func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32
if preallocate > 0 { if preallocate > 0 {
glog.V(0).Infof("Preallocated disk space for %s is not supported", fileName) glog.V(0).Infof("Preallocated disk space for %s is not supported", fileName)
} }
return backend.NewDiskFile(file), nil return NewDiskFile(file), nil
} }

View file

@ -1,16 +1,15 @@
// +build linux // +build linux
package storage package backend
import ( import (
"os" "os"
"syscall" "syscall"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/storage/backend"
) )
func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32) (backend.BackendStorageFile, error) { func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32) (BackendStorageFile, error) {
file, e := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) file, e := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if e != nil { if e != nil {
return nil, e return nil, e
@ -19,5 +18,5 @@ func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32
syscall.Fallocate(int(file.Fd()), 1, 0, preallocate) syscall.Fallocate(int(file.Fd()), 1, 0, preallocate)
glog.V(0).Infof("Preallocated %d bytes disk space for %s", preallocate, fileName) glog.V(0).Infof("Preallocated %d bytes disk space for %s", preallocate, fileName)
} }
return backend.NewDiskFile(file), nil return NewDiskFile(file), nil
} }

View file

@ -1,17 +1,16 @@
// +build windows // +build windows
package storage package backend
import ( import (
"github.com/chrislusf/seaweedfs/weed/storage/backend/memory_map" "github.com/chrislusf/seaweedfs/weed/storage/backend/memory_map"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/storage/backend"
"github.com/chrislusf/seaweedfs/weed/storage/backend/memory_map/os_overloads" "github.com/chrislusf/seaweedfs/weed/storage/backend/memory_map/os_overloads"
) )
func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32) (backend.BackendStorageFile, error) { func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32) (BackendStorageFile, error) {
if preallocate > 0 { if preallocate > 0 {
glog.V(0).Infof("Preallocated disk space for %s is not supported", fileName) glog.V(0).Infof("Preallocated disk space for %s is not supported", fileName)
} }
@ -27,7 +26,7 @@ func CreateVolumeFile(fileName string, preallocate int64, memoryMapSizeMB uint32
if e != nil { if e != nil {
return nil, e return nil, e
} }
return backend.NewDiskFile(file), nil return NewDiskFile(file), nil
} }
} }

View file

@ -54,7 +54,7 @@ func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool, needleMapKind
v.DataBackend = backend.NewDiskFile(dataFile) v.DataBackend = backend.NewDiskFile(dataFile)
} else { } else {
if createDatIfMissing { if createDatIfMissing {
v.DataBackend, err = CreateVolumeFile(fileName+".dat", preallocate, v.MemoryMapMaxSizeMb) v.DataBackend, err = backend.CreateVolumeFile(fileName+".dat", preallocate, v.MemoryMapMaxSizeMb)
} else { } else {
return fmt.Errorf("Volume Data file %s.dat does not exist.", fileName) return fmt.Errorf("Volume Data file %s.dat does not exist.", fileName)
} }

View file

@ -354,7 +354,7 @@ func (v *Volume) copyDataAndGenerateIndexFile(dstName, idxName string, prealloca
var ( var (
dst backend.BackendStorageFile dst backend.BackendStorageFile
) )
if dst, err = CreateVolumeFile(dstName, preallocate, 0); err != nil { if dst, err = backend.CreateVolumeFile(dstName, preallocate, 0); err != nil {
return return
} }
defer dst.Close() defer dst.Close()
@ -383,7 +383,7 @@ func copyDataBasedOnIndexFile(srcDatName, srcIdxName, dstDatName, datIdxName str
srcDatBackend, dstDatBackend backend.BackendStorageFile srcDatBackend, dstDatBackend backend.BackendStorageFile
dataFile *os.File dataFile *os.File
) )
if dstDatBackend, err = CreateVolumeFile(dstDatName, preallocate, 0); err != nil { if dstDatBackend, err = backend.CreateVolumeFile(dstDatName, preallocate, 0); err != nil {
return return
} }
defer dstDatBackend.Close() defer dstDatBackend.Close()