Documentation
¶
Overview ¶
Package jsonstreamer provides efficient streaming processing capabilities for large JSON array files.
Index ¶
- func Read[T any](ctx context.Context, filename string, bufferSize int, options ...Func[T, T]) error
- func ReadTo[Source any, Target any](ctx context.Context, filename string, bufferSize int, ...) (errP error)
- type CallbackPerBufferFunc
- type CallbackPerEntryFunc
- type Errors
- type Func
- func WithAmountOfEntries[Source any, Target any](amountOfEntries int) Func[Source, Target]
- func WithBufferCh[Source any, Target any](bufferCh chan<- []Target) Func[Source, Target]
- func WithCallbackPerBuffer[Source any, Target any](callback CallbackPerBufferFunc[Target]) Func[Source, Target]
- func WithCallbackPerEntry[Source any, Target any](callback CallbackPerEntryFunc[Target]) Func[Source, Target]
- func WithCounterCh[Source any, Target any](counterCh chan<- int) Func[Source, Target]
- func WithErrorCh[Source any, Target any](errorCh chan<- error) Func[Source, Target]
- func WithProcessor[Source any, Target any](processor ProcessorFunc[Source, Target]) Func[Source, Target]
- func WithStartFrom[Source any, Target any](startFrom int) Func[Source, Target]
- type Options
- type ProcessorFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Read ¶
func Read[T any]( ctx context.Context, filename string, bufferSize int, options ...Func[T, T], ) error
Read is like ReadTo but with one defined type, setting default processor that returns the same document.
func ReadTo ¶
func ReadTo[Source any, Target any]( ctx context.Context, filename string, bufferSize int, options ...Func[Source, Target], ) (errP error)
ReadTo reads JSON array entries from a file, processes them, and outputs the results through channels or callback functions. It supports batch processing with buffering and entry skipping capabilities.
Parameters:
- ctx: Context for cancellation control
- filename: Path to the JSON file containing an array of Source type elements
- bufferSize: Number of processed entries to accumulate before triggering output
- options: Configuration options.
The function expects the input file to contain a valid JSON array. It processes entries one by one, allowing for efficient memory usage with large files. The function provides multiple output mechanisms that can be used independently or in combination:
- Buffer channel output (BufferCh): Processed entries are sent in batches
- Per-entry callback: Called immediately after processing each entry
- Per-buffer callback: Called when a buffer reaches bufferSize or at end of processing
Error Handling:
- Implements panic recovery with stack trace capture
- Returns custom errors for various failure scenarios
- Maintains error threshold (maxErrorCount) to prevent infinite processing
- Sends errors through ErrorCh for asynchronous handling
- Accumulates errors for final error reporting
Recovery Mechanism:
- Tracks the last valid entry offset for potential error recovery
- Maintains processing state through currentOffset
- Supports skipping invalid entries to continue processing
Context Support:
- Honors context cancellation for graceful shutdown
- Returns appropriate error when context is cancelled
The function implements graceful error handling and ensures resources are properly managed even in error conditions. It supports early termination through context cancellation and provides detailed error information for debugging purposes.
Flowchart:
```mermaid flowchart TD
Start([Start]) --> PanicRecover[Set up panic recovery]
PanicRecover --> ApplyOptions[Apply options]
ApplyOptions --> InputValidation{Check outputs}
InputValidation -->|Invalid| ReturnError1[Return Error: Missing outputs]
InputValidation -->|Valid| ValidateAmount{Validate amount}
ValidateAmount -->|Invalid| ReturnError2[Return Error: Invalid amount]
ValidateAmount -->|Valid| OpenFile[Open JSON file]
OpenFile -->|Error| ReturnError3[Return Error: Failed to open]
OpenFile -->|Success| ValidateArray[Validate JSON array structure]
ValidateArray -->|Invalid| ReturnError4[Return Error: Invalid format]
ValidateArray -->|Valid| ProcessLoop[Enter processing loop]
ProcessLoop --> CheckContext{Check context}
CheckContext -->|Cancelled| ReturnError5[Return Error: Context done]
CheckContext -->|Active| CheckErrors{Check error count}
CheckErrors -->|Too many| ReturnError6[Return Error: Too many errors]
CheckErrors -->|OK| CheckAmount{Check amount limit}
CheckAmount -->|Reached| HandleRemaining[Handle remaining buffer]
CheckAmount -->|Not reached| ReadEntry[Read JSON entry]
ReadEntry -->|Error| HandleDecodeError[Handle decode error]
ReadEntry -->|Success| CheckSkip{Skip entry?}
CheckSkip -->|Yes| UpdateOffset[Update last valid offset]
CheckSkip -->|No| ProcessDoc[Process document]
ProcessDoc -->|Error| SendError[Send to ErrorCh]
ProcessDoc -->|Success| UpdateCounter[Update CounterCh]
UpdateCounter --> HandleCallbacks[Handle callbacks]
SendError --> ProcessLoop
HandleCallbacks --> BufferCheck{Buffer full?}
BufferCheck -->|Yes| SendBuffer[Send buffer]
BufferCheck -->|No| ProcessLoop
SendBuffer --> ProcessLoop
HandleRemaining --> End([End])
HandleDecodeError --> ProcessLoop
UpdateOffset --> ProcessLoop
classDef error fill:#ff9999
class ReturnError1,ReturnError2,ReturnError3,ReturnError4,ReturnError5,ReturnError6 error
```.
Types ¶
type CallbackPerBufferFunc ¶ added in v0.0.7
CallbackPerBufferFunc is a function applied to a buffer of JSON entries AFTER they are processed.
type CallbackPerEntryFunc ¶ added in v0.0.7
CallbackPerEntryFunc is a function applied to each JSON entry AFTER it being processed.
type Errors ¶ added in v0.0.6
type Errors []error
Errors is a slice of errors.
type Func ¶ added in v0.0.7
Func allows to set options.
func WithAmountOfEntries ¶ added in v0.0.7
WithAmountOfEntries sets the amount of entries.
func WithBufferCh ¶ added in v0.0.7
WithBufferCh sets the buffer channel.
func WithCallbackPerBuffer ¶ added in v0.0.7
func WithCallbackPerBuffer[Source any, Target any](callback CallbackPerBufferFunc[Target]) Func[Source, Target]
WithCallbackPerBuffer sets the callback per buffer function.
func WithCallbackPerEntry ¶ added in v0.0.7
func WithCallbackPerEntry[Source any, Target any](callback CallbackPerEntryFunc[Target]) Func[Source, Target]
WithCallbackPerEntry sets the callback per entry function.
func WithCounterCh ¶ added in v0.0.8
WithCounterCh sets the counter channel.
func WithErrorCh ¶ added in v0.0.7
WithErrorCh sets the error channel.
func WithProcessor ¶ added in v0.0.7
func WithProcessor[Source any, Target any](processor ProcessorFunc[Source, Target]) Func[Source, Target]
WithProcessor sets the processor function.
type Options ¶ added in v0.0.7
type Options[Source any, Target any] struct { AmountOfEntries int BufferCh chan<- []Target CallbackPerBuffer CallbackPerBufferFunc[Target] CallbackPerEntry CallbackPerEntryFunc[Target] CounterCh chan<- int ErrorCh chan<- error Processor ProcessorFunc[Source, Target] StartFrom int }
Options contains the fields shared between request's options.
type ProcessorFunc ¶ added in v0.0.7
ProcessorFunc is a function that processes a JSON entry transforming it from the Source type to the Target type.