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 pathcolumn.cpp
118 lines (99 loc) · 2.91 KB
/
column.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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// column.cpp
//
// Identification: src/catalog/column.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "catalog/column.h"
#include <sstream>
#include "common/internal_types.h"
namespace peloton {
namespace catalog {
void Column::SetLength(size_t column_length) {
// Set the column length based on whether it is inlined
if (is_inlined) {
fixed_length = column_length;
variable_length = 0;
} else {
fixed_length = sizeof(uintptr_t);
variable_length = column_length;
}
}
void Column::SetInlined() {
switch (column_type) {
case type::TypeId::VARCHAR:
case type::TypeId::VARBINARY:
break; // No change of inlined setting
default:
is_inlined = true;
break;
}
}
// Serialize this column
void Column::SerializeTo(SerializeOutput &out) {
// Column basic information
out.WriteTextString(column_name);
out.WriteInt((int)column_type);
out.WriteInt(GetLength());
out.WriteInt(column_offset);
out.WriteBool(is_inlined);
// Column constraints
out.WriteLong(constraints.size());
for (auto constraint : constraints) {
constraint.SerializeTo(out);
}
}
// Deserialize this column
Column Column::DeserializeFrom(SerializeInput &in) {
// read basic column information
std::string column_name = in.ReadTextString();
type::TypeId column_type = (type::TypeId)in.ReadInt();
size_t column_length = in.ReadInt();
oid_t column_offset = in.ReadInt();
bool is_inlined = in.ReadBool();
auto column = catalog::Column(column_type, column_length, column_name,
is_inlined, column_offset);
// recover column constraints
size_t column_constraint_count = in.ReadLong();
for (oid_t constraint_idx = 0; constraint_idx < column_constraint_count;
constraint_idx++) {
auto column_constraint = Constraint::DeserializeFrom(in, column_type);
// Foreign key constraint will be stored by DataTable deserializer
if (column_constraint.GetType() != ConstraintType::FOREIGN) {
column.AddConstraint(column_constraint);
}
}
return column;
}
const std::string Column::GetInfo() const {
std::ostringstream os;
os << "Column[" << column_name << ", " << TypeIdToString(column_type) << ", "
<< "Offset:" << column_offset << ", ";
if (is_inlined) {
os << "FixedLength:" << fixed_length;
} else {
os << "VarLength:" << variable_length;
}
if (constraints.empty() == false) {
os << ", {";
bool first = true;
for (auto constraint : constraints) {
if (first) {
first = false;
} else {
os << ", ";
}
os << constraint.GetInfo();
}
os << "}";
}
os << "]";
return (os.str());
}
} // namespace catalog
} // namespace peloton