-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartition.cc
218 lines (198 loc) · 5.4 KB
/
partition.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
#include <errno.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <limits.h>
//#include "assoc.h"
#include "partition.h"
using namespace std;
struct timeval tp;
int num_partitions = 1;
int *DATAFD, *IDXFD, *IDXFLEN, **ITEMIDX;
void partition_alloc(char *dataf, char *idxf)
{
DATAFD = new int[num_partitions];
IDXFD = new int[num_partitions];
IDXFLEN = new int[num_partitions];
ITEMIDX = new int*[num_partitions];
char tmpnam[300];
for (int i=0; i < num_partitions; i++){
if (num_partitions > 1) sprintf(tmpnam, "%s.P%d", dataf, i);
else sprintf(tmpnam, "%s", dataf);
DATAFD[i] = open(tmpnam, O_RDONLY);
if (DATAFD[i] < 0){
perror("can't open data file");
exit(errno);
}
if (num_partitions > 1) sprintf(tmpnam, "%s.P%d", idxf, i);
else sprintf(tmpnam, "%s", idxf);
IDXFD[i] = open(tmpnam, O_RDONLY);
if (IDXFD[i] < 0){
perror("can't open idx file");
exit(errno);
}
IDXFLEN[i] = lseek(IDXFD[i],0,SEEK_END);
lseek(IDXFD[i],0,SEEK_SET);
#ifdef DEC
//cout << "IDXLEN " << IDXFLEN[0] << endl;
//long pgsz = sysconf(_SC_PAGE_SIZE);
//long fl = ((IDXFLEN[i]+pgsz-1)/pgsz)*pgsz;
ITEMIDX[i] = (int *) mmap((char *)NULL, IDXFLEN[i], PROT_READ,
(MAP_FILE|MAP_VARIABLE|MAP_PRIVATE),
IDXFD[i], 0);
#else
ITEMIDX[i] = (int *) mmap((char *)NULL, IDXFLEN[i], PROT_READ,
MAP_PRIVATE,IDXFD[i], 0);
#endif
if (ITEMIDX[i] == (int *)-1){
perror("MMAP ERROR:item_idx");
exit(errno);
}
}
}
void partition_dealloc()
{
for (int i=0; i < num_partitions; i++){
close(DATAFD[i]);
close(IDXFD[i]);
munmap((caddr_t)ITEMIDX[i], IDXFLEN[i]);
}
delete [] DATAFD;
delete [] IDXFD;
delete [] IDXFLEN;
delete [] ITEMIDX;
}
//partition p's size
int partition_get_blk_sz(int p)
{
return lseek(DATAFD[p],0,SEEK_END);
}
//maximum partition size
int partition_get_max_blksz()
{
int max = 0;
int flen;
for (int i=0; i < num_partitions; i++){
flen = lseek(DATAFD[i],0,SEEK_END);
if (max < flen) max = flen;
}
return max;
}
//read the whole partition into MAINBUF
void partition_get_blk(int *MAINBUF, int p)
{
int flen = lseek(DATAFD[p],0,SEEK_END);
cout << "FILESZ " << flen << endl;
lseek(DATAFD[p],0,SEEK_SET);
if (read(DATAFD[p], (char *)MAINBUF, flen) < 0){
perror("read item1");
exit(errno);
}
}
//support over all partitions
int partition_get_idxsup(int it)
{
int supsz = 0;
//cout << "IT " << it << " " << ITEMIDX[0][it+1]-ITEMIDX[0][it] << endl;
for (int i=0; i < num_partitions; i++){
supsz += partition_get_lidxsup(i, it);
}
return supsz;
}
//support only in the local partition
int partition_get_lidxsup(int idx, int it)
{
int i, sup = 0;
if (ITEMIDX[idx][it] != -1){
i = it+1;
while (ITEMIDX[idx][i] == -1) i++;
sup = ITEMIDX[idx][i]-ITEMIDX[idx][it];
}
return sup;
}
//item's index
int partition_get_idx(int idx, int it)
{
return ITEMIDX[idx][it];
}
//return index array
int *partition_idx(int idx)
{
return ITEMIDX[idx];
}
int partition_idxval(int it)
{
for (int i=0; i < num_partitions; i++){
if (ITEMIDX[i][it] > -1) return 1;
}
return -1;
}
//read item it from all partitions
void partition_read_item(int *ival, int it)
{
int ipos=0;
int supsz;
for (int i=0; i < num_partitions; i++){
supsz = partition_get_lidxsup(i, it);
if (supsz > 0){
lseek(DATAFD[i], ITEMIDX[i][it]*ITSZ, SEEK_SET);
if (read(DATAFD[i], (char *)&ival[ipos], supsz*ITSZ) < 0){
perror("read item1");
exit(errno);
}
ipos+=supsz;
}
}
}
//read item it only from local partition
void partition_lclread_item(int *ival, int pnum, int it)
{
int supsz;
supsz = partition_get_lidxsup(pnum, it);
if (supsz > 0){
lseek(DATAFD[pnum], ITEMIDX[pnum][it]*ITSZ, SEEK_SET);
if (read(DATAFD[pnum], (char *)ival, supsz*ITSZ) < 0){
perror("read item1");
exit(errno);
}
}
}
//return the max and min transcation id for partition pnum
void partition_get_minmaxtid(int pnum, int it, int &minv, int &maxv)
{
int tid, supsz;
supsz = partition_get_lidxsup(pnum, it);
if (supsz > 0){
lseek(DATAFD[pnum], ITEMIDX[pnum][it]*ITSZ, SEEK_SET);
read(DATAFD[pnum], (char *)&tid, ITSZ);
if (minv > tid) minv = tid;
lseek(DATAFD[pnum], (supsz-2)*ITSZ, SEEK_CUR);
read(DATAFD[pnum], (char *)&tid, ITSZ);
if (maxv < tid) maxv = tid;
}
}
void partition_get_minmaxcustid(int *backidx, int numit, int pnum,
int &minv, int &maxv)
{
int custid, it, i, supsz;
minv = INT_MAX;
maxv = 0;
for (i=0; i < numit; i++){
it = backidx[i];
supsz = ITEMIDX[pnum][it+1]-ITEMIDX[pnum][it];
if (supsz > 0){
lseek(DATAFD[pnum], ITEMIDX[pnum][it]*sizeof(int), SEEK_SET);
read(DATAFD[pnum], (char *)&custid, sizeof(int));
if (minv > custid) minv = custid;
lseek(DATAFD[pnum], (supsz-3)*sizeof(int), SEEK_CUR);
read(DATAFD[pnum], (char *)&custid, sizeof(int));
if (maxv < custid) maxv = custid;
}
}
}