-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.go
executable file
·324 lines (274 loc) · 8.26 KB
/
split.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package splitter
import (
"bytes"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"sync/atomic"
ct "github.com/digisan/csv-tool"
qry "github.com/digisan/csv-tool/query"
spl "github.com/digisan/csv-tool/split"
spl2 "github.com/digisan/csv-tool/split2"
. "github.com/digisan/go-generics/v2"
fd "github.com/digisan/gotk/file-dir"
"github.com/digisan/gotk/strs"
lk "github.com/digisan/logkit"
"github.com/gosuri/uiprogress"
"github.com/gosuri/uiprogress/util/strutil"
)
func EnableProgBar(enable bool) {
progBar = enable
}
// NrtSplit :
func NrtSplit(configurations ...string) (err error) {
// fit toml configuration for 'Trim', 'Split' etc
setConfig(configurations...)
// by default, if Trim & Split are both disabled, disable progress-bar anyway.
EnableProgBar(enableTrim || enableSplit)
// prepare a temporary dir for trim & split
const tempDir = "./ts_tmp/"
if err = os.RemoveAll(tempDir); err != nil {
return err
}
// -- progress bar 1 -- //
if progBar {
uip = uiprogress.New()
defer uip.Stop()
uip.Start()
files, _, err := fd.WalkFileDir(inFolderAbs, goSubFolder)
if err != nil {
lk.WarnOnErr("%v", err)
return err
}
bar = uip.AddBar(len(files))
bar.AppendCompleted().PrependElapsed()
bar.PrependFunc(func(b *uiprogress.Bar) string {
return strutil.Resize(" Trimming & Splitting...:", 35)
})
}
mFileEmptyCSV := make(map[string][]byte)
err = filepath.Walk(inFolderAbs, func(fPath string, info os.FileInfo, err error) error {
lk.FailOnErr("error [%v] at a path [%q], check your config.toml [InFolder] \n", err, fPath)
// only process csv file
if info.IsDir() || filepath.Ext(fPath) != ".csv" {
return nil
}
// if NOT goSubFolder, if jump into deeper, then return
if !goSubFolder {
fDirAbs, err := filepath.Abs(filepath.Dir(fPath))
lk.FailOnErr("Error when walk through abs %s, @%v", inFolderAbs, err)
if inFolderAbs != fDirAbs {
return nil
}
}
// trim inFolder Path for each file path, only keep 'filename.csv' or '/sub/filename.csv'
tailPath := fPath[len(inFolderAbs):]
// Split first
if enableSplit {
// fmt.Printf("Split Processing...: %v\n", path)
// split setting
if bySplit2 {
spl2.RmSchemaCol(true) // after splitting, remove those columns which are used by splitting
spl2.RmSchemaColInIgn(false) // keep all columns when a file cannot be split
spl2.StrictSchema(true, ignoreFolder4Split) // strict for split, if doesn't meet Schema, then ignore this csv
} else {
// spl.ForceSglProc(true)
spl.RmSchemaCol(true)
spl.RmSchemaColInIgn(false)
spl.StrictSchema(true, ignoreFolder4Split)
}
outFile := filepath.Join(out4Split, tailPath) // output file
outFolder := filepath.Dir(outFile) // output file's folder
// do split
var fPathsSplit, fPathsIgnore []string
if bySplit2 {
fPathsSplit, fPathsIgnore, err = spl2.Split(fPath, outFolder, splitSchema...)
} else {
fPathsSplit, fPathsIgnore, err = spl.Split(fPath, outFolder, splitSchema...)
}
if err != nil {
return err
}
// trim columns also apply to split result if set
if enableTrim && trimColAfterSplit {
for _, fPath := range fPathsSplit {
if ok, err := ct.FileHeaderHasAny(fPath, trimCols...); err == nil && ok {
qry.QueryFile(fPath, false, trimCols, '&', nil, fPath)
}
}
}
// find schema is valid, but empty content file to spread in future
for _, fPath := range fPathsIgnore {
hdr, n, err := ct.FileInfo(fPath)
if err != nil {
lk.Warn("%v @ %s", err, fPath)
return err
}
if n == 0 && IsSuper(hdr, splitSchema) {
emptyCsv, err := os.ReadFile(fPath)
if err != nil {
lk.Warn("%v @ %s", err, fPath)
return err
}
// Trim Columns if needed
rmHdr := splitSchema
if enableTrim && trimColAfterSplit {
rmHdr = Settify(append(rmHdr, trimCols...)...)
}
var buf bytes.Buffer
qry.Subset(emptyCsv, false, rmHdr, false, nil, io.Writer(&buf))
emptyCsv = buf.Bytes()
mFileEmptyCSV[fPath] = emptyCsv
}
}
}
if enableTrim {
// fmt.Printf("Trim Processing...: %v\n", path)
if ok, err := ct.FileHeaderHasAny(fPath, trimCols...); err == nil && ok {
outFolder := out4Trim
// if trim output folder is identical to original input folder, make a temp output, then overwrite the input
if out4Trim == inFolder {
outFolder = tempDir
}
outFile := filepath.Join(outFolder, tailPath)
qry.QueryFile(fPath, false, trimCols, '&', nil, outFile)
}
}
// -- progress bar 2 -- //
if progBar {
atomic.AddUint64(&procSize, 1)
bar.Set(int(procSize))
bar.Incr()
}
return nil
}) // end of walk
lk.FailOnErr("error walking the path %q: %v\n", inFolderAbs, err)
// if temp folder was created as Trim.OutFolder is the same as InFolder, use temp folder to replace input folder
if fd.DirExists(tempDir) {
// copy each file to inFolder, then delete tempDir
err := filepath.WalkDir(tempDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
srcF, err := os.Open(path)
if err != nil {
lk.Warn("%v (%v)", err, path)
return err
}
defer srcF.Close()
subPath := strs.TrimHeadToFirst(path, filepath.Clean(tempDir))
// lk.Log("%v -- %v -- %v", path, tempDir, subPath)
dst := filepath.Join(inFolder, subPath)
// lk.Log("%v", dst)
dstF, err := os.Create(dst)
if err != nil {
lk.Warn("%v (%v)", err, dst)
return err
}
defer dstF.Close()
if _, err := io.Copy(dstF, srcF); err != nil {
lk.Warn("%v (%v) (%v)", err, path, dst)
return err
}
return nil
})
if err == nil {
lk.Log("removing temp dir [%v]", tempDir)
lk.WarnOnErr("%v", os.RemoveAll(tempDir))
}
// cause files missing issue !!!
// os.RemoveAll(inFolder)
// os.Rename(tempDir, inFolder)
}
// spread all valid schema & empty csv to each split folder
mOutRecord := make(map[string]struct{})
if enableSplit {
err = filepath.Walk(out4Split, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) != ".csv" || info.IsDir() {
return nil
}
mOutRecord[filepath.Dir(path)] = struct{}{}
return nil
})
lk.WarnOnErr("error walking the path %q: %v\n", inFolderAbs, err)
nSchema := len(splitSchema)
for outPath := range mOutRecord {
for emptyPath, csv := range mFileEmptyCSV {
emptyRoot := filepath.Base(filepath.Dir(emptyPath))
emptyFile := filepath.Base(emptyPath)
outDir := outPath
for i := 0; i < nSchema; i++ {
outDir = filepath.Dir(outDir)
}
if strings.HasSuffix(outDir, "/"+emptyRoot) || strings.HasSuffix(outDir, "\\"+emptyRoot) {
fd.MustWriteFile(filepath.Join(outPath, emptyFile), csv)
}
}
}
}
watched := []string{}
for _, m := range merges {
schema := AnysToTypes[string](m["Schema"].([]any))
watched = append(watched, schema...)
}
watched = Settify(watched...)
_, dirs4split, err := fd.WalkFileDir(out4Split, true)
if err != nil {
if !enableSplit {
if _, err = os.Stat(out4Split); os.IsNotExist(err) {
err = nil
}
}
}
if err != nil {
return err
}
mDirBase := make(map[string][]string)
for _, dir := range dirs4split {
base := filepath.Base(dir)
dir1 := filepath.Dir(dir)
if In(base, watched...) {
mDirBase[dir1] = append(mDirBase[dir1], base)
}
}
onConflict := func(existing []byte, incoming []byte) (overwrite bool, overwriteData []byte) {
iLF := bytes.Index(incoming, []byte{'\n'})
return true, append(existing, incoming[iLF:]...)
}
for dir1, folders := range mDirBase {
for _, folder := range folders {
dir := filepath.Join(dir1, folder)
for _, m := range merges {
if m["Enabled"].(bool) {
temp := filepath.Join(dir1, m["MergedName"].(string)+"#")
// merged := filepath.Join(dir1, m.MergedName)
schema := AnysToTypes[string](m["Schema"].([]any))
for _, s := range schema {
if s == folder {
// fmt.Println(dir, "=>", merged)
fd.MergeDir(temp, true, onConflict, dir)
}
}
}
}
}
}
err = nil
if enableSplit {
_, dirs4split, err = fd.WalkFileDir(out4Split, true)
if err != nil {
return err
}
for _, dir := range dirs4split {
if strings.HasSuffix(dir, "#") {
os.Rename(dir, dir[:len(dir)-1])
}
}
}
return err
}