forked from pkumod/gStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexJoin.cpp
2642 lines (2440 loc) · 78.2 KB
/
IndexJoin.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
/*=============================================================================
# Filename: Join.cpp
# Author: Bookug Lobert
# Mail: [email protected]
# Last Modified: 2015-12-13 16:44
# Description: implement functions in Join.h
=============================================================================*/
#include "Join.h"
using namespace std;
Join::Join()
{
this->kvstore = NULL;
this->result_list = NULL;
}
Join::Join(KVstore* _kvstore)
{
this->kvstore = _kvstore;
this->result_list = NULL;
}
Join::~Join()
{
//noting to do necessarily
}
void
Join::init(BasicQuery* _basic_query)
{
//BETTER:only common are placed here!
this->basic_query = _basic_query;
this->var_num = this->basic_query->getVarNum();
int mapping_len = this->basic_query->getPreVarNum() + this->var_num;
this->id2pos = (int*)malloc(sizeof(int) * mapping_len);
memset(id2pos, -1, sizeof(int) * mapping_len);
//this->id2pos = (int*)malloc(sizeof(int) * this->var_num);
//memset(id2pos, -1, sizeof(int) * this->var_num);
this->pos2id = (int*)malloc(sizeof(int) * mapping_len);
memset(pos2id, -1, sizeof(int) * mapping_len);
//this->pos2id = (int*)malloc(sizeof(int) * this->var_num);
//memset(pos2id, -1, sizeof(int) * this->var_num);
this->id_pos = 0;
this->start_id = -1;
int triple_num = this->basic_query->getTripleNum();
this->dealed_triple = (bool*)calloc(triple_num, sizeof(bool));
this->index_lists = NULL;
this->result_list = _basic_query->getResultListPointer();
}
void
Join::clear()
{
//BETTER:only common are released here!
free(this->id2pos);
free(this->pos2id);
//NOTICE:maybe many BasicQuery
this->current_table.clear();
while (this->mystack.empty() == false) this->mystack.pop();
free(this->dealed_triple);
//NULL if using multi-join method
delete[] this->index_lists;
this->result_list = NULL;
this->satellites.clear();
}
double
Join::score_node(unsigned _degree, unsigned _size)
{
//PARAM_DEGREE * _degree - PARAM_SIZE * _size
//BETTER?:use other cost model
return Join::PARAM_DEGREE * (double)_degree + Join::PARAM_SIZE / (double)_size;
}
int
Join::judge(int _smallest, int _biggest)
{
return 0; //DEBUG:remove when index_join is ok
//BETTER?:use appropiate method according to size and structure
int edge_num = this->basic_query->getTripleNum();
double dense = (double)edge_num / this->var_num;
//BETTER:how to guess the size of can_lists
double size = (_smallest + _biggest) / 2.0;
double ans = Join::PARAM_DENSE * dense - size / Join::PARAM_SIZE;
if (ans > Join::JUDGE_LIMIT)
return 0; //multi_join method
else
return 1; //index_join method
}
//select the start point, and maybe search order for index_join
//just for multi_join here, maybe diffrent for index_join later
void
Join::select()
{
//NOTICE: only consider vars in select here
double max = 0;
int maxi = -1;
//int border = this->basic_query->getVarNum();
for(int i = 0; i < this->var_num; ++i)
{
//satellites which are not retrieved
if(!this->basic_query->isReady(i))
{
continue;
}
double tmp = this->score_node(this->basic_query->getVarDegree(i), this->basic_query->getCandidateSize(i));
if (tmp > max)
{
max = tmp;
maxi = i;
}
}
if (maxi == -1)
{
cout << "error to select the first one to join" << endl;
}
else
{
this->start_id = maxi;
}
#ifdef DEBUG_JOIN
//printf("the start id is: %d\n", this->start_id);
cerr << "the start id is: " << this->start_id << endl;
#endif
}
//join on the vector of CandidateList, available after
//retrieved from the VSTREE and store the resut in _result_set
bool
Join::join_sparql(SPARQLquery& _sparql_query)
{
int basic_query_num = _sparql_query.getBasicQueryNum();
//join each basic query
for (int i = 0; i < basic_query_num; i++)
{
//fprintf(stderr, "Basic query %d\n", i);
cerr << "Basic query " << i << endl;
bool ret = this->join_basic(&(_sparql_query.getBasicQuery(i)));
if (!ret)
cerr << "end directly for this basic query: " << i << endl;
}
return true;
}
bool
Join::join_basic(BasicQuery* _basic_query)
{
this->init(_basic_query);
long begin = Util::get_cur_time();
bool ret1 = this->filter_before_join();
long after_constant_filter = Util::get_cur_time();
//fprintf(stderr, "after filter_before_join: used %ld ms\n", after_filter - begin);
cerr << "after filter_before_join: used " << (after_constant_filter - begin) << " ms" << endl;
if (!ret1)
{
this->clear();
return false;
}
this->add_literal_candidate();
long after_add_literal = Util::get_cur_time();
cerr << "after add_literal_candidate: used " << (after_add_literal - after_constant_filter) << " ms" << endl;
bool ret2 = this->allFilterByPres();
//bool ret2 = true;
long after_pre_filter = Util::get_cur_time();
cerr << "after allFilterByPres: used " << (after_pre_filter - after_add_literal) << " ms" << endl;
if (!ret2)
{
this->clear();
return false;
}
bool ret3 = this->join();
long after_joinbasic = Util::get_cur_time();
cerr << "after join_basic: used " << (after_joinbasic - after_pre_filter) << " ms" << endl;
if (!ret3)
{
this->clear();
return false;
}
//TODO+DEBUG:only_pre_filter needed to deal with undealed isolated edge
//only when allFilterByPres not used!!!
//BETTER:we need to choose a way to decide a set of isolated edges to be dealed before join(one by one in join or just before?)
//the other should be dealed later(filter efficience is low!!!)
//NOTICE:we do pre_var_handler first, and generate all satellites when coping to result list
//
//this->generateAllSatellites();
//long after_generate_satellite = Util::get_cur_time();
//cerr<<"after generate satellite: used "<<(after_generate_satellite - after_joinbasic)<<" ms"<<endl;
//BETTER+QUERY: consider satellite with pre var, which first?
//I think s2p first is better but s2o is also ok
//1. filter by predicate vars first, so num decreases, need to specify the case that a var is not retrieved
//generate later use sp2o or op2s (for each pre var, sevearl candidates)
//2. generate candidates for satellites first using sp2o or s2o(op2s or o2s), later filtered by pre vars
//the generating process had better been placed at the final, just before copying result
this->pre_var_handler();
//TODO+BETTER:maybe also reduce to empty, return false
long after_pre_var = Util::get_cur_time();
cerr << "after pre var: used " << (after_pre_var - after_joinbasic) << " ms" << endl;
this->copyToResult();
long after_copy = Util::get_cur_time();
cerr << "after copy to result list: used " << (after_copy - after_pre_var) << " ms" << endl;
cerr << "Final result size: " << this->basic_query->getResultList().size() << endl;
this->clear();
return true;
}
void
Join::generateAllSatellites()
{
//BETTER: directly generate to result list, avoiding copying cost?
int core_var_num = this->basic_query->getRetrievedVarNum();
for (int i = 0; i < core_var_num; ++i)
{
int id = this->pos2id[i];
int degree = this->basic_query->getVarDegree(id);
for (int j = 0; j < degree; ++j)
{
int id2 = this->basic_query->getEdgeNeighborID(id, j);
if (this->basic_query->isSatelliteInJoin(id2) == false)
continue;
char edge_type = this->basic_query->getEdgeType(id, j);
int preid = this->basic_query->getEdgePreID(id, j);
for (TableIterator it = this->current_table.begin(); it != this->new_start; ++it)
{
//TODO:generate and add just like join
}
this->new_start = this->current_table.end();
}
}
}
bool
Join::pre_var_handler()
{
//int core_var_num = this->basic_query->getRetrievedVarNum();
unsigned pre_var_num = this->basic_query->getPreVarNum();
#ifdef DEBUG_JOIN
cerr << "pre var num: " << pre_var_num << endl;
#endif
//QUERY+BETTER:filter by pre vars one by one or each record together?
for (unsigned i = 0; i < pre_var_num; ++i)
{
#ifdef DEBUG_JOIN
cerr << "current pre var id: " << i << endl;
#endif
const PreVar& pre_var = this->basic_query->getPreVarByID(i);
#ifdef DEBUG_JOIN
cerr << "current table size: " << this->current_table.size() << endl;
#endif
//WARN:do not conflict with original var id
//1 core var, id can be 1, then pos can be 1 + 0 = 1 for pre var!!! conflict!
//int pos = core_var_num + i;
int pos = this->var_num + i;
this->add_id_pos_mapping(pos);
//cout<<"id 1 pos "<<this->id2pos[1]<<endl;
bool if_new_start = false;
//for each record, use s/o2p for each triple containing this pre var to filter
for (TableIterator it = this->current_table.begin(); it != this->new_start;)
{
IDList valid_ans;
//bool ok = true;
unsigned triple_num = pre_var.triples.size();
#ifdef DEBUG_JOIN
//cerr<<"triple num for this var: "<<triple_num<<endl;
#endif
for (unsigned j = 0; j < triple_num; ++j)
{
const Triple& triple = this->basic_query->getTriple(pre_var.triples[j]);
string sub_name = triple.subject;
string obj_name = triple.object;
#ifdef DEBUG_JOIN
//cerr << sub_name << endl << triple.predicate << endl << obj_name << endl;
#endif
int sub_id = -1, obj_id = -1, var1 = -1, var2 = -1;
if (sub_name[0] != '?')
{
sub_id = this->kvstore->getIDByEntity(sub_name);
}
else
{
if (!(this->basic_query->isOneDegreeNotJoinVar(sub_name)))
var1 = this->basic_query->getIDByVarName(sub_name);
//satellite in join not retrieved
if (var1 != -1 && this->basic_query->isSatelliteInJoin(var1))
var1 = -1;
}
if (obj_name[0] != '?')
{
obj_id = this->kvstore->getIDByEntity(obj_name);
if (obj_id == -1)
obj_id = this->kvstore->getIDByLiteral(obj_name);
}
else
{
if (!(this->basic_query->isOneDegreeNotJoinVar(obj_name)))
var2 = this->basic_query->getIDByVarName(obj_name);
//satellite in join not retrieved
if (var2 != -1 && this->basic_query->isSatelliteInJoin(var2))
var2 = -1;
}
//cout<<"var1: "<<var1<<" var2: "<<var2<<endl;
int* id_list = NULL;
int id_list_len = 0;
//two vars in query
if (sub_id == -1 && obj_id == -1)
{
if (var1 == -1 && var2 == -1)
{
//NOTICE: this is a special case: select ?p where { ?s ?p ?o . }
//must be only one triple, otherwise exist a node > 1
//(?s2 ?p ?o2 is not ok, not connected query graph)
//WARN+QUERY: if only this triple, no answer for ?p
//we need to output all predicates in data graph, so store one file containing
//entity/literal/predicate num when building, and reoutput all when changing
//(binary file i snot visible, so use character file)
//
//we shall deal with this case in the Strategy module in time
}
else if (var1 == -1 && var2 != -1)
{
//TODO+NOTICE: we must add literals here, also enum all predicates and using p2o
//but literals should only be added once for each predicate
//QUERY:maybe many in edges all with unbound predicates
//we think this case is very rare, so not consider now
//
//how about bound predicates? ?s1 p1 ?o ?s2 p2 ?o (?o retrieved, p2o to add literals)
//all unbound predicates: ?s1 ?p1 ?o ?s2 ?p2 ?o
//if exist constant neighbor, just use s2o to add literals(already discussed)
//NOTICE+WARN+TODO: in these cases, all subject degree is 1, but we can not start from ?o because
//it is a literal var!!!
//(so join can not be processed, however, do not need to join here)
//must add literal for ?o before or treat it as special case, considered in Strategy!!
//cout<<"pos: "<<this->id2pos[var2]<<" ele: "<<(*it)[0]<<endl;
this->kvstore->getpreIDlistByobjID((*it)[this->id2pos[var2]], id_list, id_list_len, true);
}
else if (var1 != -1 && var2 == -1)
{
this->kvstore->getpreIDlistBysubID((*it)[this->id2pos[var1]], id_list, id_list_len, true);
}
else if (var1 != -1 && var2 != -1)
{
//if(this->is_literal_var(var2))
//{
//int* oid_list = NULL;
//int oid_list_len = 0;
//this->kvstore->getobjIDlistBysubID((*it)[this->id2pos[var1]], oid_list, oid_list_len);
//this->kvstore->getpreIDlistBysubID((*it)[this->id2pos[var1]], id_list, id_list_len);
//}
//cerr<<"sub str: "<<this->kvstore->getEntityByID((*it)[this->id2pos[var1]])<<endl;
//cerr<<"obj str: "<<this->kvstore->getEntityByID((*it)[this->id2pos[var2]])<<endl;
//this->kvstore->getpreIDlistBysubIDobjID((*it)[this->id2pos[var1]], (*it)[this->id2pos[var2]], id_list, id_list_len);
int sid = (*it)[this->id2pos[var1]], oid = (*it)[this->id2pos[var2]];
this->kvstore->getpreIDlistBysubIDobjID(sid, oid, id_list, id_list_len, true);
//NOTICE:no need to add literals here because they are added when join using s2o
}
}
//two constants in query
else if (sub_id != -1 && obj_id != -1)
{
//just use so2p in query graph to find predicates
//this->kvstore->getpreIDlistBysubIDobjID(sub_id, obj_id, id_list, id_list_len);
int sid = sub_id, oid = obj_id;
this->kvstore->getpreIDlistBysubIDobjID(sid, oid, id_list, id_list_len, true);
}
//sub is var while obj is constant
else if (sub_id == -1 && obj_id != -1)
{
if (var1 == -1)
{
this->kvstore->getpreIDlistByobjID(obj_id, id_list, id_list_len, true);
}
else
{
this->kvstore->getpreIDlistBysubIDobjID((*it)[this->id2pos[var1]], obj_id, id_list, id_list_len, true);
int sid = (*it)[this->id2pos[var1]], oid = obj_id;
this->kvstore->getpreIDlistBysubIDobjID(sid, oid, id_list, id_list_len, true);
}
}
//sub is constant while obj is var
else if (sub_id != -1 && obj_id == -1)
{
if (var2 == -1)
{
this->kvstore->getpreIDlistBysubID(sub_id, id_list, id_list_len, true);
}
else
{
//NOTICE:no need to add literals here because they are added in add_literal_candidate using s2o
//this->kvstore->getpreIDlistBysubIDobjID(sub_id, (*it)[this->id2pos[var2]], id_list, id_list_len);
int sid = sub_id, oid = (*it)[this->id2pos[var2]];
this->kvstore->getpreIDlistBysubIDobjID(sid, oid, id_list, id_list_len, true);
}
}
//cout<<"the idlist len "<<id_list_len<<endl;
if (j == 0)
{
valid_ans.unionList(id_list, id_list_len);
}
else
{
valid_ans.intersectList(id_list, id_list_len);
}
delete[] id_list;
if (valid_ans.size() == 0)
{
#ifdef DEBUG_JOIN
cerr << "already empty!" << endl;
#endif
//ok = false;
break;
}
else
{
#ifdef DEBUG_JOIN
//for(int k = 0; k < valid_ans.size(); ++k)
//cerr << this->kvstore->getPredicateByID(valid_ans[k])<<endl;
#endif
}
}
//add the candidates of this pre var if selected,
//beyond graph_var_num if satellites are generated first;
//beyond core_var_num if not
//
//NOTICE: we add all here(select/not) because they maybe needed by generating satellites
//we need to copy only the selected ones in copyToResult
int size = valid_ans.size();
if (size > 0)
{
it->push_back(valid_ans[0]);
int begin = 1;
if (!if_new_start && size > 1)
{
this->add_new_to_results(it, valid_ans[1]);
if_new_start = true;
this->new_start = this->current_table.end();
this->new_start--;
begin = 2;
}
for (int j = begin; j < size; ++j)
{
this->add_new_to_results(it, valid_ans[j]);
}
it++;
}
else
{
it = this->current_table.erase(it);
}
}
this->new_start = this->current_table.end();
}
cout << "table size after pre_var " << this->current_table.size() << endl;
return true;
}
void
Join::copyToResult()
{
//copy to result list, adjust the vars order
this->result_list->clear();
int select_var_num = this->basic_query->getSelectVarNum();
int core_var_num = this->basic_query->getRetrievedVarNum();
int pre_var_num = this->basic_query->getPreVarNum();
//TODO:set right selected_pre_var_num here
int selected_pre_var_num = pre_var_num;
if (this->id_pos != core_var_num + selected_pre_var_num)
{
cerr << "terrible error in copyToResult!" << endl;
return;
}
#ifdef DEBUG_JOIN
cerr << "core var num: " << core_var_num << " select var num: " << select_var_num << endl;
#endif
this->record_len = select_var_num + pre_var_num;
this->record = new int[this->record_len];
for (TableIterator it = this->current_table.begin(); it != this->current_table.end(); ++it)
{
int i = 0;
for (; i < core_var_num; ++i)
{
//This is because sleect var id is always smaller
if (this->pos2id[i] < select_var_num)
this->record[this->pos2id[i]] = (*it)[i];
}
#ifdef DEBUG_JOIN
//cerr<<"current id_pos: "<<this->id_pos<<endl;
#endif
//below are for selected pre vars
while (i < this->id_pos)
{
//TODO:only add selected ones
//int pre_var_id = this->pos2id[i] - core_var_num;
int pre_var_id = this->pos2id[i] - this->var_num;
this->record[select_var_num + pre_var_id] = (*it)[i];
++i;
}
//generate satellites when constructing records
//NOTICE: satellites in join must be selected
//core vertex maybe not in select
//
//vector<Satellite> satellites;
for (i = 0; i < core_var_num; ++i)
{
int id = this->pos2id[i];
int ele = (*it)[i];
int degree = this->basic_query->getVarDegree(id);
for (int j = 0; j < degree; ++j)
{
int id2 = this->basic_query->getEdgeNeighborID(id, j);
if (this->basic_query->isSatelliteInJoin(id2) == false)
continue;
#ifdef DEBUG_JOIN
//cerr << "to generate "<<id2<<endl;
#endif
int* idlist = NULL;
int idlist_len = 0;
int triple_id = this->basic_query->getEdgeID(id, j);
Triple triple = this->basic_query->getTriple(triple_id);
int preid = this->basic_query->getEdgePreID(id, j);
if (preid == -2) //?p
{
string predicate = triple.predicate;
int pre_var_id = this->basic_query->getPreVarID(predicate);
preid = (*it)[core_var_num + pre_var_id];
}
else if (preid == -1)
{
//ERROR
}
char edge_type = this->basic_query->getEdgeType(id, j);
if (edge_type == Util::EDGE_OUT)
{
this->kvstore->getobjIDlistBysubIDpreID(ele, preid, idlist, idlist_len, true);
}
else
{
this->kvstore->getsubIDlistByobjIDpreID(ele, preid, idlist, idlist_len, true);
}
this->satellites.push_back(Satellite(id2, idlist, idlist_len));
#ifdef DEBUG_JOIN
//cerr<<"push a new satellite in"<<endl;
#endif
}
}
#ifdef DEBUG_JOIN
//cerr<<"satellites all prepared!"<<endl;
#endif
int size = satellites.size();
this->cartesian(0, size);
#ifdef DEBUG_JOIN
//cerr<<"after cartesian"<<endl;
#endif
for (int k = 0; k < size; ++k)
{
delete[] this->satellites[k].idlist;
//this->satellites[k].idlist = NULL;
}
//WARN:use this to avoid influence on the next loop
this->satellites.clear();
#ifdef DEBUG_JOIN
//cerr<<"after clear the satellites"<<endl;
#endif
}
delete[] this->record;
#ifdef DEBUG_JOIN
//cerr<<"after delete the record"<<endl;
#endif
this->record = NULL;
this->record_len = 0;
}
void
Join::cartesian(int pos, int end)
{
if (pos == end)
{
int* new_record = new int[this->record_len];
memcpy(new_record, this->record, sizeof(int) * this->record_len);
this->result_list->push_back(new_record);
return;
}
int size = this->satellites[pos].idlist_len;
int id = this->satellites[pos].id;
int* list = this->satellites[pos].idlist;
for (int i = 0; i < size; ++i)
{
this->record[id] = list[i];
this->cartesian(pos + 1, end);
}
}
void
Join::toStartJoin()
{
for (int i = 0; i < this->var_num; ++i)
{
if (this->basic_query->isReady(i))
{
return;
}
}
cout << "toStartJoin(): need to prepare a ready node"<<endl;
int maxi = -1;
double max = 0;
for (int i = 0; i < this->var_num; ++i)
{
if (!this->basic_query->isSatelliteInJoin(i))
{
double tmp = this->score_node(this->basic_query->getVarDegree(i), this->basic_query->getCandidateSize(i));
if (tmp > max)
{
max = tmp;
maxi = i;
}
}
}
//TODO+DEBUG:add literals error?
//NOTICE:not add literal, so no constant neighbor, this must be free literal variable
int var_id = maxi;
int var_degree = this->basic_query->getVarDegree(var_id);
//cout<<"var id: "<<var_id<<" "<<"var degree: "<<var_degree<<endl;
IDList literal_candidate_list;
for (int j = 0; j < var_degree; ++j)
{
//int neighbor_id = this->basic_query->getEdgeNeighborID(var_id, j);
int predicate_id = this->basic_query->getEdgePreID(var_id, j);
int triple_id = this->basic_query->getEdgeID(var_id, j);
Triple triple = this->basic_query->getTriple(triple_id);
string neighbor_name = triple.subject;
IDList this_edge_literal_list;
int* object_list = NULL;
int object_list_len = 0;
if (predicate_id >= 0)
(this->kvstore)->getobjIDlistBypreID(predicate_id, object_list, object_list_len, true);
//cout<<"predicate id: "<<predicate_id<<endl<<this->kvstore->getPredicateByID(predicate_id)<<endl;
//for(int sb = 0; sb < object_list_len; ++sb)
//{
//cout<<object_list[sb]<<" ";
//}
//cout<<endl;
this_edge_literal_list.unionList(object_list, object_list_len, true);
delete[] object_list;
cout<<"preid: "<<predicate_id<<" length: "<<object_list_len<<" literals: "<<this_edge_literal_list.size()<<endl;
if (j == 0)
{
literal_candidate_list.unionList(this_edge_literal_list);
}
else
{
literal_candidate_list.intersectList(this_edge_literal_list);
}
}
IDList& origin_candidate_list = this->basic_query->getCandidateList(var_id);
//int origin_candidate_list_len = origin_candidate_list.size();
origin_candidate_list.unionList(literal_candidate_list, true);
//int after_add_literal_candidate_list_len = origin_candidate_list.size();
this->basic_query->setReady(var_id);
cout<<"add literals num: "<<literal_candidate_list.size()<<endl;
cout<<"current can size: "<<origin_candidate_list.size()<<endl;
}
// use the appropriate method to join candidates
bool
Join::join()
{
//in case of no start point, if all core vertices are literal vars
this->toStartJoin();
//the smallest candidate list size of the not-satellite vars
int id = this->basic_query->getVarID_FirstProcessWhenJoin();
int smallest = this->basic_query->getCandidateSize(id);
if(!this->is_literal_var(id) && smallest == 0)
return false; //empty result
int biggest = this->basic_query->getVarID_MaxCandidateList();
int method = this->judge(smallest, biggest);
bool ret = true;
switch (method)
{
case 0:
//printf("use multi-join here!\n");
cerr << "use multi-join here!" << endl;
ret = this->multi_join();
break;
case 1:
//printf("use index-join here!\n");
cerr << "use index-join here!" << endl;
ret = this->index_join();
break;
default:
//printf("ERROR: no method found!\n");
cerr << "ERROR: no method found!" << endl;
break;
}
return ret;
}
int
Join::choose_next_node(int id)
{
//choose a child to search deeply
int degree = this->basic_query->getVarDegree(id);
int maxi = -1;
double max = 0;
for (int i = 0; i < degree; ++i)
{
int var_id2 = this->basic_query->getEdgeNeighborID(id, i);
if (var_id2 == -1) //not in join, including constant
{
continue;
}
//satellites which are not retrieved
if (this->basic_query->if_need_retrieve(var_id2) == false)
{
continue;
}
// each triple/edge need to be processed only once.
int edge_id = this->basic_query->getEdgeID(id, i);
if (this->dealed_triple[edge_id])
{
continue;
}
//NTC:not using updated degrees, other not the whole loop
double tmp = this->score_node(this->basic_query->getVarDegree(var_id2), this->basic_query->getCandidateSize(var_id2));
if (max < tmp)
{
max = tmp;
maxi = i;
}
}
return maxi;
}
bool
Join::is_literal_var(int _id)
{
//if(!this->basic_query->isFreeLiteralVariable(_id) || this->basic_query->isAddedLiteralCandidate(_id))
//if(!this->basic_query->isFreeLiteralVariable(_id))
//{
//return false;
//}
//BETTER?:this is not needed because we ensure that
//all dealed nodes's literals are added!
//this->basic_query->setAddedLiteralCandidate(_id);
//if(this->basic_query->isAddedLiteralCandidate(_id))
if (this->basic_query->isReady(_id))
return false;
else
return true;
//NOTICE:satellites are not considered in join, so only free literal variable checked here
//(some free literal var maybe also added)
}
//===================================================================================================
//Below are functions to do multi-join method
//===================================================================================================
void
Join::add_new_to_results(TableIterator it, int id)
{
//NTC:already have one more in *it if need to push back
RecordType tmp(*it);
*(tmp.rbegin()) = id;
this->current_table.push_back(tmp);
}
void
Join::acquire_all_id_lists(IdLists& _id_lists, IdListsLen& _id_lists_len, IDList& _can_list, vector<int>& _edges, int _id, int can_list_size)
{
int* tmp_id_list;
int tmp_id_list_len;
for (int i = 0; i < this->id_pos; ++i)
{
// keep empty if not valid/used
_id_lists.push_back(vector<int*>());
_id_lists_len.push_back(vector<int>());
int edge_index = _edges[i];
if (edge_index != -1)
{
int pre_id = this->basic_query->getEdgePreID(_id, edge_index);
//int edge_id = this->basic_query->getEdgeID(_id, edge_index);
int edge_type = this->basic_query->getEdgeType(_id, edge_index);
if (pre_id >= 0) // valid
{
for (int j = 0; j < can_list_size; ++j)
{
if (edge_type == Util::EDGE_IN)
{
this->kvstore->getsubIDlistByobjIDpreID(_can_list[i], \
pre_id, tmp_id_list, tmp_id_list_len, true);
}
else //EDGE_OUT
{
this->kvstore->getobjIDlistBysubIDpreID(_can_list[i], \
pre_id, tmp_id_list, tmp_id_list_len, true);
}
_id_lists.rbegin()->push_back(tmp_id_list);
_id_lists_len.rbegin()->push_back(tmp_id_list_len);
}
}
}
}
}
//DEBUG:add debug info and check when the var is not free
bool
Join::new_join_with_multi_vars_prepared(IdLists& _id_lists, IdListsLen& _id_lists_len, vector<int>& _edges, IDList& _can_list, int _can_list_size)
{
if (_can_list_size == 0)
{
return false; //empty result
}
bool found = false; //no record matched
bool if_new_start = false; //the first to add to end in while
//list< list<int> > temp_table;
for (TableIterator it0 = this->current_table.begin(); it0 != this->new_start;)
{
bool matched = false; //this record matched
bool added = false; //if one ele added already
for (int i = 0; i < _can_list_size; ++i)
{
int cnt = 0;
bool linked = true;
for (RecordIterator it1 = it0->begin(); it1 != it0->end(); ++it1, ++cnt)
{
int edge_index = _edges[cnt];
if (edge_index == -1)
{
continue;
}
int ele = *it1;
if (_id_lists_len[cnt][i] == 0)
{
linked = false;
break;
}
if (Util::bsearch_int_uporder(ele, _id_lists[cnt][i], _id_lists_len[cnt][i]) == -1)
{
linked = false;
break;
}
}
if (linked)
{
if (added)
{
this->add_new_to_results(it0, _can_list[i]);
if (!if_new_start)
{
if_new_start = true;
this->new_start = this->current_table.end();
this->new_start--;
}
}
else
{
added = true;
it0->push_back(_can_list[i]);
}
matched = true;
}
}
if (matched)
{
found = true;
it0++;
//it3++;
}
else
{
it0 = this->current_table.erase(it0);
//it3 = this->table_row_new.erase(it3);
}
}
return found;
}
bool
Join::new_join_with_multi_vars_not_prepared(vector<int>& _edges, IDList& _can_list, int _can_list_size, int _id, bool _is_literal)
{
if (_can_list_size == 0 && !_is_literal)
{
return false; //empty result
}
bool found = false;
bool if_new_start = false; //the first to add to end in while
for (TableIterator it0 = this->current_table.begin(); it0 != this->new_start;)
{
#ifdef DEBUG_JOIN
if (this->new_start != this->current_table.end())
{
//printf("now the new_start is:");
cerr << "now the new_start is:";
for (RecordIterator it1 = this->new_start->begin(); it1 != this->new_start->end(); ++it1)
{
//printf(" %d", *it1);
cerr << " " << *it1;
}
//printf("\n");
cerr << endl;
}
else
//printf("new_start still in end?!\n");
cerr << "new_start still in end?!" << endl;
//printf("now the record is:");
cerr << "now the record is:";
for (RecordIterator it1 = it0->begin(); it1 != it0->end(); ++it1)
{
//printf(" %d", *it1);
cerr << " " << *it1;
}
//printf("\n");
cerr << endl;
#endif
int cnt = 0;
//update the valid id num according to restrictions by multi vars
//also ordered while id_list and can_list are ordered
//IDList valid_ans_list;
IDList* valid_ans_list = NULL;
//list<int> valid_ans_list;
bool matched = true;
//NOTICE:we can generate cans from either direction, but this way is convenient and better
for (RecordIterator it1 = it0->begin(); it1 != it0->end(); ++it1, ++cnt)
{
#ifdef DEBUG_JOIN
//printf("cnt is: %d\n", cnt);
cerr << "cnt is: " << cnt << endl;
#endif
int edge_index = _edges[cnt];
if (edge_index == -1)
{
continue;
}
#ifdef DEBUG_JOIN
cerr << "edge exists!" << endl;
#endif
int ele = *it1;
int edge_type = this->basic_query->getEdgeType(_id, edge_index);
int pre_id = this->basic_query->getEdgePreID(_id, edge_index);
if (pre_id == -2) //predicate var
{
#ifdef DEBUG_JOIN
cerr << "this is a predicate var!" << endl;
#endif
//if(valid_ans_list == NULL)
//{
//valid_ans_list = IDList::intersect(_can_list, NULL, 0);
//}
//else
//{