Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::ZSTDScanState Struct Reference
Inheritance diagram for duckdb::ZSTDScanState:
Collaboration diagram for duckdb::ZSTDScanState:

Public Member Functions

 ZSTDScanState (ColumnSegment &segment)
idx_t GetVectorIndex (idx_t start_index, idx_t &offset)
ZSTDVectorScanMetadata GetVectorMetadata (idx_t vector_idx)
shared_ptr< BlockHandle > LoadPage (block_id_t block_id)
bool UseVectorStateCache (idx_t vector_idx, idx_t internal_offset)
ZSTDVectorScanState & LoadVector (idx_t vector_idx, idx_t internal_offset)
void LoadNextPageForVector (ZSTDVectorScanState &scan_state)
void DecompressString (ZSTDVectorScanState &scan_state, data_ptr_t destination, idx_t uncompressed_length)
void Skip (ZSTDVectorScanState &scan_state, idx_t count)
void ScanInternal (ZSTDVectorScanState &scan_state, idx_t count, Vector &result, idx_t result_offset)
void ScanPartial (idx_t start_idx, Vector &result, idx_t offset, idx_t count)
Public Member Functions inherited from duckdb::SegmentScanState
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Public Attributes

UncompressedStringSegmentState & state
BlockManager & block_manager
BufferManager & buffer_manager
duckdb_zstd::ZSTD_DCtx * decompression_context = nullptr
idx_t segment_block_offset
BufferHandle segment_handle
page_id_t * page_ids
page_offset_t * page_offsets
uncompressed_size_t * uncompressed_sizes
compressed_size_t * compressed_sizes
unique_ptr< ZSTDVectorScanState > current_vector
 Cache of (the scan state of) the current vector being read.
idx_t segment_count
 The amount of tuples stored in the segment.
idx_t scanned_count = 0
 The amount of tuples consumed.
ColumnSegment & segment
AllocatedData skip_buffer
 Buffer for skipping data.

Constructor & Destructor Documentation

◆ ZSTDScanState()

duckdb::ZSTDScanState::ZSTDScanState ( ColumnSegment & segment)
inlineexplicit
410917 : state(segment.GetSegmentState()->Cast<UncompressedStringSegmentState>()),
410918 block_manager(segment.block->GetBlockManager()), buffer_manager(BufferManager::GetBufferManager(segment.db)),
410919 segment_block_offset(segment.GetBlockOffset()), segment(segment) {
410920 decompression_context = duckdb_zstd::ZSTD_createDCtx();
410921 segment_handle = buffer_manager.Pin(segment.block);
410922
410923 auto data = segment_handle.Ptr() + segment.GetBlockOffset();
410924 idx_t offset = 0;
410925
410926 segment_count = segment.count.load();
410927 idx_t amount_of_vectors = (segment_count / ZSTD_VECTOR_SIZE) + ((segment_count % ZSTD_VECTOR_SIZE) != 0);
410928
410929 // Set pointers to the Vector Metadata
410930 offset = AlignValue<idx_t, sizeof(page_id_t)>(offset);
410931 page_ids = reinterpret_cast<page_id_t *>(data + offset);
410932 offset += (sizeof(page_id_t) * amount_of_vectors);
410933
410935 page_offsets = reinterpret_cast<page_offset_t *>(data + offset);
410936 offset += (sizeof(page_offset_t) * amount_of_vectors);
410937
410939 uncompressed_sizes = reinterpret_cast<uncompressed_size_t *>(data + offset);
410940 offset += (sizeof(uncompressed_size_t) * amount_of_vectors);
410941
410943 compressed_sizes = reinterpret_cast<compressed_size_t *>(data + offset);
410944 offset += (sizeof(compressed_size_t) * amount_of_vectors);
410945
410946 scanned_count = 0;
410947 }
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static T AlignValue(T n)
Align value (ceiling) (not for pointer types).
Definition duckdb.hpp:2126
idx_t segment_count
The amount of tuples stored in the segment.
Definition duckdb.cpp:411198
idx_t scanned_count
The amount of tuples consumed.
Definition duckdb.cpp:411200

◆ ~ZSTDScanState()

duckdb::ZSTDScanState::~ZSTDScanState ( )
inlineoverride
410948 {
410949 duckdb_zstd::ZSTD_freeDCtx(decompression_context);
410950 }

Member Function Documentation

◆ GetVectorIndex()

idx_t duckdb::ZSTDScanState::GetVectorIndex ( idx_t start_index,
idx_t & offset )
inline
410953 {
410954 idx_t vector_idx = start_index / ZSTD_VECTOR_SIZE;
410955 offset = start_index % ZSTD_VECTOR_SIZE;
410956 return vector_idx;
410957 }

◆ GetVectorMetadata()

ZSTDVectorScanMetadata duckdb::ZSTDScanState::GetVectorMetadata ( idx_t vector_idx)
inline
410959 {
410960 idx_t previous_value_count = vector_idx * ZSTD_VECTOR_SIZE;
410961 idx_t value_count = MinValue<idx_t>(segment_count - previous_value_count, ZSTD_VECTOR_SIZE);
410962
410963 return ZSTDVectorScanMetadata {/* vector_idx = */ vector_idx,
410964 /* block_id = */ page_ids[vector_idx],
410965 /* block_offset = */ page_offsets[vector_idx],
410966 /* uncompressed_size = */ uncompressed_sizes[vector_idx],
410967 /* compressed_size = */ compressed_sizes[vector_idx],
410968 /* count = */ value_count};
410969 }

◆ LoadPage()

shared_ptr< BlockHandle > duckdb::ZSTDScanState::LoadPage ( block_id_t block_id)
inline
410971 {
410972 return state.GetHandle(block_manager, block_id);
410973 }

◆ UseVectorStateCache()

bool duckdb::ZSTDScanState::UseVectorStateCache ( idx_t vector_idx,
idx_t internal_offset )
inline
410975 {
410976 if (!current_vector) {
410977 // No vector loaded yet
410978 return false;
410979 }
410980 if (current_vector->metadata.vector_idx != vector_idx) {
410981 // Not the same vector
410982 return false;
410983 }
410984 if (current_vector->scanned_count != internal_offset) {
410985 // Not the same scan offset
410986 return false;
410987 }
410988 return true;
410989 }
unique_ptr< ZSTDVectorScanState > current_vector
Cache of (the scan state of) the current vector being read.
Definition duckdb.cpp:411195

◆ LoadVector()

ZSTDVectorScanState & duckdb::ZSTDScanState::LoadVector ( idx_t vector_idx,
idx_t internal_offset )
inline

We know that the compressed size is too big to fit on the current page

410991 {
410992 if (UseVectorStateCache(vector_idx, internal_offset)) {
410993 return *current_vector;
410994 }
410995 current_vector = make_uniq<ZSTDVectorScanState>();
410996 current_vector->metadata = GetVectorMetadata(vector_idx);
410997 auto &metadata = current_vector->metadata;
410998 auto &scan_state = *current_vector;
410999 data_ptr_t handle_start;
411000 idx_t ptr_offset = 0;
411001 if (metadata.block_id == INVALID_BLOCK) {
411002 // Data lives on the segment's page
411003 handle_start = segment_handle.Ptr();
411004 ptr_offset += segment_block_offset;
411005 } else {
411006 // Data lives on an extra page, have to load the block first
411007 auto block = LoadPage(metadata.block_id);
411008 auto data_handle = buffer_manager.Pin(block);
411009 handle_start = data_handle.Ptr();
411010 scan_state.buffer_handles.push_back(std::move(data_handle));
411011 }
411012
411013 ptr_offset += metadata.block_offset;
411014 ptr_offset = AlignValue<idx_t, sizeof(string_length_t)>(ptr_offset);
411015 scan_state.current_buffer_ptr = handle_start + ptr_offset;
411016
411017 auto vector_size = metadata.count;
411018
411019 auto string_lengths_size = (sizeof(string_length_t) * vector_size);
411020 scan_state.string_lengths = reinterpret_cast<string_length_t *>(scan_state.current_buffer_ptr);
411021 scan_state.current_buffer_ptr += string_lengths_size;
411022
411023 // Update the in_buffer to point to the start of the compressed data frame
411024 idx_t current_offset = UnsafeNumericCast<idx_t>(scan_state.current_buffer_ptr - handle_start);
411025 scan_state.in_buffer.src = scan_state.current_buffer_ptr;
411026 scan_state.in_buffer.pos = 0;
411027 if (scan_state.metadata.block_offset + string_lengths_size + scan_state.metadata.compressed_size >
411028 (segment.SegmentSize() - sizeof(block_id_t))) {
411030 scan_state.in_buffer.size =
411031 MinValue(metadata.compressed_size, block_manager.GetBlockSize() - sizeof(block_id_t) - current_offset);
411032 } else {
411033 scan_state.in_buffer.size =
411034 MinValue(metadata.compressed_size, block_manager.GetBlockSize() - current_offset);
411035 }
411036
411037 // Initialize the context for streaming decompression
411038 duckdb_zstd::ZSTD_DCtx_reset(decompression_context, duckdb_zstd::ZSTD_reset_session_only);
411039 duckdb_zstd::ZSTD_DCtx_refDDict(decompression_context, nullptr);
411040
411041 if (internal_offset) {
411042 Skip(scan_state, internal_offset);
411043 }
411044 return scan_state;
411045 }
ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx *dctx, ZSTD_ResetDirective reset)
Definition duckdb.cpp:641241
ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx *dctx, const ZSTD_DDict *ddict)
Definition duckdb.cpp:641074
Here is the call graph for this function:

◆ LoadNextPageForVector()

void duckdb::ZSTDScanState::LoadNextPageForVector ( ZSTDVectorScanState & scan_state)
inline
411047 {
411048 if (scan_state.in_buffer.pos != scan_state.in_buffer.size) {
411049 throw InternalException(
411050 "(ZSTDScanState::LoadNextPageForVector) Trying to load the next page before consuming the current one");
411051 }
411052 // Read the next block id from the end of the page
411053 auto base_ptr =
411054 reinterpret_cast<data_ptr_t>(const_cast<void *>(scan_state.in_buffer.src)); // NOLINT: const cast
411055 auto next_id_ptr = base_ptr + scan_state.in_buffer.size;
411056 block_id_t next_id = Load<block_id_t>(next_id_ptr);
411057
411058 // Load the next page
411059 auto block = LoadPage(next_id);
411060 auto handle = buffer_manager.Pin(block);
411061 auto ptr = handle.Ptr();
411062 scan_state.buffer_handles.push_back(std::move(handle));
411063 scan_state.current_buffer_ptr = ptr;
411064
411065 // Update the in_buffer to point to the new page
411066 scan_state.in_buffer.src = ptr;
411067 scan_state.in_buffer.pos = 0;
411068
411069 idx_t page_size = segment.SegmentSize() - sizeof(block_id_t);
411070 idx_t remaining_compressed_data = scan_state.metadata.compressed_size - scan_state.compressed_scan_count;
411071 scan_state.in_buffer.size = MinValue<idx_t>(page_size, remaining_compressed_data);
411072 }

◆ DecompressString()

void duckdb::ZSTDScanState::DecompressString ( ZSTDVectorScanState & scan_state,
data_ptr_t destination,
idx_t uncompressed_length )
inline

Done decompressing the relevant portion

411074 {
411075 if (uncompressed_length == 0) {
411076 return;
411077 }
411078
411079 auto &in_buffer = scan_state.in_buffer;
411080 duckdb_zstd::ZSTD_outBuffer out_buffer;
411081
411082 out_buffer.dst = destination;
411083 out_buffer.pos = 0;
411084 out_buffer.size = uncompressed_length;
411085
411086 while (true) {
411087 idx_t old_pos = in_buffer.pos;
411089 /* zds = */ decompression_context,
411090 /* output =*/&out_buffer,
411091 /* input =*/&in_buffer);
411092 scan_state.compressed_scan_count += in_buffer.pos - old_pos;
411093 if (duckdb_zstd::ZSTD_isError(res)) {
411094 throw InvalidInputException("ZSTD Decompression failed: %s", duckdb_zstd::ZSTD_getErrorName(res));
411095 }
411096 if (out_buffer.pos == out_buffer.size) {
411098 break;
411099 }
411100 if (!res) {
411101 D_ASSERT(out_buffer.pos == out_buffer.size);
411102 D_ASSERT(in_buffer.pos == in_buffer.size);
411103 break;
411104 }
411105 D_ASSERT(in_buffer.pos == in_buffer.size);
411106 // Did not fully decompress, it needs a new page to read from
411107 LoadNextPageForVector(scan_state);
411108 }
411109 }
ZSTDLIB_API unsigned ZSTD_isError(size_t code)
Definition duckdb.cpp:614327
ZSTDLIB_API const char * ZSTD_getErrorName(size_t code)
Definition duckdb.cpp:614331
ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream *zds, ZSTD_outBuffer *output, ZSTD_inBuffer *input)
Definition duckdb.cpp:641380
void * dst
Definition duckdb.cpp:407813
size_t size
Definition duckdb.cpp:407814
size_t pos
Definition duckdb.cpp:407815
Here is the call graph for this function:

◆ Skip()

void duckdb::ZSTDScanState::Skip ( ZSTDVectorScanState & scan_state,
idx_t count )
inline
411111 {
411112 if (!skip_buffer) {
411113 skip_buffer = Allocator::DefaultAllocator().Allocate(duckdb_zstd::ZSTD_DStreamOutSize());
411114 }
411115
411116 D_ASSERT(scan_state.scanned_count + count <= scan_state.metadata.count);
411117
411118 // Figure out how much we need to skip
411119 string_length_t *string_lengths = &scan_state.string_lengths[scan_state.scanned_count];
411120 idx_t uncompressed_length = 0;
411121 for (idx_t i = 0; i < count; i++) {
411122 uncompressed_length += string_lengths[i];
411123 }
411124
411125 // Skip that many bytes by decompressing into the skip_buffer
411126 idx_t remaining = uncompressed_length;
411127 while (remaining) {
411128 idx_t to_scan = MinValue<idx_t>(skip_buffer.GetSize(), remaining);
411129 DecompressString(scan_state, skip_buffer.get(), to_scan);
411130 remaining -= to_scan;
411131 }
411132 scan_state.scanned_count += count;
411133 scanned_count += count;
411134 }
ZSTDLIB_API size_t ZSTD_DStreamOutSize(void)
Definition duckdb.cpp:640991
void DecompressString(ZSTDVectorScanState &scan_state, data_ptr_t destination, idx_t uncompressed_length)
Definition duckdb.cpp:411074
AllocatedData skip_buffer
Buffer for skipping data.
Definition duckdb.cpp:411204

◆ ScanInternal()

void duckdb::ZSTDScanState::ScanInternal ( ZSTDVectorScanState & scan_state,
idx_t count,
Vector & result,
idx_t result_offset )
inline
411136 {
411137 D_ASSERT(scan_state.scanned_count + count <= scan_state.metadata.count);
411138 D_ASSERT(result.GetType().InternalType() == PhysicalType::VARCHAR);
411139
411140 string_length_t *string_lengths = &scan_state.string_lengths[scan_state.scanned_count];
411141 idx_t uncompressed_length = 0;
411142 for (idx_t i = 0; i < count; i++) {
411143 uncompressed_length += string_lengths[i];
411144 }
411145 auto &buffer = StringVector::GetStringBuffer(result);
411146 auto uncompressed_data = buffer.AllocateShrinkableBuffer(uncompressed_length);
411147 auto string_data = FlatVector::GetData<string_t>(result);
411148
411149 DecompressString(scan_state, uncompressed_data, uncompressed_length);
411150
411151 idx_t offset = 0;
411152 for (idx_t i = 0; i < count; i++) {
411153 string_data[result_offset + i] = string_t(char_ptr_cast(uncompressed_data + offset), string_lengths[i]);
411154 offset += string_lengths[i];
411155 }
411156 scan_state.scanned_count += count;
411157 scanned_count += count;
411158 }
@ VARCHAR
DuckDB Extensions.
Definition duckdb.hpp:2847
static DUCKDB_API VectorStringBuffer & GetStringBuffer(Vector &vector)
Returns a reference to the underlying VectorStringBuffer - throws an error if vector is not of type V...
Definition duckdb.cpp:137905

◆ ScanPartial()

void duckdb::ZSTDScanState::ScanPartial ( idx_t start_idx,
Vector & result,
idx_t offset,
idx_t count )
inline
411160 {
411161 idx_t remaining = count;
411162 idx_t scanned = 0;
411163 while (remaining) {
411164 idx_t internal_offset;
411165 idx_t vector_idx = GetVectorIndex(start_idx + scanned, internal_offset);
411166 auto &scan_state = LoadVector(vector_idx, internal_offset);
411167 idx_t remaining_in_vector = scan_state.metadata.count - scan_state.scanned_count;
411168 idx_t to_scan = MinValue<idx_t>(remaining, remaining_in_vector);
411169 ScanInternal(scan_state, to_scan, result, offset + scanned);
411170 remaining -= to_scan;
411171 scanned += to_scan;
411172 }
411173 D_ASSERT(scanned == count);
411174 }
ZSTDVectorScanState & LoadVector(idx_t vector_idx, idx_t internal_offset)
Definition duckdb.cpp:410991

The documentation for this struct was generated from the following file: