2011-12-16 14:51:26 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2013-07-13 20:51:47 +00:00
|
|
|
"bytes"
|
2013-08-09 06:57:22 +00:00
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
2012-11-07 09:51:43 +00:00
|
|
|
"errors"
|
2012-11-20 09:45:36 +00:00
|
|
|
"fmt"
|
2013-01-21 03:44:23 +00:00
|
|
|
"io"
|
2011-12-16 14:51:26 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2011-12-24 08:40:56 +00:00
|
|
|
"sync"
|
2013-08-12 23:53:32 +00:00
|
|
|
"time"
|
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
|
|
|
)
|
|
|
|
|
2013-01-21 03:44:23 +00:00
|
|
|
type SuperBlock struct {
|
2014-03-03 06:16:54 +00:00
|
|
|
Version Version
|
|
|
|
ReplicaPlacement *ReplicaPlacement
|
2013-01-21 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SuperBlock) Bytes() []byte {
|
|
|
|
header := make([]byte, SuperBlockSize)
|
|
|
|
header[0] = byte(s.Version)
|
2014-03-03 06:16:54 +00:00
|
|
|
header[1] = s.ReplicaPlacement.Byte()
|
2013-01-21 03:44:23 +00:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2011-12-16 14:51:26 +00:00
|
|
|
type Volume struct {
|
2013-11-12 10:21:22 +00:00
|
|
|
Id VolumeId
|
|
|
|
dir string
|
|
|
|
Collection string
|
|
|
|
dataFile *os.File
|
|
|
|
nm NeedleMapper
|
|
|
|
readOnly bool
|
2011-12-16 14:51:26 +00:00
|
|
|
|
2013-01-21 03:44:23 +00:00
|
|
|
SuperBlock
|
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
|
|
|
}
|
|
|
|
|
2014-03-03 06:16:54 +00:00
|
|
|
func NewVolume(dirname string, collection string, id VolumeId, replicaPlacement *ReplicaPlacement) (v *Volume, e error) {
|
2013-11-12 10:21:22 +00:00
|
|
|
v = &Volume{dir: dirname, Collection: collection, Id: id}
|
2014-03-03 06:16:54 +00:00
|
|
|
v.SuperBlock = SuperBlock{ReplicaPlacement: replicaPlacement}
|
2014-02-07 01:32:06 +00:00
|
|
|
e = v.load(true, true)
|
2013-01-21 03:44:23 +00:00
|
|
|
return
|
|
|
|
}
|
2013-11-12 10:21:22 +00:00
|
|
|
func loadVolumeWithoutIndex(dirname string, collection string, id VolumeId) (v *Volume, e error) {
|
|
|
|
v = &Volume{dir: dirname, Collection: collection, Id: id}
|
2014-03-03 06:16:54 +00:00
|
|
|
v.SuperBlock = SuperBlock{}
|
2014-02-07 01:32:06 +00:00
|
|
|
e = v.load(false, false)
|
2012-11-07 09:51:43 +00:00
|
|
|
return
|
|
|
|
}
|
2014-01-22 04:51:46 +00:00
|
|
|
func (v *Volume) FileName() (fileName string) {
|
2014-02-07 01:32:06 +00:00
|
|
|
if v.Collection == "" {
|
|
|
|
fileName = path.Join(v.dir, v.Id.String())
|
|
|
|
} else {
|
|
|
|
fileName = path.Join(v.dir, v.Collection+"_"+v.Id.String())
|
|
|
|
}
|
|
|
|
return
|
2014-01-22 04:51:46 +00:00
|
|
|
}
|
2014-02-07 01:32:06 +00:00
|
|
|
func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool) error {
|
2012-11-07 09:51:43 +00:00
|
|
|
var e error
|
2014-01-22 04:51:46 +00:00
|
|
|
fileName := v.FileName()
|
2014-02-07 01:32:06 +00:00
|
|
|
|
|
|
|
if exists, canRead, canWrite, _ := checkFile(fileName + ".dat"); exists {
|
|
|
|
if !canRead {
|
|
|
|
return fmt.Errorf("cannot read Volume Data file %s.dat", fileName)
|
|
|
|
}
|
|
|
|
if canWrite {
|
|
|
|
v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
} else {
|
|
|
|
glog.V(0).Infoln("opening " + fileName + ".dat in READONLY mode")
|
|
|
|
v.dataFile, e = os.Open(fileName + ".dat")
|
|
|
|
v.readOnly = true
|
|
|
|
}
|
2013-08-13 06:48:10 +00:00
|
|
|
} else {
|
2014-02-07 01:32:06 +00:00
|
|
|
if createDatIfMissing {
|
|
|
|
v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Volume Data file %s.dat does not exist.", fileName)
|
|
|
|
}
|
2013-08-12 23:53:32 +00:00
|
|
|
}
|
2014-02-07 01:32:06 +00:00
|
|
|
|
2011-12-16 14:51:26 +00:00
|
|
|
if e != nil {
|
2013-04-15 02:30:26 +00:00
|
|
|
if !os.IsPermission(e) {
|
2013-08-12 23:53:32 +00:00
|
|
|
return fmt.Errorf("cannot load Volume Data %s.dat: %s", fileName, e.Error())
|
2013-04-15 02:30:26 +00:00
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-08-12 23:53:32 +00:00
|
|
|
|
2014-03-03 06:16:54 +00:00
|
|
|
if v.ReplicaPlacement == nil {
|
2013-02-10 22:00:06 +00:00
|
|
|
e = v.readSuperBlock()
|
2012-09-13 08:33:47 +00:00
|
|
|
} else {
|
2013-02-10 22:00:06 +00:00
|
|
|
e = v.maybeWriteSuperBlock()
|
2012-09-13 08:33:47 +00:00
|
|
|
}
|
2013-02-10 22:00:06 +00:00
|
|
|
if e == nil && alsoLoadIndex {
|
2013-06-20 01:10:38 +00:00
|
|
|
if v.readOnly {
|
2013-08-12 23:53:32 +00:00
|
|
|
if v.ensureConvertIdxToCdb(fileName) {
|
|
|
|
v.nm, e = OpenCdbMap(fileName + ".cdb")
|
|
|
|
return e
|
2013-06-20 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-12 23:53:32 +00:00
|
|
|
var indexFile *os.File
|
2013-08-11 18:38:55 +00:00
|
|
|
if v.readOnly {
|
|
|
|
glog.V(1).Infoln("open to read file", fileName+".idx")
|
2013-08-12 23:53:32 +00:00
|
|
|
if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644); e != nil {
|
2013-08-11 18:38:55 +00:00
|
|
|
return fmt.Errorf("cannot read Volume Data %s.dat: %s", fileName, e.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glog.V(1).Infoln("open to write file", fileName+".idx")
|
2013-08-12 23:53:32 +00:00
|
|
|
if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644); e != nil {
|
2013-08-11 20:15:11 +00:00
|
|
|
return fmt.Errorf("cannot write Volume Data %s.dat: %s", fileName, e.Error())
|
2013-08-11 18:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
glog.V(0).Infoln("loading file", fileName+".idx", "readonly", v.readOnly)
|
|
|
|
if v.nm, e = LoadNeedleMap(indexFile); e != nil {
|
|
|
|
glog.V(0).Infoln("loading error:", e)
|
2013-08-09 06:57:22 +00:00
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-02-10 22:00:06 +00:00
|
|
|
return e
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2012-12-18 01:51:39 +00:00
|
|
|
func (v *Volume) Version() Version {
|
2013-01-21 03:44:23 +00:00
|
|
|
return v.SuperBlock.Version
|
2012-12-18 01:51:39 +00:00
|
|
|
}
|
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
|
|
|
}
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infof("Failed to read file size %s %s", v.dataFile.Name(), e.Error())
|
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() {
|
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()
|
2013-02-27 06:54:22 +00:00
|
|
|
_ = v.dataFile.Close()
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-02-10 22:00:06 +00:00
|
|
|
func (v *Volume) maybeWriteSuperBlock() error {
|
2013-01-20 11:40:04 +00:00
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e != nil {
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infof("failed to stat datafile %s: %s", v.dataFile, e.Error())
|
2013-02-10 22:00:06 +00:00
|
|
|
return e
|
2013-01-20 11:40:04 +00:00
|
|
|
}
|
2012-06-29 07:53:47 +00:00
|
|
|
if stat.Size() == 0 {
|
2013-01-21 03:44:23 +00:00
|
|
|
v.SuperBlock.Version = CurrentVersion
|
2013-02-10 22:00:06 +00:00
|
|
|
_, e = v.dataFile.Write(v.SuperBlock.Bytes())
|
2013-04-15 02:30:26 +00:00
|
|
|
if e != nil && os.IsPermission(e) {
|
|
|
|
//read-only, but zero length - recreate it!
|
|
|
|
if v.dataFile, e = os.Create(v.dataFile.Name()); e == nil {
|
|
|
|
if _, e = v.dataFile.Write(v.SuperBlock.Bytes()); e == nil {
|
|
|
|
v.readOnly = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-24 08:40:56 +00:00
|
|
|
}
|
2013-02-10 22:00:06 +00:00
|
|
|
return e
|
2011-12-24 08:40:56 +00:00
|
|
|
}
|
2013-01-17 08:56:56 +00:00
|
|
|
func (v *Volume) readSuperBlock() (err error) {
|
2013-02-27 06:54:22 +00:00
|
|
|
if _, err = v.dataFile.Seek(0, 0); err != nil {
|
2013-10-31 19:55:19 +00:00
|
|
|
return fmt.Errorf("cannot seek to the beginning of %s: %s", v.dataFile.Name(), err.Error())
|
2013-02-27 06:54:22 +00:00
|
|
|
}
|
2012-09-13 08:33:47 +00:00
|
|
|
header := make([]byte, SuperBlockSize)
|
2012-11-20 09:45:36 +00:00
|
|
|
if _, e := v.dataFile.Read(header); e != nil {
|
2013-08-12 23:53:32 +00:00
|
|
|
return fmt.Errorf("cannot read superblock: %s", e.Error())
|
2012-11-20 09:45:36 +00:00
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
v.SuperBlock, err = ParseSuperBlock(header)
|
2012-12-21 06:32:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
func ParseSuperBlock(header []byte) (superBlock SuperBlock, err error) {
|
|
|
|
superBlock.Version = Version(header[0])
|
2014-03-03 06:16:54 +00:00
|
|
|
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
|
2013-08-12 23:53:32 +00:00
|
|
|
err = fmt.Errorf("cannot read replica type: %s", err.Error())
|
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 {
|
2014-03-03 06:16:54 +00:00
|
|
|
return v.ReplicaPlacement.GetCopyCount() > 1
|
2012-11-12 09:26:18 +00:00
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
|
2013-07-12 05:44:59 +00:00
|
|
|
func (v *Volume) isFileUnchanged(n *Needle) bool {
|
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
if ok && nv.Offset > 0 {
|
|
|
|
oldNeedle := new(Needle)
|
2014-03-19 06:48:01 +00:00
|
|
|
oldNeedle.Read(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
|
2013-07-13 20:51:47 +00:00
|
|
|
if oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
|
2013-07-12 05:44:59 +00:00
|
|
|
n.Size = oldNeedle.Size
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2014-03-10 18:43:54 +00:00
|
|
|
|
|
|
|
func (v *Volume) Destroy() (err error) {
|
|
|
|
if v.readOnly {
|
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.Close()
|
|
|
|
err = os.Remove(v.dataFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = v.nm.Destroy()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-01-20 11:40:04 +00:00
|
|
|
func (v *Volume) write(n *Needle) (size uint32, err error) {
|
2013-04-15 02:30:26 +00:00
|
|
|
if v.readOnly {
|
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile)
|
|
|
|
return
|
|
|
|
}
|
2011-12-24 08:40:56 +00:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2013-07-12 05:44:59 +00:00
|
|
|
if v.isFileUnchanged(n) {
|
|
|
|
size = n.Size
|
2013-10-31 19:55:19 +00:00
|
|
|
glog.V(4).Infof("needle is unchanged!")
|
2013-07-12 05:44:59 +00:00
|
|
|
return
|
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
var offset int64
|
|
|
|
if offset, err = v.dataFile.Seek(0, 2); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-05 23:05:35 +00:00
|
|
|
|
|
|
|
//ensure file writing starting from aligned positions
|
|
|
|
if offset%NeedlePaddingSize != 0 {
|
|
|
|
offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
|
|
|
|
if offset, err = v.dataFile.Seek(offset, 0); err != nil {
|
2013-10-31 19:55:19 +00:00
|
|
|
glog.V(4).Infof("failed to align in datafile %s: %s", v.dataFile.Name(), err.Error())
|
2013-07-05 23:05:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 03:44:23 +00:00
|
|
|
if size, err = n.Append(v.dataFile, v.Version()); err != nil {
|
2013-02-27 06:54:22 +00:00
|
|
|
if e := v.dataFile.Truncate(offset); e != nil {
|
2013-08-12 23:53:32 +00:00
|
|
|
err = fmt.Errorf("%s\ncannot truncate %s: %s", err, v.dataFile, e.Error())
|
2013-02-27 06:54:22 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
|
|
|
}
|
2012-08-24 06:06:15 +00:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2013-01-20 11:40:04 +00:00
|
|
|
if !ok || int64(nv.Offset)*NeedlePaddingSize < offset {
|
2013-10-31 19:55:19 +00:00
|
|
|
if _, err = v.nm.Put(n.Id, uint32(offset/NeedlePaddingSize), n.Size); err != nil {
|
|
|
|
glog.V(4).Infof("failed to save in needle map %d: %s", n.Id, err.Error())
|
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-07-05 23:05:35 +00:00
|
|
|
|
2013-01-20 11:40:04 +00:00
|
|
|
func (v *Volume) delete(n *Needle) (uint32, error) {
|
2013-04-15 02:30:26 +00:00
|
|
|
if v.readOnly {
|
|
|
|
return 0, fmt.Errorf("%s is read-only", v.dataFile)
|
|
|
|
}
|
2012-01-19 00:49:41 +00:00
|
|
|
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 {
|
2013-07-29 05:49:17 +00:00
|
|
|
size := nv.Size
|
2013-08-09 06:57:22 +00:00
|
|
|
if err := v.nm.Delete(n.Id); err != nil {
|
2013-07-29 05:49:17 +00:00
|
|
|
return size, err
|
2013-02-27 06:54:22 +00:00
|
|
|
}
|
2013-08-09 06:57:22 +00:00
|
|
|
if _, err := v.dataFile.Seek(0, 2); err != nil {
|
2013-07-29 05:49:17 +00:00
|
|
|
return size, err
|
2013-02-27 06:54:22 +00:00
|
|
|
}
|
2013-07-12 07:55:21 +00:00
|
|
|
n.Data = make([]byte, 0)
|
2013-07-29 05:53:25 +00:00
|
|
|
_, err := n.Append(v.dataFile, v.Version())
|
2013-07-29 05:49:17 +00:00
|
|
|
return size, err
|
2012-01-19 00:49:41 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
return 0, nil
|
2012-01-19 00:49:41 +00:00
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
|
2012-06-29 07:53:47 +00:00
|
|
|
func (v *Volume) read(n *Needle) (int, error) {
|
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 {
|
2014-03-19 06:48:01 +00:00
|
|
|
return n.Read(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, 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 {
|
2013-04-18 07:23:14 +00:00
|
|
|
return float64(v.nm.DeletedSize()) / float64(v.ContentSize())
|
2012-11-24 01:03:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-29 05:18:52 +00:00
|
|
|
func (v *Volume) Compact() error {
|
2012-11-07 09:51:43 +00:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
|
|
|
|
|
|
|
filePath := path.Join(v.dir, v.Id.String())
|
2013-01-21 03:44:23 +00:00
|
|
|
return v.copyDataAndGenerateIndexFile(filePath+".cpd", filePath+".cpx")
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
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()
|
2013-02-27 06:54:22 +00:00
|
|
|
_ = 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
|
|
|
}
|
2014-02-07 01:32:06 +00:00
|
|
|
if e = v.load(true, false); 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
|
|
|
}
|
2013-06-20 01:10:38 +00:00
|
|
|
func (v *Volume) freeze() error {
|
|
|
|
if v.readOnly {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
nm, ok := v.nm.(*NeedleMap)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2013-11-18 23:05:11 +00:00
|
|
|
bn, _ := baseFilename(v.dataFile.Name())
|
2013-06-20 01:10:38 +00:00
|
|
|
cdbFn := bn + ".cdb"
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infof("converting %s to %s", nm.indexFile.Name(), cdbFn)
|
2013-06-20 01:10:38 +00:00
|
|
|
err := DumpNeedleMapToCdb(cdbFn, nm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v.nm, err = OpenCdbMap(cdbFn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nm.indexFile.Close()
|
|
|
|
os.Remove(nm.indexFile.Name())
|
|
|
|
v.readOnly = true
|
|
|
|
return nil
|
|
|
|
}
|
2012-11-07 09:51:43 +00:00
|
|
|
|
2013-11-12 10:21:22 +00:00
|
|
|
func ScanVolumeFile(dirname string, collection string, id VolumeId,
|
2013-01-21 03:44:23 +00:00
|
|
|
visitSuperBlock func(SuperBlock) error,
|
2013-09-19 18:06:14 +00:00
|
|
|
visitNeedle func(n *Needle, offset int64) error) (err error) {
|
2013-01-21 03:44:23 +00:00
|
|
|
var v *Volume
|
2013-11-12 10:21:22 +00:00
|
|
|
if v, err = loadVolumeWithoutIndex(dirname, collection, id); err != nil {
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
if err = visitSuperBlock(v.SuperBlock); err != nil {
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 03:44:23 +00:00
|
|
|
version := v.Version()
|
2012-11-07 09:51:43 +00:00
|
|
|
|
2013-09-19 18:06:14 +00:00
|
|
|
offset := int64(SuperBlockSize)
|
2014-03-19 06:48:01 +00:00
|
|
|
n, rest, e := ReadNeedleHeader(v.dataFile, version, offset)
|
2013-01-21 03:44:23 +00:00
|
|
|
if e != nil {
|
|
|
|
err = fmt.Errorf("cannot read needle header: %s", e)
|
|
|
|
return
|
2013-01-20 11:40:04 +00:00
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
for n != nil {
|
2014-03-19 06:48:01 +00:00
|
|
|
offset += int64(NeedleHeaderSize)
|
|
|
|
if err = n.ReadNeedleBody(v.dataFile, version, offset, rest); err != nil {
|
2013-01-21 03:44:23 +00:00
|
|
|
err = fmt.Errorf("cannot read needle body: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = visitNeedle(n, offset); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-03-19 06:48:01 +00:00
|
|
|
offset += int64(rest)
|
|
|
|
if n, rest, err = ReadNeedleHeader(v.dataFile, version, offset); err != nil {
|
2013-01-21 03:44:23 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("cannot read needle header: %s", err)
|
|
|
|
}
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 03:44:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) copyDataAndGenerateIndexFile(dstName, idxName string) (err error) {
|
|
|
|
var (
|
|
|
|
dst, idx *os.File
|
|
|
|
)
|
2014-03-10 01:50:09 +00:00
|
|
|
if dst, err = os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
defer dst.Close()
|
2012-12-21 06:32:21 +00:00
|
|
|
|
2014-03-10 01:50:09 +00:00
|
|
|
if idx, err = os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
defer idx.Close()
|
|
|
|
|
2013-09-19 18:06:14 +00:00
|
|
|
new_offset := int64(SuperBlockSize)
|
2013-01-21 03:44:23 +00:00
|
|
|
|
2013-11-12 10:21:22 +00:00
|
|
|
err = ScanVolumeFile(v.dir, v.Collection, v.Id, func(superBlock SuperBlock) error {
|
2013-01-21 03:44:23 +00:00
|
|
|
_, err = dst.Write(superBlock.Bytes())
|
|
|
|
return err
|
2013-09-19 18:06:14 +00:00
|
|
|
}, func(n *Needle, offset int64) error {
|
2012-11-07 09:51:43 +00:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2013-08-09 06:57:22 +00:00
|
|
|
//glog.V(0).Infoln("file size is", n.Size, "rest", rest)
|
2014-03-19 06:48:01 +00:00
|
|
|
if ok && int64(nv.Offset)*NeedlePaddingSize == offset && nv.Size > 0 {
|
|
|
|
if _, err = n.Append(dst, v.Version()); err != nil {
|
|
|
|
return fmt.Errorf("cannot append needle: %s", err)
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
2014-03-19 06:48:01 +00:00
|
|
|
new_offset += n.DiskSize()
|
|
|
|
//glog.V(0).Infoln("saving key", n.Id, "volume offset", old_offset, "=>", new_offset, "data_size", n.Size, "rest", rest)
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
return nil
|
|
|
|
})
|
2012-11-07 09:51:43 +00:00
|
|
|
|
2013-01-21 03:44:23 +00:00
|
|
|
return
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
2012-12-21 06:32:21 +00:00
|
|
|
func (v *Volume) ContentSize() uint64 {
|
2013-04-18 07:23:14 +00:00
|
|
|
return v.nm.ContentSize()
|
2012-12-04 06:54:08 +00:00
|
|
|
}
|
2013-08-12 23:53:32 +00:00
|
|
|
|
|
|
|
func checkFile(filename string) (exists, canRead, canWrite bool, modTime time.Time) {
|
|
|
|
exists = true
|
|
|
|
fi, err := os.Stat(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
exists = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fi.Mode()&0400 != 0 {
|
|
|
|
canRead = true
|
|
|
|
}
|
|
|
|
if fi.Mode()&0200 != 0 {
|
|
|
|
canWrite = true
|
|
|
|
}
|
|
|
|
modTime = fi.ModTime()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func (v *Volume) ensureConvertIdxToCdb(fileName string) (cdbCanRead bool) {
|
|
|
|
var indexFile *os.File
|
|
|
|
var e error
|
|
|
|
_, cdbCanRead, cdbCanWrite, cdbModTime := checkFile(fileName + ".cdb")
|
|
|
|
_, idxCanRead, _, idxModeTime := checkFile(fileName + ".idx")
|
|
|
|
if cdbCanRead && cdbModTime.After(idxModeTime) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !cdbCanWrite {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !idxCanRead {
|
|
|
|
glog.V(0).Infoln("Can not read file", fileName+".idx!")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
glog.V(2).Infoln("opening file", fileName+".idx")
|
|
|
|
if indexFile, e = os.Open(fileName + ".idx"); e != nil {
|
|
|
|
glog.V(0).Infoln("Failed to read file", fileName+".idx !")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer indexFile.Close()
|
|
|
|
glog.V(0).Infof("converting %s.idx to %s.cdb", fileName, fileName)
|
|
|
|
if e = ConvertIndexToCdb(fileName+".cdb", indexFile); e != nil {
|
|
|
|
glog.V(0).Infof("error converting %s.idx to %s.cdb: %s", fileName, fileName, e.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|