diff --git a/lg/metric.go b/lg/metric.go index 9976fd4..8aa6478 100644 --- a/lg/metric.go +++ b/lg/metric.go @@ -38,9 +38,11 @@ func initMetrics(ctx context.Context, name string) (context.Context, func() erro goversion := "" pkg := "" host := "" + version := "0.0.1" if info, ok := debug.ReadBuildInfo(); ok { goversion = info.GoVersion pkg = info.Path + version = info.Main.Version } if h, err := os.Hostname(); err == nil { host = h @@ -69,7 +71,7 @@ func initMetrics(ctx context.Context, name string) (context.Context, func() erro ) meter := provider.Meter(name, - api.WithInstrumentationVersion("0.0.1"), + api.WithInstrumentationVersion(version), api.WithInstrumentationAttributes( attribute.String("app", name), attribute.String("host", host), diff --git a/lg/tracer.go b/lg/tracer.go index 99b0190..16305ae 100644 --- a/lg/tracer.go +++ b/lg/tracer.go @@ -64,12 +64,11 @@ func (w wrapSpan) AddEvent(name string, options ...trace.EventOption) { cfg := trace.NewEventConfig(options...) attrs := cfg.Attributes() - args := make([]any, len(attrs)*2) + args := make([]any, len(attrs)) for i, a := range attrs { - args[2*i] = a.Key - args[2*i+1] = a.Value - } + args[i] = slog.Attr{Key: string(a.Key), Value: slog.StringValue(a.Value.AsString())} + } slog.Debug(name, args...) } diff --git a/lsm/sst.go b/lsm/sst.go index 199ad50..e95731c 100644 --- a/lsm/sst.go +++ b/lsm/sst.go @@ -316,15 +316,24 @@ func (l *logFile) LoadSegment(pos int64) (*segmentBytes, error) { return &segmentBytes{b, -1}, nil } func (l *logFile) Find(needle []byte, first bool) (*entryBytes, bool, error) { - var last segmentReader + var cur, last segmentReader for _, s := range l.segments { - e, err := s.FirstEntry() + cur = s + e, err := cur.FirstEntry() if err != nil { return nil, false, err } k, _ := e.KeyValue() - if first && bytes.Compare(k, needle) >= 0 { + + if first && bytes.Equal(k, needle) { + break + } + if first && bytes.Compare(k, needle) > 0 { + e, ok, err := cur.Find(needle, first) + if ok || err != nil{ + return e, ok, err + } break } if !first && bytes.Compare(k, needle) > 0 { @@ -333,7 +342,12 @@ func (l *logFile) Find(needle []byte, first bool) (*entryBytes, bool, error) { last = s } - return last.Find(needle, first) + e, ok, err := last.Find(needle, first) + if ok || err != nil{ + return e, ok, err + } + // if by mistake it was not found in the last.. check the next segment. + return cur.Find(needle, first) } func (l *logFile) WriteTo(w io.Writer) (int64, error) { return l.rd.WriteTo(w) diff --git a/lsm/sst_test.go b/lsm/sst_test.go index 319a09a..7e842ac 100644 --- a/lsm/sst_test.go +++ b/lsm/sst_test.go @@ -40,7 +40,7 @@ func TestLargeFile(t *testing.T) { } t.Log(f.Stat()) - tt, ok, err := sf.Find(needle, false) + tt, ok, err := sf.Find(needle, true) is.NoErr(err) is.True(ok) key, val := tt.KeyValue() @@ -183,6 +183,17 @@ func TestFindRange(t *testing.T) { is.Equal(key, []byte("AB")) is.Equal(val, uint64(2)) + last, ok, err = sf.Find([]byte("AB"), false) + is.NoErr(err) + + key, val = last.KeyValue() + t.Log(string(key), val) + + is.True(ok) + is.Equal(key, []byte("AB")) + is.Equal(val, uint64(4)) + + last, ok, err = sf.Find([]byte("AC"), false) is.NoErr(err)