From 3bc01a1ad646454c0f9232143c0b21c8fbd4f275 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 11 May 2024 01:10:07 +0200 Subject: [PATCH] wip --- lib/logstorage/block.go | 6 ++--- lib/logstorage/values_encoder.go | 33 +++++++++++++-------------- lib/logstorage/values_encoder_test.go | 2 +- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/lib/logstorage/block.go b/lib/logstorage/block.go index 9060d9e94..d4bfcc0cc 100644 --- a/lib/logstorage/block.go +++ b/lib/logstorage/block.go @@ -433,7 +433,7 @@ func (b *block) InitFromBlockData(bd *blockData, sbu *stringsBlockUnmarshaler, v if err != nil { return fmt.Errorf("cannot unmarshal column %d: %w", i, err) } - if err = vd.decodeInplace(c.values, cd.valueType, &cd.valuesDict); err != nil { + if err = vd.decodeInplace(c.values, cd.valueType, cd.valuesDict.values); err != nil { return fmt.Errorf("cannot decode column values: %w", err) } } @@ -577,9 +577,7 @@ func getColumnIdxs() map[string]int { } func putColumnIdxs(m map[string]int) { - for k := range m { - delete(m, k) - } + clear(m) columnIdxsPool.Put(m) } diff --git a/lib/logstorage/values_encoder.go b/lib/logstorage/values_encoder.go index 9ccb0164d..de0a38dd5 100644 --- a/lib/logstorage/values_encoder.go +++ b/lib/logstorage/values_encoder.go @@ -135,10 +135,10 @@ func (vd *valuesDecoder) reset() { vd.buf = vd.buf[:0] } -// decodeInplace decodes values encoded with the given vt and the given dict inplace. +// decodeInplace decodes values encoded with the given vt and the given dictValues inplace. // // the decoded values remain valid until vd.reset() is called. -func (vd *valuesDecoder) decodeInplace(values []string, vt valueType, dict *valuesDict) error { +func (vd *valuesDecoder) decodeInplace(values []string, vt valueType, dictValues []string) error { // do not reset vd.buf, since it may contain previously decoded data, // which must be preserved until reset() call. dstBuf := vd.buf @@ -146,6 +146,14 @@ func (vd *valuesDecoder) decodeInplace(values []string, vt valueType, dict *valu switch vt { case valueTypeString: // nothing to do - values are already decoded. + case valueTypeDict: + for i, v := range values { + id := int(v[0]) + if id >= len(dictValues) { + return fmt.Errorf("unexpected dictionary id: %d; it must be smaller than %d", id, len(dictValues)) + } + values[i] = dictValues[id] + } case valueTypeUint8: for i, v := range values { if len(v) != 1 { @@ -189,14 +197,14 @@ func (vd *valuesDecoder) decodeInplace(values []string, vt valueType, dict *valu dstBuf = marshalUint64(dstBuf, n) values[i] = bytesutil.ToUnsafeString(dstBuf[dstLen:]) } - case valueTypeDict: - dictValues := dict.values + case valueTypeFloat64: for i, v := range values { - id := int(v[0]) - if id >= len(dictValues) { - return fmt.Errorf("unexpected dictionary id: %d; it must be smaller than %d", id, len(dictValues)) + if len(v) != 8 { + return fmt.Errorf("unexpected value length for uint64; got %d; want 8", len(v)) } - values[i] = dictValues[id] + dstLen := len(dstBuf) + dstBuf = toFloat64String(dstBuf, v) + values[i] = bytesutil.ToUnsafeString(dstBuf[dstLen:]) } case valueTypeIPv4: for i, v := range values { @@ -216,15 +224,6 @@ func (vd *valuesDecoder) decodeInplace(values []string, vt valueType, dict *valu dstBuf = toTimestampISO8601String(dstBuf, v) values[i] = bytesutil.ToUnsafeString(dstBuf[dstLen:]) } - case valueTypeFloat64: - for i, v := range values { - if len(v) != 8 { - return fmt.Errorf("unexpected value length for uint64; got %d; want 8", len(v)) - } - dstLen := len(dstBuf) - dstBuf = toFloat64String(dstBuf, v) - values[i] = bytesutil.ToUnsafeString(dstBuf[dstLen:]) - } default: return fmt.Errorf("unknown valueType=%d", vt) } diff --git a/lib/logstorage/values_encoder_test.go b/lib/logstorage/values_encoder_test.go index 8de5d290d..693d71495 100644 --- a/lib/logstorage/values_encoder_test.go +++ b/lib/logstorage/values_encoder_test.go @@ -26,7 +26,7 @@ func TestValuesEncoder(t *testing.T) { putValuesEncoder(ve) vd := getValuesDecoder() - if err := vd.decodeInplace(encodedValues, vt, &dict); err != nil { + if err := vd.decodeInplace(encodedValues, vt, dict.values); err != nil { t.Fatalf("unexpected error in decodeInplace(): %s", err) } if len(values) == 0 {