stream_test.go
stream_test.go - Overview
This file contains tests for the streaming functionality of the BadgerDB. It tests the ability to stream data with various filters and configurations, including prefix filtering, key selection, and custom key-to-list conversion.
Detailed Documentation
keyWithPrefix
Function
- Purpose: Constructs a key by combining a prefix string and an integer.
- Parameters:
prefix
(string): The prefix to be added to the key.k
(int): The integer to be included in the key.
- Returns:
[]byte
: A byte slice representing the constructed key.
keyToInt
Function
- Purpose: Extracts the prefix and integer value from a key string.
- Parameters:
k
([]byte): The key to be parsed.
- Returns:
string
: The prefix extracted from the key.int
: The integer value extracted from the key.
value
Function
- Purpose: Creates a value byte slice from an integer.
- Parameters:
k
(int): The integer to be converted to a value.
- Returns:
[]byte
: A byte slice representing the value.
collector
struct
- Purpose: A helper struct to collect
bpb.KV
(key-value) pairs received during streaming. - Fields:
kv
([]*bpb.KV): A slice to store the collected key-value pairs.
collector.Send
Function
- Purpose: Receives a buffer of key-value pairs, converts it to a
bpb.KVList
, and appends the key-value pairs to the collector'skv
slice. - Parameters:
buf
(*z.Buffer): The buffer containing the key-value pairs.
- Returns:
error
: An error if any occurred during the process. Returnsnil
ifStreamDone
is true in any of thekv
pairs.
TestStream
Function
- Purpose: Tests the basic streaming functionality with different prefix filters and key selection criteria.
- Parameters:
t
(*testing.T): The testing object.
- Returns: None
TestStreamMaxSize
Function
- Purpose: Tests the stream functionality with a specified maximum size. This test is intended for manual execution.
- Parameters:
t
(*testing.T): The testing object.
- Returns: None
TestStreamWithThreadId
Function
- Purpose: Tests the stream functionality with thread ID verification within the
KeyToList
function. - Parameters:
t
(*testing.T): The testing object.
- Returns: None
TestBigStream
Function
- Purpose: Tests the stream functionality with a large dataset. This test is intended for manual execution.
- Parameters:
t
(*testing.T): The testing object.
- Returns: None
TestStreamCustomKeyToList
Function
- Purpose: Tests the stream functionality with a custom
KeyToList
function to verify correct memory management. - Parameters:
t
(*testing.T): The testing object.
- Returns: None
Code Examples
keyWithPrefix
Example
prefix := "test"
key := keyWithPrefix(prefix, 123)
// key will be "test-123" as a byte array
collector.Send
Example
c := &collector{}
buffer := &z.Buffer{} // Assuming buffer is populated with KV data.
err := c.Send(buffer)
if err != nil {
// Handle error
}
// c.kv now contains the key-value pairs from the buffer.