This repository was archived by the owner on Jan 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
Concurrent Decoder #589
Closed
Closed
Concurrent Decoder #589
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
119462c
feat: add Avro Object Container File (OCF) encoding and decoding with…
e54081b
Merge branch 'main' of https://github.com/TuSKan/avro into header
e5cf869
support reseting encoder to reduce load on GC. (#587)
aryehlev 83e8673
Revert "support reseting encoder to reduce load on GC. (#587)"
5ea0bc7
Merge branch 'header' of https://github.com/TuSKan/avro into header
d9175e7
test: add comprehensive tests for Avro reader skip methods
60eaece
fix tests
bcc8be6
test
069ff00
golangci-lint
282a47f
fix: change writer container config to default on ocf.Encoder
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,20 @@ type decoderConfig struct { | |
| CodecOptions codecOptions | ||
| } | ||
|
|
||
| func newDecoderConfig(opts ...DecoderFunc) *decoderConfig { | ||
| cfg := decoderConfig{ | ||
| DecoderConfig: avro.DefaultConfig, | ||
| SchemaCache: avro.DefaultSchemaCache, | ||
| CodecOptions: codecOptions{ | ||
| DeflateCompressionLevel: flate.DefaultCompression, | ||
| }, | ||
| } | ||
| for _, opt := range opts { | ||
| opt(&cfg) | ||
| } | ||
| return &cfg | ||
| } | ||
|
|
||
| // DecoderFunc represents a configuration function for Decoder. | ||
| type DecoderFunc func(cfg *decoderConfig) | ||
|
|
||
|
|
@@ -96,23 +110,16 @@ type Decoder struct { | |
| codec Codec | ||
|
|
||
| count int64 | ||
| size int64 | ||
| n int64 | ||
| } | ||
|
|
||
| // NewDecoder returns a new decoder that reads from reader r. | ||
| func NewDecoder(r io.Reader, opts ...DecoderFunc) (*Decoder, error) { | ||
| cfg := decoderConfig{ | ||
| DecoderConfig: avro.DefaultConfig, | ||
| SchemaCache: avro.DefaultSchemaCache, | ||
| CodecOptions: codecOptions{ | ||
| DeflateCompressionLevel: flate.DefaultCompression, | ||
| }, | ||
| } | ||
| for _, opt := range opts { | ||
| opt(&cfg) | ||
| } | ||
|
|
||
| reader := avro.NewReader(r, 1024) | ||
|
|
||
| cfg := newDecoderConfig(opts...) | ||
|
|
||
| h, err := readHeader(reader, cfg.SchemaCache, cfg.CodecOptions) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("decoder: %w", err) | ||
|
|
@@ -131,6 +138,31 @@ func NewDecoder(r io.Reader, opts ...DecoderFunc) (*Decoder, error) { | |
| }, nil | ||
| } | ||
|
|
||
| // NewDecoderWithHeader returns a new decoder that reads from reader r using the provided header. | ||
| func NewDecoderWithHeader(r *avro.Reader, h *OCFHeader, opts ...DecoderFunc) (*Decoder, error) { | ||
| cfg := newDecoderConfig(opts...) | ||
| decReader := bytesx.NewResetReader([]byte{}) | ||
| return &Decoder{ | ||
| reader: r, | ||
| resetReader: decReader, | ||
| decoder: cfg.DecoderConfig.NewDecoder(h.Schema, decReader), | ||
| meta: h.Meta, | ||
| sync: h.Sync, | ||
| codec: h.Codec, | ||
| schema: h.Schema, | ||
| }, nil | ||
| } | ||
|
|
||
| // DecodeHeader reads and decodes the Avro container file header from r. | ||
| func DecodeHeader(r *avro.Reader, opts ...DecoderFunc) (*OCFHeader, error) { | ||
| cfg := newDecoderConfig(opts...) | ||
| h, err := readHeader(r, cfg.SchemaCache, cfg.CodecOptions) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("decoder: %w", err) | ||
| } | ||
| return h, nil | ||
| } | ||
|
|
||
| // Metadata returns the header metadata. | ||
| func (d *Decoder) Metadata() map[string][]byte { | ||
| return d.meta | ||
|
|
@@ -145,8 +177,8 @@ func (d *Decoder) Schema() avro.Schema { | |
| // HasNext determines if there is another value to read. | ||
| func (d *Decoder) HasNext() bool { | ||
| if d.count <= 0 { | ||
| count := d.readBlock() | ||
| d.count = count | ||
| d.count, d.size = d.readBlock() | ||
| d.n = d.count | ||
| } | ||
|
|
||
| if d.reader.Error != nil { | ||
|
|
@@ -184,11 +216,29 @@ func (d *Decoder) Close() error { | |
| return nil | ||
| } | ||
|
|
||
| func (d *Decoder) readBlock() int64 { | ||
| // BlockStatus represents the status of the current block. | ||
| type BlockStatus struct { | ||
| Current int64 | ||
| Count int64 | ||
| Size int64 | ||
| Offset int64 | ||
| } | ||
|
|
||
| // BlockStatus returns the current block status. | ||
| func (d *Decoder) BlockStatus() *BlockStatus { | ||
| return &BlockStatus{ | ||
| Current: d.n - d.count + 1, | ||
| Count: d.n, | ||
| Size: d.size, | ||
| Offset: d.reader.InputOffset(), | ||
| } | ||
| } | ||
|
|
||
| func (d *Decoder) readBlock() (int64, int64) { | ||
| _ = d.reader.Peek() | ||
| if errors.Is(d.reader.Error, io.EOF) { | ||
| // There is no next block | ||
| return 0 | ||
| return 0, 0 | ||
| } | ||
|
|
||
| count := d.reader.ReadLong() | ||
|
|
@@ -220,7 +270,7 @@ func (d *Decoder) readBlock() int64 { | |
| d.reader.Error = errors.New("decoder: invalid block") | ||
| } | ||
|
|
||
| return count | ||
| return count, size | ||
| } | ||
|
|
||
| type encoderConfig struct { | ||
|
|
@@ -379,7 +429,7 @@ func newEncoder(schema avro.Schema, w io.Writer, cfg encoderConfig) (*Encoder, e | |
| return nil, err | ||
| } | ||
|
|
||
| writer := avro.NewWriter(w, 512, avro.WithWriterConfig(cfg.EncodingConfig)) | ||
| writer := avro.NewWriter(w, 512, avro.WithWriterConfig(avro.DefaultConfig)) | ||
|
Comment on lines
-382
to
+432
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix bug #590 |
||
| buf := &bytes.Buffer{} | ||
| e := &Encoder{ | ||
| writer: writer, | ||
|
|
@@ -420,7 +470,7 @@ func newEncoder(schema avro.Schema, w io.Writer, cfg encoderConfig) (*Encoder, e | |
| return nil, err | ||
| } | ||
|
|
||
| writer := avro.NewWriter(w, 512, avro.WithWriterConfig(cfg.EncodingConfig)) | ||
| writer := avro.NewWriter(w, 512, avro.WithWriterConfig(avro.DefaultConfig)) | ||
|
Comment on lines
-423
to
+473
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same above |
||
| writer.WriteVal(HeaderSchema, header) | ||
| if err = writer.Flush(); err != nil { | ||
| return nil, err | ||
|
|
@@ -567,14 +617,15 @@ func (e *Encoder) writerBlock() error { | |
| return e.writer.Flush() | ||
| } | ||
|
|
||
| type ocfHeader struct { | ||
| // OCFHeader represents the parsed header of an OCF file. | ||
| type OCFHeader struct { //nolint:revive | ||
| Schema avro.Schema | ||
| Codec Codec | ||
| Meta map[string][]byte | ||
| Sync [16]byte | ||
| } | ||
|
|
||
| func readHeader(reader *avro.Reader, schemaCache *avro.SchemaCache, codecOpts codecOptions) (*ocfHeader, error) { | ||
| func readHeader(reader *avro.Reader, schemaCache *avro.SchemaCache, codecOpts codecOptions) (*OCFHeader, error) { | ||
| var h Header | ||
| reader.ReadVal(HeaderSchema, &h) | ||
| if reader.Error != nil { | ||
|
|
@@ -594,7 +645,7 @@ func readHeader(reader *avro.Reader, schemaCache *avro.SchemaCache, codecOpts co | |
| return nil, err | ||
| } | ||
|
|
||
| return &ocfHeader{ | ||
| return &OCFHeader{ | ||
| Schema: schema, | ||
| Codec: codec, | ||
| Meta: h.Meta, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that this change helped anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoiding duplicate code.