-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition.C
120 lines (96 loc) · 3.06 KB
/
partition.C
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
#include <sys/types.h>
#include <functional>
#include <string.h>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
#include "partition.h"
// The Partition class splits a heap file into P partitions, using
// a hash function provided by the caller. The hash function must
// return an integer in the range 0 to P-1.
//
// Variable rel is a heap file that has already been opened by the
// caller. fileName is the (base) name of the heap file, and will be
// used as the base part of the partition file names which are of the
// form fileName.p where p is in the range 0 to P-1.
//
// Returns OK if heap file was split successfully, otherwise an error
// code is returned. If OK is returned, variable partName will return
// the names of the partition files. The caller can open the partition
// files as HeapFiles. The partition files are destroyed by the destructor
// of the Partition class.
Partition::Partition(HeapFileScan *rel,
const string &fileName,
const int P,
const int (*hashfcn)(const Record & record,
const int P),
string* &partName,
Status &status) :
P(P), partName(NULL)
{
InsertFileScan **part;
int p;
#ifdef DEBUGPART
cerr << "%% Partitioning " << fileName << "..." << endl;
#endif
// create list of partition heap files and file names
if (!(part = new InsertFileScan * [P]) || !(partName = new string[P])) {
status = INSUFMEM;
return;
}
// construct names of partition files (fileName.p where p = 0 to P-1)
// and create heap files on disk
for(p = 0; p < P; p++) {
stringstream s;
s << "/tmp/" << fileName << '.' << p << ends;
partName[p] = s.str();
if (!(part[p] = new InsertFileScan(partName[p], status))) {
status = INSUFMEM;
return;
}
if (status != OK)
return;
}
this->partName = partName;
// perform a sequential scan on the file to be partitioned, and
// for each record read, get its hash value (using hash function
// provided by the caller) and then insert the record into the
// corresponding partition file
if ((status = rel->startScan(0, sizeof(int), INTEGER, NULL,
EQ)) != OK)
return;
while(1) {
Record rec;
RID rid;
status = rel->scanNext(rid);
if (status != OK)
break;
if ((status = rel->getRecord(rec)) != OK)
return;
p = hashfcn(rec, P);
if ((status = part[p]->insertRecord(rec, rid)) != OK)
return;
}
if (status != OK && status != FILEEOF)
return;
// close partition files and deallocate memory
for(p = 0; p < P; p++)
delete part[p];
delete part;
if ((status = rel->endScan()) != OK)
return;
status = OK;
return;
}
// The destructor will destroy the heap files where partitions were stored.
Partition::~Partition()
{
if (!partName)
return;
for(int p = 0; p < P; p++) {
if (db.destroyFile(partName[p]) != OK)
cerr << "error destroying " << partName[p] << endl;
}
delete partName;
}