seaweedfs/weed/storage/erasure_coding/ec_volume_delete.go

99 lines
2.0 KiB
Go
Raw Normal View History

2019-06-20 13:57:14 +08:00
package erasure_coding
import (
"fmt"
"io"
"os"
"github.com/chrislusf/seaweedfs/weed/storage/types"
"github.com/chrislusf/seaweedfs/weed/util"
)
var (
MarkNeedleDeleted = func(file *os.File, offset int64) error {
2019-06-20 13:57:14 +08:00
b := make([]byte, types.SizeSize)
2020-08-19 10:22:16 +08:00
types.SizeToBytes(b, types.TombstoneFileSize)
2019-06-20 13:57:14 +08:00
n, err := file.WriteAt(b, offset+types.NeedleIdSize+types.OffsetSize)
if err != nil {
return fmt.Errorf("sorted needle write error: %v", err)
2019-06-20 13:57:14 +08:00
}
if n != types.SizeSize {
return fmt.Errorf("sorted needle written %d bytes, expecting %d", n, types.SizeSize)
2019-06-20 13:57:14 +08:00
}
return nil
}
)
2019-06-20 15:17:11 +08:00
func (ev *EcVolume) DeleteNeedleFromEcx(needleId types.NeedleId) (err error) {
2019-06-20 13:57:14 +08:00
_, _, err = SearchNeedleFromSortedIndex(ev.ecxFile, ev.ecxFileSize, needleId, MarkNeedleDeleted)
2019-06-20 13:57:14 +08:00
if err != nil {
if err == NotFoundError {
return nil
}
return err
}
b := make([]byte, types.NeedleIdSize)
types.NeedleIdToBytes(b, needleId)
ev.ecjFileAccessLock.Lock()
ev.ecjFile.Seek(0, io.SeekEnd)
ev.ecjFile.Write(b)
ev.ecjFileAccessLock.Unlock()
return
}
func RebuildEcxFile(baseFileName string) error {
if !util.FileExists(baseFileName + ".ecj") {
return nil
}
ecxFile, err := os.OpenFile(baseFileName+".ecx", os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("rebuild: failed to open ecx file: %v", err)
}
defer ecxFile.Close()
fstat, err := ecxFile.Stat()
if err != nil {
return err
}
ecxFileSize := fstat.Size()
ecjFile, err := os.OpenFile(baseFileName+".ecj", os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("rebuild: failed to open ecj file: %v", err)
}
buf := make([]byte, types.NeedleIdSize)
for {
n, _ := ecjFile.Read(buf)
if n != types.NeedleIdSize {
break
}
needleId := types.BytesToNeedleId(buf)
_, _, err = SearchNeedleFromSortedIndex(ecxFile, ecxFileSize, needleId, MarkNeedleDeleted)
2019-06-20 13:57:14 +08:00
if err != nil && err != NotFoundError {
ecxFile.Close()
return err
}
}
ecxFile.Close()
os.Remove(baseFileName + ".ecj")
return nil
}