如何使用io.Pipe()通过ReedSolomon库通过擦除编码对大文件进行分块

时间:2018-08-30 18:23:13

标签: go io erasure-code

使用https://github.com/klauspost/reedsolomon,将大文件擦除成较小的块,我想使用io.Pipe()创建相互连接的管道流。例如:一个管道有助于分块文件,另一个管道则调用func Upload来上传文件

func (rd *ReedSolomon) Chunking() {
    fname := rd.File

    // Create encoding matrix.
    enc, err := reedsolomon.NewStream(rd.data, rd.Par)
    checkErr(err)

    fmt.Println("Opening", fname)
    f, err := os.Open(fname)
    checkErr(err)

    instat, err := f.Stat()
    checkErr(err)

    shards := rd.Data + rd.Par
    out := make([]*os.File, shards)

    // Create the resulting files.
    dir, file := filepath.Split(fname)
    if fi.OutDir != "" {
        dir = fi.OutDir
    }

    for i := range out {
        outfn := fmt.Sprintf("%s.%d", file, i)
        out[i], err = os.Create(filepath.Join(dir, outfn))
        checkErr(err)
    }

    // Split into files.
    data := make([]io.Writer, rd.Data)
    for i := range data {
        data[i] = out[i]
    }

    // Do the split
    err = enc.Split(f, data, instat.Size())
    checkErr(err)

    // Close and re-open the files.
    input := make([]io.Reader, rd.Data)
    target_url := "http://localhost:8080"

    for i := range data {
        out[i].Close()
        f, err := os.Open(out[i].Name())
        checkErr(err)
        input[i] = f
        defer f.Close()
    }

    // Create parity output writers
    parity := make([]io.Writer, rd.Par)
    for i := range parity {
        parity[i] = out[rd.Data+i]
        defer out[rd.Data+i].Close()
    }

    err = enc.Encode(input, parity)
    checkErr(err)

    for _, sd := range out {
        postFile(sd.Name(), target_url)
    }

}

我对io.Pipe()

的使用非常陌生

0 个答案:

没有答案