-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.cpp
218 lines (193 loc) · 5.55 KB
/
Buffer.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "Buffer.h"
// Base class: Buffer
Buffer::Buffer(u_int32_t recordCount, RowSize recordSize):
recordCount(recordCount), recordSize(recordSize), pageSize(recordCount * recordSize)
{
TRACE(false);
_rows = (byte *) malloc(recordSize * recordCount * sizeof(byte));
if (_rows == nullptr) {
throw std::runtime_error("Buffer allocation failed");
}
toBeRead = _rows;
toBeFilled = _rows;
_rowsEnd = _rows + recordSize * recordCount;
#if defined(VERBOSEL2)
traceprintf("Buffer created with %d records of size %d, page size %llu.\n", recordCount, recordSize, pageSize);
#endif
}
Buffer::~Buffer ()
{
TRACE (false);
free(_rows);
}
byte * Buffer::copy (byte const * source)
{
if (toBeFilled >= _rowsEnd) {
toBeFilled = _rows;
return nullptr;
}
std::copy(source, source + recordSize, toBeFilled);
byte * filled = toBeFilled;
toBeFilled += recordSize;
return filled;
}
byte * Buffer::next ()
{
TRACE (false);
if (toBeRead >= _rowsEnd || toBeRead >= toBeFilled) {
toBeRead = _rows;
toBeFilled = _rows; // Filled rows are all read
return nullptr;
}
byte * read = toBeRead;
toBeRead += recordSize;
return read;
}
byte * Buffer::peekNext ()
{
TRACE (false);
// Only update toBeRead and toBeFilled when the buffer is exhausted
// This means calling peekNext multiple times will return the same record
// except when the buffer is exhausted
if (toBeRead >= _rowsEnd || toBeRead >= toBeFilled) {
toBeRead = _rows;
toBeFilled = _rows;
return nullptr;
}
return toBeRead;
}
byte * Buffer::batchFillByOverwrite (u_int64_t sizeToBeFilled)
{
if (sizeToBeFilled > recordSize * recordCount) {
throw std::runtime_error("Buffer overflow");
} else if (sizeToBeFilled < recordSize * recordCount && sizeToBeFilled > 0) {
#if defined(VERBOSEL1)
traceprintf("Buffer under-filled %llu / %d.\n", sizeToBeFilled, recordSize * recordCount);
#endif
}
if (toBeFilled > _rows) {
std::cerr << "Warning: Existing content overwritten by batchFillByOverwrite" << std::endl;
}
toBeFilled = _rows + sizeToBeFilled;
toBeRead = _rows;
return _rows;
}
// Derived class: RandomBuffer
RandomBuffer::RandomBuffer (u_int32_t recordCount, RowSize recordSize):
Buffer(recordCount, recordSize),
_engine(std::chrono::system_clock::now().time_since_epoch().count()),
_distribution(0, RANDOM_BYTE_UPPER_BOUND - 1)
{
TRACE (false);
// nothing to do here right now
}
RandomBuffer::~RandomBuffer ()
{
TRACE (false);
}
/*
Generate random bytes and convert them to alphanumeric characters (A-Z, a-z, 0-9)
randomByte: a random byte (0-61)
0-9 -> '0'-'9' (48-57)
10-35 -> 'A'-'Z' (65-90)
36-61 -> 'a'-'z' (97-122)
*/
byte RandomBuffer::toAlphaNumeric (const byte randomByte) {
const byte numericOffset = 48;
const byte upperCaseOffset = 65;
const byte lowerCaseOffset = 97;
if (randomByte < 10) {
return randomByte + numericOffset;
} else if (randomByte < 36) {
return randomByte - 10 + upperCaseOffset;
} else {
return randomByte - 36 + lowerCaseOffset;
}
}
byte RandomBuffer::getRandomAlphaNumeric ()
{
byte randomByte = _distribution(_engine);
return toAlphaNumeric(randomByte);
}
byte * RandomBuffer::fillRandomly ()
{
if (toBeFilled >= _rowsEnd) {
toBeFilled = _rows;
#if defined(VERBOSEL1) || defined(VERBOSEL2)
traceprintf("Buffer cleared for refill.\n");
#endif
return nullptr;
}
std::generate(toBeFilled, toBeFilled + recordSize, [this](){ return getRandomAlphaNumeric(); });
byte * filled = toBeFilled;
toBeFilled += recordSize;
return filled;
}
byte * RandomBuffer::next ()
{
TRACE (false);
byte * nextRecord = fillRandomly();
// toBeRead is always the same as toBeFilled, since
// we generate random records on the fly
toBeRead = toBeFilled;
return nextRecord;
}
// Derived class: InFileBuffer
InFileBuffer::InFileBuffer (u_int32_t recordCount, RowSize recordSize, const string & inputPath):
Buffer(recordCount, recordSize)
{
TRACE (false);
_inputFile = ifstream (inputPath, std::ios::binary);
if (!_inputFile.is_open()) {
throw std::runtime_error("Input file not found");
}
}
InFileBuffer::~InFileBuffer ()
{
TRACE (false);
_inputFile.close();
}
bool InFileBuffer::isAlphaNumeric (byte c)
{
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
bool InFileBuffer::readRecordFromFile ()
{
u_int16_t numToRead = recordSize;
char c;
while (numToRead > 0) {
if (_inputFile.eof()) {
return false;
}
_inputFile.read(&c, sizeof(char));
if (isAlphaNumeric(c)) {
*toBeFilled = (byte) c;
++toBeFilled;
--numToRead;
}
}
return true;
}
byte * InFileBuffer::next ()
{
TRACE (false);
if (toBeRead >= _rowsEnd || toBeRead >= toBeFilled) {
// has exhausted all records previously read, need to read more
toBeRead = toBeFilled = _rows;
// get one more record
for (u_int16_t i = 0; i < recordCount; ++i) {
auto res = readRecordFromFile();
if (!res) {
break;
}
}
}
// still no data, return nullptr
if (toBeRead >= _rowsEnd || toBeRead >= toBeFilled) {
return nullptr;
}
// return the next record
byte * read = toBeRead;
toBeRead += recordSize;
return read;
}