Column Store
DataRecord.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <any>
4 #include <iostream>
5 #include <memory>
6 #include <string>
7 #include <unordered_map>
8 #include <vector>
9 
10 #include "../Fort/fort.hpp"
11 #include "Column.h"
12 
16 namespace ColumnStore {
28 class DataValue {
29  std::any value;
30 
31  public:
32  DataValue(std::any value) : value(value) {}
33 
39  template <typename T>
40  const T as() const {
41  return std::any_cast<T>(value);
42  }
43 
44  void operator=(std::any value) { this->value = value; }
45 
46  std::any get() { return value; }
47 
48  operator int() { return as<int>(); }
49  operator float() { return as<float>(); }
50  operator std::string() { return as<std::string>(); }
51 };
52 
64 struct DataRecord {
65  std::vector<DataValue> values;
67  DataRecord(std::vector<DataValue>& values) : values(std::move(values)) {}
68 
74  DataValue& operator[](int index) { return values[index]; }
75 };
76 
89  std::vector<Column> columns;
94  std::unordered_map<std::string, int> columnMap;
95 
96  public:
97  DataRecordMetadata(std::vector<Column> columns) : columns(columns) {
98  for (int i = 0; i < int(columns.size()); i++) {
99  columnMap[columns[i].name] = i;
100  columns[i].index = i;
101  }
102  }
108  std::vector<Column>& getColumns() { return columns; }
109 
116  Column& operator[](int i) {
117  assert(i < int(columns.size()));
118  return columns[i];
119  }
120 
127  Column& getColumn(std::string name) { return (*this)[name]; }
128 
135  Column& getColumn(int i) { return (*this)[i]; }
136 
143  Column& operator[](std::string name) {
144  assert(columnMap.find(name) != columnMap.end());
145  return columns[columnMap[name]];
146  }
147 
155  void print(DataRecord& record) {
156  fort::char_table table;
157  table << fort::header;
158 
159  for (int i = 0; i < int(columns.size()); i++) table << columns[i].name;
160 
161  table << fort::endr;
162 
163  for (int i = 0; i < int(columns.size()); i++) {
164  auto type = columns[i].type;
165  if (type == DataType::INT)
166  table << record[i].as<int>();
167  else if (type == DataType::FLOAT)
168  table << record[i].as<float>();
169  else if (type == DataType::STRING)
170  table << record[i].as<std::string>();
171  else
172  throw std::runtime_error("Unknown DataType");
173  }
174  table << fort::endr;
175 
176  std::cout << table.to_string() << std::endl;
177  }
178 
184  void print() {
185  for (int i = 0; i < int(columns.size()); i++) {
186  auto type = columns[i].type;
187  std::cout << columns[i].name << " - ";
188  if (type == DataType::INT)
189  std::cout << "INT";
190  else if (type == DataType::FLOAT)
191  std::cout << "FLOAT";
192  else if (type == DataType::STRING)
193  std::cout << "STRING";
194  else
195  throw std::runtime_error("Unknown DataType");
196 
197  std::cout << std::endl;
198  }
199  std::cout << std::endl;
200  }
201 };
202 
208 typedef std::shared_ptr<DataRecordMetadata> Metadata;
209 }; // namespace ColumnStore
ColumnStore::DataRecordMetadata::getColumn
Column & getColumn(std::string name)
Get the Column object by name.
Definition: DataRecord.h:127
ColumnStore::DataRecordMetadata::print
void print(DataRecord &record)
Pretty print a single data record.
Definition: DataRecord.h:155
ColumnStore::DataRecordMetadata::operator[]
Column & operator[](std::string name)
Access column object by index.
Definition: DataRecord.h:143
ColumnStore::DataValue::value
std::any value
Definition: DataRecord.h:29
ColumnStore::DataRecordMetadata::columns
std::vector< Column > columns
Definition: DataRecord.h:89
ColumnStore::DataRecordMetadata::getColumns
std::vector< Column > & getColumns()
Get the Columns object.
Definition: DataRecord.h:108
ColumnStore::DataRecordMetadata
Stores metadata information of the data record.
Definition: DataRecord.h:88
ColumnStore::DataRecord::DataRecord
DataRecord(std::vector< DataValue > &values)
Definition: DataRecord.h:67
ColumnStore::DataRecord::values
std::vector< DataValue > values
Definition: DataRecord.h:65
ColumnStore::DataValue::as
const T as() const
Extracts value by type.
Definition: DataRecord.h:40
ColumnStore
Definition: ColStoreDataGenerator.h:22
ColumnStore::DataType::STRING
@ STRING
ColumnStore::Column
Struct which maintains metadata of a single column.
Definition: Column.h:22
ColumnStore::Metadata
std::shared_ptr< DataRecordMetadata > Metadata
Shared pointer to DataRecordMetadata.
Definition: DataRecord.h:208
ColumnStore::DataRecord
Stores a row of data.
Definition: DataRecord.h:64
ColumnStore::DataValue::operator=
void operator=(std::any value)
Definition: DataRecord.h:44
ColumnStore::DataType::INT
@ INT
Column.h
ColumnStore::DataRecordMetadata::columnMap
std::unordered_map< std::string, int > columnMap
Map from column name to column index.
Definition: DataRecord.h:94
ColumnStore::DataValue::DataValue
DataValue(std::any value)
Definition: DataRecord.h:32
ColumnStore::DataRecord::operator[]
DataValue & operator[](int index)
Extracts data value by index.
Definition: DataRecord.h:74
ColumnStore::DataRecordMetadata::getColumn
Column & getColumn(int i)
Get the Column object by index.
Definition: DataRecord.h:135
ColumnStore::DataValue
Implementation of a single data element.
Definition: DataRecord.h:28
ColumnStore::DataValue::get
std::any get()
Definition: DataRecord.h:46
ColumnStore::DataRecord::DataRecord
DataRecord()
Definition: DataRecord.h:66
ColumnStore::DataRecordMetadata::print
void print()
Print metadata.
Definition: DataRecord.h:184
ColumnStore::DataType::FLOAT
@ FLOAT
ColumnStore::DataRecordMetadata::DataRecordMetadata
DataRecordMetadata(std::vector< Column > columns)
Definition: DataRecord.h:97
ColumnStore::DataRecordMetadata::operator[]
Column & operator[](int i)
Access Column by index.
Definition: DataRecord.h:116