This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathindex_selection_util.cpp
146 lines (122 loc) · 4.27 KB
/
index_selection_util.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//===----------------------------------------------------------------------===//
//
// Peloton
//
// index_selection_util.cpp
//
// Identification: src/brain/index_selection_util.cpp
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "brain/index_selection_util.h"
#include "common/logger.h"
namespace peloton {
namespace brain {
//===--------------------------------------------------------------------===//
// IndexObject
//===--------------------------------------------------------------------===//
const std::string IndexObject::ToString() const {
std::stringstream str_stream;
str_stream << "Database: " << db_oid << "\n";
str_stream << "Table: " << table_oid << "\n";
str_stream << "Columns: ";
for (auto col : column_oids) {
str_stream << col << ", ";
}
str_stream << "\n";
return str_stream.str();
}
bool IndexObject::operator==(const IndexObject &obj) const {
if (db_oid == obj.db_oid && table_oid == obj.table_oid &&
column_oids == obj.column_oids) {
return true;
}
return false;
}
bool IndexObject::IsCompatible(std::shared_ptr<IndexObject> index) const {
return (db_oid == index->db_oid) && (table_oid == index->table_oid);
}
IndexObject IndexObject::Merge(std::shared_ptr<IndexObject> index) {
IndexObject result;
result.db_oid = db_oid;
result.table_oid = table_oid;
result.column_oids = column_oids;
for (auto column : index->column_oids) {
result.column_oids.insert(column);
}
return result;
}
//===--------------------------------------------------------------------===//
// IndexConfiguration
//===--------------------------------------------------------------------===//
void IndexConfiguration::Merge(IndexConfiguration &config) {
auto indexes = config.GetIndexes();
for (auto it = indexes.begin(); it != indexes.end(); it++) {
indexes_.insert(*it);
}
}
void IndexConfiguration::Set(IndexConfiguration &config) {
indexes_.clear();
auto indexes = config.GetIndexes();
for (auto it = indexes.begin(); it != indexes.end(); it++) {
indexes_.insert(*it);
}
}
void IndexConfiguration::RemoveIndexObject(
std::shared_ptr<IndexObject> index_info) {
indexes_.erase(index_info);
}
void IndexConfiguration::AddIndexObject(
std::shared_ptr<IndexObject> index_info) {
indexes_.insert(index_info);
}
size_t IndexConfiguration::GetIndexCount() const { return indexes_.size(); }
bool IndexConfiguration::IsEmpty() const { return indexes_.size() == 0; }
const std::set<std::shared_ptr<IndexObject>> &IndexConfiguration::GetIndexes()
const {
return indexes_;
}
const std::string IndexConfiguration::ToString() const {
std::stringstream str_stream;
str_stream << "Num of indexes: " << GetIndexCount() << "\n";
for (auto index : indexes_) {
str_stream << index->ToString() << " ";
}
return str_stream.str();
}
bool IndexConfiguration::operator==(const IndexConfiguration &config) const {
auto config_indexes = config.GetIndexes();
return indexes_ == config_indexes;
}
IndexConfiguration IndexConfiguration::operator-(
const IndexConfiguration &config) {
auto config_indexes = config.GetIndexes();
std::set<std::shared_ptr<IndexObject>> result;
std::set_difference(indexes_.begin(), indexes_.end(), config_indexes.begin(),
config_indexes.end(),
std::inserter(result, result.end()));
return IndexConfiguration(result);
}
void IndexConfiguration::Clear() { indexes_.clear(); }
//===--------------------------------------------------------------------===//
// IndexObjectPool
//===--------------------------------------------------------------------===//
std::shared_ptr<IndexObject> IndexObjectPool::GetIndexObject(IndexObject &obj) {
auto ret = map_.find(obj);
if (ret != map_.end()) {
return ret->second;
}
return nullptr;
}
std::shared_ptr<IndexObject> IndexObjectPool::PutIndexObject(IndexObject &obj) {
auto index_s_ptr = GetIndexObject(obj);
if (index_s_ptr != nullptr) return index_s_ptr;
IndexObject *index_copy = new IndexObject();
*index_copy = obj;
index_s_ptr = std::shared_ptr<IndexObject>(index_copy);
map_[*index_copy] = index_s_ptr;
return index_s_ptr;
}
} // namespace brain
} // namespace peloton