2011-12-16 14:51:26 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2012-11-07 09:51:43 +00:00
|
|
|
"errors"
|
2012-11-20 09:45:36 +00:00
|
|
|
"fmt"
|
2013-01-13 16:07:38 +00:00
|
|
|
"log"
|
2011-12-16 14:51:26 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2013-01-14 20:42:35 +00:00
|
|
|
"pkg/util"
|
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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2012-11-21 18:52:08 +00:00
|
|
|
version Version
|
2012-12-21 06:32:21 +00:00
|
|
|
replicaType ReplicationType
|
2012-11-20 09:45:36 +00:00
|
|
|
|
2011-12-24 08:40:56 +00:00
|
|
|
accessLock sync.Mutex
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-05 22:06:44 +00:00
|
|
|
func NewVolume(dirname string, id VolumeId, replicationType ReplicationType) (v *Volume, e error) {
|
2012-09-13 08:33:47 +00:00
|
|
|
v = &Volume{dir: dirname, Id: id, replicaType: replicationType}
|
2013-01-05 22:06:44 +00:00
|
|
|
e = v.load()
|
2012-11-07 09:51:43 +00:00
|
|
|
return
|
|
|
|
}
|
2012-11-20 09:45:36 +00:00
|
|
|
func (v *Volume) load() error {
|
2012-11-07 09:51:43 +00: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 14:51:26 +00:00
|
|
|
if e != nil {
|
2013-01-14 21:18:00 +00:00
|
|
|
if os.IsPermission(e) {
|
|
|
|
if util.FileExists(fileName + ".cdb") {
|
|
|
|
v.dataFile, e = os.Open(fileName + ".dat")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
|
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2012-11-07 09:51:43 +00:00
|
|
|
if v.replicaType == CopyNil {
|
2012-11-20 09:45:36 +00:00
|
|
|
if e = v.readSuperBlock(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2012-09-13 08:33:47 +00:00
|
|
|
} else {
|
|
|
|
v.maybeWriteSuperBlock()
|
|
|
|
}
|
2013-01-05 22:06:44 +00:00
|
|
|
// TODO: if .idx not exists, but .cdb exists, then use (but don't load!) that
|
2013-01-14 20:42:35 +00:00
|
|
|
if !util.FileIsWritable(v.dataFile.Name()) { //Read-Only
|
|
|
|
v.nm, e = NewFrozenNeedleMap(fileName)
|
|
|
|
} else {
|
|
|
|
indexFile, ie := os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
if ie != nil {
|
|
|
|
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
|
|
|
|
}
|
|
|
|
v.nm, e = LoadNeedleMap(indexFile)
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-01-12 18:56:47 +00:00
|
|
|
return e
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-01-14 20:42:35 +00:00
|
|
|
|
2012-12-18 01:51:39 +00:00
|
|
|
func (v *Volume) Version() Version {
|
2012-12-21 10:13:02 +00:00
|
|
|
return v.version
|
2012-12-18 01:51:39 +00:00
|
|
|
}
|
2011-12-18 07:22:04 +00:00
|
|
|
func (v *Volume) Size() int64 {
|
2012-12-21 06:32:21 +00:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
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
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
fmt.Printf("Failed to read file size %s %s\n", v.dataFile.Name(), e.Error())
|
2011-12-22 04:04:47 +00:00
|
|
|
return -1
|
2011-12-17 06:47:23 +00:00
|
|
|
}
|
2013-01-13 16:07:38 +00:00
|
|
|
|
|
|
|
// a volume is writable, if its data file is writable and the index is not frozen
|
|
|
|
func (v *Volume) IsWritable() bool {
|
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e != nil {
|
|
|
|
log.Printf("Failed to read file permission %s %s\n", v.dataFile.Name(), e.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// 4 for r, 2 for w, 1 for x
|
|
|
|
return stat.Mode().Perm()&0222 > 0 && !v.nm.IsFrozen()
|
|
|
|
}
|
|
|
|
|
2011-12-16 14:51:26 +00:00
|
|
|
func (v *Volume) Close() {
|
2012-12-21 06:32:21 +00:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
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 {
|
2012-12-21 06:32:21 +00:00
|
|
|
v.version = CurrentVersion
|
2011-12-24 08:40:56 +00:00
|
|
|
header := make([]byte, SuperBlockSize)
|
2012-12-18 01:51:39 +00:00
|
|
|
header[0] = byte(v.version)
|
2012-09-30 09:20:33 +00:00
|
|
|
header[1] = v.replicaType.Byte()
|
2011-12-24 08:40:56 +00:00
|
|
|
v.dataFile.Write(header)
|
|
|
|
}
|
|
|
|
}
|
2013-01-05 22:06:44 +00:00
|
|
|
func (v *Volume) readSuperBlock() (err error) {
|
2012-09-13 08:33:47 +00:00
|
|
|
v.dataFile.Seek(0, 0)
|
|
|
|
header := make([]byte, SuperBlockSize)
|
2012-11-20 09:45:36 +00:00
|
|
|
if _, e := v.dataFile.Read(header); e != nil {
|
|
|
|
return fmt.Errorf("cannot read superblock: %s", e)
|
|
|
|
}
|
2012-12-21 06:32:21 +00:00
|
|
|
v.version, v.replicaType, err = ParseSuperBlock(header)
|
|
|
|
return err
|
|
|
|
}
|
2013-01-05 22:06:44 +00:00
|
|
|
func ParseSuperBlock(header []byte) (version Version, replicaType ReplicationType, err error) {
|
2012-12-21 06:32:21 +00:00
|
|
|
version = Version(header[0])
|
2013-01-05 22:06:44 +00:00
|
|
|
if version == 0 {
|
|
|
|
err = errors.New("Zero version impossible - bad superblock!")
|
|
|
|
return
|
|
|
|
}
|
2012-12-21 06:32:21 +00:00
|
|
|
if replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
|
2013-01-05 22:06:44 +00:00
|
|
|
err = fmt.Errorf("cannot read replica type: %s", err)
|
2012-09-13 08:33:47 +00:00
|
|
|
}
|
2012-12-21 06:32:21 +00:00
|
|
|
return
|
2012-09-13 08:33:47 +00:00
|
|
|
}
|
2012-11-20 08:54:37 +00:00
|
|
|
func (v *Volume) NeedToReplicate() bool {
|
|
|
|
return v.replicaType.GetCopyCount() > 1
|
2012-11-12 09:26:18 +00:00
|
|
|
}
|
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)
|
2012-12-21 08:36:55 +00:00
|
|
|
ret := n.Append(v.dataFile, v.version)
|
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)
|
2013-01-02 23:39:55 +00:00
|
|
|
//fmt.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-12-21 08:36:55 +00:00
|
|
|
v.dataFile.Seek(int64(nv.Offset*NeedlePaddingSize), 0)
|
|
|
|
n.Append(v.dataFile, v.version)
|
2012-01-19 00:49:41 +00:00
|
|
|
return nv.Size
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
|
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 {
|
2012-12-21 08:36:55 +00:00
|
|
|
v.dataFile.Seek(int64(nv.Offset)*NeedlePaddingSize, 0)
|
2012-11-21 18:52:08 +00:00
|
|
|
return n.Read(v.dataFile, nv.Size, v.version)
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2012-09-27 03:30:05 +00:00
|
|
|
return -1, errors.New("Not Found")
|
2011-12-24 08:40:56 +00:00
|
|
|
}
|
2012-11-07 09:51:43 +00:00
|
|
|
|
2012-11-24 01:03:27 +00:00
|
|
|
func (v *Volume) garbageLevel() float64 {
|
2012-12-21 06:32:21 +00:00
|
|
|
return float64(v.nm.deletionByteCounter) / float64(v.ContentSize())
|
2012-11-24 01:03:27 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 09:51:43 +00: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")
|
|
|
|
}
|
2012-12-21 06:32:21 +00:00
|
|
|
func (v *Volume) commitCompact() error {
|
2012-11-07 09:51:43 +00:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
|
|
|
v.dataFile.Close()
|
2012-11-20 09:45:36 +00: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 {
|
2012-11-24 01:03:27 +00:00
|
|
|
return e
|
2012-11-20 09:45:36 +00:00
|
|
|
}
|
|
|
|
if e = os.Rename(path.Join(v.dir, v.Id.String()+".cpx"), path.Join(v.dir, v.Id.String()+".idx")); e != nil {
|
2012-11-24 01:03:27 +00:00
|
|
|
return e
|
2012-11-20 09:45:36 +00:00
|
|
|
}
|
|
|
|
if e = v.load(); e != nil {
|
2012-11-24 01:03:27 +00:00
|
|
|
return e
|
2012-11-20 09:45:36 +00:00
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
return nil
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2012-12-21 06:32:21 +00:00
|
|
|
version, _, _ := ParseSuperBlock(header)
|
|
|
|
|
2012-12-21 08:36:55 +00:00
|
|
|
n, rest := ReadNeedleHeader(src, version)
|
2012-11-07 09:51:43 +00:00
|
|
|
nm := NewNeedleMap(idx)
|
|
|
|
old_offset := uint32(SuperBlockSize)
|
2012-11-20 08:54:37 +00:00
|
|
|
new_offset := uint32(SuperBlockSize)
|
2012-11-07 09:51:43 +00:00
|
|
|
for n != nil {
|
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
//log.Println("file size is", n.Size, "rest", rest)
|
2012-12-21 08:36:55 +00:00
|
|
|
if !ok || nv.Offset*NeedlePaddingSize != old_offset {
|
2012-11-07 09:51:43 +00:00
|
|
|
src.Seek(int64(rest), 1)
|
|
|
|
} else {
|
|
|
|
if nv.Size > 0 {
|
2012-12-21 08:36:55 +00:00
|
|
|
nm.Put(n.Id, new_offset/NeedlePaddingSize, n.Size)
|
|
|
|
n.ReadNeedleBody(src, version, rest)
|
|
|
|
n.Append(dst, v.version)
|
|
|
|
new_offset += rest + NeedleHeaderSize
|
2012-11-24 02:07:43 +00:00
|
|
|
//log.Println("saving key", n.Id, "volume offset", old_offset, "=>", new_offset, "data_size", n.Size, "rest", rest)
|
2012-12-21 08:36:55 +00:00
|
|
|
} else {
|
|
|
|
src.Seek(int64(rest), 1)
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-21 08:36:55 +00:00
|
|
|
old_offset += rest + NeedleHeaderSize
|
|
|
|
n, rest = ReadNeedleHeader(src, version)
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2012-12-21 06:32:21 +00:00
|
|
|
func (v *Volume) ContentSize() uint64 {
|
|
|
|
return v.nm.fileByteCounter
|
2012-12-04 06:54:08 +00:00
|
|
|
}
|
2013-01-05 22:06:44 +00:00
|
|
|
|
|
|
|
// Walk over the contained needles (call the function with each NeedleValue till error is returned)
|
|
|
|
func (v *Volume) WalkValues(pedestrian func(*Needle) error) error {
|
|
|
|
pedplus := func(nv *NeedleValue) (err error) {
|
|
|
|
n := new(Needle)
|
|
|
|
if nv.Offset > 0 {
|
|
|
|
v.dataFile.Seek(int64(nv.Offset)*NeedlePaddingSize, 0)
|
|
|
|
if _, err = n.Read(v.dataFile, nv.Size, v.version); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = pedestrian(n); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.nm.Walk(pedplus)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk over the keys
|
|
|
|
func (v *Volume) WalkKeys(pedestrian func(Key) error) error {
|
|
|
|
pedplus := func(nv *NeedleValue) (err error) {
|
|
|
|
if nv.Offset > 0 && nv.Key > 0 {
|
|
|
|
if err = pedestrian(nv.Key); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.nm.Walk(pedplus)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) String() string {
|
|
|
|
return fmt.Sprintf("%d@%s:v%d:r%s", v.Id, v.dataFile.Name(),
|
|
|
|
v.Version(), v.replicaType)
|
|
|
|
}
|