Faster and leaner comparisons in fileset
This commit is contained in:
43
files/set.go
43
files/set.go
@@ -2,7 +2,6 @@
|
|||||||
package files
|
package files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/calmh/syncthing/cid"
|
"github.com/calmh/syncthing/cid"
|
||||||
@@ -11,13 +10,6 @@ import (
|
|||||||
"github.com/calmh/syncthing/scanner"
|
"github.com/calmh/syncthing/scanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
type key struct {
|
|
||||||
Name string
|
|
||||||
Version uint64
|
|
||||||
Modified int64
|
|
||||||
Hash [md5.Size]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type fileRecord struct {
|
type fileRecord struct {
|
||||||
Usage int
|
Usage int
|
||||||
File scanner.File
|
File scanner.File
|
||||||
@@ -25,34 +17,6 @@ type fileRecord struct {
|
|||||||
|
|
||||||
type bitset uint64
|
type bitset uint64
|
||||||
|
|
||||||
func keyFor(f scanner.File) key {
|
|
||||||
h := md5.New()
|
|
||||||
for _, b := range f.Blocks {
|
|
||||||
h.Write(b.Hash)
|
|
||||||
}
|
|
||||||
return key{
|
|
||||||
Name: f.Name,
|
|
||||||
Version: f.Version,
|
|
||||||
Modified: f.Modified,
|
|
||||||
Hash: md5.Sum(nil),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a key) newerThan(b key) bool {
|
|
||||||
if a.Version != b.Version {
|
|
||||||
return a.Version > b.Version
|
|
||||||
}
|
|
||||||
if a.Modified != b.Modified {
|
|
||||||
return a.Modified > b.Modified
|
|
||||||
}
|
|
||||||
for i := 0; i < md5.Size; i++ {
|
|
||||||
if a.Hash[i] != b.Hash[i] {
|
|
||||||
return a.Hash[i] > b.Hash[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
type Set struct {
|
type Set struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
files map[key]fileRecord
|
files map[key]fileRecord
|
||||||
@@ -148,9 +112,10 @@ func (m *Set) Need(id uint) []scanner.File {
|
|||||||
rkID := m.remoteKey[id]
|
rkID := m.remoteKey[id]
|
||||||
for name, gk := range m.globalKey {
|
for name, gk := range m.globalKey {
|
||||||
if gk.newerThan(rkID[name]) {
|
if gk.newerThan(rkID[name]) {
|
||||||
if m.files[gk].File.Flags&protocol.FlagDirectory == 0 || // Regular file
|
file := m.files[gk].File
|
||||||
m.files[gk].File.Flags&(protocol.FlagDirectory|protocol.FlagDeleted) == protocol.FlagDirectory { // Non-deleted directory
|
if file.Flags&protocol.FlagDirectory == 0 || // Regular file
|
||||||
fs = append(fs, m.files[gk].File)
|
file.Flags&(protocol.FlagDirectory|protocol.FlagDeleted) == protocol.FlagDirectory { // Non-deleted directory
|
||||||
|
fs = append(fs, file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
44
files/set_anal.go
Normal file
44
files/set_anal.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
//+build anal
|
||||||
|
|
||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
|
||||||
|
"github.com/calmh/syncthing/scanner"
|
||||||
|
)
|
||||||
|
|
||||||
|
type key struct {
|
||||||
|
Name string
|
||||||
|
Version uint64
|
||||||
|
Modified int64
|
||||||
|
Hash [md5.Size]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func keyFor(f scanner.File) key {
|
||||||
|
h := md5.New()
|
||||||
|
for _, b := range f.Blocks {
|
||||||
|
h.Write(b.Hash)
|
||||||
|
}
|
||||||
|
return key{
|
||||||
|
Name: f.Name,
|
||||||
|
Version: f.Version,
|
||||||
|
Modified: f.Modified,
|
||||||
|
Hash: md5.Sum(nil),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a key) newerThan(b key) bool {
|
||||||
|
if a.Version != b.Version {
|
||||||
|
return a.Version > b.Version
|
||||||
|
}
|
||||||
|
if a.Modified != b.Modified {
|
||||||
|
return a.Modified > b.Modified
|
||||||
|
}
|
||||||
|
for i := 0; i < md5.Size; i++ {
|
||||||
|
if a.Hash[i] != b.Hash[i] {
|
||||||
|
return a.Hash[i] > b.Hash[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
21
files/set_fast.go
Normal file
21
files/set_fast.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
//+build !anal
|
||||||
|
|
||||||
|
package files
|
||||||
|
|
||||||
|
import "github.com/calmh/syncthing/scanner"
|
||||||
|
|
||||||
|
type key struct {
|
||||||
|
Name string
|
||||||
|
Version uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func keyFor(f scanner.File) key {
|
||||||
|
return key{
|
||||||
|
Name: f.Name,
|
||||||
|
Version: f.Version,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a key) newerThan(b key) bool {
|
||||||
|
return a.Version > b.Version
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user