-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_engine.cpp
executable file
·84 lines (74 loc) · 2.78 KB
/
storage_engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <algorithm>
#include "storage_engine.h"
bool Value::operator==(const Value &other) const {
if (this->data_type != other.data_type)
return false;
if (this->data_type == ColumnAttribute::INT)
return this->n == other.n;
return this->s == other.s;
}
bool Value::operator!=(const Value &other) const {
return !(*this == other);
}
bool Value::operator<(const Value &other) const {
if (this->data_type != other.data_type) {
// arbitrary ordering of data types: BOOLEAN < INT < TEXT
if (this->data_type == ColumnAttribute::BOOLEAN)
return true;
if (other.data_type == ColumnAttribute::BOOLEAN)
return false;
if (this->data_type == ColumnAttribute::INT)
return true;
if (other.data_type == ColumnAttribute::INT)
return false;
return false; // should never reach this
}
if (this->data_type == ColumnAttribute::TEXT)
return this->s < other.s;
return this->n < other.n;
}
// Get only selected column attributes
ColumnAttributes* DbRelation::get_column_attributes(const ColumnNames &select_column_names) const {
ColumnAttributes *ret = new ColumnAttributes();
for (auto const& column_name: select_column_names) {
auto it = std::find(this->column_names.begin(), this->column_names.end(), column_name);
if (it == this->column_names.end()) {
delete ret;
throw DbRelationError("unknown column " + column_name);
}
ptrdiff_t index = it - this->column_names.begin();
ret->push_back(this->column_attributes[index]);
}
return ret;
}
// Just pulls out the column names from a ValueDict and passes that to the usual form of project().
ValueDict* DbRelation::project(Handle handle, const ValueDict* where) {
ColumnNames t;
for (auto const& column: *where)
t.push_back(column.first);
return this->project(handle, &t);
}
// Do a projection for each of a list of handles
ValueDicts* DbRelation::project(Handles *handles) {
ValueDicts *ret = new ValueDicts();
for (auto const& handle: *handles)
ret->push_back(project(handle));
return ret;
}
// Do a projection for each of a list of handles
ValueDicts* DbRelation::project(Handles *handles, const ColumnNames *column_names) {
ValueDicts *ret = new ValueDicts();
for (auto const& handle: *handles)
ret->push_back(project(handle, column_names));
return ret;
}
// Do a projection for each of a list of handles
ValueDicts* DbRelation::project(Handles *handles, const ValueDict* where) {
ColumnNames t;
for (auto const& column: *where)
t.push_back(column.first);
ValueDicts *ret = new ValueDicts();
for (auto const& handle: *handles)
ret->push_back(project(handle, &t));
return ret;
}