cleanup rwSymbolTable

Signed-off-by: Nicolás Pazos <npazosmendez@gmail.com>
This commit is contained in:
Nicolás Pazos 2023-11-29 13:07:58 -03:00
parent ec9300fc1a
commit 25c8baeb6c

View file

@ -15,13 +15,11 @@ package remote
import ( import (
"context" "context"
"encoding/binary"
"errors" "errors"
"math" "math"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"unsafe"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/go-kit/log/level" "github.com/go-kit/log/level"
@ -1746,83 +1744,36 @@ func buildWriteRequest(samples []prompb.TimeSeries, metadata []prompb.MetricMeta
return compressed, highest, nil return compressed, highest, nil
} }
type offLenPair struct {
Off uint32
Len uint32
}
type rwSymbolTable struct { type rwSymbolTable struct {
symbols []byte strings []string
strings []string symbolsMap map[string]uint32
symbolsMap map[string]offLenPair
symbolsMapBytes map[string]uint32
} }
func newRwSymbolTable() rwSymbolTable { func newRwSymbolTable() rwSymbolTable {
return rwSymbolTable{ return rwSymbolTable{
symbolsMap: make(map[string]offLenPair), symbolsMap: make(map[string]uint32),
symbolsMapBytes: make(map[string]uint32),
} }
} }
func (r *rwSymbolTable) Ref(str string) (uint32, uint32) {
if offlen, ok := r.symbolsMap[str]; ok {
return offlen.Off, offlen.Len
}
off, length := uint32(len(r.symbols)), uint32(len(str))
if int(off) > len(r.symbols) {
panic(1)
}
r.symbols = append(r.symbols, str...)
if len(r.symbols) < int(off+length) {
panic(2)
}
r.symbolsMap[str] = offLenPair{off, length}
return off, length
}
func (r *rwSymbolTable) RefLen(str string) uint32 {
if ref, ok := r.symbolsMapBytes[str]; ok {
return ref
}
ref := uint32(len(r.symbols))
r.symbols = binary.AppendUvarint(r.symbols, uint64(len(str)))
r.symbols = append(r.symbols, str...)
r.symbolsMapBytes[str] = ref
return ref
}
func (r *rwSymbolTable) RefStr(str string) uint32 { func (r *rwSymbolTable) RefStr(str string) uint32 {
if ref, ok := r.symbolsMapBytes[str]; ok { if ref, ok := r.symbolsMap[str]; ok {
return ref return ref
} }
ref := uint32(len(r.strings)) ref := uint32(len(r.strings))
r.strings = append(r.strings, str) r.strings = append(r.strings, str)
r.symbolsMapBytes[str] = ref r.symbolsMap[str] = ref
return ref return ref
} }
func (r *rwSymbolTable) LabelsString() string {
return *((*string)(unsafe.Pointer(&r.symbols)))
}
func (r *rwSymbolTable) LabelsStrings() []string { func (r *rwSymbolTable) LabelsStrings() []string {
return r.strings return r.strings
} }
func (r *rwSymbolTable) LabelsData() []byte {
return r.symbols
}
func (r *rwSymbolTable) clear() { func (r *rwSymbolTable) clear() {
r.strings = r.strings[:0] r.strings = r.strings[:0]
for k := range r.symbolsMap { for k := range r.symbolsMap {
delete(r.symbolsMap, k) delete(r.symbolsMap, k)
} }
for k := range r.symbolsMapBytes {
delete(r.symbolsMapBytes, k)
}
r.symbols = r.symbols[:0]
} }
func buildMinimizedWriteRequestStr(samples []prompb.MinimizedTimeSeriesStr, labels []string, pBuf *[]byte, buf *[]byte) ([]byte, int64, error) { func buildMinimizedWriteRequestStr(samples []prompb.MinimizedTimeSeriesStr, labels []string, pBuf *[]byte, buf *[]byte) ([]byte, int64, error) {