Coder Social home page Coder Social logo

shenwei356 / breader Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 2.0 17 KB

breader (Buffered File Reader), asynchronous parsing and pre-processing while reading file. Safe cancellation is also supported.

License: MIT License

Go 100.00%
buffered golang asynchronous

breader's Introduction

breader

GoDoc Go Report Card

breader (Buffered File Reader), asynchronous parsing and pre-processing while reading file. Safe cancellation is also supported.

Example

1). Simple example with default parameters (ChunkSize: 100; BufferSize: #. of CPUs, ProcessFunc: trimming new-line symbol)

import "github.com/shenwei356/breader"

reader, err := breader.NewDefaultBufferedReader(file)
checkErr(err)

for chunk := range reader.Ch {
    checkError(chunk.Err)
    for _, data := range chunk.Data {
        line := data.(string)
        fmt.Println(line)
    }
}

2). Example with custom pre-processing function: splitting line to slice. Note the processing of interface{} containing slice, using a custom struct is recommended.

type Slice []string // custom type
fn := func(line string) (interface{}, bool, error) {
    line = strings.TrimRight(line, "\n")
    if line == "" || line[0] == '#' { // ignoring blank line and comment line
        return "", false, nil
    }
    items := strings.Split(line, "\t")
    if len(items) != 2 {
        return items, false, nil
    }
    return Slice(items), true, nil
}

reader, err := breader.NewBufferedReader(file, runtime.NumCPU(), 100, fn)
checkErr(err)

for chunk := range reader.Ch {
    checkError(chunk.Err)

    for _, data := range chunk.Data {
        // do not simply use: data.(slice)
        fmt.Println(data.(Slice))
    }
}

3). Example with custom pre-processing function: creating object from line data.

type string2int struct {
    id    string
    value int
}

fn := func(line string) (interface{}, bool, error) {
    line = strings.TrimRight(line, "\n")
    if line == "" || line[0] == '#' {
        return nil, false, nil
    }
    items := strings.Split(line, "\t")
    if len(items) != 2 {
        return nil, false, nil
    }
    if items[0] == "" || items[1] == "" {
        return nil, false, nil
    }
    id := items[0]
    value, err := strconv.Atoi(items[1])
    if err != nil {
        return nil, false, err
    }
    return string2int{id, value}, true, nil
}


reader, err := breader.NewBufferedReader(file, runtime.NumCPU(), 100, fn)
checkErr(err)

for chunk := range reader.Ch {
    checkError(chunk.Err)

    for _, data := range chunk.Data {
        obj := data.(string2int)
        // handle of the string2int object
    }
}

4). Example of cancellation. Note that range chanel is buffered, therefore, for-select-case is used.

reader, err := breader.NewBufferedReader(testfile, 0, 1, breader.DefaultFunc)
checkErr(err)

// note that range is bufferd. using range will be failed
// for chunk := range reader.Ch {
LOOP:
    for {
        select {
        case chunk := <-reader.Ch:
            if chunk.Err != nil {
                t.Log(chunk.Err)
                return
            }
            reader.Cancel()
            break LOOP
        default:
        }
    }

License

MIT License

breader's People

Contributors

shenwei356 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

boogermann iamh2o

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.