-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFilterSearch.cpp
706 lines (651 loc) · 26.3 KB
/
FilterSearch.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
//
// FilterSearch.cpp
// double array for filter
// This soft filter a lot of special charactors for an article, and support http server.
// client access it by http protocol and post an article and return exists charactors,
// this file is the core algorithm of the soft.
// filter algorithm for key.
//
// Created by wyt on 13-5-6.
// Copyright (c) 2013年 wyt. All rights reserved.
//
#include "FilterSearch.h"
#include "Util.h"
#include <fstream>
FilterSearchMgr *FilterSearchMgr::intance_ = NULL;
FilterSearchMgr *FilterSearchMgr::Instance() {
if(intance_ == NULL) {
intance_ = new FilterSearchMgr();
}
return intance_;
}
FilterSearchMgr::FilterSearchMgr():nodestable(0),
length(1000),totalLine(0) {
}
FilterSearchMgr::~FilterSearchMgr(){
}
void *GentFindUtil::Gmalloc(size_t size) {
void *p;
p = malloc(size);
if(!p) {
LOG(Util::ERROR,"malloc size:%u failed", size);
return 0;
}
memset(p,0,size);
return p;
}
void *GentFindUtil::Gcalloc(size_t size,int len) {
void *p;
p = calloc(size,len);
if(!p) {
LOG(Util::ERROR,"calloc size:%u,len:%d failed", size, len);
exit(0);
}
return p;
}
void GentFindUtil::Gfree(void *p) {
if(p) {
free(p);
}
}
int GentFindUtil::Charwchar(char *str,int len,wchar_t *out) {
int ret = mbstowcs(out,str,len);
if(ret == -1) {
LOG(Util::ERROR,"charwchar failed:%s", str);
}
return ret;
}
size_t GentFindUtil::Len(wchar_t *str) {
return wcslen(str);
}
size_t GentFindUtil::Wcstombs(char *buf,int buf_size,wchar_t *str) {
size_t ntotal = wcstombs(buf,str,buf_size);
if(ntotal == 0) {
LOG(Util::ERROR,"wcstombs failed:%s", buf);
}
*(buf+buf_size-1)='\0';
return ntotal;
}
node *FilterSearchMgr::NodeSet(int base,int check,int account,const char *name,short is_word) {
node *it = (node *)GentFindUtil::Gcalloc(1,sizeof(node));
it->base = base;
it->check = check;
it->child_count = account;
it->is_word = is_word;
strcpy(it->name,name);
return it;
}
void FilterSearchMgr::AddExQueue(int index, int childIndex) {
nodestats_t::iterator it;
it = nodestats.find(index);
if(it == nodestats.end()) {
std::vector<int> v;
v.push_back(childIndex);
nodestats[index] = v;
}else {
nodestats[index].push_back(childIndex);
}
}
void FilterSearchMgr::AddExQueue(int index, std::vector<int> &vt) {
if(vt.size()==0) return;
nodestats[index] = vt;
}
void FilterSearchMgr::DelExQueue(int index) {
nodestats_t::iterator it;
it = nodestats.find(index);
if(it == nodestats.end()) return;
nodestats.erase(it);
/*
for(size_t k =0; k<nodestats[index].size();k++){
cout << "del: "<< nodestats[index][k] << endl;
}
*/
/*
queue_t *q = nodestats.head_ex;
queue_t *pre_q = 0;
do{
if(q->index != index){
pre_q = q;
q = q->next;
}else {
queue_t *qu = q->next;
if(pre_q == 0) {
Gfree(nodestats.head_ex);
nodestats.head_ex = qu;
}else{
Gfree(pre_q->next);
pre_q->next = qu;
}
break;
}
}while (q);
*/
}
long FilterSearchMgr::GetEncode(const char *key, int base_val, int is_asc)
{
if(is_asc == 1){
return base_val+(key[0]);
}else{
return base_val+(uint8_t)key[0]*256*256+(uint8_t)key[1]*256+(uint8_t)key[2];
}
}
void FilterSearchMgr::ItemAdd(string &iterm) {
int bufsize=120;
char abc[120]={0};
memcpy(abc,iterm.c_str(),iterm.size());
wchar_t tmp[bufsize];
size_t wc_len = GentFindUtil::Charwchar(abc,strlen(abc),tmp);
//cout << "len: "<< wc_len <<endl;
ItemCreate(tmp,wc_len);
}
void FilterSearchMgr::SetAddFile(const std::string &fileKeyName)
{
string::size_type pos = fileKeyName.rfind("/");
fileAddName = "./add.txt";
if(pos != string::npos) {
fileAddName = fileKeyName.substr(0,pos) + "/add.txt";
}
ofstream fout;
fout.open(fileAddName.c_str(), ios::trunc|ios::out);
fout.close();
}
uint32_t FilterSearchMgr::Init(const std::string &fileKeyName) {
if(access(fileKeyName.c_str(),0)==-1) {
cout << fileKeyName << " file not exist" << endl;
return 0;
}
SetAddFile(fileKeyName);
nodestable = (node**)GentFindUtil::Gmalloc(length*sizeof(node *));
memset(nodestable,0,length*sizeof(node *));
nodestable[0] = NodeSet(1,1,0,"",0);
AddExQueue(0, 1);
//nodestats.head_ex = NULL;
setlocale(LC_ALL, "zh_CN.UTF-8");
FILE *fp;
int bufsize=120;
if((fp=fopen(fileKeyName.c_str(),"r"))==NULL){
printf("%s file no exit\n",fileKeyName.c_str());
exit(0);
}
struct stat fileStats;
if(stat(fileAddName.c_str(), &fileStats) != 0) {
cout << fileAddName << " stat failed " << endl;
exit(0);
}
filemtime = fileStats.st_mtime;
cout << "filemtime: " << filemtime << endl;
filename = fileKeyName;
char *oneLine=(char *)malloc(sizeof(char)*bufsize);
uint32_t num = 0;
while(fgets(oneLine,bufsize,fp)!=NULL){
string iterm(oneLine, strlen(oneLine));
Util::ReplaceSpace(iterm);
//iterm = GentFindUtil::Trim(iterm);
if(iterm == "") continue;
ItemAdd(iterm);
num++;
}
free(oneLine);
fclose(fp);
return num;
/*
string str="";
GentFind f;
std::ifstream fin("data.txt", ios::in);
string st;
while(!fin.eof()){
fin >> st;
str+=st;
}
cout << "file data:" << str << endl;
std::vector<string> ret;
f.Search(str, ret);
fin.close();
*/
//vector<string> v;
//f.Search(str, v);
// exit(1);
}
const string &FilterSearchMgr::GetFilename()
{
return filename;
}
const string &FilterSearchMgr::GetFileAddName()
{
return fileAddName;
}
void FilterSearchMgr::CheckAddItem()
{
struct stat fileStats;
if(stat(fileAddName.c_str(), &fileStats) != 0) {
LOG(Util::ERROR,"%s stat failed", fileAddName.c_str());
return;
}
if(fileStats.st_mtime <= filemtime) return;
setlocale(LC_ALL, "zh_CN.UTF-8");
FILE *fp;
int bufsize=120;
if((fp=fopen(fileAddName.c_str(),"r"))==NULL){
LOG(Util::ERROR,"%s fopen failed", fileAddName.c_str());
return;
}
filemtime = fileStats.st_mtime;
char *oneLine=(char *)malloc(sizeof(char)*bufsize);
uint32_t num = 0;
while(fgets(oneLine,bufsize,fp)!=NULL){
if(totalLine < num){
num++;
continue;
}
string iterm(oneLine, strlen(oneLine));
Util::ReplaceSpace(iterm);
num++;
totalLine++;
cout << "add to list: " << iterm << endl;
//iterm = GentFindUtil::Trim(iterm);
if(iterm == "") continue;
ItemAdd(iterm);
}
free(oneLine);
fclose(fp);
}
//分配内存
void FilterSearchMgr::IncreMemary(int cur_len)
{
int last = cur_len*2;
void *p = GentFindUtil::Gmalloc(last*sizeof(node *));
p = memcpy(p,nodestable, sizeof(node *)*length);
GentFindUtil::Gfree(nodestable);
nodestable = (node **)p;
length = last;
}
//获得孩子的数目
int FilterSearchMgr::GetChildCount(int parent_key) {
nodestats_t::iterator it;
it = nodestats.find(parent_key);
if(it == nodestats.end()) {
return 0;
}
return nodestats[parent_key].size();
}
void FilterSearchMgr::GetChild(int parent_index, std::vector<int> &ret) {
nodestats_t::iterator it;
it = nodestats.find(parent_index);
if(it == nodestats.end()) {
return;
}
ret = nodestats[parent_index];
/*
for(size_t k =0; k<nodestats[parent_index].size();k++){
ret.push_back(nodestats[parent_index][k]);
}
*/
}
/**
*@parent_index old node index
*@new_index new node index
*/
void FilterSearchMgr::SetChildCheck(int parent_index,int new_index) {
//find parent_index node's child
nodestats_t::iterator it;
it = nodestats.find(parent_index);
if(it == nodestats.end()) {
return;
}
for(size_t j=0; j<nodestats[parent_index].size();j++) {
nodestable[nodestats[parent_index][j]]->check = new_index;
}
/*
int i;
queue_t *q = nodestats.head_ex;
do{
i = q->index;
if(nodestable[i]->check == parent_index) {
nodestable[i]->check = val;
}
q = q->next;
}while(q);
*/
}
int FilterSearchMgr::GetBaseValue(int parent_index,const char *key,
int is_asc,std::vector<int> &child) {
int cur_base = nodestable[parent_index]->base;
int tmp_base = cur_base+1;
//找出child节点
GetChild(parent_index,child);
//std::vector<int>::iterator it;
size_t j=0;
int is_find=0;
while(!is_find) {
int is_able = 1;
//for(it=child.begin();it!=child.end();it++){
for(j=0; j<child.size(); j++) {
int pos_index = child[j] - cur_base + tmp_base;
//分配内存
if(length < pos_index){
IncreMemary(pos_index);
}else if(nodestable[pos_index] != 0){
//表示该节点不可以存放
is_able = 0;
tmp_base++;
break;
}
}
if(is_able == 0) continue;
//已经找到
if(key == NULL) break;
//找到了,但同时也需要保证新增加节点也是空的
int t = GetEncode(key,tmp_base,is_asc);
if(!nodestable[t]) break;
tmp_base++;
}
nodestable[parent_index]->base = tmp_base;
return tmp_base;
}
int FilterSearchMgr::MoveNode(int child_count,std::vector<int> &child,int real_base,int is_asc,
int index,int parent_index) {
int ret = index;
std::vector<int> new_child;
size_t childCount = child.size();
for(size_t i=0; i<childCount; i++){
int new_index = GetEncode(nodestable[child[i]]->name,real_base,is_asc);
nodestable[new_index] = NodeSet(nodestable[child[i]]->base,nodestable[child[i]]->check,
nodestable[child[i]]->child_count,nodestable[child[i]]->name,
nodestable[child[i]]->is_word);
//可能是移动父节点
if(child[i] == index) {
ret = new_index;
}
new_child.push_back(new_index);
//改变new_index的孩子节点的check值
SetChildCheck(child[i],new_index);
//取得节点的孩子index把赋值给新的节点
std::vector<int> vt;
GetChild(child[i], vt);
AddExQueue(new_index,vt);
//删除老的节点
DelExQueue(child[i]);
node *p = nodestable[child[i]];
nodestable[child[i]] = NULL;
GentFindUtil::Gfree(p);
}
AddExQueue(parent_index, new_child);
return ret;
}
long FilterSearchMgr::NodesConflict(long encode_t, const char *name,int index,int is_asc) {
//寻找节点,冲突发生
//检查t节点的check值,(查看t有多少个兄弟节点)
//此时encode_t是新节点和index还未建立真正的父子关系
int tcount = GetChildCount(nodestable[encode_t]->check);
//查看index节点的孩子数目
int icount = GetChildCount(index);
//包括需要插入的节点
icount++;
//printf("check:%d,tcount:%d , icount:%d\n",nodestable[encode_t]->check,tcount,icount);
//改变child小的那个base值,且 新的节点
if(tcount<=icount && encode_t != index){
//让出encode_t节点,为了不使用改变index(父节点),所以不能有encode_t == index (与父节点冲突)
std::vector<int> child;
//寻找数组dArray[dArray[t].check]的base值,所有孩子当中check[]=0,base[]=0,使当前t节点为可用 (dArray[t].check 为父节点)
int real_base = GetBaseValue(nodestable[encode_t]->check,NULL,is_asc,child);
//get_child(nodestable[encode_t]->check,tcount);
//把子节点的数据挪到新的位置, dArray[childArr[i]]为老的节点
//printf("index2:%d,index_base2:%d\n",index,nodestable[index]->base);
//特殊情况:孩子节点当中正好有index节点,哈哈哈 => 需要改变index的值
index = MoveNode(tcount,child,real_base,is_asc,index, nodestable[encode_t]->check);
AddExQueue(index,encode_t);
nodestable[encode_t] = NodeSet(nodestable[index]->base,index,0,name,0);
}else{
//改变index的base值.来达到index的子节点(包括当前需要插入的节点),同时把新的插入节点(不是t)
//t的值是index新的base值加encode的值
icount=icount-1;
//int child[icount];
std::vector<int> child;
int real_base = GetBaseValue(index/*,icount*/,name,is_asc,child);
//改变t的值
encode_t = GetEncode(name,real_base,is_asc);
//printf("index: %d , base: %d\n",index,realBase);
//get_child(index,icount);
//把字节点的数据挪到新的位置, dArray[childArr[i]]为老的节点
index = MoveNode(icount,child,real_base,is_asc,index, index);
//new_child.push_back(encode_t);
//DelExQueue(index);
AddExQueue(index,encode_t);
nodestable[encode_t] = NodeSet(nodestable[index]->base,index,0,name,0);
}
return encode_t;
}
int FilterSearchMgr::NodesAdd(char *name,int index,int is_asc) {
long encode_t = GetEncode(name,nodestable[index]->base,is_asc);
if(length <= encode_t ){
//分配内存
IncreMemary(encode_t);
//给节点赋值
nodestable[encode_t] = NodeSet(1,index,0,name,0);
AddExQueue(index, encode_t);
//display();
return encode_t;
}
node *p = nodestable[encode_t];
if(p == 0) {
nodestable[encode_t] = NodeSet(1,index,0,name,0);
AddExQueue(index, encode_t);
//display();
return encode_t;
}
//该节点是重复的节点,不需要处理
if(p->check == index && p->base != 0) return encode_t;
//conflict occur
//printf("index:%d,index_base:%d\n",index,nodestable[index]->base);
encode_t = NodesConflict(encode_t, name, index, is_asc);
// display();
return encode_t;
}
void FilterSearchMgr::ItemCreate(wchar_t *name,size_t name_len)
{
//wprintf(L"%s",name);
size_t len = name_len;
size_t i;
std::vector<int> c;
int parent = 0;
c.push_back(0);
for(i=0; i<len; i++) {
nodestable[parent]->child_count++;
if(name[i] < 128) {
char buff[2];
GentFindUtil::Wcstombs(buff,2,&name[i]);
//cout<< buff << endl;
if(strcmp(buff,"\n") == 0 || strcmp(buff," ") == 0) break;
parent=NodesAdd(buff,parent,1);
}else{
char buff[4];
GentFindUtil::Wcstombs(buff, 4, &name[i]);
// cout<< buff << endl;
parent=NodesAdd(buff,parent,0);
}
c.push_back(parent);
}
nodestable[parent]->is_word = 1;
}
int FilterSearchMgr::ItemSearch(char *name,int base_index,int is_asc) {
if(nodestable[base_index] == 0) return -1;
int index = GetEncode(name, nodestable[base_index]->base, is_asc);
if(length < index) return -1;
if(nodestable[index] == 0) return -1;
if(nodestable[index]->check != base_index){
return -1;
}
return index;
}
short FilterSearchMgr::ItemAttr(int index,const string &field) {
if(field == "child_count"){
return nodestable[index]->child_count;
}else if(field == "is_word") {
return nodestable[index]->is_word;
}
return -1;
}
GentFind::GentFind() {
}
GentFind::~GentFind(){
}
void GentFind::stack_init() {
stack_s = (stack *)GentFindUtil::Gmalloc(sizeof(stack));
stack_s->head = NULL;
stack_s->tail = NULL;
ret_s = (ret *)GentFindUtil::Gmalloc(sizeof(ret));
ret_s->head = NULL;
ret_s->tail = NULL;
}
void GentFind::stack_free() {
item_st *it = stack_s->head;
item_st *its;
while(it) {
its = it->next;
GentFindUtil::Gfree(it);
it = its;
}
stack_s->head = NULL;
stack_s->tail = NULL;
}
void GentFind::stack_push(char *name,int len) {
item_st *it = (item_st *)GentFindUtil::Gmalloc(sizeof(item_st));
memcpy(it->name,name,len);
if(stack_s->head == NULL) {
stack_s->head = it;
stack_s->tail = it;
}else {
stack_s->tail->next = it;
stack_s->tail = it;
}
}
void GentFind::stack_pop() {
item_ret *ret_it = (item_ret *)GentFindUtil::Gmalloc(sizeof(item_ret));
char *buf = (char *)GentFindUtil::Gmalloc(50);
item_st *it = stack_s->head;
strcpy(buf,"");
while(it) {
strcat(buf,it->name);
it = it->next;
}
ret_it->key = buf;
if(ret_s->head == NULL) {
ret_s->head = ret_it;
ret_s->tail = ret_it;
}else {
ret_s->tail->next = ret_it;
ret_s->tail = ret_it;
}
stack_free();
}
void GentFind::Search(string &str, std::vector<string> &ret) {
FilterSearchMgr::Instance()->CheckAddItem();
string special[4] = {"\t"," ","\r"};
string rep = "\n";
for(int i=0; i<4; i++) {
size_t pos=0;
while((pos = str.find_first_of(special[i], pos))!=string::npos) {
str.replace(pos, 1, rep);
pos += 1;
}
}
size_t pos = 0;
size_t pre = 0;
std::vector<string> str_parts;
while((pos = str.find_first_of(rep, pos))!=string::npos) {
string s = str.substr(pre, pos-pre);
if(s!="" && s != rep) str_parts.push_back(s);
pre = pos;
pos++;
}
string s = str.substr(pre);
if(s!="" && s != rep) str_parts.push_back(s);
for(size_t j=0;j<str_parts.size(); j++) {
Match(str_parts[j], ret);
}
}
int GentFind::Match(string &str, std::vector<string> &ret) {
setlocale(LC_ALL, "zh_CN.UTF-8");
wchar_t *buff = (wchar_t *)GentFindUtil::Gmalloc(sizeof(wchar_t)*str.size()+1);
char *str2 = const_cast<char *>(str.c_str());
int wc_len = GentFindUtil::Charwchar(str2,str.size(),buff);
if(wc_len == -1) {
GentFindUtil::Gfree(buff);
return -1;
}
stack_init();
int i = 0;
int index = 0;
int tindex;
int pos = 0;
int rsize = 0;
while(i < wc_len){
char c[4];
int len;
if(*(buff+i) < 128) {
GentFindUtil::Wcstombs(c,2,buff+i);
if(strcmp(c," ") == 0){
if(stack_s->head != NULL && FilterSearchMgr::Instance()->ItemAttr(index,"is_word") == 1){
stack_pop();
pos = 0;
}
i++;
continue;
}
tindex = FilterSearchMgr::Instance()->ItemSearch(c,index,1);
len = 2;
}else {
GentFindUtil::Wcstombs(c, 4, buff+i);
tindex = FilterSearchMgr::Instance()->ItemSearch(c,index,0);
len = 4;
}
if(tindex == -1){
//没有找到,检查上一个节点是否为最后一个节点
if(stack_s->head != NULL && FilterSearchMgr::Instance()->ItemAttr(index,"is_word") == 0) {
//表示上个节点不是个词组,回溯到节点的下一个字位置
i = pos + 1;
pos = 0;
stack_free();
}else if(stack_s->head != NULL && FilterSearchMgr::Instance()->ItemAttr(index,"is_word") == 1) {
//上一次是一个词组,把词组保存
stack_pop();
//i的值不用加
pos = 0;
} else {
i++;
}
//从开始寻找
index = 0;
}else {
if(index == 0) {
//记住当前位置
pos = i;
}
//压栈
stack_push(c,len);
i++;
index = tindex;
}
}
if(tindex != -1) {
if(stack_s->head != NULL && FilterSearchMgr::Instance()->ItemAttr(index,"is_word") == 1){
stack_pop();
}
}
item_ret *ret_it = ret_s->head;
string laststr = "";
string sp = "";
while(ret_it) {
ret.push_back(string(ret_it->key));
laststr += sp + string(ret_it->key);
sp = ",";
rsize += strlen(ret_it->key);
rsize++;
ret_it = ret_it->next;
}
if(laststr != "") {
LOG(Util::INFO,"filter exist keys are %s", laststr.c_str());
}
free(buff);
return rsize;
}