-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.C
More file actions
84 lines (61 loc) · 1.73 KB
/
Copy pathload.C
File metadata and controls
84 lines (61 loc) · 1.73 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
#include <unistd.h>
#include <fcntl.h>
#include "catalog.h"
#include "utility.h"
//
// Loads a file of (binary) tuples from a standard file into the relation.
// Any indices on the relation are updated appropriately.
//
// Returns:
// OK on success
// an error code otherwise
//
const Status UT_Load(const string & relation, const string & fileName)
{
Status status;
RelDesc rd;
AttrDesc *attrs;
int attrCnt;
if (relation.empty() || fileName.empty() || relation == string(RELCATNAME)
|| relation == string(ATTRCATNAME))
return BADCATPARM;
// open Unix data file
int fd;
if ((fd = open(fileName.c_str(), O_RDONLY, 0)) < 0)
return UNIXERR;
// get relation data
if ((status = relCat->getInfo(relation, rd)) != OK) return status;
// get attribute data
if ((status = attrCat->getRelInfo(rd.relName, attrCnt, attrs)) != OK)
return status;
// open data file
InsertFileScan* iFile = new InsertFileScan(rd.relName, status);
if (!iFile) return INSUFMEM;
if (status != OK) return status;
int records = 0;
// compute width of tuple and open index files, if any
int width = 0;
int i;
for(i = 0; i < attrCnt; i++) {
width += attrs[i].attrLen;
}
// create a record for constructing the tuple
char *record;
if (!(record = new char [width])) return INSUFMEM;
int nbytes;
Record rec;
while((nbytes = read(fd, record, width)) == width) {
RID rid;
rec.data = record;
rec.length = width;
if ((status = iFile->insertRecord(rec, rid)) != OK) return status;
records++;
}
cout << "Number of records inserted: " << records << endl;
// close heap file and data file
delete iFile;
if (close(fd) < 0) return UNIXERR;
delete [] record;
free(attrs);
return OK;
}