handle cases when .idx files are also readonly

adjusting log level
This commit is contained in:
Chris Lu 2013-08-11 11:38:55 -07:00
parent 27f04a382a
commit 7cef280bdc
5 changed files with 27 additions and 17 deletions

View file

@ -206,12 +206,12 @@ func DumpNeedleMapToCdb(cdbName string, nm *NeedleMap) error {
func openTempCdb(fileName string) (cdb.AdderFunc, cdb.CloserFunc, error) {
fh, err := os.Create(fileName)
if err != nil {
return nil, nil, fmt.Errorf("cannot create cdb file %s: %s", fileName, err)
return nil, nil, fmt.Errorf("cannot create cdb file %s: %s", fileName, err.Error())
}
adder, closer, err := cdb.MakeFactory(fh)
if err != nil {
fh.Close()
return nil, nil, fmt.Errorf("error creating factory: %s", err)
return nil, nil, fmt.Errorf("error creating factory: %s", err.Error())
}
return adder, func() error {
if e := closer(); e != nil {

View file

@ -37,11 +37,11 @@ func GzipData(input []byte) ([]byte, error) {
buf := new(bytes.Buffer)
w, _ := gzip.NewWriterLevel(buf, flate.BestCompression)
if _, err := w.Write(input); err != nil {
glog.V(4).Infoln("error compressing data:", err)
glog.V(2).Infoln("error compressing data:", err)
return nil, err
}
if err := w.Close(); err != nil {
glog.V(4).Infoln("error closing compressed data:", err)
glog.V(2).Infoln("error closing compressed data:", err)
return nil, err
}
return buf.Bytes(), nil
@ -52,7 +52,7 @@ func UnGzipData(input []byte) ([]byte, error) {
defer r.Close()
output, err := ioutil.ReadAll(r)
if err != nil {
glog.V(4).Infoln("error uncompressing data:", err)
glog.V(2).Infoln("error uncompressing data:", err)
}
return output, err
}

View file

@ -19,7 +19,7 @@ func NewFileId(VolumeId VolumeId, Key uint64, Hashcode uint32) *FileId {
func ParseFileId(fid string) *FileId {
a := strings.Split(fid, ",")
if len(a) != 2 {
glog.V(4).Infoln("Invalid fid", fid, ", split length", len(a))
glog.V(1).Infoln("Invalid fid", fid, ", split length", len(a))
return nil
}
vid_string, key_hash_string := a[0], a[1]

View file

@ -73,12 +73,12 @@ func (v *Volume) load(alsoLoadIndex bool) error {
if e == nil && alsoLoadIndex {
var indexFile *os.File
if v.readOnly {
glog.V(4).Infoln("opening file", fileName+".idx")
glog.V(2).Infoln("opening file", fileName+".idx")
if indexFile, e = os.Open(fileName + ".idx"); e != nil && !os.IsNotExist(e) {
return fmt.Errorf("cannot open index file %s.idx: %s", fileName, e.Error())
}
if indexFile != nil {
glog.V(4).Infoln("check file", fileName+".cdb")
glog.V(2).Infoln("check file", fileName+".cdb")
if _, err := os.Stat(fileName + ".cdb"); os.IsNotExist(err) {
glog.V(0).Infof("converting %s.idx to %s.cdb", fileName, fileName)
if e = ConvertIndexToCdb(fileName+".cdb", indexFile); e != nil {
@ -89,7 +89,7 @@ func (v *Volume) load(alsoLoadIndex bool) error {
}
}
}
glog.V(4).Infoln("open file", fileName+".cdb")
glog.V(2).Infoln("open file", fileName+".cdb")
if v.nm, e = OpenCdbMap(fileName + ".cdb"); e != nil {
if os.IsNotExist(e) {
glog.V(0).Infof("Failed to read cdb file :%s, fall back to normal readonly mode.", fileName)
@ -99,14 +99,23 @@ func (v *Volume) load(alsoLoadIndex bool) error {
}
}
}
glog.V(4).Infoln("open to write file", fileName+".idx")
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.Error())
if v.readOnly {
glog.V(1).Infoln("open to read file", fileName+".idx")
indexFile, e = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644)
if e != nil {
return fmt.Errorf("cannot read Volume Data %s.dat: %s", fileName, e.Error())
}
} else {
glog.V(1).Infoln("open to write file", fileName+".idx")
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.Error())
}
}
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)
}
glog.V(4).Infoln("loading file", fileName+".idx")
v.nm, e = LoadNeedleMap(indexFile)
glog.V(4).Infoln("loading error:", e)
}
return e
}

View file

@ -1,11 +1,12 @@
package main
import (
"code.google.com/p/weed-fs/go/glog"
"net/http"
"testing"
"time"
)
func TestXYZ(t *testing.T) {
glog.V(4).Infoln("Last-Modified", time.Unix(int64(1373273596), 0).UTC().Format(http.TimeFormat))
glog.V(0).Infoln("Last-Modified", time.Unix(int64(1373273596), 0).UTC().Format(http.TimeFormat))
}