Documentation
¶
Overview ¶
Package concurrentlineprocessor provides a high-performance, concurrent line-by-line processor for large files or streams.
See reader.go for full package documentation and usage examples.
Package concurrentlineprocessor provides a high-performance, concurrent line-by-line processor for large files or streams.
This package allows you to efficiently process large files or streams by splitting the input into chunks and processing each line concurrently using multiple goroutines.
Features ¶
- Concurrent processing of lines using a configurable number of workers (goroutines)
- Custom line processor function for transforming or filtering lines
- Metrics reporting (bytes read, rows read, processing time, etc.)
- Optional row read limit
Basic Usage ¶
import (
"os"
clp "github.com/anvesh9652/concurrent-line-processor"
)
f, err := os.Open("largefile.txt")
clp.ExitOnError(err)
defer f.Close()
pr := clp.NewConcurrentLineProcessor(f, clp.WithWorkers(4), clp.WithChunkSize(1024*1024))
output, err := io.ReadAll(pr)
clp.ExitOnError(err)
fmt.Println(string(output))
Custom Line Processing ¶
pr := clp.NewConcurrentLineProcessor(f, clp.WithCustomLineProcessor(func(line []byte) ([]byte, error) {
// Transform or filter the line
return bytes.ToUpper(line), nil
}))
Metrics ¶
metrics := pr.Metrics()
fmt.Printf("Rows read: %d, Bytes read: %d, Time took: %s\n", metrics.RowsRead, metrics.BytesRead, metrics.TimeTook)
For more advanced usage, see the examples/ directory.
Package concurrentlineprocessor provides a high-performance, concurrent line-by-line processor for large files or streams.
See reader.go for full package documentation and usage examples.
Package concurrentlineprocessor provides a high-performance, concurrent line-by-line processor for large files or streams.
See reader.go for full package documentation and usage examples.
Index ¶
- Variables
- func AppendNewLine(b *[]byte)
- func ErrWithDebugStack(err error) error
- func ExitOnError(err error)
- func FormatBytes(size int) string
- func IfNull[T any](org *T, def T) T
- func NewConcurrentLineProcessor(r io.Reader, opts ...Option) *concurrentLineProcessor
- func PrintAsJsonString(v any)
- func WithOpts(p *concurrentLineProcessor, opts ...Option)
- type Chunk
- type LineProcessor
- type Metrics
- type Option
Constants ¶
This section is empty.
Variables ¶
var Files = []string{
"/Users/agali/go-workspace/src/github.com/anvesh9652/concurrent-line-processor/data/temp_example.csv",
"/Users/agali/go-workspace/src/github.com/anvesh9652/concurrent-line-processor/tmp/2024-06-04-details.jsonl",
"/Users/agali/go-workspace/src/github.com/anvesh9652/concurrent-line-processor/tmp/transform-00002_1.csv.jsonl",
"/Users/agali/Desktop/Work/go-lang/tryouts/1brc/src_data.txt",
}
Files contains a list of test files used for development and testing. This variable is used internally for testing and benchmarking purposes.
Functions ¶
func AppendNewLine ¶ added in v1.0.10
func AppendNewLine(b *[]byte)
func ErrWithDebugStack ¶
func ExitOnError ¶ added in v1.0.3
func ExitOnError(err error)
func FormatBytes ¶ added in v1.0.10
func NewConcurrentLineProcessor ¶
NewConcurrentLineProcessor creates a new concurrentLineProcessor that reads from the provided io.Reader. It starts processing immediately in background goroutines and returns a processor that implements io.Reader.
The processor splits input into chunks, processes each line concurrently using multiple workers, and provides the processed output through the Read method.
Example:
file, err := os.Open("large-file.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
processor := clp.NewConcurrentLineProcessor(file,
clp.WithWorkers(8),
clp.WithChunkSize(1024*1024),
)
output, err := io.ReadAll(processor)
if err != nil {
log.Fatal(err)
}
func PrintAsJsonString ¶
func PrintAsJsonString(v any)
Types ¶
type Chunk ¶
type Chunk struct {
// contains filtered or unexported fields
}
Chunk represents a piece of data to be processed, containing an ID for ordering and a pointer to the actual data buffer.
type LineProcessor ¶
LineProcessor is a function type for processing individual lines. It receives a line as []byte and returns the processed line and any error. Implementations must be thread-safe as they may be called concurrently.
type Metrics ¶
type Metrics struct {
// BytesRead is the total number of bytes read from the source reader.
// When RowsReadLimit is set, it might read more bytes than the transformed bytes.
BytesRead int64 `json:"bytes_read"`
// BytesTransformed is the total number of bytes after processing each line.
BytesTransformed int64 `json:"bytes_transformed"`
// RowsRead is the total number of rows read from the source reader.
RowsRead int64 `json:"rows_read"`
// RowsWritten is the total number of rows written to the output stream.
RowsWritten int64 `json:"rows_written"`
// TimeTook is the total time taken to read and process the data.
TimeTook string `json:"time_took"`
}
Metrics contains performance and processing statistics for a concurrentLineProcessor.
type Option ¶
type Option func(*concurrentLineProcessor)
Option is a function type for configuring concurrentLineProcessor instances. Options are passed to NewConcurrentLineProcessor to customize behavior.
func WithChannelSize ¶ added in v1.0.10
WithChannelSize sets the size of the channels used for input and output streams. A larger channel size can improve throughput for high-volume data processing, The default channel size is 100.
Example:
clp.NewConcurrentLineProcessor(reader, clp.WithChannelSize(1000)) // 1000 items in channel
func WithChunkSize ¶
WithChunkSize sets the chunk size for reading data from the source. Larger chunk sizes can improve performance for large files but may use more memory. The default chunk size is 64KB.
Example:
clp.NewConcurrentLineProcessor(reader, clp.WithChunkSize(1024*1024)) // 1MB chunks
func WithCustomLineProcessor ¶
func WithCustomLineProcessor(c LineProcessor) Option
WithCustomLineProcessor sets a custom function to process each line. The function receives a line as []byte and should return the processed line. The function must be thread-safe and should not modify external state without proper synchronization.
Example:
// Convert lines to uppercase
processor := func(line []byte) ([]byte, error) {
return bytes.ToUpper(line), nil
}
clp.NewConcurrentLineProcessor(reader, clp.WithCustomLineProcessor(processor))
func WithRowsReadLimit ¶
WithRowsReadLimit sets a limit on the number of rows to read from the source. Use -1 for no limit (default). This is useful for processing only a subset of a large file for testing or sampling purposes.
Example:
clp.NewConcurrentLineProcessor(reader, clp.WithRowsReadLimit(1000)) // Process only first 1000 lines
func WithWorkers ¶
WithWorkers sets the number of worker goroutines for concurrent processing. More workers can improve performance for CPU-intensive line processing, but may not help for I/O-bound operations. The default is runtime.NumCPU().
Example:
clp.NewConcurrentLineProcessor(reader, clp.WithWorkers(8))