Merge pull request #2402 from calmh/truncfaster

Don't load block list in ...Truncated methods
This commit is contained in:
Audrius Butkevicius 2015-10-21 23:03:48 +01:00
commit 967424a538
2 changed files with 35 additions and 9 deletions

View File

@ -6,22 +6,44 @@
package db package db
import "github.com/syncthing/syncthing/lib/protocol" import (
"bytes"
"github.com/calmh/xdr"
"github.com/syncthing/syncthing/lib/protocol"
)
type FileInfoTruncated struct { type FileInfoTruncated struct {
protocol.FileInfo protocol.FileInfo
ActualSize int64
} }
func (f *FileInfoTruncated) UnmarshalXDR(bs []byte) error { func (o *FileInfoTruncated) UnmarshalXDR(bs []byte) error {
err := f.FileInfo.UnmarshalXDR(bs) var br = bytes.NewReader(bs)
f.ActualSize = f.FileInfo.Size() var xr = xdr.NewReader(br)
f.FileInfo.Blocks = nil return o.DecodeXDRFrom(xr)
return err
} }
func (f FileInfoTruncated) Size() int64 { func (o *FileInfoTruncated) DecodeXDRFrom(xr *xdr.Reader) error {
return f.ActualSize 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 { func BlocksToSize(num int) int64 {

View File

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