From 75d4d2df8b7ca738de24256a50b58c96802792bc Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 21 Jul 2014 10:20:17 +0200 Subject: [PATCH] Remove SyncOrder, at least temporarily (sorry fREW) Doesn't actually work very well with the batched approach to needed files, not documented, not exposed in UI. I'll be happy to reintegrate if this is solved. --- config/config.go | 58 ++++------------------------ config/config_test.go | 90 ------------------------------------------- files/sort.go | 33 ---------------- model/model.go | 3 -- 4 files changed, 7 insertions(+), 177 deletions(-) delete mode 100644 files/sort.go diff --git a/config/config.go b/config/config.go index bfe17553..f910e483 100644 --- a/config/config.go +++ b/config/config.go @@ -11,7 +11,6 @@ import ( "io" "os" "reflect" - "regexp" "sort" "strconv" @@ -31,42 +30,14 @@ type Configuration struct { XMLName xml.Name `xml:"configuration" json:"-"` } -// SyncOrderPattern allows a user to prioritize file downloading based on a -// regular expression. If a file matches the Pattern the Priority will be -// assigned to the file. If a file matches more than one Pattern the -// Priorities are summed. This allows a user to, for example, prioritize files -// in a directory, as well as prioritize based on file type. The higher the -// priority the "sooner" a file will be downloaded. Files can be deprioritized -// by giving them a negative priority. While Priority is represented as an -// integer, the expected range is something like -1000 to 1000. -type SyncOrderPattern struct { - Pattern string `xml:"pattern,attr"` - Priority int `xml:"priority,attr"` - compiledPattern *regexp.Regexp -} - -func (s *SyncOrderPattern) CompiledPattern() *regexp.Regexp { - if s.compiledPattern == nil { - re, err := regexp.Compile(s.Pattern) - if err != nil { - l.Warnln("Could not compile regexp (" + s.Pattern + "): " + err.Error()) - s.compiledPattern = regexp.MustCompile("^\\0$") - } else { - s.compiledPattern = re - } - } - return s.compiledPattern -} - type RepositoryConfiguration struct { - ID string `xml:"id,attr"` - Directory string `xml:"directory,attr"` - Nodes []NodeConfiguration `xml:"node"` - ReadOnly bool `xml:"ro,attr"` - IgnorePerms bool `xml:"ignorePerms,attr"` - Invalid string `xml:"-"` // Set at runtime when there is an error, not saved - Versioning VersioningConfiguration `xml:"versioning"` - SyncOrderPatterns []SyncOrderPattern `xml:"syncorder>pattern"` + ID string `xml:"id,attr"` + Directory string `xml:"directory,attr"` + Nodes []NodeConfiguration `xml:"node"` + ReadOnly bool `xml:"ro,attr"` + IgnorePerms bool `xml:"ignorePerms,attr"` + Invalid string `xml:"-"` // Set at runtime when there is an error, not saved + Versioning VersioningConfiguration `xml:"versioning"` nodeIDs []protocol.NodeID } @@ -121,21 +92,6 @@ func (r *RepositoryConfiguration) NodeIDs() []protocol.NodeID { return r.nodeIDs } -func (r RepositoryConfiguration) FileRanker() func(protocol.FileInfo) int { - if len(r.SyncOrderPatterns) <= 0 { - return nil - } - return func(f protocol.FileInfo) int { - ret := 0 - for _, v := range r.SyncOrderPatterns { - if v.CompiledPattern().MatchString(f.Name) { - ret += v.Priority - } - } - return ret - } -} - type NodeConfiguration struct { NodeID protocol.NodeID `xml:"id,attr"` Name string `xml:"name,attr,omitempty"` diff --git a/config/config_test.go b/config/config_test.go index afac496a..349cb256 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -11,7 +11,6 @@ import ( "reflect" "testing" - "github.com/calmh/syncthing/files" "github.com/calmh/syncthing/protocol" ) @@ -233,92 +232,3 @@ func TestNodeAddresses(t *testing.T) { t.Errorf("Nodes differ;\n E: %#v\n A: %#v", expected, cfg.Nodes) } } - -func TestSyncOrders(t *testing.T) { - data := []byte(` - - - - - - - -`) - - expected := []SyncOrderPattern{ - { - Pattern: "\\.jpg$", - Priority: 1, - }, - } - - cfg, err := Load(bytes.NewReader(data), node1) - if err != nil { - t.Error(err) - } - - for i := range expected { - if !reflect.DeepEqual(cfg.Repositories[0].SyncOrderPatterns[i], expected[i]) { - t.Errorf("Patterns[%d] differ;\n E: %#v\n A: %#v", i, expected[i], cfg.Repositories[0].SyncOrderPatterns[i]) - } - } -} - -func TestFileSorter(t *testing.T) { - rcfg := RepositoryConfiguration{ - SyncOrderPatterns: []SyncOrderPattern{ - {"\\.jpg$", 10, nil}, - {"\\.mov$", 5, nil}, - {"^camera-uploads", 100, nil}, - }, - } - - f := []protocol.FileInfo{ - {Name: "bar.mov"}, - {Name: "baz.txt"}, - {Name: "foo.jpg"}, - {Name: "frew/foo.jpg"}, - {Name: "frew/lol.go"}, - {Name: "frew/rofl.copter"}, - {Name: "frew/bar.mov"}, - {Name: "camera-uploads/foo.jpg"}, - {Name: "camera-uploads/hurr.pl"}, - {Name: "camera-uploads/herp.mov"}, - {Name: "camera-uploads/wee.txt"}, - } - - files.SortBy(rcfg.FileRanker()).Sort(f) - - expected := []protocol.FileInfo{ - {Name: "camera-uploads/foo.jpg"}, - {Name: "camera-uploads/herp.mov"}, - {Name: "camera-uploads/hurr.pl"}, - {Name: "camera-uploads/wee.txt"}, - {Name: "foo.jpg"}, - {Name: "frew/foo.jpg"}, - {Name: "bar.mov"}, - {Name: "frew/bar.mov"}, - {Name: "frew/lol.go"}, - {Name: "baz.txt"}, - {Name: "frew/rofl.copter"}, - } - - if !reflect.DeepEqual(f, expected) { - t.Errorf( - "\n\nexpected:\n" + - formatFiles(expected) + "\n" + - "got:\n" + - formatFiles(f) + "\n\n", - ) - } -} - -func formatFiles(f []protocol.FileInfo) string { - ret := "" - - for _, v := range f { - ret += " " + v.Name + "\n" - } - - return ret -} diff --git a/files/sort.go b/files/sort.go deleted file mode 100644 index 34f4e583..00000000 --- a/files/sort.go +++ /dev/null @@ -1,33 +0,0 @@ -package files - -import ( - "github.com/calmh/syncthing/protocol" - "sort" -) - -type SortBy func(p protocol.FileInfo) int - -func (by SortBy) Sort(files []protocol.FileInfo) { - ps := &fileSorter{ - files: files, - by: by, - } - sort.Sort(ps) -} - -type fileSorter struct { - files []protocol.FileInfo - by func(p1 protocol.FileInfo) int -} - -func (s *fileSorter) Len() int { - return len(s.files) -} - -func (s *fileSorter) Swap(i, j int) { - s.files[i], s.files[j] = s.files[j], s.files[i] -} - -func (s *fileSorter) Less(i, j int) bool { - return s.by(s.files[i]) > s.by(s.files[j]) -} diff --git a/model/model.go b/model/model.go index 1d645331..1e468904 100644 --- a/model/model.go +++ b/model/model.go @@ -318,9 +318,6 @@ func (m *Model) NeedFilesRepo(repo string) []protocol.FileInfo { fs = append(fs, f) return len(fs) < indexBatchSize }) - if r := m.repoCfgs[repo].FileRanker(); r != nil { - files.SortBy(r).Sort(fs) - } return fs } return nil