forked from manticoresoftware/manticoresearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indextool.cpp
1647 lines (1336 loc) · 46.8 KB
/
indextool.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
//
// Copyright (c) 2017-2023, Manticore Software LTD (https://manticoresearch.com)
// Copyright (c) 2001-2016, Andrew Aksyonoff
// Copyright (c) 2008-2016, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
#include "sphinxstd.h"
#include "sphinxutils.h"
#include "sphinxint.h"
#include "fileutils.h"
#include "sphinxrt.h"
#include "killlist.h"
#include "docidlookup.h"
#include "indexfiles.h"
#include "stripper/html_stripper.h"
#include "tokenizer/charset_definition_parser.h"
#include "indexcheck.h"
#include "secondarylib.h"
#include "detail/indexlink.h"
#include <ctime>
static CSphString g_sDataDir;
static bool g_bConfigless = false;
static bool IsConfigless()
{
return g_bConfigless;
}
static CSphString GetPathForNewIndex ( const CSphString & sIndexName )
{
CSphString sRes;
if ( g_sDataDir.Length() && !g_sDataDir.Ends("/") && !g_sDataDir.Ends("\\") )
sRes.SetSprintf ( "%s/%s", g_sDataDir.cstr(), sIndexName.cstr() );
else
sRes.SetSprintf ( "%s%s", g_sDataDir.cstr(), sIndexName.cstr() );
return sRes;
}
static void MakeRelativePath ( CSphString & sPath )
{
bool bAbsolute = strchr ( sPath.cstr(), '/' ) || strchr ( sPath.cstr(), '\\' );
if ( !bAbsolute )
sPath.SetSprintf ( "%s/%s/%s", g_sDataDir.cstr(), sPath.cstr(), sPath.cstr() );
}
class FilenameBuilder_c : public FilenameBuilder_i
{
public:
FilenameBuilder_c ( CSphString sIndex );
CSphString GetFullPath ( const CSphString & sName ) const final;
private:
const CSphString m_sIndex;
};
FilenameBuilder_c::FilenameBuilder_c ( CSphString sIndex )
: m_sIndex ( std::move ( sIndex ) )
{}
CSphString FilenameBuilder_c::GetFullPath ( const CSphString & sName ) const
{
if ( !IsConfigless() || !sName.Length() )
return sName;
CSphString sPath = GetPathForNewIndex ( m_sIndex );
StrVec_t dFiles;
StringBuilder_c sNewValue {" "};
// we assume that path has been stripped before
StrVec_t dValues = sphSplit ( sName.cstr(), sName.Length(), " \t," );
for ( auto & i : dValues )
{
if ( !i.Length() )
continue;
CSphString & sNew = dFiles.Add();
sNew.SetSprintf ( "%s/%s", sPath.cstr(), i.Trim().cstr() );
sNewValue << sNew;
}
return sNewValue.cstr();
}
static std::unique_ptr<FilenameBuilder_i> CreateFilenameBuilder ( const char * szIndex )
{
if ( IsConfigless() )
return std::make_unique<FilenameBuilder_c> ( szIndex );
return nullptr;
}
static void StripStdin ( const char * sIndexAttrs, const char * sRemoveElements )
{
CSphString sError;
CSphHTMLStripper tStripper ( true );
if ( !tStripper.SetIndexedAttrs ( sIndexAttrs, sError )
|| !tStripper.SetRemovedElements ( sRemoveElements, sError ) )
sphDie ( "failed to configure stripper: %s", sError.cstr() );
CSphVector<BYTE> dBuffer;
while ( !feof(stdin) )
{
char sBuffer[1024];
auto iLen = (int) fread ( sBuffer, 1, sizeof(sBuffer), stdin );
if ( !iLen )
break;
dBuffer.Append ((BYTE*)sBuffer, iLen);
}
dBuffer.Add ( 0 );
tStripper.Strip ( &dBuffer[0] );
fprintf ( stdout, "dumping stripped results...\n%s\n", &dBuffer[0] );
}
static void ApplyMorphology ( CSphIndex * pIndex )
{
CSphVector<BYTE> dInBuffer, dOutBuffer;
const int READ_BUFFER_SIZE = 1024;
dInBuffer.Reserve ( READ_BUFFER_SIZE );
char sBuffer[READ_BUFFER_SIZE];
while ( !feof(stdin) )
{
auto iLen = (int) fread ( sBuffer, 1, sizeof(sBuffer), stdin );
if ( !iLen )
break;
dInBuffer.Append ( sBuffer, iLen );
}
dInBuffer.Add(0);
dOutBuffer.Reserve ( dInBuffer.GetLength() );
TokenizerRefPtr_c pTokenizer = pIndex->GetTokenizer()->Clone ( SPH_CLONE_INDEX );
DictRefPtr_c pDict = pIndex->GetDictionary();
BYTE * sBufferToDump = &dInBuffer[0];
if ( pTokenizer )
{
pTokenizer->SetBuffer ( &dInBuffer[0], dInBuffer.GetLength() );
while ( BYTE * sToken = pTokenizer->GetToken() )
{
if ( pDict )
pDict->ApplyStemmers ( sToken );
auto iLen = (int) strlen ( (char *)sToken );
sToken[iLen] = ' ';
dOutBuffer.Append ( sToken, iLen+1 );
}
if ( dOutBuffer.GetLength() )
dOutBuffer[dOutBuffer.GetLength()-1] = 0;
else
dOutBuffer.Add(0);
sBufferToDump = &dOutBuffer[0];
}
fprintf ( stdout, "dumping stemmed results...\n%s\n", sBufferToDump );
}
static void CharsetFold ( CSphIndex * pIndex, FILE * fp )
{
CSphVector<BYTE> sBuf1 ( 16384 );
CSphVector<BYTE> sBuf2 ( 16384 );
const CSphLowercaser& tLC = pIndex->GetTokenizer()->GetLowercaser();
#if _WIN32
setmode ( fileno(stdout), O_BINARY );
#endif
int iBuf1 = 0; // how many leftover bytes from previous iteration
while ( !feof(fp) )
{
auto iGot = (int) fread ( sBuf1.Begin()+iBuf1, 1, sBuf1.GetLength()-iBuf1, fp );
if ( iGot<0 )
sphDie ( "read error: %s", strerrorm(errno) );
if ( iGot==0 )
if ( feof(fp) )
if ( iBuf1==0 )
break;
const BYTE * pIn = sBuf1.Begin();
const BYTE * pInMax = pIn + iBuf1 + iGot;
if ( pIn==pInMax && feof(fp) )
break;
// tricky bit
// on full buffer, and not an eof, terminate a bit early
// to avoid codepoint vs buffer boundary issue
if ( ( iBuf1+iGot )==sBuf1.GetLength() && iGot!=0 )
pInMax -= 16;
// do folding
BYTE * pOut = sBuf2.Begin();
BYTE * pOutMax = pOut + sBuf2.GetLength() - 16;
while ( pIn < pInMax )
{
int iCode = sphUTF8Decode ( pIn );
if ( iCode==0 )
pIn++; // decoder does not do that!
assert ( iCode>=0 );
if ( iCode!=0x09 && iCode!=0x0A && iCode!=0x0D )
{
iCode = tLC.ToLower ( iCode ) & 0xffffffUL;
if ( !iCode )
iCode = 0x20;
}
pOut += sphUTF8Encode ( pOut, iCode );
if ( pOut>=pOutMax )
{
fwrite ( sBuf2.Begin(), 1, pOut-sBuf2.Begin(), stdout );
pOut = sBuf2.Begin();
}
}
fwrite ( sBuf2.Begin(), 1, pOut-sBuf2.Begin(), stdout );
// now move around leftovers
BYTE * pRealEnd = sBuf1.Begin() + iBuf1 + iGot;
if ( pIn < pRealEnd )
{
iBuf1 = int ( pRealEnd - pIn );
memmove ( sBuf1.Begin(), pIn, iBuf1 );
}
}
}
#pragma pack(push,4)
struct IDFWord_t
{
uint64_t m_uWordID;
DWORD m_iDocs;
};
#pragma pack(pop)
STATIC_SIZE_ASSERT ( IDFWord_t, 12 );
static bool BuildIDF ( const CSphString & sFilename, const StrVec_t & dFiles, CSphString & sError, bool bSkipUnique )
{
// text dictionaries are ordered alphabetically - we can use that fact while reading
// to merge duplicates, calculate total number of occurrences and process bSkipUnique
// this method is about 3x faster and consumes ~2x less memory than a hash based one
typedef char StringBuffer_t [ 3*SPH_MAX_WORD_LEN+16+128 ]; // { dict-keyowrd, 32bit number, 32bit number, 64bit number }
int64_t iTotalDocuments = 0;
int64_t iTotalWords = 0;
int64_t iReadWords = 0;
int64_t iMergedWords = 0;
int64_t iSkippedWords = 0;
int64_t iReadBytes = 0;
int64_t iTotalBytes = 0;
const int64_t tmStart = sphMicroTimer ();
int iFiles = dFiles.GetLength ();
CSphVector<CSphAutoreader> dReaders ( iFiles );
ARRAY_FOREACH ( i, dFiles )
{
if ( !dReaders[i].Open ( dFiles[i], sError ) )
return false;
iTotalBytes += dReaders[i].GetFilesize ();
}
// internal state
CSphFixedVector<StringBuffer_t> dWords ( iFiles );
CSphVector<int> dDocs ( iFiles );
CSphVector<bool> dFinished ( iFiles );
dFinished.Fill ( false );
bool bPreread = false;
// current entry
StringBuffer_t sWord = {0};
DWORD iDocs = 0;
// output vector, preallocate 10M
CSphTightVector<IDFWord_t> dEntries;
dEntries.Reserve ( 1024*1024*10 );
for ( int i=0;; )
{
// read next input
while (true)
{
int iLen;
char * sBuffer = dWords[i];
if ( ( iLen = dReaders[i].GetLine ( sBuffer, sizeof(StringBuffer_t) ) )>=0 )
{
iReadBytes += iLen;
// find keyword pattern ( ^<keyword>,<docs>,... )
char * p1 = strchr ( sBuffer, ',' );
if ( p1 )
{
char * p2 = strchr ( p1+1, ',' );
if ( p2 )
{
*p1 = *p2 = '\0';
int iDocuments = atoi ( p1+1 );
if ( iDocuments )
{
dDocs[i] = iDocuments;
iReadWords++;
break;
}
}
} else
{
// keyword pattern not found (rather rare case), try to parse as a header, then
char sSearch[] = "total-documents: ";
if ( strstr ( sBuffer, sSearch )==sBuffer )
iTotalDocuments += atoi ( sBuffer+strlen(sSearch) );
}
} else
{
dFinished[i] = true;
break;
}
}
bool bEnd = !dFinished.Contains ( false );
i++;
if ( !bPreread && i==iFiles )
bPreread = true;
if ( bPreread )
{
// find the next smallest input
i = 0;
for ( int j=0; j<iFiles; j++ )
if ( !dFinished[j] && ( dFinished[i] || strcmp ( dWords[i], dWords[j] )>0 ) )
i = j;
// merge if we got the same word
if ( !strcmp ( sWord, dWords[i] ) && !bEnd )
{
iDocs += dDocs[i];
iMergedWords++;
} else
{
if ( sWord[0]!='\0' )
{
if ( !bSkipUnique || iDocs>1 )
{
IDFWord_t & tEntry = dEntries.Add ();
tEntry.m_uWordID = sphFNV64 ( sWord );
tEntry.m_iDocs = iDocs;
iTotalWords++;
} else
iSkippedWords++;
}
strncpy ( sWord, dWords[i], sizeof ( dWords[i] ) - 1 );
iDocs = dDocs[i];
}
}
if ( ( iReadWords & 0xffff )==0 || bEnd )
fprintf ( stderr, "read %.1f of %.1f MB, %.1f%% done%c", ( bEnd ? float(iTotalBytes) : float(iReadBytes) )/1000000.0f,
float(iTotalBytes)/1000000.0f, bEnd ? 100.0f : float(iReadBytes)*100.0f/float(iTotalBytes), bEnd ? '\n' : '\r' );
if ( bEnd )
break;
}
fprintf ( stdout, INT64_FMT" documents, " INT64_FMT " words (" INT64_FMT " read, " INT64_FMT " merged, " INT64_FMT " skipped)\n",
iTotalDocuments, iTotalWords, iReadWords, iMergedWords, iSkippedWords );
// write to disk
fprintf ( stdout, "writing %s (%1.fM)...\n", sFilename.cstr(), float(iTotalWords*sizeof(IDFWord_t))/1000000.0f );
dEntries.Sort ( bind ( &IDFWord_t::m_uWordID ) );
CSphWriter tWriter;
if ( !tWriter.OpenFile ( sFilename, sError ) )
return false;
// write file header
tWriter.PutOffset ( iTotalDocuments );
// write data
tWriter.PutBytes ( dEntries.Begin(), dEntries.GetLength()*sizeof(IDFWord_t) );
tWriter.CloseFile();
int tmWallMsec = (int)( ( sphMicroTimer() - tmStart )/1000 );
fprintf ( stdout, "finished in %d.%d sec\n", tmWallMsec/1000, (tmWallMsec/100)%10 );
return true;
}
static bool MergeIDF ( const CSphString & sFilename, const StrVec_t & dFiles, CSphString & sError, bool bSkipUnique )
{
// binary dictionaries are ordered by 64-bit word id, we can use that for merging.
// read every file, check repeating word ids, merge if found, write to disk if not
// memory requirements are about ~4KB per input file (used for buffered reading)
int64_t iTotalDocuments = 0;
int64_t iTotalWords = 0;
int64_t iReadWords = 0;
int64_t iMergedWords = 0;
int64_t iSkippedWords = 0;
int64_t iReadBytes = 0;
int64_t iTotalBytes = 0;
const int64_t tmStart = sphMicroTimer ();
int iFiles = dFiles.GetLength ();
// internal state
CSphVector<CSphAutoreader> dReaders ( iFiles );
CSphVector<IDFWord_t> dWords ( iFiles );
CSphVector<int64_t> dRead ( iFiles );
CSphVector<int64_t> dSize ( iFiles );
CSphVector<BYTE*> dBuffers ( iFiles );
CSphVector<bool> dFinished ( iFiles );
dFinished.Fill ( false );
bool bPreread = false;
// current entry
IDFWord_t tWord;
tWord.m_uWordID = 0;
tWord.m_iDocs = 0;
// preread buffer
const int iEntrySize = sizeof(int64_t)+sizeof(DWORD);
const int iBufferSize = iEntrySize*256;
// initialize vectors
ARRAY_FOREACH ( i, dFiles )
{
if ( !dReaders[i].Open ( dFiles[i], sError ) )
return false;
iTotalDocuments += dReaders[i].GetOffset ();
dRead[i] = 0;
dSize[i] = dReaders[i].GetFilesize() - sizeof( SphOffset_t );
dBuffers[i] = new BYTE [ iBufferSize ];
iTotalBytes += dSize[i];
}
// open output file
CSphWriter tWriter;
if ( !tWriter.OpenFile ( sFilename, sError ) )
return false;
// write file header
tWriter.PutOffset ( iTotalDocuments );
for ( int i=0;; )
{
if ( dRead[i]<dSize[i] )
{
iReadBytes += iEntrySize;
// This part basically does the following:
// dWords[i].m_uWordID = dReaders[i].GetOffset ();
// dWords[i].m_iDocs = dReaders[i].GetDword ();
// but reading by 12 bytes seems quite slow (SetBuffers doesn't help)
// the only way to speed it up is to buffer up a few entries manually
int iOffset = (int)( dRead[i] % iBufferSize );
if ( iOffset==0 )
dReaders[i].GetBytes ( dBuffers[i], ( dSize[i]-dRead[i] )<iBufferSize ? (int)( dSize[i]-dRead[i] ) : iBufferSize );
dWords[i].m_uWordID = *(uint64_t*)( dBuffers[i]+iOffset );
dWords[i].m_iDocs = *(DWORD*)( dBuffers[i]+iOffset+sizeof(uint64_t) );
dRead[i] += iEntrySize;
iReadWords++;
} else
dFinished[i] = true;
bool bEnd = !dFinished.Contains ( false );
i++;
if ( !bPreread && i==iFiles )
bPreread = true;
if ( bPreread )
{
// find the next smallest input
i = 0;
for ( int j=0; j<iFiles; j++ )
if ( !dFinished[j] && ( dFinished[i] || dWords[i].m_uWordID>dWords[j].m_uWordID ) )
i = j;
// merge if we got the same word
if ( tWord.m_uWordID==dWords[i].m_uWordID && !bEnd )
{
tWord.m_iDocs += dWords[i].m_iDocs;
iMergedWords++;
} else
{
if ( tWord.m_uWordID )
{
if ( !bSkipUnique || tWord.m_iDocs>1 )
{
tWriter.PutOffset ( tWord.m_uWordID );
tWriter.PutDword ( tWord.m_iDocs );
iTotalWords++;
} else
iSkippedWords++;
}
tWord = dWords[i];
}
}
if ( ( iReadWords & 0xffff )==0 || bEnd )
fprintf ( stderr, "read %.1f of %.1f MB, %.1f%% done%c", ( bEnd ? float(iTotalBytes) : float(iReadBytes) )/1000000.0f,
float(iTotalBytes)/1000000.0f, bEnd ? 100.0f : float(iReadBytes)*100.0f/float(iTotalBytes), bEnd ? '\n' : '\r' );
if ( bEnd )
break;
}
ARRAY_FOREACH ( i, dFiles )
SafeDeleteArray ( dBuffers[i] );
fprintf ( stdout, INT64_FMT" documents, " INT64_FMT " words (" INT64_FMT " read, " INT64_FMT " merged, " INT64_FMT " skipped)\n",
iTotalDocuments, iTotalWords, iReadWords, iMergedWords, iSkippedWords );
int tmWallMsec = (int)( ( sphMicroTimer() - tmStart )/1000 );
fprintf ( stdout, "finished in %d.%d sec\n", tmWallMsec/1000, (tmWallMsec/100)%10 );
return true;
}
//////////////////////////////////////////////////////////////////////////
static const DWORD META_HEADER_MAGIC = 0x54525053; ///< my magic 'SPRT' header
static const DWORD META_VERSION = 18;
static void InfoMetaSchemaColumn ( CSphReader & rdInfo, DWORD uVersion )
{
CSphString sName = rdInfo.GetString ();
fprintf ( stdout, "%s", sName.cstr());
fprintf ( stdout, " %s", AttrType2Str ((ESphAttr)rdInfo.GetDword ()) );
rdInfo.GetDword (); // ignore rowitem
fprintf ( stdout, " offset %u/", rdInfo.GetDword () );
fprintf ( stdout, "count %u", rdInfo.GetDword() );
fprintf ( stdout, " payload %d", rdInfo.GetByte() );
if ( uVersion>=61 )
fprintf ( stdout, " attr flags %u", rdInfo.GetDword() );
}
static void InfoMetaSchemaField ( CSphReader & rdInfo, DWORD uVersion )
{
if ( uVersion>=57 )
{
CSphString sName = rdInfo.GetString();
fprintf ( stdout, "%s", sName.cstr() );
fprintf ( stdout, " field flags %u", rdInfo.GetDword() );
fprintf ( stdout, " payload %d", rdInfo.GetByte () );
}
else
InfoMetaSchemaColumn ( rdInfo, uVersion );
}
static void InfoMetaSchema ( CSphReader &rdMeta, DWORD uVersion )
{
fprintf ( stdout, "\n ======== SCHEMA ========" );
int iNumFields = rdMeta.GetDword ();
fprintf ( stdout, "\n Fields: %d", iNumFields );
for ( int i = 0; i<iNumFields; ++i )
{
fprintf ( stdout, "\n%02d. ", i + 1 );
InfoMetaSchemaField ( rdMeta, uVersion );
}
int iNumAttrs = rdMeta.GetDword ();
fprintf ( stdout, "\n Attributes: %d", iNumAttrs );
for ( int i = 0; i<iNumAttrs; i++ )
{
fprintf ( stdout, "\n%02d. ", i + 1 );
InfoMetaSchemaColumn ( rdMeta, uVersion );
}
}
static void InfoMetaIndexSettings ( CSphReader &tReader, DWORD uVersion )
{
fprintf ( stdout, "\n ======== TABLE SETTINGS ========" );
fprintf ( stdout, "\nMinPrefixLen: %u", tReader.GetDword () );
fprintf ( stdout, "\nMinInfixLen: %u", tReader.GetDword () );
fprintf ( stdout, "\nMaxSubstringLen: %u", tReader.GetDword () );
fprintf ( stdout, "\nbHtmlStrip: %d", tReader.GetByte () );
fprintf ( stdout, "\nsHtmlIndexAttrs: %s", tReader.GetString ().cstr() );
fprintf ( stdout, "\nsHtmlRemoveElements: %s", tReader.GetString ().cstr() );
fprintf ( stdout, "\nbIndexExactWords: %d", tReader.GetByte () );
fprintf ( stdout, "\neHitless: %u", tReader.GetDword () );
fprintf ( stdout, "\neHitFormat: %u", tReader.GetDword () );
fprintf ( stdout, "\nbIndexSP: %d", tReader.GetByte () );
fprintf ( stdout, "\nsZones: %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\niBoundaryStep: %u", tReader.GetDword () );
fprintf ( stdout, "\niStopwordStep: %u", tReader.GetDword () );
fprintf ( stdout, "\niOvershortStep: %u", tReader.GetDword () );
fprintf ( stdout, "\niEmbeddedLimit: %u", tReader.GetDword () );
fprintf ( stdout, "\neBigramIndex: %d", tReader.GetByte () );
fprintf ( stdout, "\nsBigramWords: %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\nbIndexFieldLens: %d", tReader.GetByte () );
fprintf ( stdout, "\nePreprocessor: %d", tReader.GetByte () );
tReader.GetString(); // was: RLP context
fprintf ( stdout, "\nsIndexTokenFilter: %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\ntBlobUpdateSpace: " INT64_FMT, tReader.GetOffset () );
if ( uVersion>=56 )
fprintf ( stdout, "\niSkiplistBlockSize: %u", tReader.GetDword () );
if ( uVersion>=60 )
fprintf ( stdout, "\nsHitlessFiles: %s", tReader.GetString ().cstr () );
}
static void InfoMetaFileInfo ( CSphReader &tReader )
{
fprintf ( stdout, "\n ======== FILE INFO ========" );
fprintf ( stdout, "\nuSize: " INT64_FMT, tReader.GetOffset () );
fprintf ( stdout, "\nuCTime: " INT64_FMT, tReader.GetOffset () );
fprintf ( stdout, "\nuMTime: " INT64_FMT, tReader.GetOffset () );
fprintf ( stdout, "\nuCRC32: %u", tReader.GetDword () );
}
static bool InfoMetaTokenizerSettings ( CSphReader &tReader, DWORD uVersion)
{
fprintf ( stdout, "\n ======== TOKENIZER SETTINGS ========" );
int m_iType = tReader.GetByte ();
fprintf ( stdout, "\niType: %d", m_iType );
if ( m_iType!=TOKENIZER_UTF8 && m_iType!=TOKENIZER_NGRAM )
{
fprintf (stdout, "\ncan't load an old table with SBCS tokenizer" );
return false;
}
fprintf ( stdout, "\nsCaseFolding: %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\niMinWordLen: %u", tReader.GetDword() );
bool bsyn = !!tReader.GetByte ();
fprintf ( stdout, "\nbEmbeddedSynonyms: %d", bsyn );
if ( bsyn )
{
int nSynonyms = ( int ) tReader.GetDword ();
fprintf ( stdout, "\nnSynonyms: %d", nSynonyms );
for ( int i=0; i<nSynonyms; ++i)
fprintf ( stdout, "\nEmbedded Syn(%d): %s", i, tReader.GetString ().cstr () );
}
fprintf ( stdout, "\nsSynonymsFile: %s", tReader.GetString ().cstr () );
InfoMetaFileInfo ( tReader );
fprintf ( stdout, "\nsBoundary: %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\nsIgnoreChars : %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\niNgramLen : %u", tReader.GetDword ());
fprintf ( stdout, "\nsNgramChars : %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\nsBlendChars : %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\nsBlendMode : %s", tReader.GetString ().cstr () );
return true;
}
static void InfoMetaDictionarySettings ( CSphReader & tReader )
{
fprintf ( stdout, "\n ======== DICTIONARY SETTINGS ========" );
fprintf ( stdout, "\nsMorphology : %s", tReader.GetString ().cstr () );
fprintf ( stdout, "\nsMorphFields : %s", tReader.GetString ().cstr () );
bool bEmbeddedStopwords = !!tReader.GetByte ();
fprintf ( stdout, "\nbEmbeddedStopwords : %d", bEmbeddedStopwords );
if ( bEmbeddedStopwords )
{
int nStopwords = ( int ) tReader.GetDword ();
fprintf ( stdout, "\nnStopwords : %d", nStopwords );
for ( int i = 0; i<nStopwords; ++i )
fprintf ( stdout, "\nEmbedded Stp(%d): " INT64_FMT, i, tReader.UnzipOffset () );
}
fprintf ( stdout, "\nsStopwords : %s", tReader.GetString ().cstr () );
int nFiles = tReader.GetDword ();
fprintf ( stdout, "\nnFiles : %d", nFiles );
for ( int i = 0; i<nFiles; ++i )
{
fprintf ( stdout, "\nFile %d: %s", i+1, tReader.GetString ().cstr () );
InfoMetaFileInfo ( tReader );
}
bool bEmbeddedWordforms = !!tReader.GetByte ();
fprintf ( stdout, "\nbEmbeddedWordforms : %d", bEmbeddedWordforms );
if ( bEmbeddedWordforms )
{
int nWordforms = ( int ) tReader.GetDword ();
fprintf ( stdout, "\nnWordforms : %d", nWordforms );
for ( int i = 0; i<nWordforms; ++i )
fprintf ( stdout, "\nEmbedded Wrd(%d): %s", i, tReader.GetString ().cstr() );
}
int iWrdForms = tReader.GetDword ();
fprintf ( stdout, "\niWordForms : %d", iWrdForms );
for ( int i = 0; i<iWrdForms; ++i )
{
fprintf ( stdout, "\nFile %d: %s", i + 1, tReader.GetString ().cstr () );
InfoMetaFileInfo ( tReader );
}
fprintf ( stdout, "\niMinStemmingLen : %u", tReader.GetDword () );
fprintf ( stdout, "\nbWordDict : %d", tReader.GetByte () );
fprintf ( stdout, "\nbStopwordsUnstemmed : %d", tReader.GetByte () );
fprintf ( stdout, "\nsMorphFingerprint : %s", tReader.GetString ().cstr() );
}
static void InfoMetaFieldFilterSettings ( CSphReader & tReader )
{
fprintf ( stdout, "\n ======== FIELD FILTER SETTINGS ========" );
int nRegexps = tReader.GetDword ();
fprintf (stdout, "\n %d filters", nRegexps);
if ( !nRegexps )
return;
for (int i=0; i<nRegexps; ++i)
fprintf (stdout, "\n Filter(%d) = %s", i, tReader.GetString ().cstr());
}
static void InfoMeta ( const CSphString & sMeta )
{
fprintf ( stdout, "\nDescribing meta %s", sMeta.cstr());
CSphString sError;
CSphAutoreader rdMeta;
if ( !rdMeta.Open ( sMeta, sError ) )
{
fprintf (stdout, "\n unable to open file: %s", sError.cstr());
return;
}
DWORD dwFoo = rdMeta.GetDword();
fprintf ( stdout, "\nMagick: 0x%x (expected 0x%x)", (uint32_t)dwFoo, (uint32_t)META_HEADER_MAGIC );
if ( dwFoo!=META_HEADER_MAGIC )
{
fprintf ( stdout, "\nwrong magick!");
return;
}
DWORD uVersion = rdMeta.GetDword ();
fprintf ( stdout, "\nVersion: %u (expected 1 to %u)", uVersion, META_VERSION );
if ( uVersion==0 || uVersion>META_VERSION )
{
fprintf ( stdout, "%s is v.%u, binary is v.%u", sMeta.cstr (), uVersion, META_VERSION );
return;
}
fprintf ( stdout, "\nTotal documents: %u", rdMeta.GetDword());
fprintf ( stdout, "\nTotal bytes: " INT64_FMT, rdMeta.GetOffset () );
fprintf ( stdout, "\nTID: " INT64_FMT, rdMeta.GetOffset () );
DWORD uSettingsVer = rdMeta.GetDword ();
fprintf (stdout, "\n Settings ver: %u", uSettingsVer );
InfoMetaSchema(rdMeta, uSettingsVer);
InfoMetaIndexSettings(rdMeta, uSettingsVer);
InfoMetaTokenizerSettings (rdMeta, uSettingsVer);
InfoMetaDictionarySettings(rdMeta);
fprintf ( stdout, "\niWordsCheckpoint: %u", rdMeta.GetDword () );
fprintf ( stdout, "\niMaxCodepointLength: %u", rdMeta.GetDword () );
fprintf ( stdout, "\niBloomKeyLen: %d", rdMeta.GetByte () );
fprintf ( stdout, "\niBloomHashesCount: %d", rdMeta.GetByte () );
InfoMetaFieldFilterSettings ( rdMeta );
CSphFixedVector<int> dChunkNames ( 0 );
int iLen = ( int ) rdMeta.GetDword ();
fprintf ( stdout, "\nNum of Chunknames: %d", iLen );
dChunkNames.Reset ( iLen );
rdMeta.GetBytes ( dChunkNames.Begin (), iLen * sizeof ( int ) );
for ( int nm : dChunkNames)
fprintf (stdout, "\n %d", nm);
if ( uVersion>=17 )
fprintf ( stdout, "\nSoft RAM limit: " INT64_FMT, rdMeta.GetOffset () );
}
struct IndexInfo_t
{
bool m_bEnabled {false};
DWORD m_nDocs {0};
CSphString m_sName;
CSphString m_sPath;
CSphFixedVector<DocID_t> m_dKilllist;
KillListTargets_c m_tTargets;
CSphMappedBuffer<BYTE> m_tLookup;
DeadRowMap_Disk_c m_tDeadRowMap;
IndexInfo_t()
: m_dKilllist ( 0 )
{}
};
static void ApplyKilllist ( IndexInfo_t & tTarget, const IndexInfo_t & tKiller, const KillListTarget_t & tSettings )
{
if ( tSettings.m_uFlags & KillListTarget_t::USE_DOCIDS )
{
LookupReaderIterator_c tTargetReader ( tTarget.m_tLookup.GetReadPtr() );
LookupReaderIterator_c tKillerReader ( tKiller.m_tLookup.GetReadPtr() );
KillByLookup ( tTargetReader, tKillerReader, tTarget.m_tDeadRowMap );
}
if ( tSettings.m_uFlags & KillListTarget_t::USE_KLIST )
{
LookupReaderIterator_c tTargetReader ( tTarget.m_tLookup.GetReadPtr() );
DocidListReader_c tKillerReader ( tKiller.m_dKilllist );
KillByLookup ( tTargetReader, tKillerReader, tTarget.m_tDeadRowMap );
}
}
static void ApplyKilllists ( CSphConfig & hConf )
{
CSphFixedVector<IndexInfo_t> dIndexes ( hConf["index"].GetLength() );
int iIndex = 0;
for ( auto& tIndex_ : hConf["index"] )
{
CSphConfigSection & hIndex = tIndex_.second;
if ( !hIndex("path") )
continue;
const CSphVariant * pType = hIndex ( "type" );
if ( pType && ( *pType=="rt" || *pType=="distributed" || *pType=="percolate" ) )
continue;
IndexInfo_t & tIndex = dIndexes[iIndex++];
tIndex.m_sName = tIndex_.first.cstr();
tIndex.m_sPath = RedirectToRealPath ( hIndex["path"].strval() );
IndexFiles_c tIndexFiles ( tIndex.m_sPath, tIndex.m_sName.cstr () );
if ( !tIndexFiles.CheckHeader() )
{
fprintf ( stdout, "WARNING: unable to index header for table %s\n", tIndex.m_sName.cstr() );
continue;
}
// no lookups prior to v.54
if ( tIndexFiles.GetVersion() < 54 )
{
fprintf ( stdout, "WARNING: table '%s' version: %u, min supported is 54\n", tIndex.m_sName.cstr(), tIndexFiles.GetVersion() );
continue;
}
CSphString sError;
{
auto pIndex = sphCreateIndexPhrase ( "", tIndex.m_sPath );
if ( !pIndex->LoadKillList ( &tIndex.m_dKilllist, tIndex.m_tTargets, sError ) )
{
fprintf ( stdout, "WARNING: unable to load kill-list for table %s: %s\n", tIndex.m_sName.cstr(), sError.cstr() );
continue;
}
StrVec_t dWarnings;
if ( !pIndex->Prealloc ( false, nullptr, dWarnings ) )
{
fprintf ( stdout, "WARNING: unable to prealloc table %s: %s\n", tIndex.m_sName.cstr(), sError.cstr() );
continue;
}
for ( const auto & i : dWarnings )
fprintf ( stdout, "WARNING: table %s: %s\n", tIndex.m_sName.cstr(), i.cstr() );
tIndex.m_nDocs = (DWORD)pIndex->GetStats().m_iTotalDocuments;
}
CSphString sLookup;
sLookup.SetSprintf ( "%s.spt", tIndex.m_sPath.cstr() );
if ( !tIndex.m_tLookup.Setup ( sLookup.cstr(), sError, false ) )
{
fprintf ( stdout, "WARNING: unable to load lookup for table %s: %s\n", tIndex.m_sName.cstr(), sError.cstr() );
continue;
}
CSphString sDeadRowMap;
sDeadRowMap.SetSprintf ( "%s.spm", tIndex.m_sPath.cstr() );
if ( !tIndex.m_tDeadRowMap.Prealloc ( tIndex.m_nDocs, sDeadRowMap, sError ) )
{
fprintf ( stdout, "WARNING: unable to load dead row map for table %s: %s\n", tIndex.m_sName.cstr(), sError.cstr() );
continue;
}
tIndex.m_bEnabled = true;
}
for ( const auto & tIndex : dIndexes )
{
if ( !tIndex.m_bEnabled || !tIndex.m_tTargets.m_dTargets.GetLength() )
continue;
fprintf ( stdout, "applying kill-list of table %s\n", tIndex.m_sName.cstr() );
for ( const auto & tTarget : tIndex.m_tTargets.m_dTargets )
{
if ( tTarget.m_sIndex==tIndex.m_sName )
{
fprintf ( stdout, "WARNING: table '%s': appying killlist to itself\n", tIndex.m_sName.cstr() );
continue;
}
for ( auto & tTargetIndex : dIndexes )
{
if ( !tTargetIndex.m_bEnabled || tTarget.m_sIndex!=tTargetIndex.m_sName )
continue;
ApplyKilllist ( tTargetIndex, tIndex, tTarget );
}
}
}
for ( auto & tIndex : dIndexes )
{
if ( !tIndex.m_bEnabled )
continue;
// flush maps
CSphString sError;
if ( !tIndex.m_tDeadRowMap.Flush ( true, sError ) )
fprintf ( stdout, "WARNING: table '%s': unable to flush dead row map: %s\n", tIndex.m_sName.cstr(), sError.cstr() );
// delete killlists
CSphString sKilllist;
sKilllist.SetSprintf ( "%s.spk", tIndex.m_sPath.cstr() );
if ( !sphIsReadable(sKilllist) )
continue;
::unlink ( sKilllist.cstr() );
}
}
static void ShowVersion()
{
const char * szColumnarVer = GetColumnarVersionStr();
CSphString sColumnar = "";
if ( szColumnarVer )
sColumnar.SetSprintf ( " (columnar %s)", szColumnarVer );
const char * sSiVer = GetSecondaryVersionStr();
CSphString sSi = "";
if ( sSiVer )
sSi.SetSprintf ( " (secondary %s)", sSiVer );
fprintf ( stdout, "%s%s%s%s", szMANTICORE_NAME, sColumnar.cstr(), sSi.cstr(), szMANTICORE_BANNER_TEXT );
}
static void ShowHelp ()
{
/// notice: set tab width=8 to correctly see content of the message