From 49d61fb3ca43b62a6ab4297a4a077a8312101137 Mon Sep 17 00:00:00 2001 From: davidlion Date: Thu, 9 May 2024 15:09:47 -0400 Subject: [PATCH] Added helper function to reader to perform a wildcard query with the max time interval. --- ir/deserializer.go | 38 +++++++++++++++++++------------------- ir/reader.go | 27 ++++++++++++++++++++++++--- ir/reader_test.go | 4 ++-- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/ir/deserializer.go b/ir/deserializer.go index e2012c5..cd0dc8d 100644 --- a/ir/deserializer.go +++ b/ir/deserializer.go @@ -33,10 +33,10 @@ const ( // failure to do so will result in a memory leak. type Deserializer interface { DeserializeLogEvent(irBuf []byte) (*ffi.LogEventView, int, error) - DeserializeWildcardMatch( + DeserializeWildcardMatchWithTimeInterval( irBuf []byte, - timeInterval search.TimestampInterval, mergedQuery search.MergedWildcardQuery, + timeInterval search.TimestampInterval, ) (*ffi.LogEventView, int, int, error) TimestampInfo() TimestampInfo Close() error @@ -161,22 +161,22 @@ func (self *eightByteDeserializer) DeserializeLogEvent( return deserializeLogEvent(self, irBuf) } -// DeserializeWildcardMatch attempts to read the next log event from the IR -// stream in irBuf that matches mergedQuery within timeInterval. It returns the -// deserialized [ffi.LogEventView], the position read to in irBuf (the end of -// the log event in irBuf), the index of the matched query in mergedQuery, -// and an error. On error returns: +// DeserializeWildcardMatchWithTimeInterval attempts to read the next log event +// from the IR stream in irBuf that matches mergedQuery within timeInterval. It +// returns the deserialized [ffi.LogEventView], the position read to in irBuf +// (the end of the log event in irBuf), the index of the matched query in +// mergedQuery, and an error. On error returns: // - nil *ffi.LogEventView // - 0 position // - -1 index // - [IrError] error: CLP failed to successfully deserialize // - [EndOfIr] error: CLP found the IR stream EOF tag -func (self *eightByteDeserializer) DeserializeWildcardMatch( +func (self *eightByteDeserializer) DeserializeWildcardMatchWithTimeInterval( irBuf []byte, - timeInterval search.TimestampInterval, mergedQuery search.MergedWildcardQuery, + timeInterval search.TimestampInterval, ) (*ffi.LogEventView, int, int, error) { - return deserializeWildcardMatch(self, irBuf, timeInterval, mergedQuery) + return deserializeWildcardMatch(self, irBuf, mergedQuery, timeInterval) } // fourByteDeserializer contains both a common CLP IR deserializer and stores @@ -201,22 +201,22 @@ func (self *fourByteDeserializer) DeserializeLogEvent( return deserializeLogEvent(self, irBuf) } -// DeserializeWildcardMatch attempts to read the next log event from the IR -// stream in irBuf that matches mergedQuery within timeInterval. It returns the -// deserialized [ffi.LogEventView], the position read to in irBuf (the end of -// the log event in irBuf), the index of the matched query in mergedQuery, -// and an error. On error returns: +// DeserializeWildcardMatchWithTimeInterval attempts to read the next log event +// from the IR stream in irBuf that matches mergedQuery within timeInterval. It +// returns the deserialized [ffi.LogEventView], the position read to in irBuf +// (the end of the log event in irBuf), the index of the matched query in +// mergedQuery, and an error. On error returns: // - nil *ffi.LogEventView // - 0 position // - -1 index // - [IrError] error: CLP failed to successfully deserialize // - [EndOfIr] error: CLP found the IR stream EOF tag -func (self *fourByteDeserializer) DeserializeWildcardMatch( +func (self *fourByteDeserializer) DeserializeWildcardMatchWithTimeInterval( irBuf []byte, - timeInterval search.TimestampInterval, mergedQuery search.MergedWildcardQuery, + timeInterval search.TimestampInterval, ) (*ffi.LogEventView, int, int, error) { - return deserializeWildcardMatch(self, irBuf, timeInterval, mergedQuery) + return deserializeWildcardMatch(self, irBuf, mergedQuery, timeInterval) } func deserializeLogEvent( @@ -264,8 +264,8 @@ func deserializeLogEvent( func deserializeWildcardMatch( deserializer Deserializer, irBuf []byte, - time search.TimestampInterval, mergedQuery search.MergedWildcardQuery, + time search.TimestampInterval, ) (*ffi.LogEventView, int, int, error) { if 0 >= len(irBuf) { return nil, 0, -1, IncompleteIr diff --git a/ir/reader.go b/ir/reader.go index b09e661..816c8d4 100644 --- a/ir/reader.go +++ b/ir/reader.go @@ -2,6 +2,7 @@ package ir import ( "io" + "math" "strings" "github.com/y-scope/clp-ffi-go/ffi" @@ -86,9 +87,29 @@ func (self *Reader) Read() (*ffi.LogEventView, error) { return event, nil } +// ReadToWildcardMatch wraps ReadToWildcardMatchWithTimeInterval, attempting to +// read the next log event that matches any query in queries, within the entire +// IR. It forwards the result of ReadToWildcardMatchWithTimeInterval. func (self *Reader) ReadToWildcardMatch( - timeInterval search.TimestampInterval, queries []search.WildcardQuery, +) (*ffi.LogEventView, int, error) { + return self.ReadToWildcardMatchWithTimeInterval( + queries, + search.TimestampInterval{0, math.MaxInt64}, + ) +} + +// ReadToWildcardMatchWithTimeInterval attempts to read the next log event that +// matches any query in queries, within timeInterval. It returns the +// deserialized [ffi.LogEventView], the index of the matched query in queries, +// and an error. On error returns: +// - nil *ffi.LogEventView +// - -1 index +// - [IrError] error: CLP failed to successfully deserialize +// - [EndOfIr] error: CLP found the IR stream EOF tag +func (self *Reader) ReadToWildcardMatchWithTimeInterval( + queries []search.WildcardQuery, + timeInterval search.TimestampInterval, ) (*ffi.LogEventView, int, error) { var event *ffi.LogEventView var pos int @@ -96,10 +117,10 @@ func (self *Reader) ReadToWildcardMatch( var err error mergedQuery := search.MergeWildcardQueries(queries) for { - event, pos, matchingQuery, err = self.DeserializeWildcardMatch( + event, pos, matchingQuery, err = self.DeserializeWildcardMatchWithTimeInterval( self.buf[self.start:self.end], - timeInterval, mergedQuery, + timeInterval, ) if IncompleteIr != err { break diff --git a/ir/reader_test.go b/ir/reader_test.go index a37cd32..4d7a439 100644 --- a/ir/reader_test.go +++ b/ir/reader_test.go @@ -42,9 +42,9 @@ func TestIrReader(t *testing.T) { // log, err = irr.Read() // log, err = irr.ReadToContains("ERROR") // var _ search.WildcardQuery - log, _, err = irr.ReadToWildcardMatch( - interval, + log, _, err = irr.ReadToWildcardMatchWithTimeInterval( queries, + interval, ) if nil != err { break