-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecord.cc
399 lines (318 loc) · 11.6 KB
/
Record.cc
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
#include "Record.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
Record::Record() {
bits = NULL;
}
Record::Record(Schema *schema) {
bits = NULL;
schema = schema;
}
Record::~Record() {
if (bits != NULL) {
delete[] bits;
}
bits = NULL;
}
int Record::SuckNextRecord(Schema *mySchema, FILE *textFile) {
// this is temporary storage
char *space = new (std::nothrow) char[PAGE_SIZE];
if (space == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
char *recSpace = new (std::nothrow) char[PAGE_SIZE];
if (recSpace == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
// clear out the present record
if (bits != NULL)
delete[] bits;
bits = NULL;
int n = mySchema->GetNumAtts();
Attribute *atts = mySchema->GetAtts();
// this is the current position (int bytes) in the binary
// representation of the record that we are dealing with
int currentPosInRec = sizeof(int) * (n + 1);
// loop through all of the attributes
for (int i = 0; i < n; i++) {
// first we suck in the next attribute value
int len = 0;
while (1) {
int nextChar = getc(textFile);
if (nextChar == '|')
break;
else if (nextChar == EOF) {
delete[] space;
delete[] recSpace;
return 0;
}
space[len] = nextChar;
len++;
}
// set up the pointer to the current attribute in the record
((int *)recSpace)[i + 1] = currentPosInRec;
// null terminate the string
space[len] = 0;
len++;
// then we convert the data to the correct binary representation
if (atts[i].myType == Int) {
*((int *)&(recSpace[currentPosInRec])) = atoi(space);
currentPosInRec += sizeof(int);
} else if (atts[i].myType == Double) {
// make sure that we are starting at a double-aligned position;
// if not, then we put some extra space in there
while (currentPosInRec % sizeof(double) != 0) {
currentPosInRec += sizeof(int);
((int *)recSpace)[i + 1] = currentPosInRec;
}
*((double *)&(recSpace[currentPosInRec])) = atof(space);
currentPosInRec += sizeof(double);
} else if (atts[i].myType == String) {
// align things to the size of an integer if needed
if (len % sizeof(int) != 0) {
len += sizeof(int) - (len % sizeof(int));
}
strcpy(&(recSpace[currentPosInRec]), space);
currentPosInRec += len;
}
}
// the last thing is to set up the pointer to just past the end of the reocrd
((int *)recSpace)[0] = currentPosInRec;
// and copy over the bits
bits = new (std::nothrow) char[currentPosInRec];
if (bits == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
memcpy(bits, recSpace, currentPosInRec);
delete[] space;
delete[] recSpace;
return 1;
}
void Record::SetBits(char *bits) {
delete[] this->bits;
this->bits = bits;
}
char *Record::GetBits(void) {
return bits;
}
void Record::CopyBits(char *bits, int b_len) {
delete[] this->bits;
this->bits = new (std::nothrow) char[b_len];
if (this->bits == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
memcpy(this->bits, bits, b_len);
}
void Record::Consume(Record *fromMe) {
delete[] bits;
bits = fromMe->bits;
fromMe->bits = NULL;
}
void Record::Copy(Record *copyMe) {
// this is a deep copy, so allocate the bits and move them over!
delete[] bits;
bits = new (std::nothrow) char[((int *)copyMe->bits)[0]];
if (bits == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
memcpy(bits, copyMe->bits, ((int *)copyMe->bits)[0]);
}
void Record::Project(int *attsToKeep, int numAttsToKeep, int numAttsNow) {
// first, figure out the size of the new record
int totSpace = sizeof(int) * (numAttsToKeep + 1);
for (int i = 0; i < numAttsToKeep; i++) {
// if we are keeping the last record, be careful!
if (attsToKeep[i] == numAttsNow - 1) {
// in this case, take the length of the record and subtract the start pos
totSpace += ((int *)bits)[0] - ((int *)bits)[attsToKeep[i] + 1];
} else {
// in this case, subtract the start of the next field from the start of this field
totSpace += ((int *)bits)[attsToKeep[i] + 2] - ((int *)bits)[attsToKeep[i] + 1];
}
}
// now, allocate the new bits
char *newBits = new (std::nothrow) char[totSpace];
if (newBits == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
// record the total length of the record
*((int *)newBits) = totSpace;
// and copy all of the fields over
int curPos = sizeof(int) * (numAttsToKeep + 1);
for (int i = 0; i < numAttsToKeep; i++) {
// this is the length (in bytes) of the current attribute
int attLen;
// if we are keeping the last record, be careful!
if (attsToKeep[i] == numAttsNow - 1) {
// in this case, take the length of the record and subtract the start pos
attLen = ((int *)bits)[0] - ((int *)bits)[attsToKeep[i] + 1];
} else {
// in this case, subtract the start of the next field from the start of this field
attLen = ((int *)bits)[attsToKeep[i] + 2] - ((int *)bits)[attsToKeep[i] + 1];
}
// set the start position of this field
((int *)newBits)[i + 1] = curPos;
// and copy over the bits
memcpy(&(newBits[curPos]), &(bits[((int *)bits)[attsToKeep[i] + 1]]), attLen);
// note that we are moving along in the record
curPos += attLen;
}
// kill the old bits
delete[] bits;
// and attach the new ones
bits = newBits;
}
// consumes right record and leaves the left record as it is
void Record::MergeRecords(Record *left, Record *right, int numAttsLeft, int numAttsRight, int *attsToKeep, int numAttsToKeep,
int startOfRight) {
delete[] bits;
bits = NULL;
// if one of the records is empty, new record is non-empty record
if (numAttsLeft == 0) {
Copy(right);
return;
} else if (numAttsRight == 0) {
Copy(left);
return;
}
// first, figure out the size of the new record
int totSpace = sizeof(int) * (numAttsToKeep + 1);
int numAttsNow = numAttsLeft;
char *rec_bits = left->bits;
for (int i = 0; i < numAttsToKeep; i++) {
if (i == startOfRight) {
numAttsNow = numAttsRight;
rec_bits = right->bits;
}
// if we are keeping the last record, be careful!
if (attsToKeep[i] == numAttsNow - 1) {
// in this case, take the length of the record and subtract the start pos
totSpace += ((int *)rec_bits)[0] - ((int *)rec_bits)[attsToKeep[i] + 1];
} else {
// in this case, subtract the start of the next field from the start of this field
totSpace += ((int *)rec_bits)[attsToKeep[i] + 2] - ((int *)rec_bits)[attsToKeep[i] + 1];
}
}
// now, allocate the new bits
bits = new (std::nothrow) char[totSpace + 1];
if (bits == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
// record the total length of the record
*((int *)bits) = totSpace;
numAttsNow = numAttsLeft;
rec_bits = left->bits;
// and copy all of the fields over
int curPos = sizeof(int) * (numAttsToKeep + 1);
for (int i = 0; i < numAttsToKeep; i++) {
if (i == startOfRight) {
numAttsNow = numAttsRight;
rec_bits = right->bits;
}
// this is the length (in bytes) of the current attribute
int attLen;
// if we are keeping the last record, be careful!
if (attsToKeep[i] == numAttsNow - 1) {
// in this case, take the length of the record and subtract the start pos
attLen = ((int *)rec_bits)[0] - ((int *)rec_bits)[attsToKeep[i] + 1];
} else {
// in this case, subtract the start of the next field from the start of this field
attLen = ((int *)rec_bits)[attsToKeep[i] + 2] - ((int *)rec_bits)[attsToKeep[i] + 1];
}
// set the start position of this field
((int *)bits)[i + 1] = curPos;
// and copy over the bits
memmove(&(bits[curPos]), &(rec_bits[((int *)rec_bits)[attsToKeep[i] + 1]]), attLen);
// note that we are moving along in the record
curPos += attLen;
}
}
void Record::Print(Schema *mySchema) {
int n = mySchema->GetNumAtts();
Attribute *atts = mySchema->GetAtts();
// loop through all of the attributes
for (int i = 0; i < n; i++) {
// print the attribute name
cout << atts[i].name << ": ";
// use the i^th slot at the head of the record to get the
// offset to the correct attribute in the record
int pointer = ((int *)bits)[i + 1];
// here we determine the type, which given in the schema;
// depending on the type we then print out the contents
cout << "[";
// first is integer
if (atts[i].myType == Int) {
int *myInt = (int *)&(bits[pointer]);
cout << *myInt;
// then is a double
} else if (atts[i].myType == Double) {
double *myDouble = (double *)&(bits[pointer]);
cout << *myDouble;
// then is a character string
} else if (atts[i].myType == String) {
char *myString = (char *)&(bits[pointer]);
cout << myString;
}
cout << "]";
// print out a comma as needed to make things pretty
if (i != n - 1) {
cout << ", ";
}
}
cout << "\n";
}
string Record::ToString(Schema *mySchema) {
int n = mySchema->GetNumAtts();
Attribute *atts = mySchema->GetAtts();
string temp = "";
// loop through all of the attributes
for (int i = 0; i < n; i++) {
// use the i^th slot at the head of the record to get the
// offset to the correct attribute in the record
int pointer = ((int *)bits)[i + 1];
// here we determine the type, which given in the schema;
// depending on the type we then print out the contents
// first is integer
if (atts[i].myType == Int) {
int *myInt = (int *)&(bits[pointer]);
temp += to_string(*myInt);
// then is a double
} else if (atts[i].myType == Double) {
double *myDouble = (double *)&(bits[pointer]);
string t = to_string(*myDouble);
t.erase(t.find_last_not_of('0') + 1, std::string::npos);
t.erase(t.find_last_not_of('.') + 1, std::string::npos);
temp += t;
// then is a character string
} else if (atts[i].myType == String) {
char *myString = (char *)&(bits[pointer]);
temp += string(myString);
}
temp += "|";
}
// cout << "Rec txt: " << temp << endl;
// s = temp.c_str();
return temp;
}
int Record::NumberOfAtts() {
int num = (((int *)bits)[1] - 4) / 4;
return num;
}
void Record::ClearRecord() {
if (bits != NULL) {
delete[] bits;
}
bits = NULL;
}
bool Record::IsRecordEmpty() {
return bits == NULL;
}