mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 02:44:59 +00:00
We centralize the creation paths for test indexes, fields, etcetera so they all have a common path, all using standard test holders. There's still two versions, one for test.* functions and one for internal. They do share a TestHolderConfig though. Large hunks of the related APIs are simplified/streamlined. * Fragments are always created with a Field and don't need a workaround in case they don't have it. * Creation of test fragments, etc., use optional FieldOptions but don't specify names because they're all using new holders for each thing created anyway. This dramatically reduces the complexity of the calls. * test fragments are created inside test views which are created inside test fields, etcetera, so everything is using the same logic; test views aren't bypassing the other layers, they're creating themselves normally within a field. * Quite a few things now use the standard runtime/production logic instead of being custom workarounds; for instance, instead of `mustOpenMutexFragment` creating a fragment and then creating a mutex vector for it, we just create a mutex-typed field and have the normal runtime code do this. * Similarly, we now use the same field creation logic that production does, instead of having our own test-only thing that validates field names directly, so our test that we're validating field names is actually testing the runtime code. Yay. * fragSpec goes away. it was a replacement for fragProxy which existed to solve memory allocation problems but replaced them with interface overhead problems. Now we just have pointers to things and maintain valid data structures. * Many panics are now Fatal or Fatalf calls. * Some specific bugs fixed, like a cluster which was requested and then had its first node directly overwritten, which isn't valid with shared clusters. * Drop the temp-dir test flag and TempDir variable, we can just use $TMPDIR. * Drop a benchmark of "write file to disk" that was purely a benchmark of file write speed, not a benchmark of rendering the data that needs to be written. * Drop the unused "flags" parameter to fragment creation, which was only used back when we changed the BSI format. * Use holder.Txf() rather than index.Txf(). The TxFactory has to be holder-level anyway, referring to it via the index is misleading. * Test holders automatically close themselves and delete themselves, we remove various other things that thought they were responsible for deleting themselves.
133 lines
4 KiB
Go
133 lines
4 KiB
Go
// Copyright 2021 Molecula Corp. All rights reserved.
|
|
package pilosa
|
|
|
|
import (
|
|
"bytes"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/molecula/featurebase/v3/roaring"
|
|
)
|
|
|
|
const countRangeMaxN = 8192
|
|
|
|
var countRangeSampleData []byte
|
|
var prepareCountRangeSampleData sync.Once
|
|
|
|
// The sample data for the counter is just a series of containers,
|
|
// each with cardinality equal to its container key.
|
|
func requireCountRangeSampleData(tb testing.TB) (*fragment, Tx) {
|
|
prepareCountRangeSampleData.Do(func() {
|
|
var arraySample [4096]uint16
|
|
// This horrible hack relies on a quirk of roaring's internals: It'll
|
|
// copy the bitmap if its length isn't exactly 1024. This lets us
|
|
// request that each container get its own copy of the bitmap.
|
|
var bitmapSample [1025]uint64
|
|
for i := range arraySample {
|
|
arraySample[i] = uint16(i * 2)
|
|
}
|
|
// Put corresponding bits in the bitmap...
|
|
for i := 0; i < 4096/32; i++ {
|
|
// bit 0 is 0x1, bit 2 is 0x4, so even-numbered bits
|
|
// are 0x5555....
|
|
bitmapSample[i] = 0x5555555555555555
|
|
}
|
|
bm := roaring.NewSliceBitmap()
|
|
for n := 0; n < 4096 && n < countRangeMaxN; n++ {
|
|
c := roaring.NewContainerArray(arraySample[:n])
|
|
bm.Put(uint64(n), c)
|
|
}
|
|
// Start filling in the missing bits. This starts us out with
|
|
// bitmap containers, but then eventually converts to things
|
|
// that are more likely to be run containers. At the end of this,
|
|
// we should have exactly the first 8,192 bits set, for a single
|
|
// run of 8k.
|
|
for n := 4096; n < 8192; n++ {
|
|
c := roaring.NewContainerBitmapN(bitmapSample[:], int32(n))
|
|
bm.Put(uint64(n), c)
|
|
w := n - 4096
|
|
bitmapSample[w/32] |= 1 << (((n % 32) * 2) + 1)
|
|
}
|
|
var asBytes bytes.Buffer
|
|
n, err := bm.WriteTo(&asBytes)
|
|
if err != nil {
|
|
tb.Fatalf("writing bitmap: %v", err)
|
|
}
|
|
countRangeSampleData = asBytes.Bytes()
|
|
tb.Logf("creating bitmap: %d containers, %d bytes of data", countRangeMaxN, n)
|
|
})
|
|
f, idx, tx := mustOpenFragment(tb)
|
|
// Properly close this transaction, but not the next one we create that the
|
|
// caller will be responsible for. The deferred callback will
|
|
// be a nop if the Commit happened.
|
|
defer tx.Rollback()
|
|
err := f.importRoaringT(tx, countRangeSampleData, false)
|
|
if err != nil {
|
|
tb.Fatalf("importing sample data: %v", err)
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
tb.Fatalf("committing sample data: %v", err)
|
|
}
|
|
tx = idx.holder.txf.NewTx(Txo{Write: false, Index: idx, Fragment: f, Shard: 0})
|
|
return f, tx
|
|
}
|
|
|
|
func TestTx_CountRange(t *testing.T) {
|
|
f, tx := requireCountRangeSampleData(t)
|
|
defer f.Clean(t)
|
|
defer tx.Rollback()
|
|
// CountRange accesses the fragment without locking. Normally we only
|
|
// call it from inside a fragment routine with locking. Otherwise, you
|
|
// can have a race condition with snapshots, for instance.
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
|
|
expected := uint64(0)
|
|
j := uint64(0)
|
|
for i := uint64(0); i < countRangeMaxN; i += 7 {
|
|
expected += i
|
|
if i%4 == 3 {
|
|
expected -= (j * 7) + 21
|
|
j += 7
|
|
}
|
|
// Every other bit gets set, for a total of i bits in container
|
|
// i, so they're all in the first (i*2) bits of the container.
|
|
got, err := tx.CountRange("i", "f", "v", 0, uint64(j)<<16, (uint64(i)<<16)+(i*2))
|
|
if err != nil {
|
|
t.Fatalf("counting range: %v", err)
|
|
}
|
|
if got != expected {
|
|
t.Fatalf("counting from container %d to %d, expected %d, got %d",
|
|
j, i, expected, got)
|
|
}
|
|
// The -i here undoes the +i at the top of this loop.
|
|
expected += (i * 7) + 21 - i
|
|
}
|
|
}
|
|
|
|
func BenchmarkTx_CountRange(b *testing.B) {
|
|
f, tx := requireCountRangeSampleData(b)
|
|
defer f.Clean(b)
|
|
defer tx.Rollback()
|
|
|
|
for k := 0; k < b.N; k++ {
|
|
expected := uint64(0)
|
|
j := uint64(0)
|
|
for i := uint64(0); i < countRangeMaxN; i += 7 {
|
|
if i%4 == 3 {
|
|
expected -= (j * 7) + 21
|
|
j += 7
|
|
}
|
|
got, err := tx.CountRange("i", "f", "v", 0, uint64(j)<<16, uint64(i)<<16)
|
|
if err != nil {
|
|
b.Fatalf("counting range: %v", err)
|
|
}
|
|
if got != expected {
|
|
b.Fatalf("counting from container %d to %d, expected %d, got %d",
|
|
j, i, expected, got)
|
|
}
|
|
expected += (i * 7) + 21
|
|
}
|
|
}
|
|
}
|