@@ -11,13 +11,13 @@ import (
1111 "github.com/y-scope/clp-ffi-go/ir"
1212)
1313
14- // Converts log events into Zstd compressed IR. Log events provided to writer are immediately
15- // converted to Zstd compressed IR and stored in [memoryWriter.ZstdBuffer]. After the Zstd buffer
16- // receives logs, they are immediately sent to s3.
14+ // Converts log events into Zstd compressed IR. Log events are immediately converted to Zstd
15+ // compressed IR and stored in [memoryWriter.zstdBuffer].
1716type memoryWriter struct {
18- zstdBuffer * bytes.Buffer
19- irWriter * ir.Writer
20- zstdWriter * zstd.Encoder
17+ zstdBuffer * bytes.Buffer
18+ irWriter * ir.Writer
19+ zstdWriter * zstd.Encoder
20+ irTotalBytes int
2121}
2222
2323// Opens a new [memoryWriter] with a memory buffer for Zstd output. For use when use_disk_store is
@@ -57,7 +57,8 @@ func NewMemoryWriter() (*memoryWriter, error) {
5757// - numEvents: Number of log events successfully written to IR writer buffer
5858// - err: Error writing IR/Zstd
5959func (w * memoryWriter ) WriteIrZstd (logEvents []ffi.LogEvent ) (int , error ) {
60- _ , numEvents , err := writeIr (w .irWriter , logEvents )
60+ numBytes , numEvents , err := writeIr (w .irWriter , logEvents )
61+ w .irTotalBytes += numBytes
6162 if err != nil {
6263 return numEvents , err
6364 }
@@ -87,6 +88,7 @@ func (w *memoryWriter) Reset() error {
8788 var err error
8889 w .zstdBuffer .Reset ()
8990 w .zstdWriter .Reset (w .zstdBuffer )
91+ w .irTotalBytes = 0
9092
9193 w .irWriter , err = ir.NewWriter [ir.FourByteEncoding ](w .zstdWriter )
9294 if err != nil {
@@ -96,14 +98,6 @@ func (w *memoryWriter) Reset() error {
9698 return nil
9799}
98100
99- // Getter for useDiskBuffer.
100- //
101- // Returns:
102- // - useDiskBuffer: On/off for disk buffering
103- func (w * memoryWriter ) GetUseDiskBuffer () bool {
104- return false
105- }
106-
107101// Getter for Zstd Output.
108102//
109103// Returns:
@@ -113,7 +107,8 @@ func (w *memoryWriter) GetZstdOutput() io.Reader {
113107}
114108
115109// Get size of Zstd output. [zstd] does not provide the amount of bytes written with each write.
116- // Instead, calling Len() on buffer.
110+ // Instead, calling Len() on buffer. Size may slightly lag the real size since some data in the
111+ // current block will be in the [zstd] encoder's internal buffer.
117112//
118113// Returns:
119114// - size: Bytes written
@@ -139,3 +134,12 @@ func (w *memoryWriter) Close() error {
139134 }
140135 return nil
141136}
137+
138+ // Checks if writer is empty. True if no events are buffered.
139+ //
140+ // Returns:
141+ // - empty: Boolean value that is true if buffer is empty
142+ // - err: nil error to comply with interface
143+ func (w * memoryWriter ) Empty () (bool , error ) {
144+ return w .irTotalBytes == 0 , nil
145+ }
0 commit comments