forked from VoltDB/voltdb-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cpp
More file actions
156 lines (138 loc) · 5.54 KB
/
Copy pathTable.cpp
File metadata and controls
156 lines (138 loc) · 5.54 KB
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
147
148
149
150
151
152
153
154
155
156
/* This file is part of VoltDB.
* Copyright (C) 2008-2015 VoltDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <vector>
#include "Table.h"
#include "TableIterator.h"
#include "Row.hpp"
namespace voltdb {
Table::Table(SharedByteBuffer buffer) : m_buffer(buffer) {
buffer.position(5);
size_t columnCount = static_cast<size_t>(buffer.getInt16());
assert(columnCount > 0);
boost::shared_ptr<std::vector< voltdb::Column> > columns(
new std::vector< voltdb::Column>(columnCount));
m_columns = columns;
std::vector<int8_t> types(columnCount);
for (size_t ii = 0; ii < columnCount; ii++) {
types[ii] = buffer.getInt8();
}
for (size_t ii = 0; ii < columnCount; ii++) {
bool wasNull = false;
m_columns->at(ii) = voltdb::Column(buffer.getString(wasNull), static_cast<WireType>(types[ii]));
assert(!wasNull);
}
m_rowStart = m_buffer.getInt32(0) + 4;
m_rowCount = m_buffer.getInt32(m_rowStart);
m_buffer.position(m_buffer.limit());
}
int8_t Table::getStatusCode() const{
return m_buffer.getInt8(4);
}
TableIterator Table::iterator() const{
m_buffer.position(m_rowStart + 4);//skip row count
return TableIterator(m_buffer.slice(), m_columns, m_rowCount);
}
int32_t Table::rowCount() const{
return m_rowCount;
}
int32_t Table::columnCount() const{
return static_cast<int32_t>(m_columns->size());
}
std::vector<voltdb::Column> Table::columns() const {
return *m_columns;
}
std::string Table::toString() const {
std::ostringstream ostream;
toString(ostream, std::string(""));
return ostream.str();
}
void Table::toString(std::ostringstream &ostream, std::string indent) const {
ostream << indent << "Table size: " << m_buffer.capacity() << std::endl;
ostream << indent << "Status code: " << static_cast<int32_t>(getStatusCode()) << std::endl;
ostream << indent << "Column names: ";
for (size_t ii = 0; ii < m_columns->size(); ii++) {
if (ii != 0) {
ostream << ", ";
}
ostream << m_columns->at(ii).m_name;
}
ostream << std::endl << indent << "Column types: ";
for (size_t ii = 0; ii < m_columns->size(); ii++) {
if (ii != 0) {
ostream << ", ";
}
ostream << wireTypeToString(m_columns->at(ii).m_type);
}
ostream << std::endl;
TableIterator iter = iterator();
while (iter.hasNext()) {
Row row = iter.next();
row.toString(ostream, indent + " ");
ostream << std::endl;
}
}
void Table::operator >> (std::ostream &ostream) const {
int32_t size = m_buffer.limit();
ostream.write((const char*)&size, sizeof(size));
if (size != 0) {
ostream.write(m_buffer.bytes(), size);
}
}
//Do easy checks first before heavyweight checks.
bool Table::operator==(const Table& rhs) const {
if (this == &rhs) return true;
bool eq = (this->rowCount() == rhs.rowCount() && this->columnCount() == rhs.columnCount());
if (!eq) return false;
//Make sure all columns and their order matches.
if (m_columns != rhs.m_columns) return false;
//Is underlying buffer same?
return (m_buffer == rhs.m_buffer);
}
//Do easy checks first before heavyweight checks.
bool Table::operator!=(const Table& rhs) const {
if (this == &rhs) return false;
bool noteq = (this->rowCount() != rhs.rowCount() || this->columnCount() != rhs.columnCount());
if (noteq) return true;
//Make sure all columns and their order matches.
if (m_columns != rhs.m_columns) return true;
//Is underlying buffer same?
return (m_buffer != rhs.m_buffer);
}
static voltdb::SharedByteBuffer readByteBuffer(std::istream &istream) {
int32_t size;
istream.read((char*)&size, sizeof(size));
if (size != 0) {
boost::shared_array<char> buffer(new char[size]);
istream.read(buffer.get(), size);
return voltdb::SharedByteBuffer(buffer, size);
} else {
return SharedByteBuffer();
}
}
Table::Table(std::istream &istream) {
voltdb::SharedByteBuffer buffer = readByteBuffer(istream);
if (buffer.limit() != 0) {
*this = Table(buffer);
}
}
}