forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Categories.cpp
1686 lines (1560 loc) · 43.3 KB
/
Categories.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gb-include.h"
#include "Categories.h"
#include "Catdb.h"
#include "Loop.h"
#include "sort.h"
#include "LanguageIdentifier.h"
using namespace std;
Categories g_categories1;
Categories g_categories2;
Categories *g_categories;
static int sortCatHash ( const void *h1, const void *h2 );
// properly read from file
int32_t Categories::fileRead ( int fileid, void *buf, size_t count ) {
char *p = (char*)buf;
int32_t n = 0;
uint32_t sizeRead = 0;
while ( sizeRead < count ) {
n = read ( fileid, p, count - sizeRead );
if ( n <= 0 || n > (int32_t)count )
return n;
sizeRead += n;
p += n;
}
return sizeRead;
}
Categories::Categories() {
m_cats = NULL;
m_numCats = 0;
m_nameBuffer = NULL;
m_nameBufferSize = 0;
m_buffer = NULL;
m_bufferSize = 0;
}
Categories::~Categories() {
reset();
}
void Categories::reset() {
if (m_buffer) {
mfree ( m_buffer,
m_bufferSize,
"Categories" );
m_buffer = NULL;
}
}
// filename usually ./catdb/gbdmoz.structure.dat
int32_t Categories::loadCategories ( char *filename ) {
//ifstream inStream;
int inStream;
// open the structure file
inStream = open(filename, O_RDONLY);
// make sure it opened okay
if ( inStream < 0 ) {
log("cat: Error opening structure file: %s", filename);
return 1;
}
// read the size of the name buffer
if ( fileRead ( inStream, &m_nameBufferSize, sizeof(int32_t) ) !=
sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
// read in the number of cats
// filename usually ./catdb/gbdmoz.structure.dat
if ( fileRead ( inStream, &m_numCats, sizeof(int32_t) ) != sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
// create the name buffer
m_bufferSize = m_nameBufferSize +
sizeof(Category)*m_numCats +
sizeof(CategoryHash)*m_numCats;
m_buffer = (char*)mmalloc(m_bufferSize, "Categories");
if (!m_buffer) {
log("cat: Could not allocate %"INT32" bytes for Category Buffer",
m_bufferSize);
close(inStream);
g_errno = ENOMEM;
return 1;
}
// assign the buffers
m_nameBuffer = m_buffer;
m_cats = (Category*)(m_buffer + (sizeof(char)*m_nameBufferSize));
m_catHash = (CategoryHash*)(m_buffer +
(sizeof(char)*m_nameBufferSize) +
(sizeof(Category)*m_numCats));
//(sizeof(int32_t)*m_numSymParents));
/*
// read and fill the name buffer
if ( fileRead ( inStream, m_nameBuffer, m_nameBufferSize ) !=
m_nameBufferSize ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
*/
// temp buffer to read the whole file first
int32_t readSize = m_nameBufferSize + (m_numCats * 30);
char *tempBuffer = (char*)mmalloc(readSize, "Categories");
if ( !tempBuffer ) {
log("cat: Could not allocate %"INT32" bytes for File Temp Buffer",
readSize);
close(inStream);
g_errno = ENOMEM;
return 1;
}
// . read the rest of the file into the temp buffer
// . filename usually ./catdb/gbdmoz.structure.dat
if ( fileRead ( inStream, tempBuffer, readSize ) != readSize ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
char *p = tempBuffer;
gbmemcpy ( m_nameBuffer, p, m_nameBufferSize );
p += m_nameBufferSize;
// read and fill the cats
for (int32_t i = 0; i < m_numCats; i++) {
gbmemcpy(&m_cats[i].m_catid, p, sizeof(int32_t));
p += sizeof(int32_t);
gbmemcpy(&m_cats[i].m_parentid, p, sizeof(int32_t));
p += sizeof(int32_t);
gbmemcpy(&m_cats[i].m_nameOffset, p, sizeof(int32_t));
p += sizeof(int32_t);
gbmemcpy(&m_cats[i].m_nameLen, p, sizeof(int16_t));
p += sizeof(int16_t);
gbmemcpy(&m_cats[i].m_structureOffset, p, sizeof(int32_t));
p += sizeof(int32_t);
gbmemcpy(&m_cats[i].m_contentOffset, p, sizeof(int32_t));
p += sizeof(int32_t);
gbmemcpy(&m_cats[i].m_numUrls, p, sizeof(int32_t));
p += sizeof(int32_t);
/*
if ( fileRead ( inStream, &m_cats[i].m_catid, sizeof(int32_t) ) !=
sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
if ( fileRead(inStream, &m_cats[i].m_parentid, sizeof(int32_t)) !=
sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
if ( fileRead ( inStream,
&m_cats[i].m_nameOffset,
sizeof(int32_t) ) != sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
if ( fileRead ( inStream,
&m_cats[i].m_nameLen,
sizeof(int16_t) ) != sizeof(int16_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
if ( fileRead ( inStream, &m_cats[i].m_structureOffset,
sizeof(int32_t) ) != sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
if ( fileRead ( inStream, &m_cats[i].m_contentOffset,
sizeof(int32_t) ) != sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
if ( fileRead ( inStream, &m_cats[i].m_numUrls,
sizeof(int32_t) ) != sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
*/
}
// read the category hash
for (int32_t i = 0; i < m_numCats; i++) {
// read the hash
/*
if ( fileRead ( inStream,
&m_catHash[i].m_hash,
sizeof(int32_t) ) != sizeof(int32_t) ) {
log("cat: Error reading structure file: %s", filename);
close(inStream);
return 1;
}
*/
gbmemcpy(&m_catHash[i].m_hash, p, sizeof(int32_t));
p += sizeof(int32_t);
// assign the index
m_catHash[i].m_catIndex = i;
}
// is this a bottleneck? shouldn't it be stored that way on disk?
int64_t start = gettimeofdayInMilliseconds();
// sort the category hash by hash value
gbsort(m_catHash, m_numCats, sizeof(CategoryHash), sortCatHash);
// sanity check - no dups allowed
uint32_t last = 0xffffffff;
for ( int32_t i = 0 ; i < m_numCats ; i++ ) {
if ( m_catHash[i].m_hash == last )
log("dmoz: hash collision on %"UINT32"",last);
last = m_catHash[i].m_hash;
}
// time it
int64_t took = gettimeofdayInMilliseconds();
if ( took - start > 100 ) log(LOG_INIT,"admin: Took %"INT64" ms to "
"sort cat hashes.",took-start);
// close the file
close(inStream);
// free the temp buffer
mfree(tempBuffer, readSize, "Categories");
// now create the "bad" hash table, so we can quickly see if a url
// url is in the adult, gambling or online pharmacies categories
if ( ! makeBadHashTable() ) return 1;
// success
return 0;
}
// returns false and sets g_errno on error
bool Categories::makeBadHashTable ( ) {
m_badTable.reset();
// . if it is on disk, load it
// . returns false and sets g_errno on load error
// . returns true if file does not exist
if ( ! m_badTable.load ( g_hostdb.m_dir , "badcattable.dat" ) )
return false;
// if it existed, we are done
if ( m_badTable.getNumSlotsUsed() > 0 ) return true;
log(LOG_INFO,"cat: Generating hash table of bad url hashes.");
for ( int32_t i = 0 ; i < m_numCats ; i++ ) {
// skip if not an bad catid
if ( ! isIdBad ( m_cats[i].m_catid ) ) continue;
// it is, add the url hash to the table
addUrlsToBadHashTable ( m_cats[i].m_catid ) ;
//log(LOG_INIT,"cat: Error making bad hash table: %s.",
// mstrerror(g_errno));
// return false;
//}
}
//log(LOG_INFO,"cat: Saving hash table to badtable.dat.");
// now try to save it to make it faster next time around
m_badTable.save ( g_hostdb.m_dir , "badcattable.dat" ) ;
return true;
}
bool Categories::isInBadCat ( Url *u ) {
// hash it
uint32_t h = hash32 ( u->getUrl() , u->getUrlLen() );
// if it is in there, it is in a bad catid
if ( m_badTable.getSlot ( h ) >= 0 ) return true;
// otherwise, not...
return false;
}
bool Categories::isInBadCat ( uint32_t h ) {
// if it is in there, it is in an bad catid
if ( m_badTable.getSlot ( h ) >= 0 ) return true;
// otherwise, not...
return false;
}
int sortCatHash ( const void *h1, const void *h2 ) {
if (((CategoryHash*)h1)->m_hash < ((CategoryHash*)h2)->m_hash)
return -1;
else if (((CategoryHash*)h1)->m_hash > ((CategoryHash*)h2)->m_hash)
return 1;
else
return 0;
}
// do a binary search to get a cat from an id
int32_t Categories::getIndexFromId ( int32_t catid ) {
int32_t low = 0;
int32_t high = m_numCats-1;
int32_t currCat;
// binary search
while (low <= high) {
// next check spot
currCat = (low + high)/2;
// check for hit
if (m_cats[currCat].m_catid == catid)
return currCat;
// shift search range
else if (m_cats[currCat].m_catid > catid)
high = currCat-1;
else
low = currCat+1;
}
// not found
return -1;
}
// do a binary search to get a cat from a path
int32_t Categories::getIndexFromPath ( char *str, int32_t strLen ) {
int32_t low = 0;
int32_t high = m_numCats-1;
int32_t currCat;
if (!str || strLen <= 0)
return -1;
// remove any leading /
if (str[0] == '/') {
str++;
strLen--;
}
// remove any trailing /
if (str[strLen-1] == '/')
strLen--;
// check for top
if (strLen == 3 &&
strncasecmp(str, "Top", 3) == 0)
// it is catid 2 right? but i guess zero is symbolic for us!
return 0;
// get the hash
uint32_t hash = hash32Lower_a(str, strLen, 0);
// debug
//char c = str[strLen];
//str[strLen] = '\0';
//log("dmoz: looking up hash %"UINT32" for %s",hash,str);
//str[strLen] = c;
// binary search
while (low <= high) {
// next check spot
currCat = (low + high)/2;
// check for hit
if (m_catHash[currCat].m_hash == hash)
return m_catHash[currCat].m_catIndex;
// shift search range
else if (m_catHash[currCat].m_hash > hash)
high = currCat-1;
else
low = currCat+1;
}
// not found
return -1;
}
// return the catid from the given path
int32_t Categories::getIdFromPath ( char *str, int32_t strLen ) {
if ( ! m_cats ) return -1;
int32_t index = getIndexFromPath(str, strLen);
return m_cats[index].m_catid;
}
// check this ID for an RTL starter
bool Categories::isIdRTLStart ( int32_t catid ) {
if ( catid == 88070 || // Top:World:Arabic
catid == 39341 || // Top:World:Farsi
catid == 118215 || // Top:World:Hebrew
catid == 1214070 || // Top:K&T:Inter:Arabic
catid == 1262316 || // Top:K&T:Inter:Farsi
catid == 910298 ) // Top:K&T:Inter:Hebrew
return true;
else
return false;
}
// check this ID for an RTL starter
bool Categories::isIndexRTLStart ( int32_t catIndex ) {
if ( catIndex > 0 )
return isIdRTLStart(m_cats[catIndex].m_catid);
return false;
}
// determine if a category is RTL from Id
bool Categories::isIdRTL ( int32_t catid ) {
int32_t index = getIndexFromId(catid);
if (index < 0)
return false;
return isIndexRTL(index);
}
// determine if a category is RTL from Index
bool Categories::isIndexRTL ( int32_t catIndex ) {
int32_t currIndex = catIndex;
while (currIndex > 0) {
// check if this is one of the RTLs
if (isIdRTLStart(m_cats[currIndex].m_catid))
return true;
// otherwise check the parent
currIndex = getIndexFromId(m_cats[currIndex].m_parentid);
}
return false;
}
// check this ID for a top Adult category
bool Categories::isIdAdultStart ( int32_t catid ) {
if ( catid == 17 ) // Top:Adult
return true;
else
return false;
}
bool Categories::isIdBadStart ( int32_t catid ) {
// Top:Adult
if ( catid == 17 )
return true;
// Top:Games:Gambling
if ( catid == 144 )
return true;
// Top:Shopping:Health:Pharmacy:Online_Pharmacies
if ( catid == 128206 )
return true;
return false;
}
// check this index for a top Adult category
bool Categories::isIndexAdultStart ( int32_t catIndex ) {
if (catIndex > 0)
return isIdAdultStart(m_cats[catIndex].m_catid);
return false;
}
// check if a category is Adult from Id
bool Categories::isIdAdult ( int32_t catid ) {
int32_t index = getIndexFromId(catid);
if (index < 0)
return false;
return isIndexAdult(index);
}
// check if a category is "bad" from Id
bool Categories::isIdBad ( int32_t catid ) {
int32_t index = getIndexFromId(catid);
if (index < 0)
return false;
return isIndexBad(index);
}
// check if a category is Adult from Index
bool Categories::isIndexAdult ( int32_t catIndex ) {
int32_t currIndex = catIndex;
while (currIndex > 0) {
// check if this is the Adult category
if ( isIdAdultStart(m_cats[currIndex].m_catid) )
return true;
// otherwise check the parent
currIndex = getIndexFromId(m_cats[currIndex].m_parentid);
}
return false;
}
// check if a category is Adult, gambling or online phrarmacy from Index
bool Categories::isIndexBad ( int32_t catIndex ) {
int32_t currIndex = catIndex;
while (currIndex > 0) {
// check if this is a "bad" category
if ( isIdBadStart(m_cats[currIndex].m_catid) )
return true;
// otherwise check the parent
currIndex = getIndexFromId(m_cats[currIndex].m_parentid);
}
return false;
}
// print cat information
void Categories::printCats ( int32_t start, int32_t end ) {
for (int32_t i = start; i < end; i++) {
char str[512];
char *s = str;
s += sprintf(s, "Cat %"INT32":\n", i);
s += sprintf(s, " CatID: %"INT32"\n", m_cats[i].m_catid);
s += sprintf(s, " Name: ");
for (int32_t n = m_cats[i].m_nameOffset;
n < m_cats[i].m_nameOffset + m_cats[i].m_nameLen;
n++)
s += sprintf(s, "%c", m_nameBuffer[n]);
s += sprintf(s, "\n");
s += sprintf(s, " Name Offset: %"INT32"\n",
m_cats[i].m_nameOffset);
s += sprintf(s, " Structure Offset: %"INT32"\n",
m_cats[i].m_structureOffset);
s += sprintf(s, " Content Offset: %"INT32"\n",
m_cats[i].m_contentOffset);
s += sprintf(s, " Parent: %"INT32"\n",
m_cats[i].m_parentid);
s += sprintf(s, "\n");
log ( LOG_INFO, "%s", str );
}
}
void Categories::printPathFromId ( SafeBuf *sb ,
int32_t catid,
bool raw,
bool isRTL ) {
int32_t catIndex;
// get the index
catIndex = getIndexFromId(catid);
//if (catIndex < 1) return;
printPathFromIndex(sb, catIndex, raw, isRTL);
}
void Categories::printPathFromIndex ( SafeBuf *sb ,
int32_t catIndex,
bool raw,
bool isRTL ) {
int32_t parentId;
if (catIndex < 1) return;
// get the parent
parentId = m_cats[catIndex].m_parentid;
int32_t catid = m_cats[catIndex].m_catid;
// include Top now. in newer dmoz it is catid2.
//if ( catid == 2 ) {
// sb->safePrintf("Top");
// return;
//}
// . print the parent(s) first
// . the new dmoz data dumps signify a parentless topic by
// havings its parentid equal its catid, so avoid infinite
// loops by checking for that here now. mdw oct 2013.
// . the new DMOZ has Top has catid 2 now, even though it is
// mistakenly labelled as Top/World, which is really catid 3.
// so make this parentId > 2...
if (parentId >= 1 && parentId != catid ) {
bool isParentRTL = isIdRTLStart(parentId);
// print spacing here if RTL
//if (isRTL && !raw)
// p += sprintf(p, " :");
printPathFromId(sb, parentId, raw, isRTL);
// print a spacing
//if (!isRTL && !raw)
// p += sprintf(p, ": ");
//else if (raw)
// p += sprintf(p, "/");
if (!raw) sb->safePrintf(": ");
else sb->safePrintf("/");
// if parent was the start of RTL, <br>
if (isParentRTL && !raw)
sb->safePrintf("</span><br>");
}
// print this category name
int32_t nameLen = m_cats[catIndex].m_nameLen;
int32_t nameOffset = m_cats[catIndex].m_nameOffset;
if (raw) {
sb->safeMemcpy(&m_nameBuffer[nameOffset], nameLen);
}
else {
// html encode the name
char encodedName[2048];
char *encodeEnd = htmlEncode ( encodedName,
encodedName + 2047,
&m_nameBuffer[nameOffset],
&m_nameBuffer[nameOffset] +
nameLen );
nameLen = encodeEnd - encodedName;
// fill it, replace _ with space
for (int32_t i = 0; i < nameLen; i++) {
if (encodedName[i] == '_')
sb->safePrintf(" ");
else
sb->safePrintf("%c", encodedName[i]);
}
}
}
void Categories::printPathCrumbFromId ( SafeBuf *sb ,
int32_t catid,
bool isRTL ) {
int32_t catIndex;
// get the index
catIndex = getIndexFromId(catid);
//if (catIndex < 1) return;
printPathCrumbFromIndex(sb, catIndex, isRTL);
}
void Categories::printPathCrumbFromIndex ( SafeBuf *sb,
int32_t catIndex,
bool isRTL ) {
int32_t parentId;
if (catIndex < 1) return;
// get the parent
parentId = m_cats[catIndex].m_parentid;
int32_t catid = m_cats[catIndex].m_catid;
// include Top now. in newer dmoz it is catid2.
// seems to already be included below... because you made it
// parentId>1 not parentId>2
//if ( catid == 2 ) {
// sb->safePrintf("Top");
// return;
//}
// . print the parent(s) first
// . the new dmoz has Top has parentid 2 now, and Top/World is
// catid 3. so make this parentId > 2 not parentId > 1
if (parentId > 1 && parentId != catid ) {
bool isParentRTL = isIdRTLStart(parentId);
printPathCrumbFromId(sb, parentId, isRTL);
// print a spacing
sb->safePrintf(": ");
// if parent starts RTL, <br>
if (isParentRTL && isRTL)
sb->safePrintf("</span><br>");
}
// print this category's link
sb->safePrintf("<a href=\"/");
printPathFromIndex(sb, catIndex, true, isRTL);
sb->safePrintf("/\">");
int32_t nameLen = m_cats[catIndex].m_nameLen;
int32_t nameOffset = m_cats[catIndex].m_nameOffset;
// fill it, replace _ with space
{
// html encode the name
char encodedName[2048];
char *encodeEnd = htmlEncode ( encodedName,
encodedName + 2047,
&m_nameBuffer[nameOffset],
&m_nameBuffer[nameOffset] +
nameLen );
nameLen = encodeEnd - encodedName;
for (int32_t i = 0; i < nameLen; i++) {
if (encodedName[i] == '_')
sb->safePrintf(" ");
else
sb->safePrintf("%c", encodedName[i]);
}
}
sb->safePrintf("</a>");
}
// increment the ptr into the file, possibly reading the next chunk
char* Categories::incRdfPtr( int32_t skip ) {
int32_t n;
for (int32_t i = 0; i < skip; i++) {
m_rdfPtr++;
m_currOffset++;
// pull the next chunk if we're at the end
if (m_rdfPtr == m_rdfEnd) {
// if nothing left, return NULL
//if (!m_rdfStream.good())
// return NULL;
// get the next chunk
//m_rdfStream.read(m_rdfBuffer, m_rdfBufferSize);
//n = m_rdfStream.gcount();
n = read ( m_rdfStream, m_rdfBuffer, m_rdfBufferSize );
if ( n <= 0 || n > m_rdfBufferSize )
return NULL;
m_rdfPtr = m_rdfBuffer;
m_rdfEnd = &m_rdfBuffer[n];
}
}
return m_rdfPtr;
}
// parse the rdf file up past a given start tag
int32_t Categories::rdfParse ( char *tagName ) {
bool inQuote = false;
do {
int32_t matchPos = 0;
// move to the next tag
while (*m_rdfPtr != '<' || inQuote ) {
// check for quotes
if (*m_rdfPtr == '"')
inQuote = !inQuote;
// next char
if (!incRdfPtr())
return -1;
}
// check if the tag is good
do {
if (!incRdfPtr())
return -1;
if (*m_rdfPtr != tagName[matchPos])
break;
matchPos++;
} while (tagName[matchPos]);
// matched if we're at the end of the tagName
if (!tagName[matchPos]) {
if (!incRdfPtr())
return -1;
return 0;
}
// otherwise it's not a match, keep going
matchPos = 0;
} while (true);
}
// move to the next tag in the file
int32_t Categories::rdfNextTag ( ) {
bool inQuote = false;
// move to the next tag
while (*m_rdfPtr != '<' || inQuote ) {
// check for quotes
if (*m_rdfPtr == '"')
inQuote = !inQuote;
// next char
if (!incRdfPtr())
return -1;
}
// skip the <
if (!incRdfPtr())
return -1;
// put the tag name in a buffer
m_tagLen = 0;
while ( *m_rdfPtr != ' ' &&
*m_rdfPtr != '>' ) {
// insert the current char
if (m_tagLen < MAX_TAG_LEN) {
m_tagRecfer[m_tagLen] = *m_rdfPtr;
m_tagLen++;
}
// next char
if (!incRdfPtr())
return -1;
}
m_tagRecfer[m_tagLen] = '\0';
// success
return 0;
}
// fill the next quoted string into the buffer
int32_t Categories::fillNextString(char *str, int32_t max) {
// get the next string, skip to the next quote
while (*m_rdfPtr != '"') {
if (!incRdfPtr())
return -1;
}
// skip the quote
if (!incRdfPtr())
return -1;
// . pointing at the string now
// dump it in the buffer
int32_t strLen = 0;
while (*m_rdfPtr != '"') {
// fill the next character
if (strLen < max) {
str[strLen] = *m_rdfPtr;
strLen++;
}
if (!incRdfPtr())
return -1;
}
// step past the quote
if (!incRdfPtr())
return -1;
// return the length
return strLen;
}
// fill the next tag body into the buffer
int32_t Categories::fillNextTagBody(char *str, int32_t max) {
// get the next string, skip to the next quote
while (*m_rdfPtr != '>') {
if (!incRdfPtr())
return -1;
}
// skip the >
if (!incRdfPtr())
return -1;
// . pointing at the string now
// dump it in the buffer
int32_t strLen = 0;
while (*m_rdfPtr != '<') {
// fill the next character
if (strLen < max) {
str[strLen] = *m_rdfPtr;
strLen++;
}
if (!incRdfPtr())
return -1;
}
// return the length
return strLen;
}
// fix root urls without a trailing /
int32_t Categories::fixUrl ( char *url, int32_t urlLen ) {
// get past the first ://
int32_t slashi = 0;
int32_t newUrlLen = urlLen;
while (url[slashi] != ':' ||
url[slashi+1] != '/' ||
url[slashi+2] != '/') {
slashi++;
if (slashi >= urlLen)
return urlLen;
}
slashi += 3;
// remove a www.
/*
if (newUrlLen - slashi >= 4 &&
strncasecmp(&url[slashi], "www.", 4) == 0) {
memmove(&url[slashi], &url[slashi+4], newUrlLen - (slashi+4));
newUrlLen -= 4;
}
*/
// look for //, cut down to single /
for (; slashi < newUrlLen; slashi++) {
if (url[slashi-1] == '/' && url[slashi] == '/') {
memmove(&url[slashi-1],
&url[slashi],
newUrlLen - slashi);
newUrlLen--;
}
if (is_wspace_a(url[slashi])) {
memmove(&url[slashi],
&url[slashi+1],
newUrlLen - (slashi+1));
newUrlLen--;
}
}
// remove any trailing /
if (url[newUrlLen-1] == '/')
newUrlLen--;
// return the new length
return newUrlLen;
}
bool Categories::addUrlsToBadHashTable ( int32_t catid ) {
return getTitleAndSummary ( NULL , // urlorig
0 , // urloriglen
catid ,
NULL , // title
0 , // titleLen
0 , // maxTitleLen
NULL , // summ
0 , // summLen
0 , // maxSummLen
NULL , // anchor
0 , // anchorLen
0 , // maxAnchorLen
0 , // niceness
true );// just add to table
}
// just show the urls in dmoz
bool Categories::printUrlsInTopic ( SafeBuf *sb, int32_t catid ) {
int32_t catIndex;
uint32_t fileOffset;
uint32_t n;
char* p;
uint32_t readSize;
char title[1024];
char summ[5000];
int32_t maxTitleLen = 1024;
int32_t maxSummLen = 5000;
int32_t titleLen;
int32_t summLen;
int32_t urlStrLen;
char urlStr[MAX_URL_LEN];
int32_t niceness = 0;
bool printedStart = false;
// lookup the index for this catid
catIndex = getIndexFromId(catid);
if (catIndex < 0)
goto errEnd;
// get the file offset
fileOffset = m_cats[catIndex].m_contentOffset;
QUICKPOLL( niceness );
// . open the file
char filename[512];
sprintf(filename, "%scatdb/%s", g_hostdb.m_dir, RDFCONTENT_FILE);
m_rdfStream = open(filename, O_RDONLY | O_NONBLOCK);
if ( m_rdfStream < 0 ) {
log("cat: Error Opening %s\n", filename);
goto errEnd;
}
// . seek to the offset
n = lseek ( m_rdfStream, fileOffset, SEEK_SET );
if ( n != fileOffset ) {
log("cat: Error seeking to Content Offset %"INT32"", fileOffset);
goto errEnd;
}
// . read in a chunk
m_rdfBuffer = m_rdfSmallBuffer;
m_rdfBufferSize = RDFSMALLBUFFER_SIZE;
p = m_rdfBuffer;
readSize = m_rdfBufferSize;
readLoop:
n = read ( m_rdfStream, p, readSize );
if(n > 0 && n != readSize) {
p += n;
readSize -= n;
}
//log(LOG_WARN,"build: reading %"INT32" bytes out of %"INT32"",n,m_rdfBufferSize);
QUICKPOLL(niceness);
if(n < 0 && errno == EAGAIN) goto readLoop;
if ( n <= 0 || n > (uint32_t)m_rdfBufferSize ) {
log("cat: Error Reading Content");
goto errEnd;
}
m_rdfPtr = m_rdfBuffer;
m_rdfEnd = &m_rdfBuffer[n];
m_currOffset = fileOffset;
// . parse to the correct url
// parse the first topic and catid
if (rdfNextTag() < 0)
goto errEnd;
if (rdfNextTag() < 0)
goto errEnd;
// parse until "ExternalPage"
nextTag:
QUICKPOLL((niceness));
if (rdfNextTag() < 0)
goto errEnd;
// check for catid of next topic to stop looking
if (m_tagLen == 5 &&
strncmp(m_tagRecfer, "catid", 5) == 0)
goto errEnd;
if (m_tagLen != 12 ) goto nextTag;
if ( strncmp(m_tagRecfer, "ExternalPage", 12) != 0) goto nextTag;
//
// got one
//
// get the next string
urlStrLen = fillNextString(urlStr, MAX_URL_LEN-1);
if (urlStrLen < 0)
goto errEnd;
// html decode the url
/*
urlStrLen = htmlDecode(decodedUrl, urlStr, urlStrLen,false,
niceness);
gbmemcpy(urlStr, decodedUrl, urlStrLen);
normUrl.set(urlStr, urlStrLen, true);
g_catdb.normalizeUrl(&normUrl, &normUrl);
// copy it back
urlStrLen = normUrl.getUrlLen();
gbmemcpy(urlStr, normUrl.getUrl(), urlStrLen);
// make sure there's a trailing / on root urls
// and no www.
//urlStrLen = fixUrl(urlStr, urlStrLen);
// check for an anchor
urlAnchor = NULL;
urlAnchorLen = 0;
//for (int32_t i = 0; i < urlStrLen; i++) {
//if (urlStr[i] == '#') {
if (normUrl.getAnchorLen() > 0) {
//urlAnchor = &urlStr[i];
//urlAnchorLen = urlStrLen - i;
//urlStrLen = i;
urlAnchor = normUrl.getAnchor();
urlAnchorLen = normUrl.getAnchorLen();
//break;
}
*/
// . parse out the title
if (rdfParse("d:Title") < 0)
goto errEnd;
titleLen = fillNextTagBody(title, maxTitleLen);
QUICKPOLL(niceness);
// . parse out the summary
if (rdfParse("d:Description") < 0)
goto errEnd;
summLen = fillNextTagBody(summ, maxSummLen);
if ( ! printedStart ) {
printedStart = true;
sb->safePrintf("<ul>");
}
// print it out
sb->safePrintf("<li><a href=\"");
sb->safeMemcpy ( urlStr , urlStrLen );