-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataProcessor.java
More file actions
277 lines (248 loc) · 8.47 KB
/
Copy pathDataProcessor.java
File metadata and controls
277 lines (248 loc) · 8.47 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
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
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
class DataProcessor {
private static final String RAW = "indexbi.bin", LINK = "links.bin", OFFSET = "offset.bin",
SORT = "sorted.bin", FINAL = "final.bin", RANK_OFFSET = "rank_offset.txt";
private static HashMap<Integer, Integer> off2idx;
private static long start, end;
private static int pageNum;
private static int getLittleEndian(byte[] b) {
return ((b[3]&0xff)<<24)+((b[2]&0xff)<<16)+((b[1]&0xff)<<8)+(b[0]&0xff);
}
private static void mapOffset2Idx() {
try {
FileChannel in = new FileInputStream(RAW).getChannel();
MappedByteBuffer mbb = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
FileOutputStream fos = new FileOutputStream(OFFSET);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
byte[] buffer = new byte[4];
mbb.position(4); //skip version number
mbb.get(buffer);
pageNum = getLittleEndian(buffer);
int offset = 16; //first page
int linkNum;
for (int i=0; i<pageNum; i++) {
off2idx.put(offset, i);
dos.writeInt(offset);
mbb.position(offset + 4); //number of links is next
mbb.get(buffer);
linkNum = getLittleEndian(buffer);
offset += (4 + linkNum) * 4; // 4 ints of meta data + links
}
if (offset == in.size()) System.out.println("Map offset success!"); // check if offset tallies
else System.out.println("Map offset failed " + offset);
in.close();
fos.close();
} catch (Exception e) {
System.out.println(e);
}
}
private static void sortLinks() {
try {
FileChannel in = new FileInputStream(LINK).getChannel();
MappedByteBuffer mbb = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
FileOutputStream fos = new FileOutputStream(SORT);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
int numPage = mbb.getInt();
dos.writeInt(numPage);
int numLink, currPage;
int[] outLinks;
for (int i=0; i<numPage; i++) {
currPage = mbb.getInt();
if (currPage != i) {
System.out.println("Sort link failure i = " + i + " page = " +currPage);
in.close();
dos.close();
System.exit(-1);
}
dos.writeInt(currPage);
numLink = mbb.getInt();
dos.writeInt(numLink);
outLinks = new int[numLink];
for (int j=0; j<numLink; j++) outLinks[j] = mbb.getInt();
Arrays.sort(outLinks);
for (int j=0; j<numLink; j++) dos.writeInt(outLinks[j]);
}
in.close();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void indexLinks() {
try {
FileChannel in = new FileInputStream(RAW).getChannel();
MappedByteBuffer mbb = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
FileOutputStream fos = new FileOutputStream(LINK);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(pageNum);
mbb.position(16); //start of first page
byte[] buffer = new byte[4];
int linkNum, offset, idx;
for (int i=0; i<pageNum; i++) {
dos.writeInt(i);
mbb.position(mbb.position() + 4); // skip the 0
mbb.get(buffer);
linkNum = getLittleEndian(buffer);
dos.writeInt(linkNum);
mbb.position(mbb.position() + 8); // skip B & M
for (int j=0; j<linkNum; j++) {
mbb.get(buffer);
offset = getLittleEndian(buffer);
idx = off2idx.get(offset);
dos.writeInt(idx);
}
}
if (mbb.position() == in.size()) System.out.println("Index links success!");
else System.out.println("Index links failed " + mbb.position());
in.close();
dos.close();
} catch (Exception e) {
System.out.println(e);
}
}
/** For debug */
private static void readBinaryMapped(String file, int num) {
try {
FileChannel in = new FileInputStream(file).getChannel();
MappedByteBuffer mbb = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
for (int i=0; i<num; i++) mbb.getInt(4*i);
in.close();
} catch (Exception e) {
System.out.println(e);
}
}
private static void blkLinks() {
try {
FileChannel in = new FileInputStream(SORT).getChannel();
MappedByteBuffer mbb = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
FileOutputStream[] fos = new FileOutputStream[9];
BufferedOutputStream[] bos = new BufferedOutputStream[9];
DataOutputStream[] dos = new DataOutputStream[9];
ArrayList<Integer>[] outLinks = new ArrayList[9];
for (int i=0; i<9; i++) {
fos[i] = new FileOutputStream(i + ".bin");
bos[i] = new BufferedOutputStream(fos[i]);
dos[i] = new DataOutputStream(bos[i]);
}
int numPage = mbb.getInt();
for (int i=0; i<9; i++) {
dos[i].writeInt(numPage);
}
int numLink, currPage;
for (int i=0; i<numPage; i++) {
currPage = mbb.getInt();
if (currPage != i) {
System.out.println("blk link failure i = " + i + " page = " +currPage);
closeAndExit(in, dos);
}
for (int j=0; j<9; j++) {
dos[j].writeInt(currPage);
}
numLink = mbb.getInt();
int dstPage;
int fileIdx = 0, accLink = 0;
for (int j=0; j<9; j++) outLinks[j] = new ArrayList<Integer>();
for (int j=0; j<numLink; j++) {
dstPage = mbb.getInt();
outLinks[dstPage/1000000].add(dstPage);
}
for (int j=0; j<9; j++) {
accLink += outLinks[j].size();
dos[j].writeInt(outLinks[j].size());
for (int l : outLinks[j]) dos[j].writeInt(l);
}
if (accLink != numLink) {
System.out.println("blk link failure currPage = " + currPage + " numLink = " + numLink + " accLink " + accLink);
closeAndExit(in, dos);
}
}
in.close();
for (int i=0; i<9; i++) {
dos[i].close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void closeAndExit(FileChannel in, DataOutputStream[] dos) {
try {
in.close();
for (int i=0; i<9; i++) {
dos[i].close();
}
System.exit(-1);
} catch (Exception e) {
e.printStackTrace();
}
}
private static class Pair implements Comparable<Pair> {
int p;
double s;
public Pair(int p, double s) {
this.p = p;
this.s = s;
}
@Override
public int compareTo(Pair o) {
if (s > o.s) return -1;
if (s == o.s) return 0;
else return 1;
}
}
private static void getTop() {
try {
FileChannel in = new FileInputStream(FINAL).getChannel();
MappedByteBuffer mbb = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
int numPage = (int)in.size() / 8;
Pair[] scores = new Pair[numPage];
int maxPage = -1, page = -1;
double maxScore = 0, score;
for (int i=0; i<numPage; i++) {
score = mbb.getDouble();
scores[i] = new Pair(i, score);
if (score > maxScore) {
maxScore = score;
maxPage = i;
}
}
in.close();
Arrays.sort(scores);
if (maxScore != scores[0].s) {
System.out.println("max score " + maxScore + "scores " + scores[0].s);
System.exit(-1);
}
in = new FileInputStream(OFFSET).getChannel();
mbb = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
FileWriter fw = new FileWriter(RANK_OFFSET);
for (int i=0; i<1000; i++)
fw.write("SELECT title from pages where offset = " + mbb.getInt(scores[i].p * 4) + ";\n");
in.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String[] args) {
/*off2idx = new HashMap<Integer, Integer>();
start = System.currentTimeMillis();
mapOffset2Idx();
end = System.currentTimeMillis();
System.out.println("Map offset took "+ (end-start) + " ms");
start = System.currentTimeMillis();
indexLinks();
end = System.currentTimeMillis();
System.out.println("Index links took "+ (end-start) + " ms");
sortLinks();*/
start = System.currentTimeMillis();
blkLinks();
end = System.currentTimeMillis();
System.out.println("Blk links took "+ (end-start) + " ms");
//getTop();
}
}