2011-12-16 22:51:26 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2012-11-07 17:51:43 +08:00
|
|
|
"errors"
|
2012-11-20 17:45:36 +08:00
|
|
|
"fmt"
|
2012-06-29 15:53:47 +08:00
|
|
|
"log"
|
2011-12-16 22:51:26 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
2011-12-24 16:40:56 +08:00
|
|
|
"sync"
|
2011-12-16 22:51:26 +08:00
|
|
|
)
|
|
|
|
|
2012-11-22 02:52:08 +08:00
|
|
|
type Version uint8
|
|
|
|
|
2011-12-24 16:40:56 +08:00
|
|
|
const (
|
2012-01-19 08:49:41 +08:00
|
|
|
SuperBlockSize = 8
|
2012-11-22 02:52:08 +08:00
|
|
|
CurrentVersion = Version(1)
|
2011-12-24 16:40:56 +08:00
|
|
|
)
|
|
|
|
|
2011-12-16 22:51:26 +08:00
|
|
|
type Volume struct {
|
2012-08-24 13:46:54 +08:00
|
|
|
Id VolumeId
|
2011-12-22 12:04:47 +08:00
|
|
|
dir string
|
|
|
|
dataFile *os.File
|
|
|
|
nm *NeedleMap
|
2011-12-16 22:51:26 +08:00
|
|
|
|
2012-09-13 16:33:47 +08:00
|
|
|
replicaType ReplicationType
|
2012-11-22 02:52:08 +08:00
|
|
|
version Version
|
2012-11-20 17:45:36 +08:00
|
|
|
|
2011-12-24 16:40:56 +08:00
|
|
|
accessLock sync.Mutex
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 08:08:52 +08:00
|
|
|
func NewVolume(dirname string, id VolumeId, replicationType ReplicationType) (v *Volume) {
|
2012-09-13 16:33:47 +08:00
|
|
|
v = &Volume{dir: dirname, Id: id, replicaType: replicationType}
|
2012-11-07 17:51:43 +08:00
|
|
|
v.load()
|
|
|
|
return
|
|
|
|
}
|
2012-11-20 17:45:36 +08:00
|
|
|
func (v *Volume) load() error {
|
2012-11-07 17:51:43 +08:00
|
|
|
var e error
|
|
|
|
fileName := path.Join(v.dir, v.Id.String())
|
|
|
|
v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
|
2011-12-16 22:51:26 +08:00
|
|
|
if e != nil {
|
2012-11-20 17:45:36 +08:00
|
|
|
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2012-11-07 17:51:43 +08:00
|
|
|
if v.replicaType == CopyNil {
|
2012-11-20 17:45:36 +08:00
|
|
|
if e = v.readSuperBlock(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2012-09-13 16:33:47 +08:00
|
|
|
} else {
|
|
|
|
v.maybeWriteSuperBlock()
|
|
|
|
}
|
2012-11-07 17:51:43 +08:00
|
|
|
indexFile, ie := os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644)
|
2011-12-22 12:04:47 +08:00
|
|
|
if ie != nil {
|
2012-11-20 17:45:36 +08:00
|
|
|
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2011-12-22 12:04:47 +08:00
|
|
|
v.nm = LoadNeedleMap(indexFile)
|
2012-11-20 17:45:36 +08:00
|
|
|
return nil
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2011-12-18 15:22:04 +08:00
|
|
|
func (v *Volume) Size() int64 {
|
2011-12-22 12:04:47 +08:00
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e == nil {
|
2012-06-29 15:53:47 +08:00
|
|
|
return stat.Size()
|
2011-12-22 12:04:47 +08:00
|
|
|
}
|
|
|
|
return -1
|
2011-12-17 14:47:23 +08:00
|
|
|
}
|
2011-12-16 22:51:26 +08:00
|
|
|
func (v *Volume) Close() {
|
2011-12-22 12:04:47 +08:00
|
|
|
v.nm.Close()
|
2011-12-16 22:51:26 +08:00
|
|
|
v.dataFile.Close()
|
|
|
|
}
|
2011-12-24 16:40:56 +08:00
|
|
|
func (v *Volume) maybeWriteSuperBlock() {
|
|
|
|
stat, _ := v.dataFile.Stat()
|
2012-06-29 15:53:47 +08:00
|
|
|
if stat.Size() == 0 {
|
2011-12-24 16:40:56 +08:00
|
|
|
header := make([]byte, SuperBlockSize)
|
2012-11-22 02:52:08 +08:00
|
|
|
header[0] = byte(CurrentVersion)
|
2012-09-30 17:20:33 +08:00
|
|
|
header[1] = v.replicaType.Byte()
|
2011-12-24 16:40:56 +08:00
|
|
|
v.dataFile.Write(header)
|
|
|
|
}
|
|
|
|
}
|
2012-11-20 17:45:36 +08:00
|
|
|
func (v *Volume) readSuperBlock() error {
|
2012-09-13 16:33:47 +08:00
|
|
|
v.dataFile.Seek(0, 0)
|
|
|
|
header := make([]byte, SuperBlockSize)
|
2012-11-20 17:45:36 +08:00
|
|
|
if _, e := v.dataFile.Read(header); e != nil {
|
|
|
|
return fmt.Errorf("cannot read superblock: %s", e)
|
|
|
|
}
|
2012-11-22 02:52:08 +08:00
|
|
|
v.version = Version(header[0])
|
2012-11-20 17:45:36 +08:00
|
|
|
var err error
|
|
|
|
if v.replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
|
|
|
|
return fmt.Errorf("cannot read replica type: %s", err)
|
2012-09-13 16:33:47 +08:00
|
|
|
}
|
2012-11-20 17:45:36 +08:00
|
|
|
return nil
|
2012-09-13 16:33:47 +08:00
|
|
|
}
|
2012-11-20 16:54:37 +08:00
|
|
|
func (v *Volume) NeedToReplicate() bool {
|
|
|
|
return v.replicaType.GetCopyCount() > 1
|
2012-11-12 17:26:18 +08:00
|
|
|
}
|
2011-12-16 22:51:26 +08:00
|
|
|
|
2011-12-24 16:40:56 +08:00
|
|
|
func (v *Volume) write(n *Needle) uint32 {
|
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2011-12-16 22:51:26 +08:00
|
|
|
offset, _ := v.dataFile.Seek(0, 2)
|
2011-12-24 09:25:22 +08:00
|
|
|
ret := n.Append(v.dataFile)
|
2012-08-24 14:06:15 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2011-12-16 22:51:26 +08:00
|
|
|
if !ok || int64(nv.Offset)*8 < offset {
|
2012-08-24 14:06:15 +08:00
|
|
|
v.nm.Put(n.Id, uint32(offset/8), n.Size)
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2011-12-24 09:25:22 +08:00
|
|
|
return ret
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2012-01-19 08:49:41 +08:00
|
|
|
func (v *Volume) delete(n *Needle) uint32 {
|
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2012-08-24 14:06:15 +08: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 08:49:41 +08:00
|
|
|
if ok {
|
2012-08-24 14:06:15 +08:00
|
|
|
v.nm.Delete(n.Id)
|
2012-01-19 08:49:41 +08:00
|
|
|
v.dataFile.Seek(int64(nv.Offset*8), 0)
|
|
|
|
n.Append(v.dataFile)
|
|
|
|
return nv.Size
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2012-06-29 15:53:47 +08:00
|
|
|
func (v *Volume) read(n *Needle) (int, error) {
|
2011-12-24 16:40:56 +08:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2012-08-24 14:06:15 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2011-12-16 22:51:26 +08:00
|
|
|
if ok && nv.Offset > 0 {
|
|
|
|
v.dataFile.Seek(int64(nv.Offset)*8, 0)
|
2012-11-22 02:52:08 +08:00
|
|
|
return n.Read(v.dataFile, nv.Size, v.version)
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2012-09-27 11:30:05 +08:00
|
|
|
return -1, errors.New("Not Found")
|
2011-12-24 16:40:56 +08:00
|
|
|
}
|
2012-11-07 17:51:43 +08:00
|
|
|
|
|
|
|
func (v *Volume) compact() error {
|
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
|
|
|
|
|
|
|
filePath := path.Join(v.dir, v.Id.String())
|
|
|
|
return v.copyDataAndGenerateIndexFile(filePath+".dat", filePath+".cpd", filePath+".cpx")
|
|
|
|
}
|
|
|
|
func (v *Volume) commitCompact() (int, error) {
|
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
|
|
|
v.dataFile.Close()
|
2012-11-20 17:45:36 +08:00
|
|
|
var e error
|
|
|
|
if e = os.Rename(path.Join(v.dir, v.Id.String()+".cpd"), path.Join(v.dir, v.Id.String()+".dat")); e != nil {
|
|
|
|
return 0, e
|
|
|
|
}
|
|
|
|
if e = os.Rename(path.Join(v.dir, v.Id.String()+".cpx"), path.Join(v.dir, v.Id.String()+".idx")); e != nil {
|
|
|
|
return 0, e
|
|
|
|
}
|
|
|
|
if e = v.load(); e != nil {
|
|
|
|
return 0, e
|
|
|
|
}
|
2012-11-07 17:51:43 +08:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) copyDataAndGenerateIndexFile(srcName, dstName, idxName string) (err error) {
|
|
|
|
src, err := os.OpenFile(srcName, os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
idx, err := os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer idx.Close()
|
|
|
|
|
|
|
|
src.Seek(0, 0)
|
|
|
|
header := make([]byte, SuperBlockSize)
|
|
|
|
if _, error := src.Read(header); error == nil {
|
|
|
|
dst.Write(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, rest := ReadNeedle(src)
|
|
|
|
nm := NewNeedleMap(idx)
|
|
|
|
old_offset := uint32(SuperBlockSize)
|
2012-11-20 16:54:37 +08:00
|
|
|
new_offset := uint32(SuperBlockSize)
|
2012-11-07 17:51:43 +08:00
|
|
|
for n != nil {
|
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
//log.Println("file size is", n.Size, "rest", rest)
|
|
|
|
if !ok || nv.Offset*8 != old_offset {
|
|
|
|
log.Println("expected offset should be", nv.Offset*8, "skipping", (rest - 16), "key", n.Id, "volume offset", old_offset, "data_size", n.Size, "rest", rest)
|
|
|
|
src.Seek(int64(rest), 1)
|
|
|
|
} else {
|
|
|
|
if nv.Size > 0 {
|
|
|
|
nm.Put(n.Id, new_offset/8, n.Size)
|
|
|
|
bytes := make([]byte, n.Size+4)
|
|
|
|
src.Read(bytes)
|
|
|
|
n.Data = bytes[:n.Size]
|
|
|
|
n.Checksum = NewCRC(n.Data)
|
|
|
|
n.Append(dst)
|
2012-11-20 16:54:37 +08:00
|
|
|
new_offset += rest + 16
|
2012-11-07 17:51:43 +08:00
|
|
|
log.Println("saving key", n.Id, "volume offset", old_offset, "=>", new_offset, "data_size", n.Size, "rest", rest)
|
|
|
|
}
|
2012-11-20 16:54:37 +08:00
|
|
|
src.Seek(int64(rest-n.Size-4), 1)
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
2012-11-20 16:54:37 +08:00
|
|
|
old_offset += rest + 16
|
2012-11-07 17:51:43 +08:00
|
|
|
n, rest = ReadNeedle(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|