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::BufferedFileWriter Class Reference
Inheritance diagram for duckdb::BufferedFileWriter:
Collaboration diagram for duckdb::BufferedFileWriter:

Public Member Functions

DUCKDB_API BufferedFileWriter (FileSystem &fs, const string &path, FileOpenFlags open_flags=DEFAULT_OPEN_FLAGS)
DUCKDB_API void WriteData (const_data_ptr_t buffer, idx_t write_size) override
DUCKDB_API void Close ()
 Flush all changes to the file and then close the file.
DUCKDB_API void Sync ()
 Flush all changes and fsync the file to disk.
DUCKDB_API void Flush ()
 Flush the buffer to the file (without sync).
DUCKDB_API idx_t GetFileSize ()
 Returns the current size of the file.
DUCKDB_API void Truncate (idx_t size)
 Truncate the size to a previous size (given that size <= GetFileSize()).
DUCKDB_API idx_t GetTotalWritten () const
Public Member Functions inherited from duckdb::WriteStream
template<class T>
void Write (T element)

Public Attributes

FileSystem & fs
string path
unsafe_unique_array< data_t > data
idx_t offset
idx_t total_written
unique_ptr< FileHandle > handle

Static Public Attributes

static constexpr FileOpenFlags DEFAULT_OPEN_FLAGS = FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE

Constructor & Destructor Documentation

◆ BufferedFileWriter()

duckdb::BufferedFileWriter::BufferedFileWriter ( FileSystem & fs,
const string & path,
FileOpenFlags open_flags = DEFAULT_OPEN_FLAGS )

Serializes to a buffer allocated by the serializer, will expand when writing past the initial threshold

100872 : fs(fs), path(path_p), data(make_unsafe_uniq_array_uninitialized<data_t>(FILE_BUFFER_SIZE)), offset(0),
100873 total_written(0) {
100874 handle = fs.OpenFile(path, open_flags | FileLockType::WRITE_LOCK);
100875}

Member Function Documentation

◆ WriteData()

void duckdb::BufferedFileWriter::WriteData ( const_data_ptr_t buffer,
idx_t write_size )
overridevirtual

Implements duckdb::WriteStream.

100885 {
100886 if (write_size >= (2ULL * FILE_BUFFER_SIZE - offset)) {
100887 idx_t to_copy = 0;
100888 // Check before performing direct IO if there is some data in the current internal buffer.
100889 // If so, then fill the buffer (to avoid to small write operation), flush it and then write
100890 // all the remain data directly.
100891 // This is to avoid to split a large buffer into N*FILE_BUFFER_SIZE buffers
100892 if (offset != 0) {
100893 // Some data are still present in the buffer let write them before
100894 to_copy = FILE_BUFFER_SIZE - offset;
100895 memcpy(data.get() + offset, buffer, to_copy);
100896 offset += to_copy;
100897 Flush(); // Flush buffer before writing every things else
100898 }
100899 idx_t remaining_to_write = write_size - to_copy;
100900 fs.Write(*handle, const_cast<data_ptr_t>(buffer + to_copy), // NOLINT: wrong API in Write
100901 UnsafeNumericCast<int64_t>(remaining_to_write));
100902 total_written += remaining_to_write;
100903 } else {
100904 // first copy anything we can from the buffer
100905 const_data_ptr_t end_ptr = buffer + write_size;
100906 while (buffer < end_ptr) {
100907 idx_t to_write = MinValue<idx_t>(UnsafeNumericCast<idx_t>((end_ptr - buffer)), FILE_BUFFER_SIZE - offset);
100908 D_ASSERT(to_write > 0);
100909 memcpy(data.get() + offset, buffer, to_write);
100910 offset += to_write;
100911 buffer += to_write;
100912 if (offset == FILE_BUFFER_SIZE) {
100913 Flush();
100914 }
100915 }
100916 }
100917}
DUCKDB_API void Flush()
Flush the buffer to the file (without sync).
Definition duckdb.cpp:100919
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ Close()

void duckdb::BufferedFileWriter::Close ( )

Flush all changes to the file and then close the file.

100928 {
100929 Flush();
100930 handle->Close();
100931 handle.reset();
100932}
Here is the call graph for this function:

◆ Sync()

void duckdb::BufferedFileWriter::Sync ( )

Flush all changes and fsync the file to disk.

100934 {
100935 Flush();
100936 handle->Sync();
100937}
Here is the call graph for this function:

◆ Flush()

void duckdb::BufferedFileWriter::Flush ( )

Flush the buffer to the file (without sync).

100919 {
100920 if (offset == 0) {
100921 return;
100922 }
100923 fs.Write(*handle, data.get(), UnsafeNumericCast<int64_t>(offset));
100924 total_written += offset;
100925 offset = 0;
100926}
Here is the caller graph for this function:

◆ GetFileSize()

idx_t duckdb::BufferedFileWriter::GetFileSize ( )

Returns the current size of the file.

100877 {
100878 return NumericCast<idx_t>(fs.GetFileSize(*handle)) + offset;
100879}

◆ Truncate()

void duckdb::BufferedFileWriter::Truncate ( idx_t size)

Truncate the size to a previous size (given that size <= GetFileSize()).

100939 {
100940 auto persistent = NumericCast<idx_t>(fs.GetFileSize(*handle));
100941 D_ASSERT(size <= persistent + offset);
100942 if (persistent <= size) {
100943 // truncating into the pending write buffer.
100944 offset = size - persistent;
100945 } else {
100946 // truncate the physical file on disk
100947 handle->Truncate(NumericCast<int64_t>(size));
100948 // reset anything written in the buffer
100949 offset = 0;
100950 // Reset the seek position if applicable
100951 if (handle->CanSeek() && handle->SeekPosition() > size) {
100952 handle->Seek(size);
100953 }
100954 }
100955}
GOpaque< Size > size(const GMat &src)

◆ GetTotalWritten()

idx_t duckdb::BufferedFileWriter::GetTotalWritten ( ) const
100881 {
100882 return total_written + offset;
100883}

The documentation for this class was generated from the following files: