do not use buffered reader, since sometimes cannot read file content out

pull/820/head
Xiang Li 2013-07-24 17:14:21 -07:00
parent 085da0973e
commit ee265c7ead
2 changed files with 12 additions and 13 deletions

14
log.go
View File

@ -160,25 +160,23 @@ func (l *Log) open(path string) error {
}
debugln("log.open.exist ", path)
reader := bufio.NewReader(l.file)
// Read the file and decode entries.
for {
if _, err := reader.Peek(1); err == io.EOF {
debugln("open.log.append: finish ")
break
}
// Instantiate log entry and decode into it.
entry, _ := newLogEntry(l, 0, 0, nil)
entry.Position, _ = l.file.Seek(0, os.SEEK_CUR)
n, err := entry.decode(reader)
n, err := entry.decode(l.file)
if err != nil {
if err == io.EOF {
debugln("open.log.append: finish ")
break
}
if err = os.Truncate(path, readBytes); err != nil {
return fmt.Errorf("raft.Log: Unable to recover: %v", err)
}
break
}
// Append entry.

View File

@ -66,7 +66,7 @@ func (e *LogEntry) encode(w io.Writer) (int, error) {
return -1, err
}
_, err = fmt.Fprintf(w, "%x\n", len(p.Bytes()))
_, err = fmt.Fprintf(w, "%8x\n", len(p.Bytes()))
if err != nil {
return -1, err
@ -80,17 +80,17 @@ func (e *LogEntry) encode(w io.Writer) (int, error) {
func (e *LogEntry) decode(r io.Reader) (int, error) {
var length int
_, err := fmt.Fscanf(r, "%x\n", &length)
_, err := fmt.Fscanf(r, "%8x\n", &length)
if err != nil {
return -1, err
}
data := make([]byte, length)
_, err = r.Read(data)
num, err := r.Read(data)
fmt.Println(data, " ", num)
if err != nil {
panic(err)
return -1, err
}
@ -99,6 +99,7 @@ func (e *LogEntry) decode(r io.Reader) (int, error) {
err = p.Unmarshal(pb)
if err != nil {
panic(err)
return -1, err
}