|
| 1 | +/* |
| 2 | + * Licensed to the Technische Universität Darmstadt under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The Technische Universität Darmstadt |
| 6 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.dkpro.jwpl.api; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +import java.sql.Connection; |
| 24 | +import java.sql.DriverManager; |
| 25 | +import java.sql.PreparedStatement; |
| 26 | +import java.sql.ResultSet; |
| 27 | +import java.sql.SQLException; |
| 28 | + |
| 29 | +import org.dkpro.jwpl.api.testdb.JwplTestDatabase; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.condition.EnabledIf; |
| 32 | + |
| 33 | +/** |
| 34 | + * Verifies that the exact, case-sensitive name lookups of {@link Wikipedia#existsPage(String)} and |
| 35 | + * {@link Category#Category(Wikipedia, String)} can use the name indexes on MySQL and MariaDB. An |
| 36 | + * explicit collation in the comparison, e.g. {@code COLLATE utf8mb4_bin}, would force a full scan |
| 37 | + * instead. |
| 38 | + */ |
| 39 | +@EnabledIf("isMySqlFamily") |
| 40 | +public class NameLookupIndexTest |
| 41 | + extends BaseJWPLTest |
| 42 | +{ |
| 43 | + |
| 44 | + static boolean isMySqlFamily() |
| 45 | + { |
| 46 | + return JwplTestDatabase.selectEngine() != JwplTestDatabase.Engine.HSQLDB; |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testExistsPageQueryUsesNameIndex() throws SQLException |
| 51 | + { |
| 52 | + assertIndexLookup(Wikipedia.PAGE_NAMES_BY_NAME_QUERY, |
| 53 | + "Exploring_the_Potential_of_Semantic_Relatedness_in_Information_Retrieval", |
| 54 | + "name_index"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testCategoryQueryUsesNameIndex() throws SQLException |
| 59 | + { |
| 60 | + assertIndexLookup(Category.CATEGORY_BY_NAME_QUERY, "People_of_UKP", "nameIndex"); |
| 61 | + } |
| 62 | + |
| 63 | + private static void assertIndexLookup(String query, String name, String index) |
| 64 | + throws SQLException |
| 65 | + { |
| 66 | + DatabaseConfiguration db = obtainDbConfiguration(); |
| 67 | + String sql = "EXPLAIN " + query.replaceAll(":\\w+", "?"); |
| 68 | + try (Connection c = DriverManager.getConnection(db.getJdbcURL(), db.getUser(), |
| 69 | + db.getPassword()); |
| 70 | + PreparedStatement ps = c.prepareStatement(sql)) { |
| 71 | + long parameters = sql.chars().filter(ch -> ch == '?').count(); |
| 72 | + for (int i = 1; i <= parameters; i++) { |
| 73 | + ps.setString(i, name); |
| 74 | + } |
| 75 | + try (ResultSet rs = ps.executeQuery()) { |
| 76 | + assertTrue(rs.next(), "EXPLAIN returned no plan for: " + sql); |
| 77 | + assertEquals("ref", rs.getString("type"), sql); |
| 78 | + assertEquals(index, rs.getString("key"), sql); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments