Implement GetGlobalTruncated

This commit is contained in:
Jakob Borg 2015-01-09 08:41:02 +01:00
parent 4c4143d9be
commit d46ce5003c
4 changed files with 24 additions and 22 deletions

View File

@ -610,7 +610,7 @@ func ldbGet(db *leveldb.DB, folder, device, file []byte) (protocol.FileInfo, boo
return f, true return f, true
} }
func ldbGetGlobal(db *leveldb.DB, folder, file []byte) (protocol.FileInfo, bool) { func ldbGetGlobal(db *leveldb.DB, folder, file []byte, truncate bool) (FileIntf, bool) {
k := globalKey(folder, file) k := globalKey(folder, file)
snap, err := db.GetSnapshot() snap, err := db.GetSnapshot()
if err != nil { if err != nil {
@ -631,7 +631,7 @@ func ldbGetGlobal(db *leveldb.DB, folder, file []byte) (protocol.FileInfo, bool)
} }
bs, err := snap.Get(k, nil) bs, err := snap.Get(k, nil)
if err == leveldb.ErrNotFound { if err == leveldb.ErrNotFound {
return protocol.FileInfo{}, false return nil, false
} }
if err != nil { if err != nil {
panic(err) panic(err)
@ -656,12 +656,11 @@ func ldbGetGlobal(db *leveldb.DB, folder, file []byte) (protocol.FileInfo, bool)
panic(err) panic(err)
} }
var f protocol.FileInfo fi, err := unmarshalTrunc(bs, truncate)
err = f.UnmarshalXDR(bs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return f, true return fi, true
} }
func ldbWithGlobal(db *leveldb.DB, folder []byte, truncate bool, fn Iterator) { func ldbWithGlobal(db *leveldb.DB, folder []byte, truncate bool, fn Iterator) {

View File

@ -189,9 +189,23 @@ func (s *Set) Get(device protocol.DeviceID, file string) (protocol.FileInfo, boo
} }
func (s *Set) GetGlobal(file string) (protocol.FileInfo, bool) { func (s *Set) GetGlobal(file string) (protocol.FileInfo, bool) {
f, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file))) fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
if !ok {
return protocol.FileInfo{}, false
}
f := fi.(protocol.FileInfo)
f.Name = osutil.NativeFilename(f.Name) f.Name = osutil.NativeFilename(f.Name)
return f, ok return f, true
}
func (s *Set) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
if !ok {
return FileInfoTruncated{}, false
}
f := fi.(FileInfoTruncated)
f.Name = osutil.NativeFilename(f.Name)
return f, true
} }
func (s *Set) Availability(file string) []protocol.DeviceID { func (s *Set) Availability(file string) []protocol.DeviceID {

View File

@ -67,17 +67,6 @@ func (f FileInfoTruncated) HasPermissionBits() bool {
return f.Flags&protocol.FlagNoPermBits == 0 return f.Flags&protocol.FlagNoPermBits == 0
} }
func Truncate(f protocol.FileInfo) FileInfoTruncated {
return FileInfoTruncated{
Name: f.Name,
Flags: f.Flags,
Modified: f.Modified,
Version: f.Version,
LocalVersion: f.LocalVersion,
NumBlocks: uint32(len(f.Blocks)),
}
}
func BlocksToSize(num uint32) int64 { func BlocksToSize(num uint32) int64 {
if num < 2 { if num < 2 {
return protocol.BlockSize / 2 return protocol.BlockSize / 2

View File

@ -440,15 +440,15 @@ func (m *Model) NeedFolderFiles(folder string, max int) ([]files.FileInfoTruncat
seen = make(map[string]bool, len(progressNames)+len(queuedNames)) seen = make(map[string]bool, len(progressNames)+len(queuedNames))
for i, name := range progressNames { for i, name := range progressNames {
if f, ok := rf.GetGlobal(name); ok { if f, ok := rf.GetGlobalTruncated(name); ok {
progress[i] = files.Truncate(f) /// XXX: Should implement GetGlobalTruncated directly progress[i] = f
seen[name] = true seen[name] = true
} }
} }
for i, name := range queuedNames { for i, name := range queuedNames {
if f, ok := rf.GetGlobal(name); ok { if f, ok := rf.GetGlobalTruncated(name); ok {
queued[i] = files.Truncate(f) /// XXX: Should implement GetGlobalTruncated directly queued[i] = f
seen[name] = true seen[name] = true
} }
} }