Skip to content

SparseIndex

class SparseIndex

@brief Timestamp to file offset mapping for fast seeking

Maintains a sparse index of (timestamp, file_offset) pairs to enable efficient random-access seeking in large trace files without loading all data into memory.

Index Strategy: - Stores every N-th timestamp (e.g., every 10,000th) - Uses binary search for O(log n) lookups - Memory: ~1 MB for 1 billion timestamps (with sampling factor ~10,000)

Use Case: When processing a streaming chunk, you encounter timestamps. Record key timestamps in the index. Later, when jumping to a specific timestamp, use binary_search() to find the appropriate file offset and resume from there.

Example:

SparseIndex index;

// While streaming, periodically record positions:
for (auto& chunk : streaming_reader) {
    // ... parse chunk ...
    if (timestamp % 10000 == 0) {
        index.add_entry(timestamp, file_offset);
    }
}

// Later, jump to timestamp 5,000,000:
auto [target_offset, lower_timestamp] = index.binary_search(5000000);
// Seek to file offset, then parse until exact timestamp

Thread Safety: NOT thread-safe. Synchronize externally if accessed from multiple threads.

See
StreamingChunkReader for reading chunks
See
LazyTraceQuery for using index in queries

Variables

Name Description
entries (timestamp -> file_offset) mapping, sorted by timestamp
sampling_factor Sampling factor: store every N-th entry encountered Configurable for memory/performance tradeoffs
entry_count Counter for sampling - incremented for each add_entry() call

Functions

Name Description
SparseIndex @brief Construct index with optional sampling sampling_factor : Store every N-th entry (default: 10,000) Use 1 for complete index (more memory) Use larger values for sparser index (less memory)
add_entry @brief Add a timestamp -> file_offset mapping Sampling is applied: entry is only added if entry_count % sampling_factor == 0. Call this for each timestamp encountered during streaming. Typical Usage in Streaming Reader: while (reader) { chunk = reader.get_chunk(); for (auto timestamp : parse_timestamps(chunk)) { index.add_entry(timestamp, current_file_offset); } reader.next(); } timestamp : Simulation timestamp file_offset : Byte offset in trace file where this timestamp begins
binary_search @brief Binary search for file offset near target timestamp Finds the largest timestamp <= target_timestamp and returns its offset.
get_entries @brief Get all index entries Used for debugging, statistics, or rebuilding indices.
entry_count_stored @brief Get number of stored entries Due to sampling, this is typically much smaller than the total number of timestamps in the file.
entry_count_total @brief Get total number of add_entry() calls This is the total number of timestamps encountered, not the number stored (due to sampling).
get_sampling_factor @brief Get configured sampling factor Return : Sampling factor (every N-th entry is stored)
empty @brief Check if index is empty Return : true if no entries have been added
clear @brief Clear all entries Resets index to empty state.
estimated_memory_bytes @brief Get memory usage estimate in bytes Rough estimate of memory used by this index.
get_stats @brief Get summary statistics Return : String with index statistics for debugging

Variable Details

entries

std::map<int64_t, int64_t> entries

(timestamp -> file_offset) mapping, sorted by timestamp

entry_count

size_t entry_count

Counter for sampling - incremented for each add_entry() call

sampling_factor

size_t sampling_factor

Sampling factor: store every N-th entry encountered Configurable for memory/performance tradeoffs

Function Details

SparseIndex

explicit SparseIndex(size_t sampling_factor = 10000)

@brief Construct index with optional sampling

sampling_factor
Store every N-th entry (default: 10,000) Use 1 for complete index (more memory) Use larger values for sparser index (less memory)

add_entry

void add_entry(int64_t timestamp, int64_t file_offset)

@brief Add a timestamp -> file_offset mapping

Sampling is applied: entry is only added if entry_count % sampling_factor == 0. Call this for each timestamp encountered during streaming.

Typical Usage in Streaming Reader:

1
2
3
4
5
6
7
while (reader) {
    chunk = reader.get_chunk();
    for (auto timestamp : parse_timestamps(chunk)) {
        index.add_entry(timestamp, current_file_offset);
    }
    reader.next();
}
timestamp
Simulation timestamp
file_offset
Byte offset in trace file where this timestamp begins

std::pair<int64_t, int64_t> binary_search(int64_t target_timestamp) const

@brief Binary search for file offset near target timestamp

Finds the largest timestamp <= target_timestamp and returns its offset. Useful for seeking to a specific time in the trace file.

Example:

1
2
3
4
auto [offset, ts] = index.binary_search(5000000);
// offset = file position to start reading from
// ts = actual timestamp at that position
// Now seek to offset and parse forward to find exact timestamp

Return Semantics: - If target_timestamp is before all entries: returns {0, -1} - Otherwise: returns {offset_of_matching_timestamp, matching_timestamp}

Complexity: O(log m) where m = number of index entries (typically much smaller than actual number of timestamps due to sampling)

target_timestamp
The timestamp to search for
Return
Pair of (file_offset, found_timestamp) file_offset: Where to seek in the file found_timestamp: Actual timestamp at that position (-1 if not found)

clear

void clear()

@brief Clear all entries

Resets index to empty state. Note: entry_count_total is NOT reset.

empty

bool empty() const

@brief Check if index is empty

Return
true if no entries have been added

entry_count_stored

size_t entry_count_stored() const

@brief Get number of stored entries

Due to sampling, this is typically much smaller than the total number of timestamps in the file.

Return
Number of index entries currently stored

entry_count_total

size_t entry_count_total() const

@brief Get total number of add_entry() calls

This is the total number of timestamps encountered, not the number stored (due to sampling).

Return
Total entries processed

estimated_memory_bytes

size_t estimated_memory_bytes() const

@brief Get memory usage estimate in bytes

Rough estimate of memory used by this index. Each entry is approximately 16 bytes (two 8-byte integers).

Return
Approximate memory usage in bytes

get_entries

const std::map<int64_t, int64_t>& get_entries() const

@brief Get all index entries

Used for debugging, statistics, or rebuilding indices.

Return
Map of (timestamp -> file_offset) pairs

get_sampling_factor

size_t get_sampling_factor() const

@brief Get configured sampling factor

Return
Sampling factor (every N-th entry is stored)

get_stats

std::string get_stats() const

@brief Get summary statistics

Return
String with index statistics for debugging