LeftHookRoll
An HTTP/1.0 compliant web server, as specified by RFC1945
Loading...
Searching...
No Matches
DataStore Class Reference

#include <DataStore.hpp>

Collaboration diagram for DataStore:

Public Member Functions

 DataStore ()
 
 DataStore (const DataStore &other)
 
DataStoreoperator= (DataStore other)
 
 ~DataStore ()
 
void append (const char *data, size_t length)
 Appends raw byte data to the store. Automatically transitions from RAM to FILE_MODE if _bufferLimit is exceeded.
 
void append (const std::string &data)
 Convenience overload to append a std::string directly.
 
void clear ()
 Resets the store, clears the vector, and closes the temp file descriptor.
 
BufferMode getMode () const
 Returns the current storage mode (RAM or FILE_MODE).
 
const std::vector< char > & getVector () const
 Returns a reference to the RAM buffer.
 
int getFd () const
 Returns the file descriptor of the temporary file.
 
size_t getSize () const
 Gets total bytes currently stored (whether in RAM or File).
 
std::string getFilePath () const
 Gets the absolute path of the temporary file.
 
size_t read (char *buffer, size_t length)
 Reads data from the store starting at the current read position. Advances the internal read position by the number of bytes read.
 
void resetReadPosition ()
 Resets the internal read position to the beginning of the data.
 
size_t getReadPosition () const
 Gets the current read position.
 
void switchToFileMode ()
 Handles the transition from RAM to a temporary file on disk. Uses the immediate unlink() trick to ensure OS-level cleanup on crashes.
 

Private Member Functions

void _generateTempFileName ()
 Generates a unique temporary filename (e.g., FILEPREFIX_XXXXXX).
 
void write_all (int fd, const char *data, size_t length)
 Ensures all data is written to the file descriptor.
 
void copy_fd_contents (const std::string &srcPath, int dstFd, size_t totalBytes)
 Copies data directly from one FD to another using a buffer.
 

Private Attributes

BufferMode _mode
 
size_t _bufferLimit
 
size_t _currentSize
 
size_t _readOffset
 
std::vector< char > _dataBuffer
 
int _fileFd
 
std::string _absolutePath
 

Detailed Description

Definition at line 34 of file DataStore.hpp.

Constructor & Destructor Documentation

◆ DataStore() [1/2]

DataStore::DataStore ( )

Definition at line 65 of file DataStore.cpp.

◆ DataStore() [2/2]

DataStore::DataStore ( const DataStore other)

Definition at line 69 of file DataStore.cpp.

◆ ~DataStore()

DataStore::~DataStore ( )

Definition at line 107 of file DataStore.cpp.

Member Function Documentation

◆ operator=()

DataStore & DataStore::operator= ( DataStore  other)

Definition at line 96 of file DataStore.cpp.

◆ append() [1/2]

void DataStore::append ( const char *  data,
size_t  length 
)

Appends raw byte data to the store. Automatically transitions from RAM to FILE_MODE if _bufferLimit is exceeded.

Parameters
dataPointer to the buffer to append.
lengthNumber of bytes to append.

Definition at line 119 of file DataStore.cpp.

◆ append() [2/2]

void DataStore::append ( const std::string &  data)

Convenience overload to append a std::string directly.

Definition at line 145 of file DataStore.cpp.

◆ clear()

void DataStore::clear ( )

Resets the store, clears the vector, and closes the temp file descriptor.

Definition at line 152 of file DataStore.cpp.

◆ getMode()

BufferMode DataStore::getMode ( ) const

Returns the current storage mode (RAM or FILE_MODE).

Definition at line 169 of file DataStore.cpp.

◆ getVector()

const std::vector< char > & DataStore::getVector ( ) const

Returns a reference to the RAM buffer.

Warning
Only valid if mode is RAM. should never use in normal operation, only for testing/debugging purposes.

Definition at line 173 of file DataStore.cpp.

◆ getFd()

int DataStore::getFd ( ) const

Returns the file descriptor of the temporary file.

Warning
Only valid if mode is FILE_MODE. should never use in normal operation, only for testing/debugging purposes.

Definition at line 177 of file DataStore.cpp.

◆ getSize()

size_t DataStore::getSize ( ) const

Gets total bytes currently stored (whether in RAM or File).

Definition at line 185 of file DataStore.cpp.

◆ getFilePath()

std::string DataStore::getFilePath ( ) const

Gets the absolute path of the temporary file.

Warning
Only valid if mode is FILE_MODE. should never use in normal operation, only for testing/debugging purposes.

Definition at line 181 of file DataStore.cpp.

◆ read()

size_t DataStore::read ( char *  buffer,
size_t  length 
)

Reads data from the store starting at the current read position. Advances the internal read position by the number of bytes read.

Parameters
bufferPointer to the buffer to read into.
lengthMaximum number of bytes to read.
Returns
Number of bytes actually read (may be less if end of data is reached).

Definition at line 197 of file DataStore.cpp.

◆ resetReadPosition()

void DataStore::resetReadPosition ( )

Resets the internal read position to the beginning of the data.

Definition at line 231 of file DataStore.cpp.

◆ getReadPosition()

size_t DataStore::getReadPosition ( ) const

Gets the current read position.

Returns
Current read offset.

Definition at line 246 of file DataStore.cpp.

◆ switchToFileMode()

void DataStore::switchToFileMode ( )

Handles the transition from RAM to a temporary file on disk. Uses the immediate unlink() trick to ensure OS-level cleanup on crashes.

Definition at line 254 of file DataStore.cpp.

◆ _generateTempFileName()

void DataStore::_generateTempFileName ( )
private

Generates a unique temporary filename (e.g., FILEPREFIX_XXXXXX).

Definition at line 277 of file DataStore.cpp.

◆ write_all()

void DataStore::write_all ( int  fd,
const char *  data,
size_t  length 
)
private

Ensures all data is written to the file descriptor.

Ensures all data is written to the file descriptor. Throws on any system error.

Definition at line 14 of file DataStore.cpp.

◆ copy_fd_contents()

void DataStore::copy_fd_contents ( const std::string &  srcPath,
int  dstFd,
size_t  totalBytes 
)
private

Copies data directly from one FD to another using a buffer.

Copies data directly from one FD to another using a buffer. Throws on error.

Definition at line 32 of file DataStore.cpp.

Member Data Documentation

◆ _mode

BufferMode DataStore::_mode
private

Definition at line 120 of file DataStore.hpp.

◆ _bufferLimit

size_t DataStore::_bufferLimit
private

Definition at line 121 of file DataStore.hpp.

◆ _currentSize

size_t DataStore::_currentSize
private

Definition at line 122 of file DataStore.hpp.

◆ _readOffset

size_t DataStore::_readOffset
private

Definition at line 123 of file DataStore.hpp.

◆ _dataBuffer

std::vector<char> DataStore::_dataBuffer
private

Definition at line 126 of file DataStore.hpp.

◆ _fileFd

int DataStore::_fileFd
private

Definition at line 129 of file DataStore.hpp.

◆ _absolutePath

std::string DataStore::_absolutePath
private

Definition at line 130 of file DataStore.hpp.


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