Skip to content

Commit

Permalink
Improve postgres tilestore (#792)
Browse files Browse the repository at this point in the history
* Align tilestore queries with the baremaps-exporter

* Remove dependency to jsqlparser

* Improve documentation and naming

Co-authored-by: Joe Polastre <[email protected]>
  • Loading branch information
bchapuis and polastre authored Oct 27, 2023
1 parent 03fd826 commit 647a273
Show file tree
Hide file tree
Showing 21 changed files with 331 additions and 684 deletions.
4 changes: 0 additions & 4 deletions baremaps-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ limitations under the License.
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public void save(List<IpLocObject> ipLocObjects) {
*/
public void save(Stream<IpLocObject> ipLocObjects) {
StreamUtils.partition(ipLocObjects, 100)
.map(Stream::toList)
.forEach(this::save);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@



import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
Expand All @@ -28,7 +30,7 @@
*
* @param <T> the type of elements returned by this {@code Spliterator}
*/
public class PartitionedSpliterator<T> implements Spliterator<Stream<T>> {
public class PartitionedSpliterator<T> implements Spliterator<List<T>> {

private final Spliterator<T> spliterator;

Expand All @@ -47,23 +49,23 @@ public PartitionedSpliterator(Spliterator<T> spliterator, int partitionSize) {

/** {@inheritDoc} */
@Override
public boolean tryAdvance(Consumer<? super Stream<T>> action) {
Stream.Builder<T> partition = Stream.builder();
public boolean tryAdvance(Consumer<? super List<T>> action) {
var list = new ArrayList<T>(partitionSize);
int size = 0;
while (size < partitionSize && spliterator.tryAdvance(partition::add)) {
while (size < partitionSize && spliterator.tryAdvance(list::add)) {
size++;
}
if (size == 0) {
return false;
}
action.accept(partition.build());
action.accept(list);
return true;
}

/** {@inheritDoc} */
@Override
public Spliterator<Stream<T>> trySplit() {
HoldingConsumer<Stream<T>> consumer = new HoldingConsumer<>();
public Spliterator<List<T>> trySplit() {
HoldingConsumer<List<T>> consumer = new HoldingConsumer<>();
tryAdvance(consumer);
return Stream.ofNullable(consumer.value()).spliterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -190,7 +191,7 @@ public static <T, U> Stream<U> bufferInSourceOrder(
}

/** Partition the provided stream according to a partition size. */
public static <T> Stream<Stream<T>> partition(
public static <T> Stream<List<T>> partition(
Stream<T> stream,
int partitionSize) {
return StreamSupport.stream(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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;

import java.nio.ByteBuffer;

public class TileEntry {

private final TileCoord tileCoord;
private final ByteBuffer byteBuffer;

public TileEntry(TileCoord tileCoord, ByteBuffer byteBuffer) {
this.tileCoord = tileCoord;
this.byteBuffer = byteBuffer;
}

public TileCoord getTileCoord() {
return tileCoord;
}

public ByteBuffer getByteBuffer() {
return byteBuffer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@


import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/** Represents a store for tiles. */
public interface TileStore {
Expand All @@ -33,6 +35,21 @@ public interface TileStore {
*/
ByteBuffer read(TileCoord tileCoord) throws TileStoreException;

/**
* Reads the content of a list of tiles.
*
* @param tileCoords the tile coordinates
* @return the content of the tiles
* @throws TileStoreException
*/
default List<ByteBuffer> read(List<TileCoord> tileCoords) throws TileStoreException {
var blobs = new ArrayList<ByteBuffer>();
for (var tileCoord : tileCoords) {
blobs.add(read(tileCoord));
}
return blobs;
}

/**
* Writes the content of a tile.
*
Expand All @@ -42,11 +59,35 @@ public interface TileStore {
*/
void write(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException;

/**
* Writes the content of a list of tiles.
*
* @param entries the tile entries
* @throws TileStoreException
*/
default void write(List<TileEntry> entries) throws TileStoreException {
for (var entry : entries) {
write(entry.getTileCoord(), entry.getByteBuffer());
}
}

/**
* Deletes the content of a tile.
*
* @param tileCoord the tile coordinate
* @throws TileStoreException
*/
void delete(TileCoord tileCoord) throws TileStoreException;

/**
* Deletes the content of a list of tiles.
*
* @param tileCoords the tile coordinates
* @throws TileStoreException
*/
default void delete(List<TileCoord> tileCoords) throws TileStoreException {
for (var tileCoord : tileCoords) {
delete(tileCoord);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.baremaps.tilestore.mbtiles;



import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.Connection;
Expand All @@ -27,9 +26,11 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.baremaps.tilestore.TileCoord;
import org.apache.baremaps.tilestore.TileEntry;
import org.apache.baremaps.tilestore.TileStore;
import org.apache.baremaps.tilestore.TileStoreException;

Expand All @@ -40,7 +41,7 @@
*/
public class MBTilesStore implements TileStore {

private static final String CREATE_TABLE_METADATA =
public static final String CREATE_TABLE_METADATA =
"CREATE TABLE IF NOT EXISTS metadata (name TEXT, value TEXT, PRIMARY KEY (name))";

private static final String CREATE_TABLE_TILES =
Expand Down Expand Up @@ -75,7 +76,9 @@ public MBTilesStore(DataSource dataSource) {
this.dataSource = dataSource;
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {
try (Connection connection = dataSource.getConnection();
Expand All @@ -95,7 +98,9 @@ public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {
}
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public void write(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException {
try (Connection connection = dataSource.getConnection();
Expand All @@ -104,13 +109,39 @@ public void write(TileCoord tileCoord, ByteBuffer blob) throws TileStoreExceptio
statement.setInt(2, tileCoord.x());
statement.setInt(3, reverseY(tileCoord.y(), tileCoord.z()));
statement.setBytes(4, blob.array());
statement.executeUpdate();
statement.execute();
} catch (SQLException e) {
throw new TileStoreException(e);
}
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public void write(List<TileEntry> tileEntries) throws TileStoreException {
try (Connection connection = dataSource.getConnection()) {
// connection.setAutoCommit(false);
try (PreparedStatement statement = connection.prepareStatement(INSERT_TILE)) {
for (TileEntry tileEntry : tileEntries) {
TileCoord tileCoord = tileEntry.getTileCoord();
ByteBuffer byteBuffer = tileEntry.getByteBuffer();
statement.setInt(1, tileCoord.z());
statement.setInt(2, tileCoord.x());
statement.setInt(3, reverseY(tileCoord.y(), tileCoord.z()));
statement.setBytes(4, byteBuffer.array());
statement.execute();
}
}
// connection.commit();
} catch (SQLException e) {
throw new TileStoreException(e);
}
}

/**
* {@inheritDoc}
*/
@Override
public void delete(TileCoord tileCoord) throws TileStoreException {
try (Connection connection = dataSource.getConnection();
Expand Down

This file was deleted.

Loading

0 comments on commit 647a273

Please sign in to comment.