Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compact: wire roaring bitmaps to compactor
Wire roaring bitmaps to the compactor. Tests still not passing. Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
- Loading branch information
Showing
17 changed files
with
350 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package postings | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/RoaringBitmap/roaring" | ||
"github.com/prometheus/prometheus/storage" | ||
"github.com/prometheus/prometheus/tsdb/encoding" | ||
"github.com/prometheus/prometheus/tsdb/index" | ||
) | ||
|
||
const ( | ||
RoaringEncoder = "roaringbitmap" | ||
RawEncoder = "raw" | ||
) | ||
|
||
var _ index.PostingsEncoder = EncodePostingsRoaring | ||
|
||
func EncodePostingsRoaring(e *encoding.Encbuf, in []uint32) error { | ||
bm := roaring.NewBitmap() | ||
|
||
bm.AddMany(in) | ||
bm.RunOptimize() | ||
|
||
out, err := bm.MarshalBinary() | ||
if err != nil { | ||
return fmt.Errorf("marshaling roaring bitmap: %w", err) | ||
} | ||
|
||
e.PutBytes(out) | ||
return nil | ||
} | ||
|
||
var _ index.PostingsDecoder = DecodePostingsRoaring | ||
|
||
func DecodePostingsRoaring(b []byte) (int, index.Postings, error) { | ||
rb := roaring.New() | ||
|
||
_, err := rb.ReadFrom(bytes.NewReader(b)) | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
|
||
rbIter := rb.Iterator() | ||
|
||
return int(rb.GetCardinality()), &postingsRoaringWrapper{IntPeekable: rbIter}, nil | ||
} | ||
|
||
type postingsRoaringWrapper struct { | ||
roaring.IntPeekable | ||
|
||
cur storage.SeriesRef | ||
} | ||
|
||
func (w *postingsRoaringWrapper) At() storage.SeriesRef { | ||
return w.cur | ||
} | ||
|
||
func (w *postingsRoaringWrapper) Next() bool { | ||
if !w.IntPeekable.HasNext() { | ||
return false | ||
} | ||
|
||
w.cur = storage.SeriesRef(w.IntPeekable.Next()) | ||
|
||
return true | ||
} | ||
|
||
func (w *postingsRoaringWrapper) Err() error { | ||
return nil | ||
} | ||
|
||
func (w *postingsRoaringWrapper) Seek(v storage.SeriesRef) bool { | ||
w.IntPeekable.AdvanceIfNeeded(uint32(v)) | ||
|
||
return w.IntPeekable.HasNext() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package postings | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/tsdb/encoding" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRoaringEncodeDecode(t *testing.T) { | ||
e := &encoding.Encbuf{} | ||
|
||
require.NoError(t, EncodePostingsRoaring(e, []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) | ||
|
||
n, p, err := DecodePostingsRoaring(e.Get()) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, 10, n) | ||
require.Equal(t, true, p.Next()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.