table_test.go
table_test.go - Overview
This file contains tests for the table
package, focusing on the creation, iteration, seeking, and merging of tables, as well as performance benchmarks.
Detailed Documentation
key
func key(prefix string, i int) string
Purpose: Constructs a key by combining a prefix string with a formatted integer. Parameters:
prefix
(string): The prefix for the key.i
(int): The integer to be formatted and appended to the prefix.
Returns:
string
: A string representing the combined key.
getTestTableOptions
func getTestTableOptions() Options
Purpose: Returns a set of default options for creating test tables.
Returns:
Options
: A struct containing the default options, including compression type, level, block size and bloom filter false positive rate.
buildTestTable
func buildTestTable(t *testing.T, prefix string, n int, opts Options) *Table
Purpose: Builds a test table with a specified number of key-value pairs, using a given prefix for keys and sequential integers as values.
Parameters:
t
(*testing.T): Testing object for assertions.prefix
(string): The prefix for the keys.n
(int): The number of key-value pairs to generate. Should be less than or equal to 10000.opts
(Options): Options to configure the table.
Returns:
*Table
: A pointer to the createdTable
object.
buildTable
func buildTable(t *testing.T, keyValues [][]string, opts Options) *Table
Purpose: Builds a table from a slice of key-value string pairs.
Parameters:
t
(*testing.T): Testing object for assertions.keyValues
([][]string): A slice of string slices, where each inner slice contains a key and a value.opts
(Options): Options to configure the table.
Returns:
*Table
: A pointer to the createdTable
object.
TestTableIterator
func TestTableIterator(t *testing.T)
Purpose: Tests the table iterator functionality.
TestSeekToFirst
func TestSeekToFirst(t *testing.T)
Purpose: Tests the seekToFirst
method of the table iterator.
TestSeekToLast
func TestSeekToLast(t *testing.T)
Purpose: Tests the seekToLast
method of the table iterator.
TestSeek
func TestSeek(t *testing.T)
Purpose: Tests the seek
method of the table iterator.
TestSeekForPrev
func TestSeekForPrev(t *testing.T)
Purpose: Tests the seekForPrev
method of the table iterator.
TestIterateFromStart
func TestIterateFromStart(t *testing.T)
Purpose: Tests iteration from the start of the table.
TestIterateFromEnd
func TestIterateFromEnd(t *testing.T)
Purpose: Tests iteration from the end of the table.
TestTable
func TestTable(t *testing.T)
Purpose: Tests basic table operations like seeking and iteration.
TestIterateBackAndForth
func TestIterateBackAndForth(t *testing.T)
Purpose: Tests iterating back and forth within a table.
TestUniIterator
func TestUniIterator(t *testing.T)
Purpose: Tests the unidirectional iterator in both forward and reversed directions.
TestConcatIteratorOneTable
func TestConcatIteratorOneTable(t *testing.T)
Purpose: Tests the ConcatIterator
with a single table.
TestConcatIterator
func TestConcatIterator(t *testing.T)
Purpose: Tests the ConcatIterator
with multiple tables.
TestMergingIterator
func TestMergingIterator(t *testing.T)
Purpose: Tests the merging iterator functionality.
TestMergingIteratorReversed
func TestMergingIteratorReversed(t *testing.T)
Purpose: Tests the merging iterator in reversed order.
TestMergingIteratorTakeOne
func TestMergingIteratorTakeOne(t *testing.T)
Purpose: Tests a merging iterator that iterates over two tables but primarily takes values from the first.
TestMergingIteratorTakeTwo
func TestMergingIteratorTakeTwo(t *testing.T)
Purpose: Tests a merging iterator that iterates over two tables but primarily takes values from the second.
TestTableBigValues
func TestTableBigValues(t *testing.T)
Purpose: Tests table operations with large values.
TestTableChecksum
func TestTableChecksum(t *testing.T)
Purpose: Tests checksum verification during table opening by intentionally corrupting the table file.
BenchmarkRead
func BenchmarkRead(b *testing.B)
Purpose: Benchmarks the read performance of iterating over an entire table.
BenchmarkReadAndBuild
func BenchmarkReadAndBuild(b *testing.B)
Purpose: Benchmarks the read and build performance of a table.
BenchmarkReadMerged
func BenchmarkReadMerged(b *testing.B)
Purpose: Benchmarks the read performance of merged iterators over multiple tables.
BenchmarkChecksum
func BenchmarkChecksum(b *testing.B)
Purpose: Benchmarks the performance of different checksum algorithms (CRC32, xxHash64, SHA256) on varying key sizes.
BenchmarkRandomRead
func BenchmarkRandomRead(b *testing.B)
Purpose: Benchmarks the performance of random reads from a table.
getTableForBenchmarks
func getTableForBenchmarks(b *testing.B, count int, cache *ristretto.Cache[[]byte, *Block]) *Table
Purpose: Helper function to create a table for benchmarking purposes.
Parameters:
- b (*testing.B): Benchmarking object.
- count (int): Number of key-value pairs to insert into the table.
- cache (*ristretto.Cache[[]byte, *Block]): Ristretto cache for blocks. If nil, a new cache is created.
Returns:
- *Table: The created table.
TestMain
func TestMain(m *testing.M)
Purpose: Initializes the random seed for tests and runs all tests.
TestDoesNotHaveRace
func TestDoesNotHaveRace(t *testing.T)
Purpose: Tests for race conditions when calling the DoesNotHave
method on a table concurrently.
TestMaxVersion
func TestMaxVersion(t *testing.T)
Purpose: Tests the MaxVersion
method of the Table
object.
Code Examples
Example of building a test table:
opts := getTestTableOptions()
table := buildTestTable(t, "key", 100, opts)
defer func() { require.NoError(t, table.DecrRef()) }()
Example of iterating over a table:
it := table.NewIterator(0)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
// Process key-value pairs
k := it.Key()
v := it.Value()
fmt.Printf("Key: %s, Value: %s\n", string(y.ParseKey(k)), string(v.Value))
}