breader

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2022 License: MIT Imports: 6 Imported by: 22

README

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

Documentation

Overview

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

Detail: https://github.com/shenwei356/breader

Index

Constants

This section is empty.

Variables

View Source
var DefaultFunc = func(line string) (interface{}, bool, error) {
	line = strings.TrimRight(line, "\r\n")
	return line, true, nil
}

DefaultFunc just trim the new line symbol

View Source
var ErrorCanceled = errors.New("reading canceled")

ErrorCanceled means that the reading process is canceled

Functions

This section is empty.

Types

type BufferedReader

type BufferedReader struct {
	BufferSize  int
	ChunkSize   int
	ProcessFunc func(string) (interface{}, bool, error)

	Ch chan Chunk
	// contains filtered or unexported fields
}

BufferedReader is BufferedReader

func NewBufferedReader

func NewBufferedReader(file string, bufferSize int, chunkSize int, fn func(line string) (interface{}, bool, error)) (*BufferedReader, error)

NewBufferedReader is the constructor of BufferedReader with full parameters

func NewDefaultBufferedReader

func NewDefaultBufferedReader(file string) (*BufferedReader, error)

NewDefaultBufferedReader creates BufferedReader with default parameter

func (*BufferedReader) Cancel

func (reader *BufferedReader) Cancel()

Cancel method cancel the reading process

type Chunk

type Chunk struct {
	ID   uint64 // useful for keeping the order of chunk in downstream process
	Data []interface{}
	Err  error
}

Chunk is a struct compossing with slice of data and error as status

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL