forked from VoltDB/voltdb-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableIterator.h
More file actions
109 lines (100 loc) · 3.62 KB
/
Copy pathTableIterator.h
File metadata and controls
109 lines (100 loc) · 3.62 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
/* 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.
*/
#ifndef VOLTDB_TABLEITERATOR_H_
#define VOLTDB_TABLEITERATOR_H_
#include "ByteBuffer.hpp"
#include "Column.hpp"
#include <boost/shared_ptr.hpp>
#include "Row.hpp"
#include "Exception.hpp"
namespace voltdb {
class Table;
/*
* Iterator for retrieving rows from a table. Retains a shared pointer to the buffer backing the table. Watch out
* for unwanted memory retention.
*/
class TableIterator {
public:
/*
* Construct an iterator for the table rows with the specified column schema and row count
*/
#ifdef SWIG
%ignore TableIterator(voltdb::SharedByteBuffer rows,
boost::shared_ptr<std::vector<voltdb::Column> > columns,
int32_t rowCount);
#endif
TableIterator(
voltdb::SharedByteBuffer rows,
boost::shared_ptr<std::vector<voltdb::Column> > columns,
int32_t rowCount) :
m_buffer(rows), m_columns(columns), m_rowCount(rowCount), m_currentRow(0) {}
// Create an empty iterator - required by error paths that return iterators by value.
TableIterator() {
m_rowCount = 0;
m_currentRow = 0;
}
/*
* Returns true if the table has more rows that can be retrieved via invoking next and false otherwise.
*/
bool hasNext() {
if (m_currentRow < m_rowCount) {
assert(m_buffer.hasRemaining());
return true;
}
return false;
}
/*
* Returns the next row in the table if there are more rows or NoMoreRowsException if there are no
* more rows. OverflowUnderflowException and IndexOutOfBoundsException only result if there are bugs
* and are not expected normally.
*/
voltdb::Row next(errType& err) {
if (m_rowCount <= m_currentRow) {
setErr(err, errNoMoreRowsException);
return voltdb::Row();
}
int32_t rowLength = m_buffer.getInt32(err);
if (!isOk(err)) {
return voltdb::Row();
}
int32_t oldLimit = m_buffer.limit();
m_buffer.limit(err, m_buffer.position() + rowLength);
if (!isOk(err)) {
return voltdb::Row();
}
SharedByteBuffer buffer = m_buffer.slice();
m_buffer.limit(err, oldLimit);
if (!isOk(err)) {
return voltdb::Row();
}
m_currentRow++;
return voltdb::Row(buffer, m_columns);
}
private:
voltdb::SharedByteBuffer m_buffer;
boost::shared_ptr<std::vector<voltdb::Column> > m_columns;
int32_t m_rowCount;
int32_t m_currentRow;
};
}
#endif /* VOLTDB_TABLEITERATOR_H_ */