-
Notifications
You must be signed in to change notification settings - Fork 218
/
text-index.c
4239 lines (3633 loc) · 114 KB
/
text-index.c
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
/*
This file is part of VK/KittenPHP-DB-Engine.
VK/KittenPHP-DB-Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
VK/KittenPHP-DB-Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VK/KittenPHP-DB-Engine. If not, see <http://www.gnu.org/licenses/>.
This program is released under the GPL with the additional exemption
that compiling, linking, and/or using OpenSSL is allowed.
You are free to remove this exemption from derived works.
Copyright 2009-2013 Vkontakte Ltd
2009-2013 Nikolai Durov
2009-2013 Andrei Lopatin
*/
#define _FILE_OFFSET_BITS 64
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <byteswap.h>
#include "crc32.h"
#include "server-functions.h"
#include "kdb-text-binlog.h"
#include "kdb-data-common.h"
#include "word-split.h"
#include "stemmer.h"
#include "listcomp.h"
#include "kfs.h"
#include "text-index-layout.h"
#define VERSION_STR "text-index-0.31"
#define MAX_NONDICT_FREQ 8
#define MAX_MESSAGE_SIZE (1 << 19)
#ifdef _LP64
//#define MAX_USER_MESSAGES ((3 << 24) + (1 << 20))
# define MAX_USER_MESSAGES (1 << 27)
#else
# define MAX_USER_MESSAGES (1 << 24)
#endif
extern int binlog_zipped;
int verbosity, search_enabled, history_enabled, use_stemmer, hashtags_enabled, searchtags_enabled;
int force_pm, ignore_delete_first_messages, extra_mask_changes;
int now;
char *fnames[3];
int fd[3];
long long fsize[3];
char *progname = "text-index", *username, *binlogname, *newidxname, *temp_binlog_directory;
char metaindex_fname_buff[256], binlog_fname_buff[256], temp_binlog_fname_buff[256];
int temp_binlog_dir_len;
#define MAX_USERS (1 << 21)
#define NEGATIVE_USER_OFFSET (1 << 20)
/* stats counters */
int start_time;
long long binlog_loaded_size;
double binlog_load_time, last_process_time;
unsigned log_limit_crc32;
int dict_size;
long long word_instances, nonword_instances;
int max_nd_freq = MAX_NONDICT_FREQ;
int last_global_id, last_global_id0;
/* file utils */
int open_file (int x, char *fname, int creat) {
fnames[x] = fname;
fd[x] = open (fname, creat > 0 ? O_RDWR | O_CREAT | O_EXCL : O_RDONLY, 0600);
if (creat < 0 && fd[x] < 0) {
if (fd[x] < 0) {
fprintf (stderr, "%s: cannot open %s: %m\n", progname, fname);
}
return -1;
}
if (fd[x] < 0) {
fprintf (stderr, "%s: cannot open %s: %m\n", progname, fname);
exit(1);
}
fsize[x] = lseek (fd[x], 0, SEEK_END);
if (fsize[x] < 0) {
fprintf (stderr, "%s: cannot seek %s: %m\n", progname, fname);
exit(2);
}
lseek (fd[x], 0, SEEK_SET);
if (verbosity) {
fprintf (stderr, "opened file %s, fd=%d, size=%lld\n", fname, fd[x], fsize[x]);
}
return fd[x];
}
/*
*
* DATA STRUCTURES
*
*/
typedef struct message message_t;
typedef struct user user_t;
struct message {
message_t *prev;
int flags;
int user_id;
int local_id;
long long legacy_id;
int global_id;
int peer_id;
int peer_msg_id;
int date;
unsigned ip;
int port;
unsigned front;
unsigned long long ua_hash;
int len;
int extra[0];
char text[1];
};
struct message_short {
message_t *prev;
int flags;
int user_id;
int local_id;
};
struct message_medium {
message_t *prev;
int flags;
int user_id;
int local_id;
int extra[0];
};
struct user {
message_t *last;
int user_id;
int first_local_id;
int last_local_id;
};
struct sublist_descr {
int xor_mask, and_mask;
};
struct sublist_descr Messages_Sublists[] = {
{ 0, TXF_OUTBOX | TXF_SPAM | TXF_DELETED },
{ TXF_OUTBOX, TXF_OUTBOX | TXF_SPAM | TXF_DELETED },
{ TXF_UNREAD, TXF_OUTBOX | TXF_UNREAD | TXF_SPAM | TXF_DELETED },
{ TXF_IMPORTANT, TXF_IMPORTANT | TXF_SPAM | TXF_DELETED },
{ TXF_FRIENDS, TXF_OUTBOX | TXF_FRIENDS | TXF_SPAM | TXF_DELETED },
{ TXF_OUTBOX | TXF_FRIENDS, TXF_OUTBOX | TXF_FRIENDS | TXF_SPAM | TXF_DELETED },
{ TXF_UNREAD | TXF_CHAT, TXF_OUTBOX | TXF_UNREAD | TXF_CHAT | TXF_SPAM | TXF_DELETED },
{ 0, TXF_OUTBOX | TXF_CHAT | TXF_SPAM | TXF_DELETED },
{ TXF_OUTBOX, TXF_OUTBOX | TXF_CHAT | TXF_SPAM | TXF_DELETED },
{ TXF_UNREAD, TXF_OUTBOX | TXF_UNREAD | TXF_CHAT | TXF_SPAM | TXF_DELETED },
{ TXF_SPAM, TXF_SPAM | TXF_DELETED },
{ TXF_UNREAD | TXF_SPAM, TXF_UNREAD | TXF_SPAM | TXF_DELETED },
{ 0, 0 }
};
struct sublist_descr Statuses_Sublists[] = {
{ 0, TXFS_WALL | TXFS_REPLY | TXFS_COPY | TXFS_SPAM | TXFS_DELETED },
{ 0, TXFS_WALL | TXFS_REPLY | TXFS_SPAM | TXFS_DELETED },
{ TXFS_COPY, TXFS_WALL | TXFS_REPLY | TXFS_COPY | TXFS_SPAM | TXFS_DELETED },
{ TXFS_COPIED, TXFS_WALL | TXFS_REPLY | TXFS_COPY | TXFS_COPIED | TXFS_SPAM | TXFS_DELETED },
{ 0, TXFS_REPLY | TXFS_SPAM | TXFS_DELETED },
{ TXFS_WALL, TXFS_WALL | TXFS_REPLY | TXFS_SPAM | TXFS_DELETED },
{ TXFS_WALL, TXFS_WALL | TXFS_SPAM | TXFS_DELETED },
{ 0, TXFS_SPAM | TXFS_DELETED },
{ TXFS_LOCATION, TXFS_WALL | TXFS_REPLY | TXFS_COPY | TXFS_LOCATION | TXFS_SPAM | TXFS_DELETED },
{ 0, TXFS_FRIENDSONLY | TXFS_WALL | TXFS_REPLY | TXFS_SPAM | TXFS_DELETED },
{ 0, TXFS_FRIENDSONLY | TXFS_REPLY | TXFS_SPAM | TXFS_DELETED },
{ 0, 0 }
};
struct sublist_descr Forum_Sublists[] = {
{ TXFP_TOPIC, TXFP_REPLY | TXFP_TOPIC | TXFP_FIXED | TXFP_CLOSED | TXFP_SPAM | TXFP_DELETED },
{ TXFP_TOPIC, TXFP_REPLY | TXFP_TOPIC | TXFP_FIXED | TXFP_SPAM | TXFP_DELETED },
{ TXFP_TOPIC | TXFP_FIXED, TXFP_REPLY | TXFP_TOPIC | TXFP_FIXED | TXFP_SPAM | TXFP_DELETED },
{ TXFP_TOPIC | TXFP_CLOSED, TXFP_REPLY | TXFP_TOPIC | TXFP_CLOSED | TXFP_SPAM | TXFP_DELETED },
{ 0, 0 }
};
struct sublist_descr Comments_Sublists[] = {
{ 0, TXF_SPAM | TXF_DELETED },
{ TXF_SPAM, TXF_SPAM | TXF_DELETED },
{ 0, 0 }
};
struct sublist_descr *Sublists = Messages_Sublists;
struct sublist_descr PeerFlagFilter = { 0, TXF_DELETED | TXF_SPAM },
Statuses_PeerFlagFilter = { 0, TXFS_DELETED | TXFS_SPAM | TXFS_COPY },
Forum_PeerFlagFilter = { 0, TXFP_TOPIC | TXFP_DELETED | TXFP_SPAM },
Comments_PeerFlagFilter = { 0, TXF_DELETED | TXF_SPAM };
union packed_sublist_descr Sublists_packed[MAX_SUBLISTS+1];
int sublists_num = 0;
int preserve_legacy_ids = 0;
int msg_date_sort = 0;
long long min_legacy_id, max_legacy_id;
#define FITS_AN_INT(a) ( (a) >= -0x80000000LL && (a) <= 0x7fffffffLL )
#define LONG_LEGACY_IDS (min_legacy_id < -0x80000000LL || max_legacy_id > 0x7fffffffLL)
message_t *threshold_msg;
/*
*
* WORD HASH SUPPORT, HUFFMAN CODER
*
*/
#ifdef _LP64
# define PRIME 30000001
#else
# define PRIME 1000003
#endif
//#define TEXT_SIZE (1L << 14)
typedef struct word word_t;
struct word {
word_t *next;
long long freq;
unsigned code;
unsigned char code_len;
unsigned char len;
char str[1];
};
typedef struct word_hash {
int cnt;
int tot_len;
// word_t *A[PRIME];
word_t **A;
} word_hash_t;
struct char_dictionary {
/* first three fields are needed to pack */
char code_len[256];
long long freq[256]; /* NB: in index they are int's */
unsigned code[256];
/* next three fields are needed to unpack */
unsigned first_codes[32];
int *code_ptr[32];
int chars[256];
int max_bits;
} WordCharDict, NotWordCharDict;
word_hash_t Words, NotWords;
word_t **WordList, **NotWordList;
word_t *WordFreqWords[256], *NotWordFreqWords[256];
static word_t *hash_get (word_hash_t *Set, char *str, int len, int force) {
int h = 0, i;
word_t *w;
assert (len >= 0 && len < 128);
for (i = 0; i < len; i++) {
if (h > (1 << (8*sizeof(int)-2))/37) {
h %= PRIME;
}
h = h * 37 + (unsigned char) str[i];
}
if (h >= PRIME) {
h %= PRIME;
}
assert (h >= 0 && h < PRIME);
w = Set->A[h];
while (w) {
if (w->len == len && !memcmp (w->str, str, len)) {
return w;
}
w = w->next;
}
if (!force) {
return w;
}
w = zmalloc (sizeof (word_t) + len);
assert (w);
w->next = Set->A[h];
Set->A[h] = w;
Set->cnt++;
Set->tot_len += len;
w->freq = 0;
w->code = 0;
w->code_len = 0;
w->len = len;
memcpy (w->str, str, len);
w->str[len] = 0;
return w;
}
int count_rare_words (const word_hash_t *Set, int max_freq) {
int cnt = 0, i;
const word_t *ptr;
for (i = 0; i < PRIME; i++) {
for (ptr = Set->A[i]; ptr; ptr = ptr->next) {
if (ptr->freq <= max_freq) {
cnt++;
}
}
}
return cnt;
}
long import_freq (long long *table, const word_hash_t *Set) {
long cnt = 0;
int i;
const word_t *ptr;
for (i = 0; i < PRIME; i++) {
for (ptr = Set->A[i]; ptr; ptr = ptr->next) {
if (ptr->freq > 0) {
*table++ = ptr->freq;
cnt++;
}
}
}
return cnt;
}
void preprocess_text (char *text, int text_len) {
char *ptr;
int len, w_c = 0;
word_t *w;
//fprintf (stderr, "TEXT: %s\n", text);
ptr = text;
while (*ptr) {
len = get_word(ptr);
if (len < 0 || len >= 128) {
fprintf (stderr, "WORD: '%.*s'\nTEXT: '%.*s'\n", len, ptr, text_len, text);
}
assert (len >= 0);
w = hash_get (&Words, ptr, len, 1);
w->freq++;
// fprintf (stderr, "%p: word='%s', freq=%d\n", w, w->str, w->freq);
ptr += len;
w_c++;
if (!*ptr) {
break;
}
len = get_notword(ptr);
if (len < 0) {
break;
}
/*if (len < 0 || len >= 4096)
fprintf (stderr, "NOT WORD: %d '%.*s'\n", len, len, ptr);*/
// fprintf (stderr, "NOT-WORD: '%.*s'\n", len, ptr);
w = hash_get (&NotWords, ptr, len, 1);
w->freq++;
// fprintf (stderr, "%p: word='%s', freq=%d\n", w, w->str, w->freq);*/
ptr += len;
w_c++;
}
word_instances += (w_c >> 1);
nonword_instances += ((w_c + 1) >> 1);
}
int pack_text (char *dest, int max_packed_len, char *text, int text_len) {
char *ptr, *wptr = dest, *wptr_e = dest + max_packed_len;
int len, x, y = 1;
word_t *w;
//fprintf (stderr, "TEXT: %s\n", text);
#define EncodeBit(__b) { if (y >= 0x100) { *wptr++ = (char) y; y = 1; } y <<= 1; y += __b; }
#define Encode(__c,__l) { int t=__l; int c=__c; while (t>0) { EncodeBit(c < 0); c<<=1; t--; } }
ptr = text;
while (*ptr) {
assert (wptr <= wptr_e);
len = get_word (ptr);
assert (len >= 0);
// fprintf (stderr, "WORD: '%.*s'\n", len, ptr);
w = hash_get (&Words, ptr, len, 0);
if (w && w->freq > 0) {
Encode (w->code, w->code_len);
ptr += len;
} else {
w = WordFreqWords[len];
Encode (w->code, w->code_len);
while (len > 0) {
x = (unsigned char) *ptr++;
Encode (WordCharDict.code[x], WordCharDict.code_len[x]);
len--;
}
}
if (!*ptr) {
break;
}
len = get_notword(ptr);
if (len < 0) {
break;
}
// fprintf (stderr, "NOT-WORD: '%.*s'\n", len, ptr);
w = hash_get (&NotWords, ptr, len, 0);
if (w && w->freq > 0) {
Encode (w->code, w->code_len);
ptr += len;
} else {
w = NotWordFreqWords[len];
Encode (w->code, w->code_len);
while (len > 0) {
x = (unsigned char) *ptr++;
Encode (NotWordCharDict.code[x], NotWordCharDict.code_len[x]);
len--;
}
}
}
EncodeBit (1);
while (y < 0x100) {
EncodeBit (0);
}
*wptr++ = (char) y;
assert (wptr <= wptr_e);
return wptr - dest;
#undef EncodeBit
#undef Encode
}
/*static void isort (int *A, int b) {
int i = 0, j = b;
int h, t;
if (b <= 0) { return; }
h = A[b >> 1];
do {
while (A[i] < h) { i++; }
while (A[j] > h) { j--; }
if (i <= j) {
t = A[i]; A[i++] = A[j]; A[j--] = t;
}
} while (i <= j);
isort (A+i, b-i);
isort (A, j);
}*/
static void ull_sort (unsigned long long *A, int b) {
int i = 0, j = b;
unsigned long long h, t;
if (b <= 0) { return; }
h = A[b >> 1];
do {
while (A[i] < h) { i++; }
while (A[j] > h) { j--; }
if (i <= j) {
t = A[i]; A[i++] = A[j]; A[j--] = t;
}
} while (i <= j);
ull_sort (A+i, b-i);
ull_sort (A, j);
}
static unsigned long long WordCRC[MAX_TEXT_LEN / 2];
int compute_message_distinct_words (char *text, int text_len) {
char *ptr = text;
int count = 0, len;
static char buff[1024];
assert ((unsigned) text_len < MAX_TEXT_LEN);
while (*ptr == 1 || *ptr == 2) {
char *tptr = ptr;
while (*tptr && *tptr != 9) {
++tptr;
}
if (*ptr == 2) {
char tptr_peek = *tptr;
if (*tptr == 9) {
*tptr = 0;
}
while (*ptr && *ptr != 32) {
++ptr;
}
while (*ptr) {
len = get_notword (ptr);
if (len < 0) {
break;
}
if (ptr + len > tptr && ptr <= tptr) {
fprintf (stderr, "get_notword went out of bounds: ptr=%p len=%d\n", ptr, len);
}
ptr += len;
len = get_word (ptr);
assert (len >= 0);
if (ptr + len > tptr && ptr <= tptr) {
fprintf (stderr, "get_word went out of bounds: ptr=%p len=%d\n", ptr, len);
}
if (len > 0) {
assert (count < MAX_TEXT_LEN / 2);
WordCRC[count++] = crc64 (buff, my_lc_str (buff, ptr, len)) & -64;
}
ptr += len;
}
*tptr = tptr_peek;
if (ptr != tptr && (ptr > tptr || verbosity > 0)) {
fprintf (stderr, "warning: error while parsing kludge in '%.*s': text=%p ptr=%p tptr=%p\n", text_len, text, text, ptr, tptr);
}
assert (ptr <= tptr);
}
ptr = tptr;
if (*ptr) {
++ptr;
}
}
while (*ptr) {
len = get_word(ptr);
assert (len >= 0);
if (len > 0) {
assert (count < MAX_TEXT_LEN / 2);
WordCRC[count++] = crc64 (buff, my_lc_str (buff, ptr, len)) & -64;
}
ptr += len;
len = get_notword (ptr);
if (len < 0) {
break;
}
ptr += len;
}
ull_sort (WordCRC, count - 1);
if (count > 0) {
int newcount = 1, i;
for (i = 1; i < count; i++) {
if (WordCRC[i] != WordCRC[i - 1]) {
WordCRC[newcount++] = WordCRC[i];
}
}
return newcount;
}
return 0;
}
/*
* CREATE/WRITE TEMPORARY FILES
*/
char prec_mask_intcount[MAX_EXTRA_MASK+2];
static inline int extra_mask_intcount (int mask) {
return prec_mask_intcount[mask & MAX_EXTRA_MASK];
}
static int conv_uid (int user_id);
#define MAX_PASSES 129
#define TEMP_WRITE_BUFFER_SIZE (1L << 23)
struct buff_file {
char *wst, *wptr, *wend;
char *filename;
int fd;
unsigned crc32;
long long wpos;
int timestamp;
int after_crc32;
int global_id;
};
int passes;
int pass_min_uid[MAX_PASSES+1];
int is_message_event;
struct buff_file temp_file[MAX_PASSES];
long long tmp_bytes_written;
void tmp_write_lev (struct buff_file *T, const void *L, int size);
int close_temp_files (int op) {
int i, c = 0;
if (!temp_binlog_directory) {
return -1;
}
for (i = 0; i < passes; i++) {
if ((op & 1) && temp_file[i].fd > 0) {
assert (close (temp_file[i].fd) >= 0);
temp_file[i].fd = 0;
c++;
}
if ((op & 2) && temp_file[i].filename) {
assert (unlink (temp_file[i].filename) >= 0);
free (temp_file[i].filename);
temp_file[i].filename = 0;
c++;
}
}
return c;
}
int open_temp_files (void) {
static struct lev_start lstart = {.type = LEV_START, .schema_id = 0x504d4554};
int l, i;
assert (temp_binlog_directory && passes);
l = strlen (temp_binlog_directory);
assert (l <= 120);
memcpy (temp_binlog_fname_buff, temp_binlog_directory, l);
if (!l) {
temp_binlog_fname_buff[0] = '.';
l = 1;
}
if (temp_binlog_fname_buff[l-1] != '/') {
temp_binlog_fname_buff[l++] = '/';
}
temp_binlog_fname_buff[l] = 0;
temp_binlog_dir_len = l;
if (access (temp_binlog_fname_buff, W_OK | X_OK) < 0) {
mkdir (temp_binlog_fname_buff, 0750);
if (access (temp_binlog_fname_buff, W_OK | X_OK) < 0) {
fprintf (stderr, "cannot create files in directory %s: %m\n", temp_binlog_fname_buff);
exit (3);
}
}
for (i = 0; i < passes; i++) {
struct buff_file *T = temp_file + i;
memset (T, 0, sizeof (*T));
sprintf (temp_binlog_fname_buff + l, "temp%d_%d_%d.bin", log_split_min, log_split_mod, i);
T->filename = strdup (temp_binlog_fname_buff);
int fd = open (T->filename, O_RDWR);
if (fd < 0) {
fd = open (T->filename, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd < 0) {
fprintf (stderr, "cannot create temporary file %s: %m\n", T->filename);
exit (3);
}
}
assert (lock_whole_file (fd, F_WRLCK) > 0);
if (ftruncate (fd, 0) < 0) {
fprintf (stderr, "cannot truncate temporary file %s: %m\n", T->filename);
exit (3);
}
assert (fd > 2);
T->fd = fd;
T->crc32 = -1;
T->wptr = T->wst = zmalloc (TEMP_WRITE_BUFFER_SIZE);
T->wend = T->wst + TEMP_WRITE_BUFFER_SIZE;
tmp_write_lev (T, &lstart, 24);
}
return passes;
}
void tmp_flush_out (struct buff_file *T) {
int b = T->wptr - T->wst;
if (!b) {
T->wptr = T->wst;
return;
}
assert (b > 0 && b <= TEMP_WRITE_BUFFER_SIZE);
int a = write (T->fd, T->wst, b);
if (a < b) {
if (a >= 0) {
int c = write (T->fd, T->wst + a, b - a);
a = (c >= 0 ? a + c : c);
}
}
if (a != b) {
if (a < 0) {
fprintf (stderr, "cannot write %d bytes to temporary file %s: %m\n", b, T->filename);
} else {
fprintf (stderr, "cannot write %d bytes to temporary file %s: only %d bytes written\n", b, T->filename, a);
}
close_temp_files (3);
exit (3);
}
T->wpos += a;
tmp_bytes_written += a;
T->wptr = T->wst;
if (verbosity > 0) {
fprintf (stderr, "%d bytes written to temporary file %s\n", a, T->filename);
}
}
void tmp_write_lev (struct buff_file *T, const void *L, int size) {
size = (size + 3) & -4;
assert (!(size & (-0x100000)));
if (T->wptr + size > T->wend) {
tmp_flush_out (T);
assert (T->wptr + size <= T->wend);
}
memcpy (T->wptr, L, size);
T->wptr += size;
T->after_crc32 += size;
T->crc32 = crc32_partial (L, size, T->crc32);
}
void tmp_write_ts (struct buff_file *T, int timestamp) {
static struct lev_timestamp LogTs = {.type = LEV_TIMESTAMP};
LogTs.timestamp = T->timestamp = timestamp;
tmp_write_lev (T, &LogTs, sizeof (LogTs));
}
void tmp_write_crc32 (struct buff_file *T, int timestamp) {
static struct lev_crc32 LogCrc = {.type = LEV_CRC32};
LogCrc.timestamp = T->timestamp = timestamp;
LogCrc.pos = T->wpos + (T->wptr - T->wst);
LogCrc.crc32 = ~T->crc32;
tmp_write_lev (T, &LogCrc, sizeof (LogCrc));
T->after_crc32 = 0;
}
void tmp_adjust_global_id (struct buff_file *T, int is_message_event) {
int v = last_global_id - T->global_id - is_message_event;
assert (v >= 0);
static struct lev_generic lgid;
if (v > 0 && v <= 0xffff) {
lgid.type = v + LEV_TX_INCREASE_GLOBAL_ID_SMALL;
tmp_write_lev (T, &lgid, 4);
} else if (v) {
lgid.type = LEV_TX_INCREASE_GLOBAL_ID_LARGE;
lgid.a = v;
tmp_write_lev (T, &lgid, 8);
}
T->global_id = last_global_id;
}
void tmp_write_logevent (struct buff_file *T, const void *L, int size) {
if (T->after_crc32 > 16384) {
tmp_write_crc32 (T, now);
} else if (now != T->timestamp && now) {
tmp_write_ts (T, now);
}
if (is_message_event) {
tmp_adjust_global_id (T, 1);
}
tmp_write_lev (T, L, size);
}
int tmp_dispatch_logevent (const void *L, int size) {
assert (size >= 8);
int user_id = ((int *) L)[1];
int uid = conv_uid (user_id);
if (uid < 0) {
return 0;
}
int a = -1, b = passes, c;
while (b - a > 1) {
c = ((a + b) >> 1);
if (pass_min_uid[c] <= uid) {
a = c;
} else {
b = c;
}
}
assert (a >= 0 && uid < pass_min_uid[b]);
tmp_write_logevent (&temp_file[a], L, size);
return 1;
}
int tmp_dispatch_logevent_all (const void *L, int size) {
int i;
for (i = 0; i < passes; i++) {
tmp_write_logevent (&temp_file[i], L, size);
}
return passes;
}
int tmp_flush_all (void) {
int i;
for (i = 0; i < passes; i++) {
struct buff_file *T = &temp_file[i];
tmp_adjust_global_id (T, 0);
if (T->after_crc32) {
tmp_write_crc32 (T, T->timestamp);
}
tmp_flush_out (T);
assert (lseek (T->fd, 0, SEEK_SET) == 0);
}
return passes;
}
static int get_logrec_size (int type, void *ptr, int size) {
struct lev_generic *E = ptr;
struct lev_add_message *EM;
int s, t;
int text_len = -1;
switch (type) {
case LEV_TX_ADD_MESSAGE ... LEV_TX_ADD_MESSAGE + 0xff:
case LEV_TX_ADD_MESSAGE_MF ... LEV_TX_ADD_MESSAGE_MF + 0xfff:
case LEV_TX_ADD_MESSAGE_LF:
if (size < sizeof (struct lev_add_message)) { return -2; }
EM = (void *) E;
s = sizeof (struct lev_add_message) + EM->text_len + 1;
if (size < s) { return -2; }
if ((unsigned) EM->text_len >= MAX_TEXT_LEN || EM->text[EM->text_len]) {
return -4;
}
text_len = EM->text_len;
return s;
case LEV_TX_ADD_MESSAGE_EXT ... LEV_TX_ADD_MESSAGE_EXT + MAX_EXTRA_MASK:
case LEV_TX_ADD_MESSAGE_EXT_ZF ... LEV_TX_ADD_MESSAGE_EXT_ZF + MAX_EXTRA_MASK:
case LEV_TX_ADD_MESSAGE_EXT_LL ... LEV_TX_ADD_MESSAGE_EXT_LL + MAX_EXTRA_MASK:
if (size < sizeof (struct lev_add_message)) { return -2; }
EM = (void *) E;
t = extra_mask_intcount (E->type & MAX_EXTRA_MASK) * 4;
s = sizeof (struct lev_add_message) + t + EM->text_len + 1;
if (size < s) { return -2; }
if ((unsigned) EM->text_len >= MAX_TEXT_LEN || EM->text[t + EM->text_len]) {
return -4;
}
text_len = EM->text_len;
return s;
case LEV_TX_DEL_MESSAGE:
return 12;
case LEV_TX_DEL_FIRST_MESSAGES:
return 12;
case LEV_TX_SET_MESSAGE_FLAGS ... LEV_TX_SET_MESSAGE_FLAGS+0xff:
return 12;
case LEV_TX_SET_MESSAGE_FLAGS_LONG:
return 16;
case LEV_TX_INCR_MESSAGE_FLAGS ... LEV_TX_INCR_MESSAGE_FLAGS+0xff:
return 12;
case LEV_TX_INCR_MESSAGE_FLAGS_LONG:
return 16;
case LEV_TX_DECR_MESSAGE_FLAGS ... LEV_TX_DECR_MESSAGE_FLAGS+0xff:
return 12;
case LEV_TX_DECR_MESSAGE_FLAGS_LONG:
return 16;
case LEV_TX_SET_EXTRA_FIELDS ... LEV_TX_SET_EXTRA_FIELDS + MAX_EXTRA_MASK:
return 12 + 4 * extra_mask_intcount (E->type & MAX_EXTRA_MASK);
case LEV_TX_INCR_FIELD ... LEV_TX_INCR_FIELD + 7:
return 16;
case LEV_TX_INCR_FIELD_LONG + 8 ... LEV_TX_INCR_FIELD_LONG + 11:
return 20;
case LEV_CHANGE_FIELDMASK_DELAYED:
return 8;
case LEV_TX_REPLACE_TEXT ... LEV_TX_REPLACE_TEXT + 0xfff:
if (size < sizeof (struct lev_replace_text)) { return -2; }
text_len = E->type & 0xfff;
s = offsetof (struct lev_replace_text, text) + text_len + 1;
if (size < s) { return -2; }
if ((unsigned) text_len >= MAX_TEXT_LEN || ((struct lev_replace_text *) E)->text[text_len]) {
return -4;
}
return s;
case LEV_TX_REPLACE_TEXT_LONG:
if (size < sizeof (struct lev_replace_text_long)) { return -2; }
text_len = ((struct lev_replace_text_long *) E)->text_len;
s = offsetof (struct lev_replace_text_long, text) + text_len + 1;
if (size < s) { return -2; }
if ((unsigned) text_len >= MAX_TEXT_LEN || ((struct lev_replace_text_long *) E)->text[text_len]) {
return -4;
}
return s;
default:
fprintf (stderr, "unknown record type %08x\n", type);
break;
}
return -1;
}
int text_split_replay_logevent (struct lev_generic *E, int size) {
int s;
is_message_event = 0;
switch (E->type) {
case LEV_START:
s = 24 + ((E->b + 3) & -4);
if (size < s) { return -1; }
return s;
case LEV_NOOP:
case LEV_TIMESTAMP:
case LEV_CRC32:
case LEV_ROTATE_FROM:
case LEV_ROTATE_TO:
case LEV_TAG:
return default_replay_logevent (E, size);
case LEV_CHANGE_FIELDMASK_DELAYED:
if (size < 8) { return -2; }
tmp_dispatch_logevent_all (E, 8);
return 8;
case LEV_TX_ADD_MESSAGE ... LEV_TX_ADD_MESSAGE + 0xff:
case LEV_TX_ADD_MESSAGE_MF ... LEV_TX_ADD_MESSAGE_MF + 0xfff:
case LEV_TX_ADD_MESSAGE_EXT ... LEV_TX_ADD_MESSAGE_EXT + MAX_EXTRA_MASK:
case LEV_TX_ADD_MESSAGE_EXT_ZF ... LEV_TX_ADD_MESSAGE_EXT_ZF + MAX_EXTRA_MASK:
case LEV_TX_ADD_MESSAGE_EXT_LL ... LEV_TX_ADD_MESSAGE_EXT_LL + MAX_EXTRA_MASK:
case LEV_TX_ADD_MESSAGE_LF:
s = get_logrec_size (E->type, E, size);
if (s > size || s == -2) {
return -2;
}
if (s < 0) {
fprintf (stderr, "error %d reading binlog at position %lld, type %08x\n", s, log_cur_pos(), E->type);
return s;
}
#define EM ((struct lev_add_message *) E)
if (conv_uid (EM->user_id) < 0) {
return s;
}
#undef EM
is_message_event = 1;
++last_global_id;
tmp_dispatch_logevent (E, s);
return s;
default:
s = get_logrec_size (E->type, E, size);
if (s > size || s == -2) {
return -2;
}
if (s < 0) {
fprintf (stderr, "error %d reading binlog at position %lld, type %08x\n", s, log_cur_pos(), E->type);
return s;
}
tmp_dispatch_logevent (E, s);
return s;
}
}
/*
* read binlog
*/
void (*process_message)(struct lev_add_message *E, int extra_bytes);
void (*adjust_message)(int user_id, int local_id, int flags, int op, int *extra);
void (*delete_first_messages)(int user_id, int first_local_id);
void (*replace_message_text)(struct lev_replace_text_long *E);
void (*change_extra_mask)(int new_mask);
int log_split_min, log_split_max, log_split_mod;
int adj_rec, discarded_rec;
long long msgs_read, msgs_bytes;
long long factual_unpacked_bytes, factual_packed_bytes, factual_messages, replaced_messages;
long long tot_search_words, max_user_search_words, max_user_search_id;
int max_uid, cur_min_uid, cur_max_uid, tot_users;
int UserMsgCnt[MAX_USERS], UserMsgBytes[MAX_USERS], UserMsgDel[MAX_USERS],
UserMsgExtras[MAX_USERS], UserSearchWords[MAX_USERS];
user_t *User[MAX_USERS];
struct file_user_list_entry **UserDirectory;
char *UserDirectoryData;
int user_dir_size;
off_t extra_field_start_offset[MAX_EXTRA_FIELDS];
int init_extra_mask, current_extra_mask, final_extra_mask, extra_ints_num, text_shift;