-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsb_tree.cpp
356 lines (295 loc) · 10.4 KB
/
sb_tree.cpp
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
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <math.h>
#include "divsufsort64.h"
#include "sb_tree.h"
#include "sb_util.h"
#include "critbit_tree.h"
#include <sdsl/bitmagic.hpp>
using namespace sdsl;
/* disk layout description of the index file:
0-4095 : [B][b][n][empty space]
4096-B+4096 : root disk page (B bytes)
followed by : [ rest of the SB-tree internal pages]
followed by : [ suffix array leaf pages ]
therefore: root page always at file offset 4096.
*/
/* creates the suffix array for a given text and creates the SB-tree ontop of that */
sbtree_t*
sbtree_create(const char* text_file,const char* outfile,uint64_t B)
{
char sa_file[256];
/* load text file */
fprintf(stderr, "LOADING TEXT\n");
uint64_t n = sb_getfilesize(text_file);;
uint8_t* T = (uint8_t*) sb_malloc(n);
FILE* t_in = fopen(text_file,"r");
if (fread(T,1,n,t_in) != n) {
fprintf(stderr, "error reading input file '%s'\n",text_file);
exit(EXIT_FAILURE);
}
fclose(t_in);
/* create sa */
fprintf(stderr, "CREATING SA\n");
uint64_t* SA = (uint64_t*) sb_malloc(n*sizeof(uint64_t));
if (divsufsort64(T,(saidx64_t*)SA,n) != 0) {
fprintf(stderr, "error creating suffix array\n");
exit(EXIT_FAILURE);
}
free(T);
/* store sa to disk */
strcpy(sa_file,outfile);
strcat(sa_file,".saraw");
FILE* sa_out = fopen(sa_file,"w");
if (fwrite(SA,sizeof(uint64_t),n,sa_out) != n) {
fprintf(stderr, "error writing sa file '%s'\n",sa_file);
exit(EXIT_FAILURE);
}
fclose(sa_out);
return sbtree_build(sa_file,text_file,outfile,25,B);
}
/* given a sa and text on disk create a SB-tree with disk page size B */
sbtree_t*
sbtree_build(const char* sa_file,const char* text_file,const char* outfile,uint64_t maxlcp,uint64_t B)
{
fprintf(stderr, "BUILT SBT\n");
sbtree_t* sbt = (sbtree_t*) sb_malloc(sizeof(sbtree_t));
sbt->n = sb_getfilesize(text_file);
sbt->bits_per_suffix = bit_magic::l1BP(sbt->n)+1;
sbt->bits_per_pos = 8*(bit_magic::l1BP(maxlcp)+1);
sbt->B = B;
sbt->b = sbtree_calc_branch_factor(sbt);
sbt->height = sbtree_calc_height(sbt);
fprintf(stderr, "n = %zu\n",sbt->n);
fprintf(stderr, "bits_per_suffix = %zu\n",sbt->bits_per_suffix);
fprintf(stderr, "bits_per_pos = %zu\n",sbt->bits_per_pos);
fprintf(stderr, "b = %zu\n",sbt->b);
fprintf(stderr, "B = %zu\n",sbt->B);
fprintf(stderr, "height = %zu\n",sbt->height);
/* open output file */
FILE* out = fopen(outfile,"w");
if (!out) {
fprintf(stderr, "cannot open output file '%s'\n",outfile);
exit(EXIT_FAILURE);
}
/* write the index header first */
sbtree_writeheader(sbt,out);
/* now write a dummy root page that are going to overwrite later.
we do this so the root file is always at the same offset in the index file */
sbtree_addpadding(out,B);
/* construct the whole sbt tree */
FILE* sa_fd = fopen(sa_file,"r");
/* we wrap the suffix array in the tmpfile to keep the createtree function simple */
sbtmpfile_t* sbtf = sbtmpfile_read_from_file(sa_fd);
/* we need the complete text in memory for construction as the critbit tree construction
randomly accesses the text during construction */
uint8_t* T = (uint8_t*) malloc(sbt->n);
FILE* t_in = fopen(text_file,"r");
if (!t_in) {
fprintf(stderr, "cannot input text file '%s'\n",text_file);
exit(EXIT_FAILURE);
}
if (fread(T,1,sbt->n,t_in) != sbt->n) {
fprintf(stderr, "error reading input text from file '%s'\n",text_file);
exit(EXIT_FAILURE);
}
fclose(t_in);
sbtree_createtree(sbt,sbtf,T,sbt->n,out);
sbtmpfile_delete(sbtf);
/* close the index file */
fclose(out);
/* open the file so we can use the sbt right away */
sbt->fd = open(outfile,O_RDONLY);
sbt->textfd = open(text_file,O_RDONLY);
return sbt;
}
/* stream sa from disk and construct the sb-tree */
void
sbtree_createtree(sbtree_t* sbt,sbtmpfile_t* suffixes,const uint8_t* T,uint64_t n,FILE* sbt_fd)
{
/* tmp file we store the next level in */
sbtmpfile_t* next_level = sbtmpfile_create_write();
/* read b suffixes and process */
sbtmpfile_open_read(suffixes);
uint64_t* suf = (uint64_t*) sb_malloc(sbt->b*sizeof(uint64_t));
uint64_t* next_suf = (uint64_t*) sb_malloc(sbt->b*sizeof(uint64_t));
uint64_t j,nsuf; j = 0;
uint64_t blocks_processed = 0;
while ((nsuf=sbtmpfile_read_block(suffixes,suf,sbt->b)) > 0) {
fprintf(stderr, "PROCESSING %lu suffixes.\n",nsuf);
fprintf(stderr, "creating critbit tree.\n");
critbit_tree_t* cbt = critbit_create_from_suffixes(T,n,suf,nsuf);
/* write node to the index file. fits into B bytes */
uint64_t written = critbit_write(cbt,sbt_fd);
critbit_free(cbt);
fprintf(stderr, "written %lu bytes to disk.\n",written);
if (written > sbt->B) {
fprintf(stderr, "ERROR! critbit tree larger than block size! (%lu,%lu)\n",written,sbt->B);
exit(EXIT_FAILURE);
}
/* add padding to fill up the disk page */
sbtree_addpadding(sbt_fd,sbt->B-written);
/* add the first suffix in block to next lvl file */
next_suf[j] = suf[0]; j++;
if (j==sbt->b) {
fprintf(stderr, "writing %lu suffixes to tmp file for next lvl.\n",j);
sbtmpfile_write_block(next_level,next_suf,sbt->b);
j = 0;
}
blocks_processed++;
}
/* write last block of the next level */
if (j>0) {
sbtmpfile_write_block(next_level,next_suf,j);
}
fprintf(stderr, "processed %lu blocks\n",blocks_processed);
free(suf);
free(next_suf);
sbtmpfile_finish(next_level);
/* recurse to the next level if we processed more than 1 block this level -> not root yet */
if (blocks_processed > 1) sbtree_createtree(sbt,next_level,T,n,sbt_fd);
}
/* load a SB-tree from disk */
sbtree_t*
sbtree_load(const char* sb_file,const char* text_file)
{
sbtree_t* sbt = (sbtree_t*) sb_malloc(sizeof(sbtree_t));
/* read index file */
FILE* in = fopen(sb_file,"r");
if (!in) {
fprintf(stderr, "cannot open output file '%s'\n",sb_file);
exit(EXIT_FAILURE);
}
/* read the header first */
sbtree_readheader(sbt,in);
/* open the file so we can use the sbt right away */
sbt->fd = open(sb_file,O_RDONLY);
sbt->textfd = open(text_file,O_RDONLY);
/* read/map the root page */
sbt->root = sbtree_load_diskpage(sbt,SBT_ROOT_OFFSET);
return sbt;
}
/* print storage statistics for the SB-tree to stdout */
void
sbtree_printstats(const sbtree_t* sbt)
{
}
/* free the sb tree data structure */
void
sbtree_free(sbtree_t* sbt)
{
if (sbt) {
sbtree_free_diskpage(sbt,sbt->root);
close(sbt->fd);
close(sbt->textfd);
free(sbt);
}
}
/* for a given disk page size B in bytes and the size of the text n,
calculate the branching factor b.
calculation as follows.
- each node in the SB-T contains b <= |n| <= 2b suffixes
- each node must fit into B bytes
- each node contains:
o |n|+1 child pointers (offset into the sb index to the
disk page containing the child)
note: as the sb-tree index is organized via disk pages,
we can use log(n/(2b)) bits per child pointer.
o a blind trie over all |n| suffixes
*/
uint64_t
sbtree_calc_branch_factor(sbtree_t* sbt)
{
return (uint64_t)(sbt->B/(0.25 + ((sbt->bits_per_pos + sbt->bits_per_suffix)/8.0f)));
}
sb_diskpage_t*
sbtree_load_diskpage(const sbtree_t* sbt,uint64_t offset)
{
return (sb_diskpage_t*) mmap(NULL,sbt->B,PROT_READ,
MAP_PRIVATE|MAP_POPULATE,sbt->fd,offset);
}
void
sbtree_free_diskpage(const sbtree_t* sbt,sb_diskpage_t* sbd)
{
munmap((void*)sbd,sbt->B);
}
/* query functions */
uint64_t*
sbtree_search(const sbtree_t* sbt,const uint8_t* P,uint64_t m,uint64_t* nres)
{
uint64_t* results = NULL;
uint64_t cur_height = 0;
uint64_t child_offset = 0;
uint64_t l = 0;
/* start with the root */
sb_diskpage_t* cur_node = sbt->root;
/*while( cur_height < sbt->height &&
(child_offset=blindtrie_search(sbt,cur_node,P,m,&l))!= 0 ) {*/
{
/* go to the next level + get new page */
sbtree_free_diskpage(sbt,cur_node); /* free old page */
cur_node = sbtree_load_diskpage(sbt,child_offset);
cur_height++;
}
/* we are either at the correct leaf or not found */
if (child_offset) {
/* traverse that part of the SA to find the matches */
} else {
/* not found */
*nres = 0;
}
return results;
}
/* calculate the SB-Tree height */
uint64_t
sbtree_calc_height(const sbtree_t* sbt)
{
return (uint64_t) log(sbt->n)/log(sbt->b) + 1;
}
/* write the index header + padding */
void
sbtree_writeheader(sbtree_t* sbt,FILE* out)
{
uint64_t written = 0;
written += fwrite(&sbt->n,sizeof(uint64_t),1,out);
written += fwrite(&sbt->bits_per_suffix,sizeof(uint64_t),1,out);
written += fwrite(&sbt->bits_per_pos,sizeof(uint64_t),1,out);
written += fwrite(&sbt->b,sizeof(uint64_t),1,out);
written += fwrite(&sbt->B,sizeof(uint64_t),1,out);
written += fwrite(&sbt->height,sizeof(uint64_t),1,out);
/* pad up to SBT_ROOT_OFFSET bytes so we have nice alignment */
sbtree_addpadding(out,SBT_ROOT_OFFSET-(written*sizeof(uint64_t)));
}
/* add padding to the index file to get nice alignment */
void
sbtree_addpadding(FILE* out,uint64_t bytes)
{
if (bytes) {
fprintf(stderr, "added %lu bytes padding.\n",bytes);
uint8_t* dummy_root = (uint8_t*) sb_malloc(bytes);
memset(dummy_root,rand()%50,bytes);
fwrite(dummy_root,1,bytes,out);
free(dummy_root);
}
}
/* read the index header */
void
sbtree_readheader(sbtree_t* sbt,FILE* in)
{
uint64_t read = 0;
read += fread(&sbt->n,sizeof(uint64_t),1,in);
read += fread(&sbt->bits_per_suffix,sizeof(uint64_t),1,in);
read += fread(&sbt->bits_per_pos,sizeof(uint64_t),1,in);
read += fread(&sbt->b,sizeof(uint64_t),1,in);
read += fread(&sbt->B,sizeof(uint64_t),1,in);
read += fread(&sbt->height,sizeof(uint64_t),1,in);
fclose(in);
if (read != 6) {
fprintf(stderr, "error reading index file.\n");
exit(EXIT_FAILURE);
}
}