From b6a9b4b04c2416bf33b7cd9ac5cb528e2ef78a9b Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 29 Apr 2024 03:49:09 +0200 Subject: [PATCH] wip --- lib/logstorage/bitmap.go | 144 ++++++++++++++++++++++++++++++++++++++ lib/logstorage/filters.go | 137 ------------------------------------ 2 files changed, 144 insertions(+), 137 deletions(-) create mode 100644 lib/logstorage/bitmap.go diff --git a/lib/logstorage/bitmap.go b/lib/logstorage/bitmap.go new file mode 100644 index 000000000..51bd77789 --- /dev/null +++ b/lib/logstorage/bitmap.go @@ -0,0 +1,144 @@ +package logstorage + +import ( + "sync" + + "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" +) + +func getBitmap(bitsLen int) *bitmap { + v := bitmapPool.Get() + if v == nil { + v = &bitmap{} + } + bm := v.(*bitmap) + bm.init(bitsLen) + return bm +} + +func putBitmap(bm *bitmap) { + bm.reset() + bitmapPool.Put(bm) +} + +var bitmapPool sync.Pool + +type bitmap struct { + a []uint64 + bitsLen int +} + +func (bm *bitmap) reset() { + bm.resetBits() + bm.a = bm.a[:0] + + bm.bitsLen = 0 +} + +func (bm *bitmap) copyFrom(src *bitmap) { + bm.reset() + + bm.a = append(bm.a[:0], src.a...) + bm.bitsLen = src.bitsLen +} + +func (bm *bitmap) init(bitsLen int) { + a := bm.a + wordsLen := (bitsLen + 63) / 64 + if n := wordsLen - cap(a); n > 0 { + a = append(a[:cap(a)], make([]uint64, n)...) + } + a = a[:wordsLen] + bm.a = a + bm.bitsLen = bitsLen +} + +func (bm *bitmap) resetBits() { + a := bm.a + for i := range a { + a[i] = 0 + } +} + +func (bm *bitmap) setBits() { + a := bm.a + for i := range a { + a[i] = ^uint64(0) + } + tailBits := bm.bitsLen % 64 + if tailBits > 0 && len(a) > 0 { + // Zero bits outside bitsLen at the last word + a[len(a)-1] &= (uint64(1) << tailBits) - 1 + } +} + +func (bm *bitmap) isZero() bool { + for _, word := range bm.a { + if word != 0 { + return false + } + } + return true +} + +func (bm *bitmap) areAllBitsSet() bool { + a := bm.a + for i, word := range a { + if word != (1<<64)-1 { + if i+1 < len(a) { + return false + } + tailBits := bm.bitsLen % 64 + if tailBits == 0 || word != (uint64(1)<= bitsLen { + break + } + if !f(idx) { + a[i] &= ^mask + } + } + } +} diff --git a/lib/logstorage/filters.go b/lib/logstorage/filters.go index 2543c38b0..d3cce634d 100644 --- a/lib/logstorage/filters.go +++ b/lib/logstorage/filters.go @@ -16,143 +16,6 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/stringsutil" ) -func getBitmap(bitsLen int) *bitmap { - v := bitmapPool.Get() - if v == nil { - v = &bitmap{} - } - bm := v.(*bitmap) - bm.init(bitsLen) - return bm -} - -func putBitmap(bm *bitmap) { - bm.reset() - bitmapPool.Put(bm) -} - -var bitmapPool sync.Pool - -type bitmap struct { - a []uint64 - bitsLen int -} - -func (bm *bitmap) reset() { - bm.resetBits() - bm.a = bm.a[:0] - - bm.bitsLen = 0 -} - -func (bm *bitmap) copyFrom(src *bitmap) { - bm.reset() - - bm.a = append(bm.a[:0], src.a...) - bm.bitsLen = src.bitsLen -} - -func (bm *bitmap) init(bitsLen int) { - a := bm.a - wordsLen := (bitsLen + 63) / 64 - if n := wordsLen - cap(a); n > 0 { - a = append(a[:cap(a)], make([]uint64, n)...) - } - a = a[:wordsLen] - bm.a = a - bm.bitsLen = bitsLen -} - -func (bm *bitmap) resetBits() { - a := bm.a - for i := range a { - a[i] = 0 - } -} - -func (bm *bitmap) setBits() { - a := bm.a - for i := range a { - a[i] = ^uint64(0) - } - tailBits := bm.bitsLen % 64 - if tailBits > 0 && len(a) > 0 { - // Zero bits outside bitsLen at the last word - a[len(a)-1] &= (uint64(1) << tailBits) - 1 - } -} - -func (bm *bitmap) isZero() bool { - for _, word := range bm.a { - if word != 0 { - return false - } - } - return true -} - -func (bm *bitmap) areAllBitsSet() bool { - a := bm.a - for i, word := range a { - if word != (1<<64)-1 { - if i+1 < len(a) { - return false - } - tailBits := bm.bitsLen % 64 - if tailBits == 0 || word != (uint64(1)<= bitsLen { - break - } - if !f(idx) { - a[i] &= ^mask - } - } - } -} - type filter interface { // String returns string representation of the filter String() string