Browse Source

Merge pull request #2402 from calmh/truncfaster

Don't load block list in ...Truncated methods
Audrius Butkevicius 10 years ago
parent
commit
967424a538
2 changed files with 35 additions and 9 deletions
  1. 31 9
      lib/db/truncated.go
  2. 4 0
      lib/protocol/message.go

+ 31 - 9
lib/db/truncated.go

@@ -6,22 +6,44 @@
 
 package db
 
-import "github.com/syncthing/syncthing/lib/protocol"
+import (
+	"bytes"
+
+	"github.com/calmh/xdr"
+	"github.com/syncthing/syncthing/lib/protocol"
+)
 
 type FileInfoTruncated struct {
 	protocol.FileInfo
-	ActualSize int64
 }
 
-func (f *FileInfoTruncated) UnmarshalXDR(bs []byte) error {
-	err := f.FileInfo.UnmarshalXDR(bs)
-	f.ActualSize = f.FileInfo.Size()
-	f.FileInfo.Blocks = nil
-	return err
+func (o *FileInfoTruncated) UnmarshalXDR(bs []byte) error {
+	var br = bytes.NewReader(bs)
+	var xr = xdr.NewReader(br)
+	return o.DecodeXDRFrom(xr)
 }
 
-func (f FileInfoTruncated) Size() int64 {
-	return f.ActualSize
+func (o *FileInfoTruncated) DecodeXDRFrom(xr *xdr.Reader) error {
+	o.Name = xr.ReadStringMax(8192)
+	o.Flags = xr.ReadUint32()
+	o.Modified = int64(xr.ReadUint64())
+	(&o.Version).DecodeXDRFrom(xr)
+	o.LocalVersion = int64(xr.ReadUint64())
+	_BlocksSize := int(xr.ReadUint32())
+	if _BlocksSize < 0 {
+		return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000)
+	}
+	if _BlocksSize > 1000000 {
+		return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000)
+	}
+
+	buf := make([]byte, 64)
+	for i := 0; i < _BlocksSize; i++ {
+		size := xr.ReadUint32()
+		o.CachedSize += int64(size)
+		xr.ReadBytesMaxInto(64, buf)
+	}
+	return xr.Error()
 }
 
 func BlocksToSize(num int) int64 {

+ 4 - 0
lib/protocol/message.go

@@ -33,9 +33,13 @@ func (f FileInfo) Size() (bytes int64) {
 	if f.IsDeleted() || f.IsDirectory() {
 		return 128
 	}
+	if f.CachedSize > 0 {
+		return f.CachedSize
+	}
 	for _, b := range f.Blocks {
 		bytes += int64(b.Size)
 	}
+	f.CachedSize = bytes
 	return
 }