usql/table.h

83 lines
2.4 KiB
C++

#pragma once
#include "index.h"
#include "parser.h"
#include "row.h"
#include <utility>
#include <vector>
#include <mutex>
namespace usql {
struct Table {
Table(const Table &other);
Table(const std::string& name, const std::vector<ColDefNode>& columns);
ColDefNode get_column_def(const std::string &col_name);
ColDefNode get_column_def(int col_index);
[[nodiscard]] int columns_count() const { return (int) m_col_defs.size(); };
[[nodiscard]] size_t rows_count() const { return m_rows.size(); };
[[nodiscard]] size_t get_rowid(const Row &row) const;
Row &create_empty_row();
void commit_row(Row &row);
void commit_copy_of_row(Row &row);
static void validate_column(const ColDefNode *col_def, ValueNode *col_val);
static void validate_column(const ColDefNode *col_def, ColValue &col_val);
void validate_row(Row &row);
std::string csv_string();
size_t load_csv_string(const std::string &content);
size_t load_csv_file(const std::string &filename);
void print();
std::string m_name;
std::vector<ColDefNode> m_col_defs;
std::vector<Row> m_rows;
std::vector<Index> m_indexes;
std::mutex m_insert_guard;
void create_row_from_vector(const std::vector<ColDefNode> &colDefs, const std::vector<std::string> &csv_line);
void create_index(const Index& index);
bool drop_index(const std::string &index_name);
static void index_row(Index &index, const ColDefNode &col_def, const Row &row, size_t rowid);
static void unindex_row(Index &index, const ColDefNode &col_def, const Row &row, size_t rowid);
static void reindex_row(Index &index, const ColDefNode &col_def, const Row &old_row, const Row &new_row, size_t rowid);
void index_row(const Row &row);
void unindex_row(const Row &row);
void reindex_row(const Row &old_row, const Row &new_row);
void index_rows(const std::string &index_name);
Index * get_index(const std::string &index_name);
Index * get_index_for_column(const std::string &col_name);
bool empty() const;
struct rows_scanner {
explicit rows_scanner(Table *tbl) : m_use_rowids(false), m_table(tbl), m_fscan_itr(tbl->m_rows.begin()) {}
rows_scanner(Table *tbl, std::vector<rowid_t> rowids) : m_use_rowids(true), m_table(tbl), m_rowids(std::move(rowids)), m_rowids_idx(0) {}
Row *next();
private:
bool m_use_rowids;
Table * m_table;
std::vector<Row>::iterator m_fscan_itr;
std::vector<rowid_t> m_rowids;
size_t m_rowids_idx{};
};
};
} // namespace