This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
/
bundle.go
282 lines (246 loc) · 7.88 KB
/
bundle.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package db
import (
"crypto/sha512"
enc "github.com/SchnorcherSepp/splitfs/encoding"
impl "github.com/SchnorcherSepp/storage/defaultimpl"
"log"
"sort"
)
// MakeBundles set bundles in the DB.
// This function changes the database!
func (db *Db) MakeBundles(keyFile *enc.KeyFile, debugLvl uint8) {
// debug (0=off, 1=debug, 2=high)
debug := debugLvl >= impl.DebugLow
// reset old bundles
resetOldBundles(db)
// groups: [][]VirtFile
for _, group := range findGroups(db, debug) {
// calc bundle PlainHash (hash all PlainSHA512)
storageSize := int64(0)
hh := sha512.New()
for _, vFile := range group {
storageSize += vFile.Parts[0].StorageSize
hh.Write(vFile.Parts[0].PlainSHA512)
}
plainHash := hh.Sum(nil)
// get bundle content (all VirtFile IDs)
content := make([]string, 0, len(group))
for _, virtFile := range group {
content = append(content, virtFile.Id())
}
// build bundle
bundle := Bundle{
VFilePart: VFilePart{
PlainSHA512: plainHash,
StorageName: BundlePrefix + keyFile.CryptName(plainHash),
StorageSize: storageSize,
StorageMd5: "", // not used
CryptDataKey: keyFile.DataKey(plainHash),
},
Content: content,
}
db.Bundles[bundle.Id()] = bundle // add bundle to db
// connect all virtual files with the bundle
for _, relPath := range bundle.Content {
tmp := db.VFiles[relPath]
tmp.AlsoInBundle = bundle.Id()
db.VFiles[relPath] = tmp
}
}
}
// ---------- HELPER -----------------------------------------------------------------------------------------------//
// resetOldBundles remove all bundles and bundle links.
func resetOldBundles(db *Db) {
// reset virtual files
for k := range db.VFiles {
tmp := db.VFiles[k]
tmp.AlsoInBundle = ""
db.VFiles[k] = tmp
}
// reset bundles
db.Bundles = make(map[string]Bundle)
}
// findGroups bundles small files to one large file
// There are NO db changes!
func findGroups(db *Db, debug bool) [][]VirtFile {
// This is a list of bundles.
// A bundle is a collection of virtual files.
foundGroups := make([][]VirtFile, 0)
// First, all files are extracted from the database that are suitable for a bundle.
// (Files that are not empty and smaller than MaxFileSizeToBundle)
files := make([]VirtFile, 0, len(db.VFiles))
for _, dbEl := range db.VFiles {
if !dbEl.IsDir && dbEl.FileSize < MaxFileSizeToBundle && dbEl.FileSize > 0 && len(dbEl.Parts) == 1 {
files = append(files, dbEl)
}
}
// To make the bundles stable (multiple calls of this function
// always return the same result), all files are sorted by path.
sort.Slice(files, func(i, j int) bool {
return files[i].RelPath < files[j].RelPath
})
// Phase 1: Combine all very small text files.
// The criterion here is the size on storage (<12 kB).
// MaxFileSizeToBundle > MaxFileSizeForCompression > 12 kB > [TARGET] > 0
smallFiles := make([]VirtFile, 0, len(files)) // phase 1 list
rest := make([]VirtFile, 0, len(files)) // left elements
for _, v := range files {
if v.Parts[0].StorageSize < 12*1024 { // StorageSize == compressed size
smallFiles = append(smallFiles, v)
} else {
rest = append(rest, v)
}
}
if debug {
size := float64(0)
for _, v := range smallFiles {
size += float64(v.FileSize) / (1024 * 1024)
}
log.Printf("DEBUG: %s/MakeBundles: Phase 1 (very small files): bundle %d files (%.0f MB)", packageName, len(smallFiles), size)
}
files = rest
foundGroups = append(foundGroups, smallFiles)
// Phase 2: All remaining compressible files are combined from the remaining files.
// Note: Due to the pre-filtering (MaxFileSizeToBundle) only small files are processed.
// MaxFileSizeToBundle > MaxFileSizeForCompression > [TARGET] > 12 kB
mediumFiles := make([]VirtFile, 0, len(files)) // phase 2 list
rest = make([]VirtFile, 0, len(files)) // left elements
for _, v := range files {
if v.UseCompression {
mediumFiles = append(mediumFiles, v)
} else {
rest = append(rest, v)
}
}
if debug {
size := float64(0)
for _, v := range smallFiles {
size += float64(v.FileSize) / (1024 * 1024)
}
log.Printf("DEBUG: %s/MakeBundles: Phase 2 (compressible files): bundle %d files (%.0f MB)", packageName, len(mediumFiles), size)
}
files = rest
foundGroups = append(foundGroups, mediumFiles)
// Phase 3: Now there should be a lot of files left. Media files such as photos in particular
// are difficult to compress and have not yet been processed.
//
// The algorithm works as follows:
// The basis for the grouping is the path.
// The maximum path length is shortened in a loop.
// Groups are created from shortened paths with the same name.
// If a group is large enough, it is saved as valid.
cutAt := 0
// find max. path len
for _, v := range files {
if len(v.RelPath) > cutAt {
cutAt = len(v.RelPath)
}
}
for cutAt > 0 {
// Reduce the maximum path length by 1
cutAt--
// Find the total size of each group in this round.
groups := make(map[string]int64)
for _, v := range files {
group := v.RelPath
if len(group) > cutAt {
group = group[:cutAt]
}
groups[group] += v.FileSize
}
// For all groups from a certain size:
// Save the group and remove the files from the files list.
for group, gSize := range groups {
if gSize > PartSize/2 || cutAt == 0 {
//----------------------------------------
newG := make([]VirtFile, 0, len(files))
rest = make([]VirtFile, 0, len(files))
for _, v := range files {
g := v.RelPath
if len(g) > cutAt {
g = g[:cutAt]
}
if g == group {
newG = append(newG, v)
} else {
rest = append(rest, v)
}
}
if debug {
size := float64(gSize) / (1024 * 1024)
log.Printf("DEBUG: %s/MakeBundles: Phase 3: bundle %d files (%.0f MB) with group path '%s'", packageName, len(newG), size, group)
}
files = rest
foundGroups = append(foundGroups, newG)
//----------------------------------------
}
}
}
// hotfix: split too big bundles
foundGroups = splitBigBundles(foundGroups, debug)
// hotfix: remove empty groups or too small groups
tmp := make([][]VirtFile, 0)
for i, group := range foundGroups {
if len(group) > 1 {
tmp = append(tmp, group)
} else {
if debug {
log.Printf("DEBUG: %s/MakeBundles: discard bundle[%d] with %d files", packageName, i, len(group))
}
}
}
foundGroups = tmp
// return
return foundGroups
}
// splitBigBundles split bundles > 2*PartSize into PartSize sub-bundles
func splitBigBundles(foundGroups [][]VirtFile, debug bool) [][]VirtFile {
// count all files (for test later)
inFileCount := 0
for _, g := range foundGroups {
inFileCount += len(g)
}
// do your thing
ret := make([][]VirtFile, 0)
for i, group := range foundGroups {
// calc group storage size
size := int64(0)
for _, f := range group {
size += f.Parts[0].StorageSize // 'len(dbEl.Parts) == 1' is enforced by first check in parent function
}
// bundle size is ok
if size <= 2*PartSize {
ret = append(ret, group) // add ok bundle
continue
}
// bundle is too big
sub := 0
newGroup := make([]VirtFile, 0)
newSize := int64(0)
for _, f := range group {
newGroup = append(newGroup, f)
newSize += f.Parts[0].StorageSize
if newSize > PartSize {
ret = append(ret, newGroup) // add new group
sub++ // count new sub-bundle
newGroup = make([]VirtFile, 0) // reset
newSize = int64(0) // reset
}
}
ret = append(ret, newGroup) // add rest
sub++ // count rest sub-bundle
if debug {
log.Printf("DEBUG: %s/splitBigBundles: split bundle[%d] with %d files and %d MB into %d sub-bundle", packageName, i, len(group), size/(1024*1024), sub)
}
}
// final test
outFileCount := 0
for _, g := range ret {
outFileCount += len(g)
}
if inFileCount != outFileCount {
log.Printf("ERROR: %s/splitBigBundles: inFileCount=%d, outFileCount=%d", packageName, inFileCount, outFileCount)
}
// return
return ret
}