Repositories » TXT0
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

ii.c now reads a TDoc at a time and builds the BST in stead of reading
the entire input file into memory. For this some read*() functions

were added to tfile.c which are factors of readTFile().

Changeset 089568eb333e

Parent bced1df4033b

by Rup Palchowdhury

Changes to 3 files · Browse files at 089568eb333e Showing diff from parent bced1df4033b Diff from another changeset...

Change 1 of 1 Show Entire File ii.c Stacked
 
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
 
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
@@ -14,72 +14,73 @@
 {   /* TODO: store docids */   int i_; - TFile *tfile;   TDoc *tdoc;   TNode *tree; - Node *p, *next;   Post *post;   Term *term;   unsigned n_doc;   char *p_txt;   + THeader *h; +   tree = NULL; - n_doc = 0;   - tfile = readTFile(stdin); + h = _newTHeader(TREC);   - if (tfile != NULL) { + if ((h = readTHeader(h, stdin)) == NULL) + exit(0); + + for(n_doc = 0; n_doc < h->n; n_doc++) {   - for (p = tfile->list; p != NULL; p = next) { + fprintf(stderr, "%d/%d\r", n_doc, h->n);   - fprintf(stderr, "%d/%d\r", n_doc, tfile->h->n); + tdoc = newTDoc(TREC); + + if ((tdoc = readTDoc(tdoc, stdin)) == NULL) + exit(0); + + i_ = 0; + p_txt = tdoc->txt;   - next = p->next; - tdoc = p->data; + for (int i = 0; i < tdoc->h->n_txt; i++) { + + if (tdoc->txt[i] == ' ') { + + post = newpost(n_doc, 1); + term = newterm(p_txt, i - i_, post); + + /* if term exists + if doc exists at front of list + post->tf++ + else + add new doc at front of list + term->df++ + else + attach node with term to tree + */ + + tree = nrinsert(tree, newtnode((void *)term), termcmp, + term_match_handler); + + i_ = i + 1; + p_txt = &(tdoc->txt[i + 1]);   - i_ = 0; - p_txt = tdoc->txt; - - for (int i = 0; i < tdoc->h->n_txt; i++) { - - if (tdoc->txt[i] == ' ') { + /* + 2 = both term and post matched + 1 = term matched not the post + */   - post = newpost(n_doc, 1); - term = newterm(p_txt, i - i_, post); - - /* if term exists - if doc exists at front of list - post->tf++ - else - add new doc at front of list - term->df++ - else - attach node with term to tree - */ - - tree = nrinsert(tree, newtnode((void *)term), termcmp, - term_match_handler); - - i_ = i + 1; - p_txt = &(tdoc->txt[i + 1]); + if (term->status == 2) + freeterm(term, freepost); + else if (term->status == 1) + freeterm(term, NULL); + } + }   - /* - 2 = both term and post matched - 1 = term matched not the post - */ - - if (term->status == 2) - freeterm(term, freepost); - else if (term->status == 1) - freeterm(term, NULL); - } - } - - freenode(p, freeTDoc); - n_doc++; - } + freeTDoc(tdoc);   } - + + _freeTHeader(h);   applyinorder(tree, printtree, "%s:%d ");     return 0;
Change 1 of 3 Show Entire File tfile.c Stacked
 
22
23
24
 
 
 
 
 
25
26
27
 
161
162
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
165
166
 
240
241
242
243
 
244
245
246
 
22
23
24
25
26
27
28
29
30
31
32
 
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
 
329
330
331
 
332
333
334
335
@@ -22,6 +22,11 @@
  return h;  }   +void _freeTHeader(void *d) +{ + free((THeader *)d); +} +  TSubHeader *_newTSubHeader(E_TDocType type)  {   TSubHeader *h; @@ -161,6 +166,90 @@
  return 1;  }   +THeader *readTHeader(THeader *h, FILE *fp) +{ + uint16_t v, check; + int n; + + /* fill the THeader */ + v = 0; + n = fread(&h->mg, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&h->crc, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&h->ver, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&h->type, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&h->b, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&h->n, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&h->r, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&h->bo, sizeof(uint32_t), 1, fp); v |= n; + if (v != 0x00FF) { /* 0000 0000 1111 1111 */ + fprintf(stderr, "ERROR: failed to read THeader\n"); + return NULL; + } + + return h; +} + +TDoc *readTDoc(TDoc *tdoc, FILE *fp) +{ + uint16_t v, check; + int n; + + /* fill the TDoc->TSubHeader */ + check = 0x000F; /* 0000 0000 0000 1111 */ + v = 0; + n = fread(&tdoc->h->crc, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&tdoc->h->r, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&tdoc->h->n_txt, sizeof(uint32_t), 1, fp); v |= n; v <<= 1; + n = fread(&tdoc->h->n_id, sizeof(uint32_t), 1, fp); v |= n; + for (int j = 0; j < tdoc->h->r; j++) { + n = fread(&tdoc->h->n_rsrc[j], sizeof(uint32_t), 1, fp); + v <<= 1; v |= n; + check <<=1; check |= 1; + } + if (v != check) { /* tdoc->h->r 1's pushed through the LSB */ + fprintf(stderr, "ERROR: failed to read TDoc->TSubHeader\n"); + return NULL; + } + + /* fill TDoc->txt */ + if (tdoc->h->n_txt > TXTBUFSIZE) + tdoc->txt = (char *)realloc(tdoc->txt, tdoc->h->n_txt); + n = fread(tdoc->txt, tdoc->h->n_txt, 1, fp); + if (n != 1) { + fprintf(stderr, "ERROR: failed to read TDoc->txt\n"); + return NULL; + } + + /* fill TDoc->id */ + if (tdoc->h->n_id > IDBUFSIZE) + tdoc->id = (char *)realloc(tdoc->id, tdoc->h->n_id); + n = fread(tdoc->id, tdoc->h->n_id, 1, fp); + if (n != 1) { + fprintf(stderr, "ERROR: failed to read TDoc->id\n"); + return NULL; + } + + /* read the TDoc->rsrc[i]->b */ + if (tdoc->h->r > 0) { + v = 0; + check = 0x0000; + for (int j = 0; j < tdoc->h->r; j++) { + if (tdoc->h->n_rsrc[j] > RSRCBUFSIZE) + tdoc->rsrc[j] = realloc(tdoc->rsrc[j], + tdoc->h->n_rsrc[j]); + n = fread(tdoc->rsrc[j], tdoc->h->n_rsrc[j], 1, fp); + v <<= 1; v |= n; + check <<= 1; check |= n; + } + if (v != check) { /* the lower tdoc->h->r bits of 'check' were set */ + fprintf(stderr, "ERROR: failed to read a TDoc->rsrc[j]\n"); + return NULL; + } + } + + return tdoc; +} +  TFile* readTFile(FILE *fp)  {   /* TODO: checks to validate a T file. */ @@ -240,7 +329,7 @@
  }   if (v != check) { /* the lower tdoc->h->r bits of 'check' were set */   fprintf(stderr, "ERROR: failed to read a TDoc->rsrc[j]\n"); - return 0; + return NULL;   }   }  
Change 1 of 1 Show Entire File tfile.h Stacked
 
45
46
47
 
48
49
50
51
52
53
 
 
54
55
56
 
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@@ -45,12 +45,15 @@
 };    THeader *_newTHeader(E_TDocType); +void _freeTHeader(void*);  TSubHeader *_newTSubHeader(E_TDocType);  TDoc *newTDoc(E_TDocType);  TFile *newTFile(E_TDocType);  void freeTDoc(void*);  void freeTFile(TFile*);  int writeTFile(TFile*, FILE*); +THeader *readTHeader(THeader*, FILE*); +TDoc *readTDoc(TDoc*, FILE*);  TFile *readTFile(FILE*);  void _printTHeader(THeader*);  void _printTSubHeader(TSubHeader*);