Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func ErrCallback() GraphBuildCallback {
return &errCallback{}
}

func Chunk(ctx context.Context, sliceSize int64, parentPath, targetPath, carDir, graphName string, parallel int, cb GraphBuildCallback) error {
func Chunk(ctx context.Context, sliceSize int64, parentPath string, targetPaths []string, carDir, graphName string, parallel int, cb GraphBuildCallback) error {
var cumuSize int64 = 0
graphSliceCount := 0
graphFiles := make([]Finfo, 0)
Expand All @@ -134,17 +134,23 @@ func Chunk(ctx context.Context, sliceSize int64, parentPath, targetPath, carDir,
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)
sliceTotal := GetGraphCount(targetPaths, sliceSize)
if sliceTotal == 0 {
log.Warn("Empty folder or file!")
return nil
}
files := GetFileListAsync(args)
var files chan Finfo
for _, thisPath := range targetPaths {
f := GetFileListAsync(targetPaths)
var p string
if parentPath == "" {
p = thisPath
} else {
p = parentPath
}
SetParentPathAsync(f, files, p)
}
for item := range files {
fileSize := item.Info.Size()
switch {
Expand All @@ -155,7 +161,7 @@ func Chunk(ctx context.Context, sliceSize int64, parentPath, targetPath, carDir,
cumuSize += fileSize
graphFiles = append(graphFiles, item)
// todo build ipld from graphFiles
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), parentPath, carDir, parallel, cb)
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), carDir, parallel, cb)
fmt.Printf("cumu-size: %d\n", cumuSize)
fmt.Printf(GenGraphName(graphName, graphSliceCount, sliceTotal))
fmt.Printf("=================\n")
Expand All @@ -181,7 +187,7 @@ func Chunk(ctx context.Context, sliceSize int64, parentPath, targetPath, carDir,
})
fileSliceCount++
// todo build ipld from graphFiles
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), parentPath, carDir, parallel, cb)
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), carDir, parallel, cb)
fmt.Printf("cumu-size: %d\n", cumuSize+firstCut)
fmt.Printf(GenGraphName(graphName, graphSliceCount, sliceTotal))
fmt.Printf("=================\n")
Expand All @@ -207,7 +213,7 @@ func Chunk(ctx context.Context, sliceSize int64, parentPath, targetPath, carDir,
fileSliceCount++
if seekEnd-seekStart == sliceSize-1 {
// todo build ipld from graphFiles
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), parentPath, carDir, parallel, cb)
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), carDir, parallel, cb)
fmt.Printf("cumu-size: %d\n", sliceSize)
fmt.Printf(GenGraphName(graphName, graphSliceCount, sliceTotal))
fmt.Printf("=================\n")
Expand All @@ -221,7 +227,7 @@ func Chunk(ctx context.Context, sliceSize int64, parentPath, targetPath, carDir,
}
if cumuSize > 0 {
// todo build ipld from graphFiles
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), parentPath, carDir, parallel, cb)
BuildIpldGraph(ctx, graphFiles, GenGraphName(graphName, graphSliceCount, sliceTotal), carDir, parallel, cb)
fmt.Printf("cumu-size: %d\n", cumuSize)
fmt.Printf(GenGraphName(graphName, graphSliceCount, sliceTotal))
fmt.Printf("=================\n")
Expand Down
4 changes: 2 additions & 2 deletions cmd/graphsplit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var chunkCmd = &cli.Command{
return xerrors.Errorf("Unexpected! Slice size has been set as 0")
}

targetPath := c.Args().First()
targetPaths := c.Args().Slice()
var cb graphsplit.GraphBuildCallback
if c.Bool("calc-commp") {
cb = graphsplit.CommPCallback(carDir, c.Bool("rename"), c.Bool("add-padding"))
Expand All @@ -108,7 +108,7 @@ var chunkCmd = &cli.Command{
} else {
cb = graphsplit.ErrCallback()
}
return graphsplit.Chunk(ctx, int64(sliceSize), parentPath, targetPath, carDir, graphName, int(parallel), cb)
return graphsplit.Chunk(ctx, int64(sliceSize), parentPath, targetPaths, carDir, graphName, int(parallel), cb)
},
}

Expand Down
28 changes: 19 additions & 9 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ const UnixfsLinksPerLevel = 1 << 10
const UnixfsChunkSize uint64 = 1 << 20

type Finfo struct {
Path string
Name string
Info os.FileInfo
SeekStart int64
SeekEnd int64
Path string
ParentPath string
Name string
Info os.FileInfo
SeekStart int64
SeekEnd int64
}

// file system tree node
Expand Down Expand Up @@ -125,8 +126,8 @@ func (b *FSBuilder) getNodeByLink(ln *format.Link) (fn fsNode, err error) {
return
}

func BuildIpldGraph(ctx context.Context, fileList []Finfo, graphName, parentPath, carDir string, parallel int, cb GraphBuildCallback) {
node, fsDetail, err := buildIpldGraph(ctx, fileList, parentPath, carDir, parallel)
func BuildIpldGraph(ctx context.Context, fileList []Finfo, graphName, carDir string, parallel int, cb GraphBuildCallback) {
node, fsDetail, err := buildIpldGraph(ctx, fileList, carDir, parallel)
if err != nil {
//log.Fatal(err)
cb.OnError(err)
Expand All @@ -135,7 +136,7 @@ func BuildIpldGraph(ctx context.Context, fileList []Finfo, graphName, parentPath
cb.OnSuccess(node, graphName, fsDetail)
}

func buildIpldGraph(ctx context.Context, fileList []Finfo, parentPath, carDir string, parallel int) (ipld.Node, string, error) {
func buildIpldGraph(ctx context.Context, fileList []Finfo, carDir string, parallel int) (ipld.Node, string, error) {
bs2 := bstore.NewBlockstore(dss.MutexWrap(datastore.NewMapDatastore()))
dagServ := merkledag.NewDAGService(blockservice.New(bs2, offline.Exchange(bs2)))

Expand Down Expand Up @@ -195,7 +196,7 @@ func buildIpldGraph(ctx context.Context, fileList []Finfo, parentPath, carDir st
// log.Info(item.Path)
// log.Infof("file name: %s, file size: %d, item size: %d, seek-start:%d, seek-end:%d", item.Name, item.Info.Size(), item.SeekEnd-item.SeekStart, item.SeekStart, item.SeekEnd)
dirStr := path.Dir(item.Path)
parentPath = path.Clean(parentPath)
parentPath := path.Clean(item.ParentPath)
// when parent path equal target path, and the parent path is also a file path
if parentPath == path.Clean(item.Path) {
dirStr = ""
Expand Down Expand Up @@ -492,6 +493,15 @@ func GetFileListAsync(args []string) chan Finfo {
return fichan
}

func SetParentPathAsync(in chan Finfo, out chan Finfo, parentPath string) {
go func() {
for item := range in {
item.ParentPath = parentPath
out <- item
}
}()
}

func GetFileList(args []string) (fileList []string, err error) {
fileList = make([]string, 0)
for _, path := range args {
Expand Down