2011-12-16 14:51:26 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2012-06-29 07:53:47 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2011-12-16 14:51:26 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2011-12-24 08:40:56 +00:00
|
|
|
"sync"
|
2011-12-16 14:51:26 +00:00
|
|
|
)
|
|
|
|
|
2011-12-24 08:40:56 +00:00
|
|
|
const (
|
2012-01-19 00:49:41 +00:00
|
|
|
SuperBlockSize = 8
|
2011-12-24 08:40:56 +00:00
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
type Volume struct {
|
2012-08-24 05:46:54 +00:00
|
|
|
Id VolumeId
|
2011-12-22 04:04:47 +00:00
|
|
|
dir string
|
|
|
|
dataFile *os.File
|
|
|
|
nm *NeedleMap
|
2011-12-16 14:51:26 +00:00
|
|
|
|
2011-12-24 08:40:56 +00:00
|
|
|
accessLock sync.Mutex
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 05:46:54 +00:00
|
|
|
func NewVolume(dirname string, id VolumeId) (v *Volume) {
|
2012-06-29 07:53:47 +00:00
|
|
|
var e error
|
2011-12-24 08:40:56 +00:00
|
|
|
v = &Volume{dir: dirname, Id: id}
|
2012-06-29 07:53:47 +00:00
|
|
|
fileName := strconv.FormatUint(uint64(v.Id), 10)
|
2011-12-22 04:04:47 +00:00
|
|
|
v.dataFile, e = os.OpenFile(path.Join(v.dir, fileName+".dat"), os.O_RDWR|os.O_CREATE, 0644)
|
2011-12-16 14:51:26 +00:00
|
|
|
if e != nil {
|
|
|
|
log.Fatalf("New Volume [ERROR] %s\n", e)
|
|
|
|
}
|
2011-12-24 08:40:56 +00:00
|
|
|
v.maybeWriteSuperBlock()
|
2011-12-22 04:04:47 +00:00
|
|
|
indexFile, ie := os.OpenFile(path.Join(v.dir, fileName+".idx"), os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
if ie != nil {
|
2011-12-24 08:40:56 +00:00
|
|
|
log.Fatalf("Write Volume Index [ERROR] %s\n", ie)
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2011-12-22 04:04:47 +00:00
|
|
|
v.nm = LoadNeedleMap(indexFile)
|
2011-12-16 14:51:26 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2011-12-18 07:22:04 +00:00
|
|
|
func (v *Volume) Size() int64 {
|
2011-12-22 04:04:47 +00:00
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e == nil {
|
2012-06-29 07:53:47 +00:00
|
|
|
return stat.Size()
|
2011-12-22 04:04:47 +00:00
|
|
|
}
|
|
|
|
return -1
|
2011-12-17 06:47:23 +00:00
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
func (v *Volume) Close() {
|
2011-12-22 04:04:47 +00:00
|
|
|
v.nm.Close()
|
2011-12-16 14:51:26 +00:00
|
|
|
v.dataFile.Close()
|
|
|
|
}
|
2011-12-24 08:40:56 +00:00
|
|
|
func (v *Volume) maybeWriteSuperBlock() {
|
|
|
|
stat, _ := v.dataFile.Stat()
|
2012-06-29 07:53:47 +00:00
|
|
|
if stat.Size() == 0 {
|
2011-12-24 08:40:56 +00:00
|
|
|
header := make([]byte, SuperBlockSize)
|
|
|
|
header[0] = 1
|
|
|
|
v.dataFile.Write(header)
|
|
|
|
}
|
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
|
2011-12-24 08:40:56 +00:00
|
|
|
func (v *Volume) write(n *Needle) uint32 {
|
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2011-12-16 14:51:26 +00:00
|
|
|
offset, _ := v.dataFile.Seek(0, 2)
|
2011-12-24 01:25:22 +00:00
|
|
|
ret := n.Append(v.dataFile)
|
2012-08-24 06:06:15 +00:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2011-12-16 14:51:26 +00:00
|
|
|
if !ok || int64(nv.Offset)*8 < offset {
|
2012-08-24 06:06:15 +00:00
|
|
|
v.nm.Put(n.Id, uint32(offset/8), n.Size)
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2011-12-24 01:25:22 +00:00
|
|
|
return ret
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2012-01-19 00:49:41 +00:00
|
|
|
func (v *Volume) delete(n *Needle) uint32 {
|
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2012-08-24 06:06:15 +00:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
//log.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
|
2012-01-19 00:49:41 +00:00
|
|
|
if ok {
|
2012-08-24 06:06:15 +00:00
|
|
|
v.nm.Delete(n.Id)
|
2012-01-19 00:49:41 +00:00
|
|
|
v.dataFile.Seek(int64(nv.Offset*8), 0)
|
|
|
|
n.Append(v.dataFile)
|
|
|
|
return nv.Size
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2012-06-29 07:53:47 +00:00
|
|
|
func (v *Volume) read(n *Needle) (int, error) {
|
2011-12-24 08:40:56 +00:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2012-08-24 06:06:15 +00:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2011-12-16 14:51:26 +00:00
|
|
|
if ok && nv.Offset > 0 {
|
|
|
|
v.dataFile.Seek(int64(nv.Offset)*8, 0)
|
2011-12-24 08:40:56 +00:00
|
|
|
return n.Read(v.dataFile, nv.Size)
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2012-06-29 07:53:47 +00:00
|
|
|
return -1, io.EOF
|
2011-12-24 08:40:56 +00:00
|
|
|
}
|