seaweedfs/weed-fs/src/pkg/storage/volume.go

108 lines
2.3 KiB
Go
Raw Normal View History

package storage
import (
"io"
"log"
"os"
"path"
"strconv"
"sync"
)
const (
SuperBlockSize = 8
)
2012-08-24 05:46:54 +00:00
type VolumeId uint32
2012-08-24 05:56:14 +00:00
type VolumeInfo struct {
Id VolumeId
Size int64
}
2012-08-24 05:46:54 +00:00
func NewVolumeId(vid string) (VolumeId,error) {
volumeId, err := strconv.ParseUint(vid, 10, 64)
return VolumeId(volumeId), err
}
func (vid *VolumeId) String() string{
return strconv.FormatUint(uint64(*vid), 10)
}
type Volume struct {
2012-08-24 05:46:54 +00:00
Id VolumeId
dir string
dataFile *os.File
nm *NeedleMap
accessLock sync.Mutex
}
2012-08-24 05:46:54 +00:00
func NewVolume(dirname string, id VolumeId) (v *Volume) {
var e error
v = &Volume{dir: dirname, Id: id}
fileName := strconv.FormatUint(uint64(v.Id), 10)
v.dataFile, e = os.OpenFile(path.Join(v.dir, fileName+".dat"), os.O_RDWR|os.O_CREATE, 0644)
if e != nil {
log.Fatalf("New Volume [ERROR] %s\n", e)
}
v.maybeWriteSuperBlock()
indexFile, ie := os.OpenFile(path.Join(v.dir, fileName+".idx"), os.O_RDWR|os.O_CREATE, 0644)
if ie != nil {
log.Fatalf("Write Volume Index [ERROR] %s\n", ie)
}
v.nm = LoadNeedleMap(indexFile)
return
}
func (v *Volume) Size() int64 {
stat, e := v.dataFile.Stat()
if e == nil {
return stat.Size()
}
return -1
}
func (v *Volume) Close() {
v.nm.Close()
v.dataFile.Close()
}
func (v *Volume) maybeWriteSuperBlock() {
stat, _ := v.dataFile.Stat()
if stat.Size() == 0 {
header := make([]byte, SuperBlockSize)
header[0] = 1
v.dataFile.Write(header)
}
}
func (v *Volume) write(n *Needle) uint32 {
v.accessLock.Lock()
defer v.accessLock.Unlock()
offset, _ := v.dataFile.Seek(0, 2)
ret := n.Append(v.dataFile)
nv, ok := v.nm.Get(n.Id)
if !ok || int64(nv.Offset)*8 < offset {
v.nm.Put(n.Id, uint32(offset/8), n.Size)
}
return ret
}
func (v *Volume) delete(n *Needle) uint32 {
v.accessLock.Lock()
defer v.accessLock.Unlock()
nv, ok := v.nm.Get(n.Id)
//log.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
if ok {
v.nm.Delete(n.Id)
v.dataFile.Seek(int64(nv.Offset*8), 0)
n.Append(v.dataFile)
return nv.Size
}
return 0
}
func (v *Volume) read(n *Needle) (int, error) {
v.accessLock.Lock()
defer v.accessLock.Unlock()
nv, ok := v.nm.Get(n.Id)
if ok && nv.Offset > 0 {
v.dataFile.Seek(int64(nv.Offset)*8, 0)
return n.Read(v.dataFile, nv.Size)
}
return -1, io.EOF
}