-
Notifications
You must be signed in to change notification settings - Fork 30
/
chunk.go
230 lines (211 loc) · 6.62 KB
/
chunk.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
package graphsplit
import (
"context"
"encoding/csv"
"fmt"
"os"
"path"
"strconv"
"time"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"
"golang.org/x/xerrors"
)
var log = logging.Logger("graphsplit")
type GraphBuildCallback interface {
OnSuccess(node ipld.Node, graphName, fsDetail string)
OnError(error)
}
type commPCallback struct {
carDir string
rename bool
addPadding bool
}
func (cc *commPCallback) OnSuccess(node ipld.Node, graphName, fsDetail string) {
fmt.Println("xxxxx")
commpStartTime := time.Now()
carfilepath := path.Join(cc.carDir, node.Cid().String()+".car")
cpRes, err := CalcCommP(context.TODO(), carfilepath, cc.rename, cc.addPadding)
if err != nil {
log.Fatal(err)
}
log.Infof("calculation of pieceCID completed, time elapsed: %s", time.Now().Sub(commpStartTime))
// Add node inof to manifest.csv
manifestPath := path.Join(cc.carDir, "manifest.csv")
_, err = os.Stat(manifestPath)
if err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
var isCreateAction bool
if err != nil && os.IsNotExist(err) {
isCreateAction = true
}
f, err := os.OpenFile(manifestPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
csvWriter := csv.NewWriter(f)
csvWriter.UseCRLF = true
defer csvWriter.Flush()
if isCreateAction {
csvWriter.Write([]string{
"playload_cid", "filename", "piece_cid", "payload_size", "piece_size", "detail",
})
}
if err := csvWriter.Write([]string{
node.Cid().String(), graphName, cpRes.Root.String(), strconv.FormatInt(cpRes.PayloadSize, 10), strconv.FormatUint(uint64(cpRes.Size), 10), fsDetail,
}); err != nil {
log.Fatal(err)
}
}
func (cc *commPCallback) OnError(err error) {
log.Fatal(err)
}
type csvCallback struct {
carDir string
}
func (cc *csvCallback) OnSuccess(node ipld.Node, graphName, fsDetail string) {
// Add node inof to manifest.csv
manifestPath := path.Join(cc.carDir, "manifest.csv")
_, err := os.Stat(manifestPath)
if err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
var isCreateAction bool
if err != nil && os.IsNotExist(err) {
isCreateAction = true
}
f, err := os.OpenFile(manifestPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if isCreateAction {
if _, err := f.Write([]byte("playload_cid,filename,detail\n")); err != nil {
log.Fatal(err)
}
}
if _, err := f.Write([]byte(fmt.Sprintf("%s,%s,%s\n", node.Cid(), graphName, fsDetail))); err != nil {
log.Fatal(err)
}
}
func (cc *csvCallback) OnError(err error) {
log.Fatal(err)
}
type errCallback struct{}
func (cc *errCallback) OnSuccess(ipld.Node, string, string) {}
func (cc *errCallback) OnError(err error) {
log.Fatal(err)
}
func CommPCallback(carDir string, rename, addPadding bool) GraphBuildCallback {
return &commPCallback{carDir: carDir, rename: rename, addPadding: addPadding}
}
func CSVCallback(carDir string) GraphBuildCallback {
return &csvCallback{carDir: carDir}
}
func ErrCallback() GraphBuildCallback {
return &errCallback{}
}
func Chunk(ctx context.Context, sliceSize int64, parentPath, targetPath, carDir, graphName string, parallel int, cb GraphBuildCallback) error {
var cumuSize int64 = 0
graphSliceCount := 0
graphFiles := make([]Finfo, 0)
if sliceSize == 0 {
return xerrors.Errorf("Unexpected! Slice size has been set as 0")
}
if parallel <= 0 {
return xerrors.Errorf("Unexpected! Parallel has to be greater than 0")
}
if parentPath == "" {
parentPath = targetPath
}
args := []string{targetPath}
sliceTotal := GetGraphCount(args, sliceSize)
if sliceTotal == 0 {
log.Warn("Empty folder or file!")
return nil
}
files := GetFileListAsync(args)
for item := range files {
fileSize := item.Info.Size()
switch {
case cumuSize+fileSize < sliceSize:
cumuSize += fileSize
graphFiles = append(graphFiles, item)
case cumuSize+fileSize == sliceSize:
cumuSize += fileSize
graphFiles = append(graphFiles, item)
// todo build ipld from graphFiles
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), parentPath, carDir, parallel, cb)
fmt.Printf("cumu-size: %d\n", cumuSize)
fmt.Printf(GenGraphName(graphName, graphSliceCount, sliceTotal))
fmt.Printf("=================\n")
cumuSize = 0
graphFiles = make([]Finfo, 0)
graphSliceCount++
case cumuSize+fileSize > sliceSize:
fileSliceCount := 0
// need to split item to fit graph slice
//
// first cut
firstCut := sliceSize - cumuSize
var seekStart int64 = 0
var seekEnd int64 = seekStart + firstCut - 1
fmt.Printf("first cut %d, seek start at %d, end at %d", firstCut, seekStart, seekEnd)
fmt.Printf("----------------\n")
graphFiles = append(graphFiles, Finfo{
Path: item.Path,
Name: fmt.Sprintf("%s.%08d", item.Info.Name(), fileSliceCount),
Info: item.Info,
SeekStart: seekStart,
SeekEnd: seekEnd,
})
fileSliceCount++
// todo build ipld from graphFiles
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), parentPath, carDir, parallel, cb)
fmt.Printf("cumu-size: %d\n", cumuSize+firstCut)
fmt.Printf(GenGraphName(graphName, graphSliceCount, sliceTotal))
fmt.Printf("=================\n")
cumuSize = 0
graphFiles = make([]Finfo, 0)
graphSliceCount++
for seekEnd < fileSize-1 {
seekStart = seekEnd + 1
seekEnd = seekStart + sliceSize - 1
if seekEnd >= fileSize-1 {
seekEnd = fileSize - 1
}
fmt.Printf("following cut %d, seek start at %d, end at %d", seekEnd-seekStart+1, seekStart, seekEnd)
fmt.Printf("----------------\n")
cumuSize += seekEnd - seekStart + 1
graphFiles = append(graphFiles, Finfo{
Path: item.Path,
Name: fmt.Sprintf("%s.%08d", item.Info.Name(), fileSliceCount),
Info: item.Info,
SeekStart: seekStart,
SeekEnd: seekEnd,
})
fileSliceCount++
if seekEnd-seekStart == sliceSize-1 {
// todo build ipld from graphFiles
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), parentPath, carDir, parallel, cb)
fmt.Printf("cumu-size: %d\n", sliceSize)
fmt.Printf(GenGraphName(graphName, graphSliceCount, sliceTotal))
fmt.Printf("=================\n")
cumuSize = 0
graphFiles = make([]Finfo, 0)
graphSliceCount++
}
}
}
}
if cumuSize > 0 {
// todo build ipld from graphFiles
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), parentPath, carDir, parallel, cb)
fmt.Printf("cumu-size: %d\n", cumuSize)
fmt.Printf(GenGraphName(graphName, graphSliceCount, sliceTotal))
fmt.Printf("=================\n")
}
return nil
}