Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/core/src/ArrayBackedPosIntSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ template<typename PosIntType>
void ArrayBackedPosIntSet<PosIntType>::increase_capacity (size_t value) {
if (value < m_data.size()) {
SPDLOG_ERROR("Calling increase_capacity on value smaller than capacity.");
throw OperationFailed(ErrorCode_BadParam, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::BadParam, __FILENAME__, __LINE__);
}
auto capacity = m_data.size();
do {
Expand Down
18 changes: 9 additions & 9 deletions components/core/src/DictionaryReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class DictionaryReader {
template <typename DictionaryIdType, typename EntryType>
void DictionaryReader<DictionaryIdType, EntryType>::open (const std::string& dictionary_path, const std::string& segment_index_path) {
if (m_is_open) {
throw OperationFailed(ErrorCode_NotReady, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotReady, __FILENAME__, __LINE__);
}

constexpr size_t cDecompressorFileReadBufferCapacity = 64 * 1024; // 64 KB
Expand All @@ -127,7 +127,7 @@ void DictionaryReader<DictionaryIdType, EntryType>::open (const std::string& dic
template <typename DictionaryIdType, typename EntryType>
void DictionaryReader<DictionaryIdType, EntryType>::close () {
if (false == m_is_open) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotInit, __FILENAME__, __LINE__);
}

m_segment_index_decompressor.close();
Expand All @@ -144,15 +144,15 @@ void DictionaryReader<DictionaryIdType, EntryType>::close () {
template <typename DictionaryIdType, typename EntryType>
void DictionaryReader<DictionaryIdType, EntryType>::read_new_entries () {
if (false == m_is_open) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotInit, __FILENAME__, __LINE__);
}

// Read dictionary header
auto num_dictionary_entries = read_dictionary_header(m_dictionary_file_reader);

// Validate dictionary header
if (num_dictionary_entries < m_entries.size()) {
throw OperationFailed(ErrorCode_Corrupt, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::Corrupt, __FILENAME__, __LINE__);
}

// Read new dictionary entries
Expand All @@ -173,7 +173,7 @@ void DictionaryReader<DictionaryIdType, EntryType>::read_new_entries () {

// Validate segment index header
if (num_segments < m_num_segments_read_from_index) {
throw OperationFailed(ErrorCode_Corrupt, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::Corrupt, __FILENAME__, __LINE__);
}

// Read new segments from index
Expand All @@ -188,10 +188,10 @@ void DictionaryReader<DictionaryIdType, EntryType>::read_new_entries () {
template <typename DictionaryIdType, typename EntryType>
const EntryType& DictionaryReader<DictionaryIdType, EntryType>::get_entry (DictionaryIdType id) const {
if (false == m_is_open) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotInit, __FILENAME__, __LINE__);
}
if (id >= m_entries.size()) {
throw OperationFailed(ErrorCode_BadParam, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::BadParam, __FILENAME__, __LINE__);
}

return m_entries[id];
Expand All @@ -200,7 +200,7 @@ const EntryType& DictionaryReader<DictionaryIdType, EntryType>::get_entry (Dicti
template <typename DictionaryIdType, typename EntryType>
const std::string& DictionaryReader<DictionaryIdType, EntryType>::get_value (DictionaryIdType id) const {
if (id >= m_entries.size()) {
throw OperationFailed(ErrorCode_Corrupt, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::Corrupt, __FILENAME__, __LINE__);
}
return m_entries[id].get_value();
}
Expand Down Expand Up @@ -247,7 +247,7 @@ void DictionaryReader<DictionaryIdType, EntryType>::read_segment_ids () {
DictionaryIdType id;
m_segment_index_decompressor.read_numeric_value(id, false);
if (id >= m_entries.size()) {
throw OperationFailed(ErrorCode_Corrupt, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::Corrupt, __FILENAME__, __LINE__);
}

m_entries[id].add_segment_containing_entry(segment_id);
Expand Down
14 changes: 7 additions & 7 deletions components/core/src/DictionaryWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class DictionaryWriter {
template <typename DictionaryIdType, typename EntryType>
void DictionaryWriter<DictionaryIdType, EntryType>::open (const std::string& dictionary_path, const std::string& segment_index_path, DictionaryIdType max_id) {
if (m_is_open) {
throw OperationFailed(ErrorCode_NotReady, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotReady, __FILENAME__, __LINE__);
}

m_dictionary_file_writer.open(dictionary_path, FileWriter::OpenMode::CREATE_FOR_WRITING);
Expand All @@ -139,7 +139,7 @@ void DictionaryWriter<DictionaryIdType, EntryType>::open (const std::string& dic
template <typename DictionaryIdType, typename EntryType>
void DictionaryWriter<DictionaryIdType, EntryType>::close () {
if (false == m_is_open) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotInit, __FILENAME__, __LINE__);
}

write_header_and_flush_to_disk();
Expand All @@ -156,7 +156,7 @@ void DictionaryWriter<DictionaryIdType, EntryType>::close () {
template <typename DictionaryIdType, typename EntryType>
void DictionaryWriter<DictionaryIdType, EntryType>::write_header_and_flush_to_disk () {
if (false == m_is_open) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotInit, __FILENAME__, __LINE__);
}

// Update header
Expand All @@ -176,7 +176,7 @@ void DictionaryWriter<DictionaryIdType, EntryType>::open_and_preload (const std:
const variable_dictionary_id_t max_id)
{
if (m_is_open) {
throw OperationFailed(ErrorCode_NotReady, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotReady, __FILENAME__, __LINE__);
}

m_max_id = max_id;
Expand All @@ -192,7 +192,7 @@ void DictionaryWriter<DictionaryIdType, EntryType>::open_and_preload (const std:
auto num_dictionary_entries = read_dictionary_header(dictionary_file_reader);
if (num_dictionary_entries > m_max_id) {
SPDLOG_ERROR("DictionaryWriter ran out of IDs.");
throw OperationFailed(ErrorCode_OutOfBounds, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::OutOfBounds, __FILENAME__, __LINE__);
}
// Loads entries from the given dictionary file
EntryType entry;
Expand All @@ -202,7 +202,7 @@ void DictionaryWriter<DictionaryIdType, EntryType>::open_and_preload (const std:
const auto& str_value = entry.get_value();
if (m_value_to_id.count(str_value)) {
SPDLOG_ERROR("Entry's value already exists in dictionary");
throw OperationFailed(ErrorCode_Corrupt, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::Corrupt, __FILENAME__, __LINE__);
}

m_value_to_id[str_value] = entry.get_id();;
Expand Down Expand Up @@ -230,7 +230,7 @@ void DictionaryWriter<DictionaryIdType, EntryType>::open_and_preload (const std:
template <typename DictionaryIdType, typename EntryType>
void DictionaryWriter<DictionaryIdType, EntryType>::index_segment (segment_id_t segment_id, const ArrayBackedPosIntSet<DictionaryIdType>& ids) {
if (false == m_is_open) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotInit, __FILENAME__, __LINE__);
}

m_segment_index_compressor.write_numeric_value(segment_id);
Expand Down
2 changes: 1 addition & 1 deletion components/core/src/EncodedVariableInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ bool EncodedVariableInterpreter::encode_and_search_dictionary (const string& var
{
size_t length = var_str.length();
if (0 == length) {
throw OperationFailed(ErrorCode_BadParam, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::BadParam, __FILENAME__, __LINE__);
}

encoded_variable_t encoded_var;
Expand Down
28 changes: 28 additions & 0 deletions components/core/src/ErrorCode.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
#ifndef ERRORCODE_HPP
#define ERRORCODE_HPP

// C libraries
#include <stdint.h>

enum class ErrorCode : uint8_t {
Success = 0,
BadParam,
BadParam_DB_URI,
Corrupt,
Errno,
EndOfFile,
FileExists,
FileNotFound,
NoMem,
NotInit,
NotReady,
OutOfBounds,
TooLong,
Truncated,
Unsupported,
NoAccess,
Failure,
Failure_Metadata_Corrupted,
MetadataCorrupted,
Failure_DB_Bulk_Write,
};

/*

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can delete the commented enum.

typedef enum {
ErrorCode_Success = 0,
ErrorCode_BadParam,
Expand All @@ -23,5 +50,6 @@ typedef enum {
ErrorCode_MetadataCorrupted,
ErrorCode_Failure_DB_Bulk_Write
} ErrorCode;
*/

#endif
42 changes: 21 additions & 21 deletions components/core/src/FileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,50 @@ FileReader::~FileReader () {

ErrorCode FileReader::try_read (char* buf, size_t num_bytes_to_read, size_t& num_bytes_read) {
if (nullptr == m_file) {
return ErrorCode_NotInit;
return ErrorCode::NotInit;
}
if (nullptr == buf) {
return ErrorCode_BadParam;
return ErrorCode::BadParam;
}

num_bytes_read = fread(buf, sizeof(*buf), num_bytes_to_read, m_file);
if (num_bytes_read < num_bytes_to_read) {
if (ferror(m_file)) {
return ErrorCode_errno;
return ErrorCode::Errno;
} else if (feof(m_file)) {
if (0 == num_bytes_read) {
return ErrorCode_EndOfFile;
return ErrorCode::EndOfFile;
}
}
}

return ErrorCode_Success;
return ErrorCode::Success;
}

ErrorCode FileReader::try_seek_from_begin (size_t pos) {
if (nullptr == m_file) {
return ErrorCode_NotInit;
return ErrorCode::NotInit;
}

int retval = fseeko(m_file, pos, SEEK_SET);
if (0 != retval) {
return ErrorCode_errno;
return ErrorCode::Errno;
}

return ErrorCode_Success;
return ErrorCode::Success;
}

ErrorCode FileReader::try_get_pos (size_t& pos) {
if (nullptr == m_file) {
return ErrorCode_NotInit;
return ErrorCode::NotInit;
}

pos = ftello(m_file);
if ((off_t)-1 == pos) {
return ErrorCode_errno;
return ErrorCode::Errno;
}

return ErrorCode_Success;
return ErrorCode::Success;
}

ErrorCode FileReader::try_open (const string& path) {
Expand All @@ -71,17 +71,17 @@ ErrorCode FileReader::try_open (const string& path) {
m_file = fopen(path.c_str(), "rb");
if (nullptr == m_file) {
if (ENOENT == errno) {
return ErrorCode_FileNotFound;
return ErrorCode::FileNotFound;
}
return ErrorCode_errno;
return ErrorCode::Errno;
}

return ErrorCode_Success;
return ErrorCode::Success;
}

void FileReader::open (const string& path) {
ErrorCode error_code = try_open(path);
if (ErrorCode_Success != error_code) {
if (ErrorCode::Success != error_code) {
throw OperationFailed(error_code, __FILENAME__, __LINE__);
}
}
Expand All @@ -104,27 +104,27 @@ ErrorCode FileReader::try_read_to_delimiter (char delim, bool keep_delimiter, bo
ssize_t num_bytes_read = getdelim(&m_getdelim_buf, &m_getdelim_buf_len, delim, m_file);
if (num_bytes_read < 1) {
if (ferror(m_file)) {
return ErrorCode_errno;
return ErrorCode::Errno;
} else if (feof(m_file)) {
return ErrorCode_EndOfFile;
return ErrorCode::EndOfFile;
}
}
if (false == keep_delimiter && delim == m_getdelim_buf[num_bytes_read - 1]) {
--num_bytes_read;
}
str.append(m_getdelim_buf, num_bytes_read);

return ErrorCode_Success;
return ErrorCode::Success;
}

ErrorCode FileReader::try_fstat (struct stat& stat_buffer) {
if (nullptr == m_file) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
throw OperationFailed(ErrorCode::NotInit, __FILENAME__, __LINE__);
}

auto return_value = fstat(fileno(m_file), &stat_buffer);
if (0 != return_value) {
return ErrorCode_errno;
return ErrorCode::Errno;
}
return ErrorCode_Success;
return ErrorCode::Success;
}
38 changes: 19 additions & 19 deletions components/core/src/FileReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ class FileReader : public ReaderInterface {
/**
* Tries to get the current position of the read head in the file
* @param pos Position of the read head in the file
* @return ErrorCode_NotInit if the file is not open
* @return ErrorCode_errno on error
* @return ErrorCode_Success on success
* @return ErrorCode::NotInit if the file is not open
* @return ErrorCode::Errno on error
* @return ErrorCode::Success on success
*/
ErrorCode try_get_pos (size_t& pos) override;
/**
* Tries to seek from the beginning of the file to the given position
* @param pos
* @return ErrorCode_NotInit if the file is not open
* @return ErrorCode_errno on error
* @return ErrorCode_Success on success
* @return ErrorCode::NotInit if the file is not open
* @return ErrorCode::Errno on error
* @return ErrorCode::Success on success
*/
ErrorCode try_seek_from_begin (size_t pos) override;

Expand All @@ -51,11 +51,11 @@ class FileReader : public ReaderInterface {
* @param buf
* @param num_bytes_to_read The number of bytes to try and read
* @param num_bytes_read The actual number of bytes read
* @return ErrorCode_NotInit if the file is not open
* @return ErrorCode_BadParam if buf is invalid
* @return ErrorCode_errno on error
* @return ErrorCode_EndOfFile on EOF
* @return ErrorCode_Success on success
* @return ErrorCode::NotInit if the file is not open
* @return ErrorCode::BadParam if buf is invalid
* @return ErrorCode::Errno on error
* @return ErrorCode::EndOfFile on EOF
* @return ErrorCode::Success on success
*/
ErrorCode try_read (char* buf, size_t num_bytes_to_read, size_t& num_bytes_read) override;

Expand All @@ -65,9 +65,9 @@ class FileReader : public ReaderInterface {
* @param keep_delimiter Whether to include the delimiter in the output string or not
* @param append Whether to append to the given string or replace its contents
* @param str The string read
* @return ErrorCode_Success on success
* @return ErrorCode_EndOfFile on EOF
* @return ErrorCode_errno otherwise
* @return ErrorCode::Success on success
* @return ErrorCode::EndOfFile on EOF
* @return ErrorCode::Errno otherwise
*/
ErrorCode try_read_to_delimiter (char delim, bool keep_delimiter, bool append, std::string& str) override;

Expand All @@ -77,9 +77,9 @@ class FileReader : public ReaderInterface {
/**
* Tries to open a file
* @param path
* @return ErrorCode_Success on success
* @return ErrorCode_FileNotFound if the file was not found
* @return ErrorCode_errno otherwise
* @return ErrorCode::Success on success
* @return ErrorCode::FileNotFound if the file was not found
* @return ErrorCode::Errno otherwise
*/
ErrorCode try_open (const std::string& path);
/**
Expand All @@ -96,8 +96,8 @@ class FileReader : public ReaderInterface {
/**
* Tries to stat the current file
* @param stat_buffer
* @return ErrorCode_errno on error
* @return ErrorCode_Success on success
* @return ErrorCode::Errno on error
* @return ErrorCode::Success on success
*/
ErrorCode try_fstat (struct stat& stat_buffer);

Expand Down
Loading