diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresGroup.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresGroup.java deleted file mode 100644 index de473ab44..000000000 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresGroup.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.baremaps.tilestore.postgres; - - - -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; -import net.sf.jsqlparser.statement.select.FromItem; -import net.sf.jsqlparser.statement.select.Join; -import net.sf.jsqlparser.statement.select.SelectItem; - -/** - * Models the groups identified in the input queries of a {@code PostgresTileStore}. These groups - * are used to form common table expressions (CTE). - */ -class PostgresGroup { - - private final List> selectItems; - private final FromItem fromItem; - private final List joins; - - /** - * Constructs a {@code PostgresGroup} with objects extracted from an AST obtained by parsing a SQL - * query with JSQLParser. - * - * @param selectItems the selected columns. - * @param fromItem the from clause - * @param joins the join clauses - */ - public PostgresGroup(List> selectItems, FromItem fromItem, List joins) { - this.selectItems = selectItems; - this.fromItem = fromItem; - this.joins = joins; - } - - /** - * Returns the selected columns. - * - * @return the selected columns - */ - public List> getSelectItems() { - return selectItems; - } - - /** - * Returns the from clause. - * - * @return the from clause - */ - public FromItem getFromItem() { - return fromItem; - } - - /** - * Returns the join clauses. - * - * @return the join clauses - */ - public List getJoins() { - return joins; - } - - /** - * Returns the unique alias of this group. - * - * @return the alias - */ - public String getAlias() { - return String.format("h%x", hashCode()); - } - - /** {@inheritDoc} */ - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof PostgresGroup)) { - return false; - } - return hashCode() == o.hashCode(); - } - - /** {@inheritDoc} */ - @Override - public int hashCode() { - String selectItemsString = selectItems.toString(); - String fromItemString = fromItem.toString(); - String joinsString = Optional.ofNullable(joins).stream().flatMap(List::stream) - .map(Join::toString).collect(Collectors.joining()); - return Objects.hash(selectItemsString, fromItemString, joinsString); - } -} diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresQuery.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresQuery.java deleted file mode 100644 index 68cbf7506..000000000 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresQuery.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.baremaps.tilestore.postgres; - - - -import net.sf.jsqlparser.JSQLParserException; -import net.sf.jsqlparser.parser.CCJSqlParserUtil; -import net.sf.jsqlparser.statement.select.PlainSelect; -import net.sf.jsqlparser.statement.select.Select; -import net.sf.jsqlparser.statement.select.SelectItem; -import net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter; - -/** Models the input queries of a {@code PostgresTileStore}. */ -public class PostgresQuery { - - private final String layer; - private final Integer minzoom; - private final Integer maxzoom; - private final String sql; - private final PlainSelect ast; - - /** - * Constructs a {@code PostgresQuery}. - * - * @param layer the layer name - * @param minzoom the min zoom - * @param maxzoom the max zoom - * @param sql the sql query - */ - public PostgresQuery(String layer, Integer minzoom, Integer maxzoom, String sql) { - this.layer = layer; - this.minzoom = minzoom; - this.maxzoom = maxzoom; - this.sql = sql; - this.ast = parse(sql); - } - - /** - * Returns the layer name. - * - * @return the layer name - */ - public String getLayer() { - return layer; - } - - /** - * Returns the min zoom. - * - * @return the min zoom - */ - public Integer getMinzoom() { - return minzoom; - } - - /** - * Returns the max zoom. - * - * @return the max zoom - */ - public Integer getMaxzoom() { - return maxzoom; - } - - /** - * Returns the SQL query. - * - * @return the SQL query - */ - public String getSql() { - return sql; - } - - /** - * Returns the abstract syntax tree (AST) obtained by parsing the query with JSQLParser. - * - * @return the AST - */ - public PlainSelect getAst() { - return ast; - } - - private PlainSelect parse(String query) { - // Try to parse the query - PlainSelect plainSelect; - try { - Select select = (Select) CCJSqlParserUtil.parse(query); - plainSelect = (PlainSelect) select.getSelectBody(); - } catch (JSQLParserException e) { - String message = String.format("The query is malformed.\n" + "\tQuery:\n\t\t%s", query); - throw new IllegalArgumentException(message, e); - } - - // Check the number of columns - if (plainSelect.getSelectItems().size() != 3) { - String message = String.format("The query is malformed.\n" - + "\tExpected format:\n\t\tSELECT c1::bigint, c2::jsonb, c3::geometry FROM t WHERE c\n" - + "\tActual query:\n\t\t%s", query); - throw new IllegalArgumentException(message); - } - - // Remove all the aliases - for (SelectItem selectItem : plainSelect.getSelectItems()) { - selectItem.accept(new SelectItemVisitorAdapter() { - @Override - public void visit(SelectItem selectExpressionItem) { - selectExpressionItem.setAlias(null); - } - }); - } - - return plainSelect; - } -} diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresTileStore.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresTileStore.java index c10c69eb6..4e9358464 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresTileStore.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresTileStore.java @@ -44,10 +44,6 @@ public class PostgresTileStore implements TileStore { private static final Logger logger = LoggerFactory.getLogger(PostgresTileStore.class); - public static final String CONTENT_ENCODING = "gzip"; - - public static final String CONTENT_TYPE = "application/vnd.mapbox-vector-tile"; - private final DataSource datasource; private final Tileset tileset; @@ -100,7 +96,6 @@ WHERE t.geom && ST_TileEnvelope(%d, %d, %d, margin => (64.0/4096)) tileCoord.z(), tileCoord.x(), tileCoord.y(), layerQuery, tileCoord.z(), tileCoord.x(), tileCoord.y())); - } queryBuilder.append(sqlBuilder) @@ -121,31 +116,31 @@ public ByteBuffer read(TileCoord tileCoord) throws TileStoreException { try (Connection connection = datasource.getConnection(); Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(query)) { + ResultSet resultSet = statement.executeQuery(query); + ByteArrayOutputStream data = new ByteArrayOutputStream()) { int length = 0; - try (ByteArrayOutputStream data = new ByteArrayOutputStream(); - OutputStream gzip = new GZIPOutputStream(data)) { + try (OutputStream gzip = new GZIPOutputStream(data)) { while (resultSet.next()) { byte[] bytes = resultSet.getBytes(1); length += bytes.length; gzip.write(bytes); } + } - long stop = System.currentTimeMillis(); - long duration = stop - start; + long stop = System.currentTimeMillis(); + long duration = stop - start; - // Log slow queries (> 10s) - if (duration > 10_000) { - logger.warn("Executed query for tile {} in {} ms: {}", tileCoord, duration, query); - } + // Log slow queries (> 10s) + if (duration > 10_000) { + logger.warn("Executed query for tile {} in {} ms: {}", tileCoord, duration, query); + } - if (length > 0) { - return ByteBuffer.wrap(data.toByteArray()); - } else { - return ByteBuffer.allocate(0); - } + if (length > 0) { + return ByteBuffer.wrap(data.toByteArray()); + } else { + return ByteBuffer.allocate(0); } } catch (Exception e) { logger.error(e.getMessage()); diff --git a/pom.xml b/pom.xml index 16a917dd2..7bb72aff0 100644 --- a/pom.xml +++ b/pom.xml @@ -96,10 +96,6 @@ limitations under the License. 2.1.6 2.37 1.35 -<<<<<<< HEAD - 4.7 -======= ->>>>>>> 74c2f1de (Improve tilestore queries and remove jsqlparser) 1.19.0 5.10.0 5.10.0