forked from dase/CLAIMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapColumn.h
executable file
·168 lines (141 loc) · 3.05 KB
/
HeapColumn.h
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
/*
* HeapColumn.h
*
* Created on: May 7, 2013
* Author: wangli
*/
#ifndef HeapColumn_H_
#define HeapColumn_H_
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string>
#include <sys/types.h>
#include <assert.h>
#include <iostream>
#include "Column_to_be_removed.h"
#include "ColumnAppenderBase.h"
using std::string;
using namespace std;
template<typename T>
class HeapColumn:public Column_TOBEREMOVED{
public:
HeapColumn(unsigned table_id,unsigned column_id)
:Column_TOBEREMOVED(table_id,column_id,sizeof(T))
{
char name[256];
sprintf(name,"/home/imdb/temp/%d_%d.column",table_id,column_id);
file_name=string(name);
};
void prt();
~HeapColumn(){};
unsigned get_type_length()
{
return sizeof(T);
}
class Appender:public ColumnAppenderBase{
public:
Appender(HeapColumn* h)
:heapColumn(h),fd(0),opened(false),record_length(sizeof(T)+sizeof(oid_t))
{
}
void p()
{
printf("%s\n",heapColumn->file_name.c_str());
}
bool open()
{
// fd=open(heapColumn->file_name.c_str(),O_APPEND|O_RDWR,0777);
// if(fd==-1)
// {
// printf("Cannot open column file %s, reason:%s",heapColumn->file_name.c_str(), errno);
// return false;
// }
//
// opened=true;
// return true;
assert(fd==0);
if((fd=fopen(heapColumn->file_name.c_str(),"ab"))==NULL)
{
printf("Cannot open column file %s!",heapColumn->file_name.c_str());
return false;
}
opened=true;
return true;
//write(fd,&c,1);
//close(fd);
}
bool append(void* src)
{
assert(opened==true);
assert(record_length!=0);
printf("oid=%d,value=%d",*(int*)src,*((int*)src+1));
printf("length=%d\n",record_length);
if(fwrite(src,record_length,1,fd)!=record_length)
{
printf("Cannot append this record!\n");
return false;
}
return true;
}
bool append(void* src,oid_t oid)
{
assert(opened==true);
assert(record_length!=0);
//printf("oid=%d,value=%d",oid,*(T *)src);
//cout<<"oid="<<oid<<",value="<<*(T *)src<<endl;
//printf("length=%d\n",record_length);
int tmp1,tmp2;
if(((tmp1=fwrite(src,record_length-sizeof(oid_t),1,fd))!=1)||((tmp2=fwrite(&oid, sizeof(oid_t),1,fd))!= 1))
{
printf("Cannot append this record!\n");
printf("Reason: %d\t%d\n",tmp1,tmp2);
return false;
}
heapColumn->record_count++;
return true;
}
void close()
{
fclose(fd);
}
private:
HeapColumn* heapColumn;
FILE* fd;
//int fd;
bool opened;
unsigned record_length;
};
ColumnAppenderBase* create_appender()
{
return new Appender(this);
}
private:
string file_name;
public:// will be private!
bool create_empty()
{
int fd;
fd=open(file_name.c_str(),O_CREAT|O_RDWR|O_TRUNC,00777);
//printf("fd=%d",fd);
if(fd==-1)
{
printf("Cannot create file: %s\n",file_name.c_str());
return false;
}
close(fd);
//printf("fd=%d",fd);
return true;
}
};
//#ifndef __HeapColumn_PT_INT__
//#define __HeapColumn_PT_INT__
//template<>
//void HeapColumn<int>::prt()
//{
// printf("INT!!!!\n");
//
//}
//#endif
#endif /* HeapColumn_H_ */