-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename file so git tracking is better
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Package implements writer that converts log events to Zstd compressed IR. Effectively chaining | ||
// together [ir.Writer] and [zstd.Encoder] in series. | ||
|
||
package irzstd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
|
||
"github.com/y-scope/clp-ffi-go/ffi" | ||
"github.com/y-scope/clp-ffi-go/ir" | ||
) | ||
|
||
type Writer interface { | ||
Check failure on line 16 in internal/irzstd/writer.go GitHub Actions / go-lint (1.22, macos-latest)
|
||
// Converts log events to Zstd compressed IR and outputs to the Zstd buffer. | ||
// | ||
// Parameters: | ||
// - logEvents: A slice of log events to be encoded | ||
// | ||
// Returns: | ||
// - err | ||
WriteIrZstd([]ffi.LogEvent) error | ||
|
||
// Closes IR stream and Zstd frame. After calling close, Writer must be Reset() prior to calling | ||
// write. | ||
// | ||
// Returns: | ||
// - err | ||
CloseStreams() error | ||
|
||
// Closes Writer. | ||
// | ||
// Returns: | ||
// - err | ||
Close() error | ||
|
||
// Reinitialize Writer after calling CloseStreams(). | ||
// | ||
// Returns: | ||
// - err | ||
Reset() error | ||
|
||
// Getter for useDiskBuffer. | ||
// | ||
// Returns: | ||
// - useDiskBuffer: On/off for disk buffering | ||
GetUseDiskBuffer() bool | ||
|
||
// Getter for Zstd Output. | ||
// | ||
// Returns: | ||
// - zstdOutput: Reader for Zstd output | ||
GetZstdOutput() io.Reader | ||
|
||
// Get size of Zstd output. | ||
// | ||
// Returns: | ||
// - size: Bytes written | ||
// - err | ||
GetZstdOutputSize() (int, error) | ||
} | ||
|
||
// Writes log events to a IR Writer. | ||
// | ||
// Parameters: | ||
// - irWriter: CLP IR writer to write each log event with | ||
// - logEvents: A slice of log events to be encoded | ||
// | ||
// Returns: | ||
// - err: error if an event could not be written | ||
func writeIr(irWriter *ir.Writer, logEvents []ffi.LogEvent) error { | ||
Check failure on line 73 in internal/irzstd/writer.go GitHub Actions / go-lint (1.22, macos-latest)
|
||
for _, event := range logEvents { | ||
_, err := irWriter.Write(event) | ||
if err != nil { | ||
err = fmt.Errorf("failed to encode event %v into ir: %w", event, err) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Opens a new [ir.Writer] and [zstd.Encoder]. | ||
// | ||
// Parameters: | ||
// - zstdOutput: Output location for Zstd | ||
// - timezone: Time zone of the log source | ||
// - size: Byte length | ||
// | ||
// Returns: | ||
// - irWriter: Writer for CLP IR | ||
// - ZstdWriter: Writer for Zstd | ||
// - err: Error opening IR/Zstd writer | ||
func newIrZstdWriters( | ||
zstdOutput io.Writer, | ||
timezone string, | ||
size int, | ||
) (*ir.Writer, *zstd.Encoder, error) { | ||
// IR buffer using bytes.Buffer internally, so it will dynamically grow if undersized. Using | ||
// FourByteEncoding as default encoding. | ||
irWriter, err := ir.NewWriterSize[ir.FourByteEncoding](size, timezone) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error opening IR writer: %w", err) | ||
} | ||
|
||
zstdWriter, err := zstd.NewWriter(zstdOutput) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error opening Zstd writer: %w", err) | ||
} | ||
return irWriter, zstdWriter, err | ||
} |