-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.cpp
426 lines (350 loc) · 13.6 KB
/
database.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*! \file
\brief Code to load .dsc files and store the information in objects
*/
#include "general.hpp"
#include "database.hpp"
#include "tokenizer.hpp"
#include "random.hpp"
#include <fstream>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>
#include <assert.h>
#include <iomanip>
using std::ifstream;
using std::find;
using std::string;
using std::ostringstream;
using std::cout;
using std::setw;
using std::for_each;
using std::fill;
void Relation::calc(Database & d)
{
System & s = d.system;
// amount of space that the relation would use if its occupancy were 1.0
int packedRelationSize = divCeil(numRecords,
s.blockSize / recordSize);
blockSize = (int)rint(packedRelationSize / occupancy);
}
int Relation::recordsInBuf(System & s, double bufferFrac, int reserve)
{
// buffer size to use (unit = 1 block)
int bufferSize = (int) ((double)(s.numBlocks - reserve) * bufferFrac);
int buffer = bufferSize * numRecords / blockSize;
if (buffer <= 0)
THROW_STREAM("Fraction of buffer to use on scan of relation " << fileNum
<< " is " << bufferFrac << ", which is not even big enough to hold a "
" single record.");
return buffer;
}
void Index::calc(Database & d)
{
System & s = d.system;
Relation & r = d.relations[relationNum];
if (conceptualType == Index::PRIMARY && physicalType == Index::BPLUS)
{
numKeys = d.relations[relationNum].blockSize;
}
else
{
numKeys = r.numRecords;
}
if (physicalType == Index::HASH)
{
keysPerNode = s.blockSize / (keySize + pointerSize);
if (keysPerNode < 1)
THROW_STREAM("Key size or pointer size is too big in index " << fileNum
<< ". If a node can't hold at least one pointer and one key it is "
"impossible to have a hash table.");
// parameter not used for hash tables
keysPerLeafNode = -1;
// amount of space that the index would use if its occupancy were 1.0
int packedIndexSize = divCeil(numKeys, keysPerNode);
blockSize = (int)rint((double)packedIndexSize / occupancy);
}
else if (physicalType == Index::BPLUS)
{
// assuming sizeof(pointer to node) == sizeof(pointer to tuple)
//
// subtract pointerSize because in internal nodes there is an extra pointer
keysPerNode = (s.blockSize - pointerSize) / (keySize + pointerSize);
if ((double)(1 + keysPerNode) * occupancy < 2.0)
THROW_STREAM("Key size or pointer size is too big or occupancy is too small in index " << fileNum
<< ". If at least two pointers can't fit in a node, it is impossible to have "
"a tree.");
// subtract 2 * pointersize to make room for doubly linked list pointers
keysPerLeafNode = (s.blockSize - 2 * pointerSize) / (keySize + pointerSize);
if (keysPerNode < 1)
THROW_STREAM("Key size or pointer size is too big in index " << fileNum
<< ". If three pointers and a key can't fit in a node, it is "
"impossible to have a B+ tree leaf.");
int numChildren = (int)rint((double)divCeil(numKeys, keysPerLeafNode) / occupancy);
treeLevels.push_back(numChildren);
blockSize = numChildren;
do
{
numChildren = (int)ceil((double)numChildren / (double) (keysPerNode+1) / occupancy);
treeLevels.push_back(numChildren);
blockSize += numChildren;
}
while (numChildren > 1);
// using do-while instead of while because I'm assuming there must be at
// least one non-leaf node even if all keys fit in one leaf
assert(treeLevels.size() > 1);
}
else
assert(false);
}
ParseException::ParseException(string inputFile_, Tokenizer & t, string description_,
char const * file_, int line_)
: inputFile(inputFile_), inputLine(t.lineNo), inputToken(t.token),
description(description_), Exception(file_, line_)
{
ostringstream s;
s << "Error parsing " << inputFile_ << " near ";
if (t.token.length() == 0 && t.in.peek() == EOF)
s << "EOF";
else
s << "'" << t.token << "'";
s << " at line " << inputLine << endl << description << endl;
message = s.str();
};
System::System(char const * filename)
{
ifstream f(filename);
if (!f.is_open()) THROW_STREAM("Failed to open " << filename << endl);
Tokenizer t(f);
bool eof;
eof = !t.next();
blockSize = t.getInt();
if (blockSize <= 0 || eof)
throw ParseException(filename, t, "Expecting block size > 0.", __FILE__, __LINE__);
eof = !t.next();
transferTime = t.getDouble();
if (transferTime < 0 || eof)
throw ParseException(filename, t, "Expecting block transfer time >= 0.", __FILE__, __LINE__);
eof = !t.next();
rotationalLatency = t.getDouble();
if (rotationalLatency < 0 || eof)
throw ParseException(filename, t, "Expecting rotational latency time >= 0.", __FILE__, __LINE__);
eof = !t.next();
seekTime = t.getDouble();
if (seekTime < 0 || eof)
throw ParseException(filename, t, "Expecting seek time >= 0.", __FILE__, __LINE__);
eof = !t.next();
numBlocks = t.getInt();
if (numBlocks < 0 || eof)
throw ParseException(filename, t, "Expecting buffer size >= 0.", __FILE__, __LINE__);
if (t.next())
throw ParseException(filename, t, "Expecting end of file.", __FILE__, __LINE__);
}
Database::Database(char const * filename, System & system_)
: system(system_)
{
ifstream f(filename);
if (!f.is_open()) THROW_STREAM("Failed to open " << filename << endl);
bool eof = false;
Tokenizer t(f);
while(t.next())
{
int fileNum = t.getInt();
if (fileNum <=0 || eof)
throw ParseException(filename, t, "Expecting file number greater than 0.", __FILE__, __LINE__);
if (find(relations.begin(), relations.end(), fileNum) != relations.end()
|| find(indices.begin(), indices.end(), fileNum) != indices.end())
throw ParseException(filename, t, "Duplicate file number", __FILE__, __LINE__);
bool eof = !t.next();
if (t.token == "relation")
{
relations.resize(relations.size()+1);
Relation & r = relations.back();
r.fileNum = fileNum;
eof = !t.next();
r.recordSize = t.getInt();
if (r.recordSize <= 0 || eof)
throw ParseException(filename, t, "Expecting relation record size > 0", __FILE__, __LINE__);
if (r.recordSize > system.blockSize)
throw ParseException(filename, t, "Expecting relation record size < block size", __FILE__, __LINE__);
eof = !t.next();
r.numRecords = t.getInt();
if (r.numRecords <= 0 || eof)
throw ParseException(filename, t, "Expecting at least one record", __FILE__, __LINE__);
eof = !t.next();
r.occupancy = t.getDouble();
if (r.occupancy <= 0 || r.occupancy > 1)
throw ParseException(filename, t, "Expecting occupied fraction to be in (0,1]", __FILE__, __LINE__);
r.calc(*this);
}
else if (t.token == "index")
{
indices.resize(indices.size()+1);
Index & i = indices.back();
i.fileNum = fileNum;
i.relationNum = relations.size() - 1;
if (i.relationNum < 0)
throw ParseException(filename, t, "At least on relation must appear before an index", __FILE__, __LINE__);
t.next();
if (t.token == "primary")
i.conceptualType = Index::PRIMARY;
else if (t.token == "secondary")
i.conceptualType = Index::SECONDARY;
else
throw ParseException(filename, t, "Expecting either 'primary' or 'secondary'", __FILE__, __LINE__);
eof = !t.next();
i.keySize = t.getInt();
if (i.keySize <= 0 || eof)
throw ParseException(filename, t, "Expecting key size > 0", __FILE__, __LINE__);
eof = !t.next();
i.pointerSize = t.getInt();
if (i.pointerSize <= 0 || eof)
throw ParseException(filename, t, "Expecting pointer size > 0", __FILE__, __LINE__);
t.next();
if (t.token == "b+tree")
i.physicalType = Index::BPLUS;
else if (t.token == "hash")
i.physicalType = Index::HASH;
else
throw ParseException(filename, t, "Expecting either 'b+tree' or 'hash'", __FILE__, __LINE__);
eof = !t.next();
i.occupancy = t.getDouble();
if (i.occupancy <= 0 || i.occupancy > 1)
throw ParseException(filename, t, "Expecting occupied fraction to be in (0,1]", __FILE__, __LINE__);
i.calc(*this);
}
else
throw ParseException(filename, t, "Expecting either 'relation' or 'index'", __FILE__, __LINE__);
eof = !t.next();
if (eof || t.getInt() != -1)
throw ParseException(filename, t, "Expecting sentinel -1 value", __FILE__, __LINE__);
}
};
/*!
\param priority 0 is low, 99 is high
*/
void Database::read(int fileNum, int startBlock, int endBlock, int priority)
{
assert(-1 <= priority && priority <= 99);
cout << "read\t" << fileNum
<< '\t' << startBlock
<< '\t' << endBlock
<< '\t' << priority
<< '\n';
};
Access::Access(char const * filename, Database & database_)
: database(database_)
{
ifstream f(filename);
if (!f.is_open()) THROW_STREAM("Failed to open " << filename << endl);
Tokenizer t(f);
bool eof = !t.next();
numOperations = t.getInt();
if (numOperations <= 0 || eof)
throw ParseException(filename, t, "Expecting number of operations to be > 0", __FILE__, __LINE__);
vector<Relation> const & r = database.relations;
vector<Index> const & i = database.indices;
double sum = 0;
while(t.next())
{
double prob = t.getDouble();
if (prob <= 0 || prob > 1)
throw ParseException(filename, t, "Expecting probability between 0 and 1", __FILE__, __LINE__);
sum += prob;
probs.push_back(sum);
t.next();
if (t.token == "scan")
{
Scan * scan;
operations.push_back(scan = new Scan());
eof = !t.next();
scan->relationNum = find(r.begin(), r.end(), t.getInt()) - r.begin();
if (scan->relationNum >= r.size() || eof)
throw ParseException(filename, t, "Relation not found", __FILE__, __LINE__);
eof = !t.next();
scan->useBuffer = t.getDouble();
if (scan->useBuffer <= 0 || scan->useBuffer > 1 || eof)
throw ParseException(filename, t, "Expecting fraction of buffer to use between 0 and 1", __FILE__, __LINE__);
}
else if (t.token == "lookup")
{
Lookup * lookup;
operations.push_back(lookup = new Lookup());
eof = !t.next();
lookup->indexNum = find(i.begin(), i.end(), t.getInt()) - i.begin();
if (lookup->indexNum >= i.size() || eof)
throw ParseException(filename, t, "Index not found", __FILE__, __LINE__);
}
else if (t.token == "block")
{
BlockJoin * blockJoin;
operations.push_back(blockJoin = new BlockJoin());
eof = !t.next();
blockJoin->outerRelation = find(r.begin(), r.end(), t.getInt()) - r.begin();
if (blockJoin->outerRelation >= r.size() || eof)
throw ParseException(filename, t, "Relation not found", __FILE__, __LINE__);
eof = !t.next();
blockJoin->innerRelation = find(r.begin(), r.end(), t.getInt()) - r.begin();
if (blockJoin->innerRelation >= r.size() || eof)
throw ParseException(filename, t, "Relation not found", __FILE__, __LINE__);
eof = !t.next();
blockJoin->outerBuffer = t.getDouble();
if (blockJoin->outerBuffer < 0 || blockJoin->outerBuffer > 1 || eof)
throw ParseException(filename, t, "Expecting fraction of buffer to use between 0 and 1", __FILE__, __LINE__);
eof = !t.next();
blockJoin->innerBuffer = t.getDouble();
double total = blockJoin->innerBuffer + blockJoin->outerBuffer;
if (equals(total,1.0)) total = 1.0;
if (equals(total,0.0)) total = 0.0;
if (total < 0.0 || total > 1.0 || eof)
throw ParseException(filename, t, "Expected inner buffer + outer buffer usage to be between 0 and 1", __FILE__, __LINE__);
}
else if (t.token == "indexloop")
{
IndexLoop * indexLoop;
operations.push_back(indexLoop = new IndexLoop());
eof = !t.next();
indexLoop->relationNum = find(r.begin(), r.end(), t.getInt()) - r.begin();
if (indexLoop->relationNum >= r.size() || eof)
throw ParseException(filename, t, "Relation not found", __FILE__, __LINE__);
eof = !t.next();
indexLoop->indexNum = find(i.begin(), i.end(), t.getInt()) - i.begin();
if (indexLoop->indexNum >= i.size() || eof)
throw ParseException(filename, t, "Index not found", __FILE__, __LINE__);
eof = !t.next();
indexLoop->matches = t.getDouble();
if (indexLoop->matches < 0 || eof)
throw ParseException(filename, t, "Expecting average number of matches not less than zero", __FILE__, __LINE__);
Index const & in = i[indexLoop->indexNum];
if (in.conceptualType == Index::PRIMARY && !equals(indexLoop->matches, 1.0))
WARN_STREAM("Index " << in.fileNum << " is a primary index, so you can "
"not expect the average number of matches to be " << indexLoop->matches
<< ". This value will be ignored." << endl);
}
else
throw ParseException(filename, t, "Expecting 'scan', 'lookup', 'block', or 'indexloop'", __FILE__, __LINE__);
};
if (!equals(sum,1.0))
THROW_STREAM("Sum of probabilities for operations is " << sum << ", should be 1.0" << endl);
assert(operations.size() == probs.size());
}
void Access::simulateOperations(System & system)
{
opCount.resize(operations.size());
fill(opCount.begin(), opCount.end(), 0);
for (int i = 0; i < numOperations; ++i)
{
int r = RandomDiscrete(probs);
assert(0 <= r && r < operations.size());
//
++opCount[r];
Operation & op = *operations[r];
op(database);
}
//for(int j = 0; j < opCount.size(); ++j)
// cerr << "op " << j << " count = " << opCount[j] << endl;
}
Access::~Access()
{
for_each(operations.begin(), operations.end(), DeletePointer<Operation *>());
}