forked from mahmoudsebak/Search-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexerDbAdapter.java
1093 lines (971 loc) · 43 KB
/
IndexerDbAdapter.java
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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;
public class IndexerDbAdapter {
// contains 2 tables, one has columns: URL, content, page_rank
// the other has columns: word, URL, tf, word_tag
// these are the column names
public static final String COL_ID = "_id";
public static final String COL_URL = "url";
// content of the url after removing tags (used in phrase search)
public static final String COL_CONTENT = "content";
public static final String COL_TITLE = "title";
public static final String COL_CRAWLED_AT = "crawled_at";
public static final String COL_INDEXED = "indexed";
public static final String COL_PAGE_RANK = "page_rank";
public static final String COL_DATE_SCORE = "date_score";
public static final String COL_GEO_SCORE = "geographic_score";
public static final String COL_WORD = "word";
public static final String COL_STEM = "stem";
public static final String COL_SCORE = "score";
public static final String COL_URL_ID = "url_id";
public static final String COL_SRC_URL = "src_url";
public static final String COL_DST_URL = "dst_url";
public static final String COL_IMAGE = "image";
public static final String COL_ALT = "alt";
public static final String COL_IMG_ID = "img_id";
public static final String COL_QUERY = "query";
public static final String COL_FREQ = "freq";
public static final String COL_PERSON = "person";
public static final String COL_REGION = "region";
public static final String COL_COUNT = "count";
private Connection conn;
private static final String DATABASE_NAME = "dba_search_indexer2";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
private static final String CONNECTION_STRING = "jdbc:mysql://localhost:3306/";
private static final String TABLE_URLS_NAME = "tb1_urls";
private static final String TABLE_URLS_INDEX_NAME = "tbl_urls_index";
private static final String TABLE_WORDS_NAME = "tb2_words";
private static final String TABLE_WORDS_INDEX_NAME = "tb2_words_url_index";
private static final String TABLE_WORDS_INDEX2_NAME = "tb2_stem_url_index";
private static final String TABLE_LINKS_NAME = "tb3_links";
private static final String TABLE_LINKS_INDEX_NAME = "tb3_links_index";
private static final String TABLE_IMAGES_NAME = "tb4_images";
// private static final String TABLE_IMAGES_INDEX_NAME = "tb4_url_images_index";
private static final String TABLE_IMAGES_INDEX2_NAME = "tb4_images_index";
private static final String TABLE_QUERIES_NAME = "tb5_queries";
private static final String TABLE_USER_URLS_NAME = "tb6_user_urls";
private static final String TABLE_IMAGE_WORDS_NAME = "tb7_image_words";
private static final String TABLE_IMAGE_WORDS_INDEX_NAME = "tb7_image_words_url_index";
private static final String TABLE_IMAGE_WORDS_INDEX2_NAME = "tb7_image_stem_url_index";
private static final String TABLE_TRENDS_NAME = "tb8_trends";
// SQL statement used to create the database
private static final String TABLE1_CREATE = String.format(
"CREATE TABLE if not exists %s( %s INTEGER PRIMARY KEY AUTO_INCREMENT, %s varchar(256),"
+ " %s MEDIUMTEXT, %s varchar(512), %s DATETIME, %s BOOLEAN DEFAULT false, %s double DEFAULT 0,"
+ " %s double DEFAULT 0, %s double DEFAULT 0)",
TABLE_URLS_NAME, COL_ID, COL_URL, COL_CONTENT, COL_TITLE, COL_CRAWLED_AT, COL_INDEXED, COL_PAGE_RANK,
COL_DATE_SCORE, COL_GEO_SCORE);
private static final String TABLE1_INDEX_CREATE = String.format("CREATE UNIQUE INDEX if not exists %s ON %s(%s)",
TABLE_URLS_INDEX_NAME, TABLE_URLS_NAME, COL_URL);
private static final String TABLE2_CREATE = String.format(
"CREATE TABLE if not exists %s(%s INTEGER PRIMARY KEY AUTO_INCREMENT,"
+ " %s varchar(100), %s varchar(100), %s INTEGER, %s DOUBLE, FOREIGN KEY (%s) REFERENCES %s(%s) ON DELETE CASCADE)",
TABLE_WORDS_NAME, COL_ID, COL_WORD, COL_STEM, COL_URL_ID, COL_SCORE, COL_URL_ID, TABLE_URLS_NAME, COL_ID);
private static final String TABLE2_INDEX_CREATE = String.format(
"CREATE UNIQUE INDEX if not exists %s ON %s(%s, %s)", TABLE_WORDS_INDEX_NAME, TABLE_WORDS_NAME, COL_WORD,
COL_URL_ID);
private static final String TABLE2_INDEX2_CREATE = String.format(
"CREATE INDEX if not exists %s ON %s(%s, %s)", TABLE_WORDS_INDEX2_NAME, TABLE_WORDS_NAME, COL_STEM,
COL_URL_ID);
public static final String TABLE3_LINKS_CREATE = String.format(
"CREATE TABLE IF NOT EXISTS %s( %s INTEGER PRIMARY KEY AUTO_INCREMENT,"
+ " %s INTEGER, %s INTEGER, FOREIGN KEY (%s) REFERENCES %s(%s) ON DELETE CASCADE,"
+ " FOREIGN KEY (%s) REFERENCES %s(%s) ON DELETE CASCADE)",
TABLE_LINKS_NAME, COL_ID, COL_SRC_URL, COL_DST_URL, COL_SRC_URL, TABLE_URLS_NAME, COL_ID, COL_DST_URL,
TABLE_URLS_NAME, COL_ID);
private static final String TABLE3_INDEX_CREATE = String.format(
"CREATE UNIQUE INDEX if not exists %s ON %s(%s, %s)", TABLE_LINKS_INDEX_NAME, TABLE_LINKS_NAME,
COL_SRC_URL, COL_DST_URL);
public static final String TABLE4_IMAGES_CREATE = String.format(
"CREATE TABLE IF NOT EXISTS %s( %s INTEGER PRIMARY KEY AUTO_INCREMENT,"
+ " %s INTEGER, %s varchar(512), %s TEXT, FOREIGN KEY (%s) REFERENCES %s(%s) ON DELETE CASCADE)",
TABLE_IMAGES_NAME, COL_ID, COL_URL_ID, COL_IMAGE, COL_ALT, COL_URL_ID, TABLE_URLS_NAME, COL_ID);
// private static final String TABLE4_INDEX_CREATE = String.format(
// "CREATE INDEX if not exists %s ON %s(%s, %s)", TABLE_IMAGES_INDEX_NAME, TABLE_IMAGES_NAME,
// COL_URL, COL_IMAGE);
private static final String TABLE4_INDEX2_CREATE = String.format("CREATE UNIQUE INDEX if not exists %s ON %s(%s)",
TABLE_IMAGES_INDEX2_NAME, TABLE_IMAGES_NAME, COL_IMAGE);
private static final String TABLE5_QUERIES_CREATE = String.format(
"CREATE TABLE IF NOT EXISTS %s(%s INTEGER PRIMARY KEY AUTO_INCREMENT, %s TEXT UNIQUE)", TABLE_QUERIES_NAME, COL_ID,
COL_QUERY);
private static final String TABLE6_USER_URLS_CREATE = String.format(
"CREATE TABLE IF NOT EXISTS %s(%s INTEGER PRIMARY KEY AUTO_INCREMENT, %s varchar(256) UNIQUE, %s INTEGER)",
TABLE_USER_URLS_NAME, COL_ID, COL_URL, COL_FREQ);
private static final String TABLE7_CREATE = String.format(
"CREATE TABLE if not exists %s(%s INTEGER PRIMARY KEY AUTO_INCREMENT,"
+ " %s varchar(100), %s varchar(100), %s INTEGER, FOREIGN KEY (%s) REFERENCES %s(%s) ON DELETE CASCADE)",
TABLE_IMAGE_WORDS_NAME, COL_ID, COL_WORD, COL_STEM, COL_IMG_ID, COL_IMG_ID, TABLE_IMAGES_NAME, COL_ID);
private static final String TABLE7_INDEX_CREATE = String.format(
"CREATE UNIQUE INDEX if not exists %s ON %s(%s, %s)", TABLE_IMAGE_WORDS_INDEX_NAME, TABLE_IMAGE_WORDS_NAME, COL_WORD,
COL_IMG_ID);
private static final String TABLE7_INDEX2_CREATE = String.format(
"CREATE INDEX if not exists %s ON %s(%s, %s)", TABLE_IMAGE_WORDS_INDEX2_NAME, TABLE_IMAGE_WORDS_NAME, COL_STEM,
COL_IMG_ID);
private static final String TABLE8_CREATE = String.format(
"CREATE TABLE IF NOT EXISTS %s(%s INTEGER PRIMARY KEY AUTO_INCREMENT,"
+ " %s varchar(256), %s varchar(256), %s INTEGER, UNIQUE(%s, %s))",
TABLE_TRENDS_NAME, COL_ID, COL_PERSON, COL_REGION, COL_COUNT, COL_PERSON, COL_REGION);
private static final String DATABASE_CREATE = String.format("CREATE DATABASE IF NOT EXISTS %s", DATABASE_NAME);
public IndexerDbAdapter() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void createTables() {
try (Statement stmt = conn.createStatement()) {
stmt.addBatch(TABLE1_CREATE);
stmt.addBatch(TABLE1_INDEX_CREATE);
stmt.addBatch(TABLE2_CREATE);
stmt.addBatch(TABLE2_INDEX_CREATE);
stmt.addBatch(TABLE2_INDEX2_CREATE);
stmt.addBatch(TABLE3_LINKS_CREATE);
stmt.addBatch(TABLE3_INDEX_CREATE);
stmt.addBatch(TABLE4_IMAGES_CREATE);
// stmt.addBatch(TABLE4_INDEX_CREATE);
stmt.addBatch(TABLE4_INDEX2_CREATE);
stmt.addBatch(TABLE5_QUERIES_CREATE);
stmt.addBatch(TABLE6_USER_URLS_CREATE);
stmt.addBatch(TABLE7_CREATE);
stmt.addBatch(TABLE7_INDEX_CREATE);
stmt.addBatch(TABLE7_INDEX2_CREATE);
stmt.addBatch(TABLE8_CREATE);
stmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void open() {
try {
conn = DriverManager.getConnection(CONNECTION_STRING, USERNAME, PASSWORD);
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
conn.close();
Properties info = new Properties();
info.setProperty("user", USERNAME);
info.setProperty("password", PASSWORD);
info.setProperty("cachePrepStmts", "true");
info.setProperty("prepStmtCacheSize", "250");
info.setProperty("prepStmtCacheSqlLimit", "2048");
info.setProperty("useServerPrepStmts", "true");
info.setProperty("useLocalSessionState", "true");
info.setProperty("rewriteBatchedStatements", "true");
info.setProperty("cacheResultSetMetadata", "true");
info.setProperty("cacheServerConfiguration", "true");
info.setProperty("elideSetAutoCommits", "true");
info.setProperty("maintainTimeStats", "false");
conn = DriverManager.getConnection(CONNECTION_STRING + DATABASE_NAME, info);
createTables();
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @param url url to get its ID
* @return id of the url or -1 if not found
*/
public int getURLID(String url) {
String sql = String.format("SELECT %s FROM %s WHERE %s = ?", COL_ID, TABLE_URLS_NAME, COL_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, url);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next())
return rs.getInt(1);
} catch (Exception e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
/**
*
* @param img image to get its ID
* @return id of the image or -1 if not found
*/
public int getImgID(String img) {
String sql = String.format("SELECT %s FROM %s WHERE %s = ?", COL_ID, TABLE_IMAGES_NAME, COL_IMAGE);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, img);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next())
return rs.getInt(1);
} catch (Exception e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public int getDocumentsNum() {
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + TABLE_URLS_NAME)) {
if (rs.next())
return rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
/**
* add a trend to the database
*
* @param word the search word to be added to database
* @param region the region of the user
* @return whether the person is added successfully or not
*/
public boolean addTrend(String person, String region) {
String sql = String.format("INSERT INTO %s(%s, %s, %s) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE %s = %s + 1",
TABLE_TRENDS_NAME, COL_PERSON, COL_REGION, COL_COUNT, COL_COUNT, COL_COUNT);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, person);
ps.setString(2, region);
ps.setInt(3, 1);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* @return most searched persons and their counts
*/
public ArrayList<HashMap<String, Object>> fetchTrends(String region) {
String sql = String.format("SELECT %s, %s FROM %s where %s = ? ORDER BY %s DESC LIMIT 10", COL_PERSON,
COL_COUNT, TABLE_TRENDS_NAME, COL_REGION, COL_COUNT);
ArrayList<HashMap<String, Object>> ret = new ArrayList<>();
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, region);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
HashMap<String, Object> elem = new HashMap<>();
elem.put("person", rs.getString(1));
elem.put("count", rs.getInt(2));
ret.add(elem);
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
/**
* add url to the database
*
* @param url the url to be added to database
* @return whether the url is added successfully
*/
public int addURL(String url) {
String sql = String.format("INSERT INTO %s(%s) VALUES(?)", TABLE_URLS_NAME, COL_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, url);
ps.executeUpdate();
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next())
return rs.getInt(1);
} catch (Exception e) {
return -1;
}
} catch (SQLException e) {
return -1;
}
return -1;
}
/**
* change the crawling date of a url and set indexed attribute to false
*
* @param url the url to change its crawled_date
*/
public void crawlURL(String url) {
String sql = String.format("UPDATE %s set %s = now(), %s = false WHERE %s = ?", TABLE_URLS_NAME, COL_CRAWLED_AT,
COL_INDEXED, COL_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, url);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @return all URLs to be crawled
*/
public ArrayList<String> getUnCrawledURLs() {
String sql = String.format("SELECT %s FROM %s WHERE %s IS NULL", COL_URL, TABLE_URLS_NAME, COL_CRAWLED_AT);
ArrayList<String> result = new ArrayList<>();
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
result.add(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @return all crawled urls
*/
public ArrayList<String> getCrawledURLs() {
String sql = String.format("SELECT %s FROM %s WHERE %s IS NOT NULL", COL_URL, TABLE_URLS_NAME, COL_CRAWLED_AT);
ArrayList<String> result = new ArrayList<>();
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
result.add(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @return URLs sorted by most recent urls
*/
public ArrayList<String> getURLsToBeRecrawled() {
String sql = String.format("SELECT %s FROM %s ORDER BY %s DESC", COL_URL, TABLE_URLS_NAME, COL_DATE_SCORE);
ArrayList<String> result = new ArrayList<>();
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
result.add(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* update a url
*
* @param url the url to be updated
* @param content the full plain text of the url without tags
* @param date_score the date score of the url (recent pages are favored to old
* pages)
* @param geo_score the geographic location score of the url
*/
public void updateURL(String url, String content, String title, double date_score, double geo_score) {
String sql = String.format("UPDATE %s set %s = ?, %s = ?, %s = ?, %s = ? WHERE %s = ?", TABLE_URLS_NAME, COL_CONTENT,
COL_TITLE, COL_DATE_SCORE, COL_GEO_SCORE, COL_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, content);
ps.setString(2, title);
ps.setDouble(3, date_score);
ps.setDouble(4, geo_score);
ps.setString(5, url);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Update a group of urls using batch
* @param pages array of pages
*/
public void updateAllURLS(ArrayList<Page>pages){
String sql = String.format("UPDATE %s set %s = ?, %s = ?, %s = ?, %s = ? WHERE %s = ?", TABLE_URLS_NAME, COL_CONTENT,
COL_TITLE, COL_DATE_SCORE, COL_GEO_SCORE, COL_URL);
try(PreparedStatement ps = conn.prepareStatement(sql)){
for(int i=0;i<pages.size();i++){
ps.setString(1, pages.get(i).getContent());
ps.setString(2, pages.get(i).getTitle());
ps.setDouble(3, pages.get(i).getDateScore());
ps.setDouble(4, pages.get(i).getGeoScore());
ps.setString(5, pages.get(i).getUrl());
ps.addBatch();
}
ps.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* update a url
*
* @param url the url to be updated
* @param pageRank the rank of the url (used in page ranking)
*/
public void updateURL(String url, double page_rank) {
String sql = String.format("UPDATE %s set %s = ? WHERE %s = ?", TABLE_URLS_NAME, COL_PAGE_RANK, COL_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setDouble(1, page_rank);
ps.setString(2, url);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* update pages ranks
*
* @param ranks hashmap of each url and its rank
*/
public void updatePagesRanks(HashMap<String, Double> ranks) {
String sql = String.format("UPDATE %s set %s = ? WHERE %s = ?", TABLE_URLS_NAME, COL_PAGE_RANK, COL_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (Entry<String, Double> page: ranks.entrySet()) {
ps.setDouble(1, page.getValue());
ps.setString(2, page.getKey());
ps.addBatch();
}
ps.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* set indexed attribute of url
*
* @param url the url to set its indexed attribute
* @param indexed whether the url is indexed or not
*/
public void setIndexedURL(String url, boolean indexed) {
String sql = String.format("UPDATE %s set %s = ? WHERE %s = ?", TABLE_URLS_NAME, COL_INDEXED, COL_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setBoolean(1, indexed);
ps.setString(2, url);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @return first un-indexed URL or null if not found
*/
public String getUnindexedURL() {
String sql = String.format("SELECT %s FROM %s WHERE %s IS false LIMIT 1", COL_URL, TABLE_URLS_NAME,
COL_INDEXED);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
return rs.getString(1);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void resetPagesRank() {
String sql = String.format("UPDATE %s SET %s = %s", TABLE_URLS_NAME, COL_PAGE_RANK, 1.0 / getDocumentsNum());
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* add all words of a certian url
*
* @param wordsScores hashmap of words with their scores
* @param url the url to add its words
*/
public void addWords(HashMap<String, Double> wordsScores, int URLID) {
if (wordsScores.size() < 1)
return;
String sql = String.format(
"INSERT IGNORE INTO %s(%s, %s, %s, %s) VALUES" + makeParentheses(wordsScores.size(), 4)
+ "ON DUPLICATE KEY UPDATE %s = VALUES(%s)",
TABLE_WORDS_NAME, COL_WORD, COL_STEM, COL_URL_ID, COL_SCORE, COL_SCORE, COL_SCORE);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
int i = 1;
for (Entry<String, Double> entry : wordsScores.entrySet()) {
ps.setString(i, entry.getKey());
ps.setString(i + 1, WordsExtractionProcess.stem(entry.getKey()));
ps.setInt(i + 2, URLID);
ps.setDouble(i + 3, entry.getValue());
i += 4;
}
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* add a word of a url
*
* @param word the word to add
* @param url the url to add its word
* @param score score is sum of (term frequency of certian tag)*(tag score) of
* different htm tags of a word
*/
public void addWord(String word, int URLID, double score) {
String sql = String.format("INSERT INTO %s(%s, %s, %s, %s) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE %s = VALUES(%s)",
TABLE_WORDS_NAME, COL_WORD, COL_STEM, COL_URL_ID, COL_SCORE, COL_SCORE, COL_SCORE);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, word);
ps.setString(2, WordsExtractionProcess.stem(word));
ps.setInt(3, URLID);
ps.setDouble(4, score);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// return true when the link is added successfully, false otherwise
public boolean addLink(int srcURL, int dstURL) {
String sql = String.format("INSERT INTO %s(%s, %s) VALUES(?, ?)", TABLE_LINKS_NAME, COL_SRC_URL, COL_DST_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, srcURL);
ps.setInt(2, dstURL);
ps.executeUpdate();
} catch (SQLException e) {
return false;
}
return true;
}
/**
* add an image of a url
*
* @param image the image to be added
* @param url the url to add its image
* @return true if added sucessfully, false otherwise
*/
public boolean addImage(int URLID, String image, String alt) {
String sql = String.format("INSERT INTO %s(%s, %s, %s) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE %s = VALUES(%s)",
TABLE_IMAGES_NAME, COL_URL_ID, COL_IMAGE, COL_ALT);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, URLID);
ps.setString(2, image);
ps.setString(3, alt);
ps.executeUpdate();
} catch (SQLException e) {
return false;
}
return true;
}
/**
* add all images of a url
* @param url the url to add its imags
* @param images list of images to be added
*/
public void addImages(int URLID, ArrayList<Image> images) {
if (images.size() < 1)
return;
String sql = String.format(
"INSERT IGNORE INTO %s(%s, %s, %s) VALUES " + makeParentheses(images.size(), 3)
+ " ON DUPLICATE KEY UPDATE %s = VALUES(%s)",
TABLE_IMAGES_NAME, COL_URL_ID, COL_IMAGE, COL_ALT, COL_ALT, COL_ALT);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
int i = 0;
for (Image image : images) {
ps.setInt(i+1, URLID);
ps.setString(i+2, image.getSrc());
ps.setString(i+3, image.getAlt());
i += 3;
}
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* add all words of a certian image
*
* @param words array of words
* @param src id of the image src to add its words
*/
public void addImageWords(int ImgID, ArrayList<String> words){
if (words.size() < 1)
return;
String sql = String.format(
"INSERT IGNORE INTO %s(%s, %s, %s) VALUES " + makeParentheses(words.size(), 3)
+ " ON DUPLICATE KEY UPDATE %s = VALUES(%s)",
TABLE_IMAGE_WORDS_NAME, COL_WORD, COL_STEM, COL_IMG_ID, COL_STEM, COL_STEM);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
int i = 0;
for (String word : words) {
ps.setString(i+1, word);
ps.setString(i+2, WordsExtractionProcess.stem(word));
ps.setInt(i+3, ImgID);
i += 3;
}
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// utility function to create placeholders (?) given a length
private String makePlaceholders(int len) {
if (len < 1) {
// It will lead to an invalid query anyway ..
throw new RuntimeException("No placeholders");
} else {
StringBuilder sb = new StringBuilder(len * 2 - 1);
sb.append("?");
for (int i = 1; i < len; i++) {
sb.append(",?");
}
return sb.toString();
}
}
/**
* utility function to create parentheses of place holders of size n*m
* @param n number of parantheses
* @param m number of placeholders inside each parentheses
* @return
*/
private String makeParentheses(int n, int m) {
if (n < 1) {
throw new RuntimeException("No placeholders");
} else {
StringBuilder string = new StringBuilder(10 * n);
string.append("(" + makePlaceholders(m) + ")");
for (int i = 1; i < n; i++) {
string.append(",(" + makePlaceholders(m) + ")");
}
return string.toString();
}
}
/**
* search database for the given words
* @param words the words to search for
* @param limit limit number of urls to be returned (recommended 10)
* @param page the page number of the result
* @return list of hashmaps that contain url, content and title
*/
public ArrayList<HashMap<String, String>> queryWords(String[] words, int limit, int page) {
String sql = String.format("SELECT %s, %s, %s FROM %s t1"
+ " JOIN (SELECT %s, log((select count(*) from %s)*1.0/count(%s)) as idf FROM %s GROUP BY %s)"
+ " as temp USING (%s) JOIN %s t3 ON t1.%s = t3.%s LEFT JOIN (SELECT %s, count(*) as matching_words from %s"
+ " where %s in (" + makePlaceholders(words.length) + ") GROUP BY %s) as temp2 USING (%s)"
+ " WHERE %s in (" + makePlaceholders(words.length) + ") and %s < 1.7 GROUP by %s"
+ " ORDER BY (4*sum(%s*idf) + COALESCE(matching_words, 0) + %s + 0.5*(%s + %s)) DESC LIMIT ?, ?",
COL_URL, COL_CONTENT, COL_TITLE, TABLE_WORDS_NAME, COL_STEM, TABLE_URLS_NAME, COL_URL_ID,
TABLE_WORDS_NAME, COL_STEM, COL_STEM, TABLE_URLS_NAME, COL_URL_ID, COL_ID, COL_URL_ID, TABLE_WORDS_NAME,
COL_WORD, COL_URL_ID, COL_URL_ID, COL_STEM, COL_SCORE, COL_URL_ID, COL_SCORE, COL_PAGE_RANK,
COL_DATE_SCORE, COL_GEO_SCORE);
ArrayList<HashMap<String, String>> ret = new ArrayList<HashMap<String, String>>();
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (int i = 0; i < words.length; i++) {
ps.setString(i + 1, words[i]);
}
for (int i = 0; i < words.length; i++) {
ps.setString(words.length + i + 1, WordsExtractionProcess.stem(words[i]));
}
ps.setInt(2 * words.length + 1, (page - 1) * limit);
ps.setInt(2 * words.length + 2, limit);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
HashMap<String, String> elem = new HashMap<String, String>();
elem.put("url", rs.getString(1));
elem.put("content", rs.getString(2));
elem.put("title", rs.getString(3));
ret.add(elem);
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
/**
* search for a phrase in database
* @param phrase the phrase to search for
* @param words splitted phrase as list of words
* @param limit limit number of urls to be returned (recommended 10)
* @param page the page number of the result
* @return list of hashmaps that contain url, content and title
*/
public ArrayList<HashMap<String, String>> queryPhrase(String phrase, String[] words, int limit, int page) {
String sql = String.format("SELECT %s, %s, %s FROM %s t1"
+ " JOIN (SELECT %s, log((select count(*) from %s)*1.0/count(%s)) as idf FROM %s GROUP BY %s)"
+ " as temp USING (%s) JOIN %s t3 ON t1.%s = t3.%s WHERE %s in (" + makePlaceholders(words.length)
+ ") and %s < 1.7 and %s like ? GROUP by %s ORDER BY (3*sum(%s*idf) + %s + 0.5*(%s + %s)) DESC LIMIT ?, ?",
COL_URL, COL_CONTENT, COL_TITLE, TABLE_WORDS_NAME, COL_STEM, TABLE_URLS_NAME, COL_URL_ID, TABLE_WORDS_NAME,
COL_STEM, COL_STEM, TABLE_URLS_NAME, COL_URL_ID, COL_ID, COL_STEM, COL_SCORE, COL_CONTENT, COL_URL_ID,
COL_SCORE, COL_PAGE_RANK, COL_DATE_SCORE, COL_GEO_SCORE);
ArrayList<HashMap<String, String>> ret = new ArrayList<HashMap<String, String>>();
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (int i = 0; i < words.length; i++) {
ps.setString(i + 1, WordsExtractionProcess.stem(words[i]));
}
// escape wildcard characters in phrase query
phrase = phrase.replace("%", "\\%").replace("_", "\\_");
ps.setString(words.length + 1, "%" + phrase + "%");
ps.setInt(words.length + 2, (page - 1) * limit);
ps.setInt(words.length + 3, limit);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
HashMap<String, String> elem = new HashMap<String, String>();
elem.put("url", rs.getString(1));
elem.put("content", rs.getString(2));
elem.put("title", rs.getString(3));
ret.add(elem);
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
/**
* get images from database by phrase
*
* @param phrase the phrase to search for
* @param limit limit number of urls to be returned (recommended 10)
* @param page the page number of the result
* @return list of hashmaps that contain url and image
*
*/
public ArrayList<HashMap<String, String>> queryImage(String phrase, int limit, int page) {
String sql = String.format(
"SELECT %s, %s, %s FROM %s t1 JOIN %s t2 ON t1.%s = t2.%s"
+ " where %s like ? ORDER BY ( %s + 0.5*(%s + %s)) DESC LIMIT ?, ?",
COL_URL, COL_IMAGE, COL_ALT, TABLE_IMAGES_NAME, TABLE_URLS_NAME, COL_URL_ID, COL_ID, COL_ALT,
COL_PAGE_RANK, COL_DATE_SCORE, COL_GEO_SCORE);
ArrayList<HashMap<String, String>> ret = new ArrayList<HashMap<String, String>>();
try (PreparedStatement ps = conn.prepareStatement(sql)) {
// escape wildcard characters in phrase query
phrase = phrase.replace("%", "\\%").replace("_", "\\_");
ps.setString(1, "%" + phrase + "%");
ps.setInt(2, (page - 1) * limit);
ps.setInt(3, limit);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
HashMap<String, String> elem = new HashMap<String, String>();
elem.put("url", rs.getString(1));
elem.put("image", rs.getString(2));
elem.put("alt", rs.getString(3));
ret.add(elem);
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
/**
* get images from database by words
*
* @param words the list of words
* @param limit the number of results in a page
* @param page page number
* @return list of hashmaps of url and image
*/
public ArrayList<HashMap<String, String>> queryImage(String[] words, int limit, int page) {
String sql = String.format(
"SELECT %s, %s, %s FROM %s t1 JOIN %s t2 ON t1.%s = t2.%s JOIN %s t3 ON t1.%s = t3.%s"
+ " LEFT JOIN (SELECT %s, COUNT(*) AS matching_words FROM %s WHERE %s in ("
+ makePlaceholders(words.length) + ")" + "GROUP BY %s) as temp USING(%s) WHERE %s in("
+ makePlaceholders(words.length) + ") GROUP BY %s"
+ " ORDER BY (COALESCE(matching_words, 0) + %s + 0.5*(%s + %s)) DESC LIMIT ?, ?",
COL_URL, COL_IMAGE, COL_ALT, TABLE_IMAGES_NAME, TABLE_IMAGE_WORDS_NAME, COL_ID, COL_IMG_ID,
TABLE_URLS_NAME, COL_URL_ID, COL_ID, COL_IMG_ID, TABLE_IMAGE_WORDS_NAME, COL_WORD, COL_IMG_ID,
COL_IMG_ID, COL_STEM, COL_IMG_ID, COL_PAGE_RANK, COL_DATE_SCORE, COL_GEO_SCORE);
ArrayList<HashMap<String, String>> ret = new ArrayList<HashMap<String, String>>();
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (int i = 0; i < words.length; i++) {
ps.setString(i + 1, words[i]);
}
for (int i = 0; i < words.length; i++) {
ps.setString(words.length + i+1, WordsExtractionProcess.stem(words[i]));
}
ps.setInt(2 * words.length + 1, (page - 1) * limit);
ps.setInt(2 * words.length + 2, limit);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
HashMap<String, String> elem = new HashMap<String, String>();
elem.put("url", rs.getString(1));
elem.put("image", rs.getString(2));
elem.put("alt", rs.getString(3));
ret.add(elem);
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
/**
* add user query in database
* @param query the query of the user
*/
public boolean addQuery(String query) {
String sql = String.format("INSERT INTO %s(%s) VALUES(?)", TABLE_QUERIES_NAME, COL_QUERY);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, query);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* fetch all queries inserted by all users
*
* @return array of strings of queries of users
*/
public ArrayList<String> fetchAllQueries() {
String sql = String.format("SELECT %s FROM %s", COL_QUERY, TABLE_QUERIES_NAME);
ArrayList<String> ret = new ArrayList<String>();
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
ret.add(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* add urls that the user clicks on, this function is used in personalized search
* @param url the url that the user clicked on
*/
public void addUserURL(String url) {
String sql = String.format("INSERT INTO %s(%s, %s) VALUES(?, 1) ON DUPLICATE KEY UPDATE %s = %s + 1",
TABLE_USER_URLS_NAME, COL_URL, COL_FREQ, COL_FREQ, COL_FREQ);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, url);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
*
* @param url the url to return its frequency
* @return the frequency of clicks on url
*/
public int getUserURLFreq(String url) {
String sql = String.format("SELECT %s FROM %s WHERE %s = ?", COL_FREQ, TABLE_USER_URLS_NAME, COL_URL);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, url);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
/**
* @return all links that are used in page rank
*/
public ArrayList<Pair<String,String>> fetchAllLinks() {
String sql = String.format(
"SELECT tb2.%s src, tb3.%s dst FROM %s tb1 JOIN tb1_urls tb2 on tb1.%s = tb2.%s JOIN %s tb3 on tb1.%s = tb3.%s",
COL_URL, COL_URL, TABLE_LINKS_NAME, COL_SRC_URL, COL_ID, TABLE_URLS_NAME, COL_DST_URL, COL_ID);
ArrayList<Pair<String,String>> ret = new ArrayList<>();
try (Statement stmt = conn.createStatement()) {