2011-12-16 22:51:26 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2013-07-14 04:51:47 +08:00
|
|
|
"bytes"
|
2012-11-07 17:51:43 +08:00
|
|
|
"errors"
|
2012-11-20 17:45:36 +08:00
|
|
|
"fmt"
|
2013-01-21 11:44:23 +08:00
|
|
|
"io"
|
2013-04-15 10:30:26 +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
|
|
|
)
|
|
|
|
|
2011-12-24 16:40:56 +08:00
|
|
|
const (
|
2012-01-19 08:49:41 +08:00
|
|
|
SuperBlockSize = 8
|
2011-12-24 16:40:56 +08:00
|
|
|
)
|
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
type SuperBlock struct {
|
|
|
|
Version Version
|
|
|
|
ReplicaType ReplicationType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SuperBlock) Bytes() []byte {
|
|
|
|
header := make([]byte, SuperBlockSize)
|
|
|
|
header[0] = byte(s.Version)
|
|
|
|
header[1] = s.ReplicaType.Byte()
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
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
|
2013-04-18 15:23:14 +08:00
|
|
|
nm NeedleMapper
|
2013-04-15 10:30:26 +08:00
|
|
|
readOnly bool
|
2011-12-16 22:51:26 +08:00
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
SuperBlock
|
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
|
|
|
}
|
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
func NewVolume(dirname string, id VolumeId, replicationType ReplicationType) (v *Volume, e error) {
|
2013-01-21 11:44:23 +08:00
|
|
|
v = &Volume{dir: dirname, Id: id}
|
|
|
|
v.SuperBlock = SuperBlock{ReplicaType: replicationType}
|
|
|
|
e = v.load(true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func LoadVolumeOnly(dirname string, id VolumeId) (v *Volume, e error) {
|
|
|
|
v = &Volume{dir: dirname, Id: id}
|
|
|
|
v.SuperBlock = SuperBlock{ReplicaType: CopyNil}
|
|
|
|
e = v.load(false)
|
2012-11-07 17:51:43 +08:00
|
|
|
return
|
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
func (v *Volume) load(alsoLoadIndex bool) 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 {
|
2013-04-15 10:30:26 +08:00
|
|
|
if !os.IsPermission(e) {
|
|
|
|
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
|
|
|
|
}
|
|
|
|
if v.dataFile, e = os.Open(fileName + ".dat"); e != nil {
|
|
|
|
return fmt.Errorf("cannot open Volume Data %s.dat: %s", fileName, e)
|
|
|
|
}
|
|
|
|
log.Printf("opening " + fileName + ".dat in READONLY mode")
|
|
|
|
v.readOnly = true
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
if v.ReplicaType == CopyNil {
|
2013-02-11 06:00:06 +08:00
|
|
|
e = v.readSuperBlock()
|
2012-09-13 16:33:47 +08:00
|
|
|
} else {
|
2013-02-11 06:00:06 +08:00
|
|
|
e = v.maybeWriteSuperBlock()
|
2012-09-13 16:33:47 +08:00
|
|
|
}
|
2013-02-11 06:00:06 +08:00
|
|
|
if e == nil && alsoLoadIndex {
|
2013-06-20 09:10:38 +08:00
|
|
|
var indexFile *os.File
|
|
|
|
if v.readOnly {
|
|
|
|
if indexFile, e = os.Open(fileName + ".idx"); e != nil && !os.IsNotExist(e) {
|
|
|
|
return fmt.Errorf("cannot open index file %s.idx: %s", fileName, e)
|
|
|
|
}
|
|
|
|
if indexFile != nil {
|
|
|
|
log.Printf("converting %s.idx to %s.cdb", fileName, fileName)
|
|
|
|
if e = ConvertIndexToCdb(fileName+".cdb", indexFile); e != nil {
|
|
|
|
log.Printf("error converting %s.idx to %s.cdb: %s", fileName, fileName)
|
|
|
|
} else {
|
|
|
|
indexFile.Close()
|
|
|
|
os.Remove(indexFile.Name())
|
|
|
|
indexFile = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.nm, e = OpenCdbMap(fileName + ".cdb")
|
|
|
|
return e
|
|
|
|
} else {
|
|
|
|
indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
if e != nil {
|
|
|
|
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 06:00:06 +08:00
|
|
|
v.nm, e = LoadNeedleMap(indexFile)
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-02-11 06:00:06 +08:00
|
|
|
return e
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2012-12-18 09:51:39 +08:00
|
|
|
func (v *Volume) Version() Version {
|
2013-01-21 11:44:23 +08:00
|
|
|
return v.SuperBlock.Version
|
2012-12-18 09:51:39 +08:00
|
|
|
}
|
2011-12-18 15:22:04 +08:00
|
|
|
func (v *Volume) Size() int64 {
|
2012-12-21 14:32:21 +08:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
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
|
|
|
}
|
2013-07-14 10:44:24 +08:00
|
|
|
log.Printf("Failed to read file size %s %s\n", v.dataFile.Name(), e.Error())
|
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() {
|
2012-12-21 14:32:21 +08:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2011-12-22 12:04:47 +08:00
|
|
|
v.nm.Close()
|
2013-02-27 14:54:22 +08:00
|
|
|
_ = v.dataFile.Close()
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-02-11 06:00:06 +08:00
|
|
|
func (v *Volume) maybeWriteSuperBlock() error {
|
2013-01-20 19:40:04 +08:00
|
|
|
stat, e := v.dataFile.Stat()
|
|
|
|
if e != nil {
|
2013-07-14 10:44:24 +08:00
|
|
|
log.Printf("failed to stat datafile %s: %s", v.dataFile, e)
|
2013-02-11 06:00:06 +08:00
|
|
|
return e
|
2013-01-20 19:40:04 +08:00
|
|
|
}
|
2012-06-29 15:53:47 +08:00
|
|
|
if stat.Size() == 0 {
|
2013-01-21 11:44:23 +08:00
|
|
|
v.SuperBlock.Version = CurrentVersion
|
2013-02-11 06:00:06 +08:00
|
|
|
_, e = v.dataFile.Write(v.SuperBlock.Bytes())
|
2013-04-15 10:30:26 +08: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 16:40:56 +08:00
|
|
|
}
|
2013-02-11 06:00:06 +08:00
|
|
|
return e
|
2011-12-24 16:40:56 +08:00
|
|
|
}
|
2013-01-17 16:56:56 +08:00
|
|
|
func (v *Volume) readSuperBlock() (err error) {
|
2013-02-27 14:54:22 +08:00
|
|
|
if _, err = v.dataFile.Seek(0, 0); err != nil {
|
|
|
|
return fmt.Errorf("cannot seek to the beginning of %s: %s", v.dataFile, err)
|
|
|
|
}
|
2012-09-13 16:33:47 +08:00
|
|
|
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)
|
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
v.SuperBlock, err = ParseSuperBlock(header)
|
2012-12-21 14:32:21 +08:00
|
|
|
return err
|
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
func ParseSuperBlock(header []byte) (superBlock SuperBlock, err error) {
|
|
|
|
superBlock.Version = Version(header[0])
|
|
|
|
if superBlock.ReplicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
|
2013-01-17 16:56:56 +08:00
|
|
|
err = fmt.Errorf("cannot read replica type: %s", err)
|
2012-09-13 16:33:47 +08:00
|
|
|
}
|
2012-12-21 14:32:21 +08:00
|
|
|
return
|
2012-09-13 16:33:47 +08:00
|
|
|
}
|
2012-11-20 16:54:37 +08:00
|
|
|
func (v *Volume) NeedToReplicate() bool {
|
2013-01-21 11:44:23 +08:00
|
|
|
return v.ReplicaType.GetCopyCount() > 1
|
2012-11-12 17:26:18 +08:00
|
|
|
}
|
2011-12-16 22:51:26 +08:00
|
|
|
|
2013-07-12 13:44:59 +08:00
|
|
|
func (v *Volume) isFileUnchanged(n *Needle) bool {
|
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
if ok && nv.Offset > 0 {
|
|
|
|
if _, err := v.dataFile.Seek(int64(nv.Offset)*NeedlePaddingSize, 0); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
oldNeedle := new(Needle)
|
|
|
|
oldNeedle.Read(v.dataFile, nv.Size, v.Version())
|
2013-07-14 04:51:47 +08:00
|
|
|
if oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
|
2013-07-12 13:44:59 +08:00
|
|
|
n.Size = oldNeedle.Size
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
func (v *Volume) write(n *Needle) (size uint32, err error) {
|
2013-04-15 10:30:26 +08:00
|
|
|
if v.readOnly {
|
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile)
|
|
|
|
return
|
|
|
|
}
|
2011-12-24 16:40:56 +08:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2013-07-12 13:44:59 +08:00
|
|
|
if v.isFileUnchanged(n) {
|
|
|
|
size = n.Size
|
|
|
|
return
|
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
var offset int64
|
|
|
|
if offset, err = v.dataFile.Seek(0, 2); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-06 07:05:35 +08: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 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
if size, err = n.Append(v.dataFile, v.Version()); err != nil {
|
2013-02-27 14:54:22 +08:00
|
|
|
if e := v.dataFile.Truncate(offset); e != nil {
|
|
|
|
err = fmt.Errorf("%s\ncannot truncate %s: %s", err, v.dataFile, e)
|
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
|
|
|
}
|
2012-08-24 14:06:15 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2013-01-20 19:40:04 +08:00
|
|
|
if !ok || int64(nv.Offset)*NeedlePaddingSize < offset {
|
|
|
|
_, err = v.nm.Put(n.Id, uint32(offset/NeedlePaddingSize), n.Size)
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-07-06 07:05:35 +08:00
|
|
|
|
2013-01-20 19:40:04 +08:00
|
|
|
func (v *Volume) delete(n *Needle) (uint32, error) {
|
2013-04-15 10:30:26 +08:00
|
|
|
if v.readOnly {
|
|
|
|
return 0, fmt.Errorf("%s is read-only", v.dataFile)
|
|
|
|
}
|
2012-01-19 08:49:41 +08:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2012-08-24 14:06:15 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2013-01-03 07:39:55 +08:00
|
|
|
//fmt.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 {
|
2013-07-29 13:49:17 +08:00
|
|
|
size := nv.Size
|
2013-07-29 13:53:25 +08:00
|
|
|
if err:= v.nm.Delete(n.Id); err != nil {
|
2013-07-29 13:49:17 +08:00
|
|
|
return size, err
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
2013-07-29 13:53:25 +08:00
|
|
|
if _, err:= v.dataFile.Seek(0, 2); err != nil {
|
2013-07-29 13:49:17 +08:00
|
|
|
return size, err
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
2013-07-12 15:55:21 +08:00
|
|
|
n.Data = make([]byte, 0)
|
2013-07-29 13:53:25 +08:00
|
|
|
_, err := n.Append(v.dataFile, v.Version())
|
2013-07-29 13:49:17 +08:00
|
|
|
return size, err
|
2012-01-19 08:49:41 +08:00
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
return 0, nil
|
2012-01-19 08:49:41 +08:00
|
|
|
}
|
2012-11-24 09:03:27 +08:00
|
|
|
|
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 {
|
2013-02-27 14:54:22 +08:00
|
|
|
if _, err := v.dataFile.Seek(int64(nv.Offset)*NeedlePaddingSize, 0); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2013-01-21 11:44:23 +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
|
|
|
|
2012-11-24 09:03:27 +08:00
|
|
|
func (v *Volume) garbageLevel() float64 {
|
2013-04-18 15:23:14 +08:00
|
|
|
return float64(v.nm.DeletedSize()) / float64(v.ContentSize())
|
2012-11-24 09:03:27 +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())
|
2013-01-21 11:44:23 +08:00
|
|
|
return v.copyDataAndGenerateIndexFile(filePath+".cpd", filePath+".cpx")
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
2012-12-21 14:32:21 +08:00
|
|
|
func (v *Volume) commitCompact() error {
|
2012-11-07 17:51:43 +08:00
|
|
|
v.accessLock.Lock()
|
|
|
|
defer v.accessLock.Unlock()
|
2013-02-27 14:54:22 +08:00
|
|
|
_ = 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 {
|
2012-11-24 09:03:27 +08:00
|
|
|
return e
|
2012-11-20 17:45:36 +08: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 09:03:27 +08:00
|
|
|
return e
|
2012-11-20 17:45:36 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
if e = v.load(true); e != nil {
|
2012-11-24 09:03:27 +08:00
|
|
|
return e
|
2012-11-20 17:45:36 +08:00
|
|
|
}
|
2012-11-24 09:03:27 +08:00
|
|
|
return nil
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
2013-06-20 09:10:38 +08: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()
|
|
|
|
bn, _ := nakeFilename(v.dataFile.Name())
|
|
|
|
cdbFn := bn + ".cdb"
|
|
|
|
log.Printf("converting %s to %s", nm.indexFile.Name(), cdbFn)
|
|
|
|
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 17:51:43 +08:00
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
func ScanVolumeFile(dirname string, id VolumeId,
|
|
|
|
visitSuperBlock func(SuperBlock) error,
|
|
|
|
visitNeedle func(n *Needle, offset uint32) error) (err error) {
|
|
|
|
var v *Volume
|
|
|
|
if v, err = LoadVolumeOnly(dirname, id); err != nil {
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
if err = visitSuperBlock(v.SuperBlock); err != nil {
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
version := v.Version()
|
2012-11-07 17:51:43 +08:00
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
offset := uint32(SuperBlockSize)
|
|
|
|
n, rest, e := ReadNeedleHeader(v.dataFile, version)
|
|
|
|
if e != nil {
|
|
|
|
err = fmt.Errorf("cannot read needle header: %s", e)
|
|
|
|
return
|
2013-01-20 19:40:04 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
for n != nil {
|
|
|
|
if err = n.ReadNeedleBody(v.dataFile, version, rest); err != nil {
|
|
|
|
err = fmt.Errorf("cannot read needle body: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = visitNeedle(n, offset); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
offset += NeedleHeaderSize + rest
|
|
|
|
if n, rest, err = ReadNeedleHeader(v.dataFile, version); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("cannot read needle header: %s", err)
|
|
|
|
}
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) copyDataAndGenerateIndexFile(dstName, idxName string) (err error) {
|
|
|
|
var (
|
|
|
|
dst, idx *os.File
|
|
|
|
)
|
|
|
|
if dst, err = os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644); err != nil {
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
defer dst.Close()
|
2012-12-21 14:32:21 +08:00
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
if idx, err = os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE, 0644); err != nil {
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
defer idx.Close()
|
|
|
|
|
2012-11-07 17:51:43 +08:00
|
|
|
nm := NewNeedleMap(idx)
|
2012-11-20 16:54:37 +08:00
|
|
|
new_offset := uint32(SuperBlockSize)
|
2013-01-21 11:44:23 +08:00
|
|
|
|
|
|
|
err = ScanVolumeFile(v.dir, v.Id, func(superBlock SuperBlock) error {
|
|
|
|
_, err = dst.Write(superBlock.Bytes())
|
|
|
|
return err
|
|
|
|
}, func(n *Needle, offset uint32) error {
|
2012-11-07 17:51:43 +08:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
//log.Println("file size is", n.Size, "rest", rest)
|
2013-01-21 11:44:23 +08:00
|
|
|
if ok && nv.Offset*NeedlePaddingSize == offset {
|
2012-11-07 17:51:43 +08:00
|
|
|
if nv.Size > 0 {
|
2013-01-20 19:40:04 +08:00
|
|
|
if _, err = nm.Put(n.Id, new_offset/NeedlePaddingSize, n.Size); err != nil {
|
|
|
|
return fmt.Errorf("cannot put needle: %s", err)
|
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
if _, err = n.Append(dst, v.Version()); err != nil {
|
2013-01-20 19:40:04 +08:00
|
|
|
return fmt.Errorf("cannot append needle: %s", err)
|
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
new_offset += n.DiskSize()
|
2012-11-24 10:07:43 +08:00
|
|
|
//log.Println("saving key", n.Id, "volume offset", old_offset, "=>", new_offset, "data_size", n.Size, "rest", rest)
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
return nil
|
|
|
|
})
|
2012-11-07 17:51:43 +08:00
|
|
|
|
2013-01-21 11:44:23 +08:00
|
|
|
return
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
2012-12-21 14:32:21 +08:00
|
|
|
func (v *Volume) ContentSize() uint64 {
|
2013-04-18 15:23:14 +08:00
|
|
|
return v.nm.ContentSize()
|
2012-12-04 14:54:08 +08:00
|
|
|
}
|