-
Notifications
You must be signed in to change notification settings - Fork 172
/
moves-distance.go
166 lines (147 loc) · 4 KB
/
moves-distance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package merger
import (
"math"
"path"
"sort"
"strings"
"github.com/pydio/cells/v4/common/proto/tree"
"github.com/pydio/cells/v4/common/utils/mtree"
"go.uber.org/zap/zapcore"
)
const (
maxUint = ^uint(0)
maxInt = int(maxUint >> 1)
)
type Move struct {
deleteOp Operation
createOp Operation
dbNode *tree.Node
source string
target string
depth int
similarity int
}
func (m *Move) folderDepth() int {
return len(strings.Split(strings.Trim(m.source, "/"), "/"))
}
func (m *Move) prepare() {
m.source = m.deleteOp.GetRefPath()
m.target = m.createOp.GetRefPath()
m.depth = m.folderDepth()
m.similarity = m.closeness()
}
func (m *Move) closeness() int {
sep := "/"
if m.source == m.target {
return maxInt
}
pref := mtree.CommonPrefix(sep[0], m.source, m.target)
prefFactor := len(strings.Split(pref, sep))
// Overall path similarity
lParts := strings.Split(m.source, sep)
rParts := strings.Split(m.target, sep)
reverseStringSlice(lParts)
reverseStringSlice(rParts)
max := math.Max(float64(len(lParts)), float64(len(rParts)))
dist := 1
for i := 0; i < int(max); i++ {
pL, pR := "", ""
if i < len(lParts) {
pL = lParts[i]
}
if i < len(rParts) {
pR = rParts[i]
}
if pL == pR {
dist += 5
}
}
return dist * prefFactor
}
func (m *Move) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
if m == nil {
return nil
}
encoder.AddString("From", m.deleteOp.GetRefPath())
encoder.AddString("To", m.createOp.GetRefPath())
encoder.AddObject("DbNode", m.dbNode)
return nil
}
func (m *Move) SameBase() bool {
return path.Base(m.deleteOp.GetRefPath()) == path.Base(m.createOp.GetRefPath())
}
// sortClosestMoves currently has an exponential complexity
// it should be rewritten as moving tons of files with similar eTag
// will inflate cpu/memory usage very quickly
func sortClosestMoves(possibleMoves []*Move) (moves []*Move) {
l := len(possibleMoves)
bySources := make(map[string][]*Move)
for _, m := range possibleMoves {
m.prepare()
bySources[m.source] = append(bySources[m.source], m)
}
for _, mm := range bySources {
sort.Slice(mm, func(i, j int) bool {
return mm[i].depth > mm[j].depth
})
}
// Dedup by source
greatestSource := make(map[string]*Move, l)
targets := make(map[string]struct{}, l)
sort.Slice(possibleMoves, func(i, j int) bool {
return possibleMoves[i].depth > possibleMoves[j].depth
})
for _, m := range possibleMoves {
var pickedTarget string
for _, m2 := range bySources[m.source] {
if _, alreadyUsed := targets[m2.target]; alreadyUsed {
continue
}
byT, ok := greatestSource[m.source]
if !ok || m2.similarity > byT.similarity {
greatestSource[m.source] = m2
pickedTarget = m2.target
}
}
if len(pickedTarget) > 0 {
targets[pickedTarget] = struct{}{}
}
}
// Dedup by target
greatestTarget := make(map[string]*Move, l)
for _, m := range greatestSource {
byT, ok := greatestTarget[m.target]
if !ok || m.similarity > byT.similarity {
greatestTarget[m.target] = m
}
}
for _, m := range greatestTarget {
moves = append(moves, m)
}
return
}
func reverseStringSlice(ss []string) {
last := len(ss) - 1
for i := 0; i < len(ss)/2; i++ {
ss[i], ss[last-i] = ss[last-i], ss[i]
}
}