Skip to content

Commit

Permalink
Change the TileStore interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Jun 23, 2023
1 parent 1cb66e4 commit bc43970
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .run/basemap-mbtiles.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<configuration default="false" name="basemap-mbtiles" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="org.apache.baremaps.cli.Baremaps" />
<module name="baremaps-cli" />
<option name="PROGRAM_PARAMETERS" value="map mbtiles --mbtiles $USER_HOME$/Datasets/Baremaps/tiles.mbtiles --tileset tileset.js --style style.js" />
<option name="PROGRAM_PARAMETERS" value="map mbtiles --mbtiles $USER_HOME$/Datasets/Baremaps/tiles.mbtiles --tilejson tileset.js --style style.js" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/basemap" />
<extension name="coverage">
<pattern>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@Fork(value = 1, warmups = 1)
public class MBTilesBenchmark {

public SecureRandom random = new SecureRandom();
public static SecureRandom random = new SecureRandom();

@Param({"10", "100", "1000"})
public int iterations;
Expand All @@ -47,7 +47,7 @@ public class MBTilesBenchmark {
@Setup
public void setup() throws IOException, TileStoreException {
file = Files.createTempFile(Paths.get("."), "baremaps", ".mbtiles");
mbTilesStore = new MBTilesStore(SqliteUtils.createDataSource(file, false));
mbTilesStore = new MBTilesStore(SqliteUtils.createDataSource(file));
mbTilesStore.initializeDatabase();
}

Expand All @@ -62,7 +62,7 @@ public void writeMBTiles(MBTilesBenchmark benchmark) throws TileStoreException {
for (int i = 0; i < benchmark.iterations; i++) {
var bytes = new byte[1 << 16];
random.nextBytes(bytes);
mbTilesStore.put(new TileCoord(0, 0, i), ByteBuffer.wrap(bytes));
mbTilesStore.write(new TileCoord(0, 0, i), ByteBuffer.wrap(bytes));
}
}

Expand All @@ -78,12 +78,12 @@ public void writeMBTilesBatch(MBTilesBenchmark benchmark) throws TileStoreExcept
buffers.add(ByteBuffer.wrap(bytes));
if (coords.size() == 100) {
random.nextBytes(bytes);
mbTilesStore.put(coords, buffers);
mbTilesStore.write(coords, buffers);
coords.clear();
buffers.clear();
}
}
mbTilesStore.put(coords, buffers);
mbTilesStore.write(coords, buffers);
coords.clear();
buffers.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import org.apache.baremaps.cli.Options;
import org.apache.baremaps.config.ConfigReader;
import org.apache.baremaps.server.*;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.tilestore.TileStore;
import org.apache.baremaps.tilestore.postgres.PostgresTileStore;
import org.apache.baremaps.utils.PostgresUtils;
import org.apache.baremaps.vectortile.style.Style;
import org.apache.baremaps.vectortile.tilejson.TileJSON;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.utils.PostgresUtils;
import org.apache.baremaps.vectortile.tileset.Tileset;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
Expand Down
41 changes: 31 additions & 10 deletions baremaps-cli/src/main/java/org/apache/baremaps/cli/map/MBTiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
import static io.servicetalk.data.jackson.jersey.ServiceTalkJacksonSerializerFeature.newContextResolver;
import static org.apache.baremaps.utils.ObjectMapperUtils.objectMapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import io.servicetalk.http.netty.HttpServers;
import io.servicetalk.http.router.jersey.HttpJerseyRouterBuilder;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import org.apache.baremaps.cli.Options;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.server.ServerResources;
import org.apache.baremaps.config.ConfigReader;
import org.apache.baremaps.server.*;
import org.apache.baremaps.tilestore.TileCache;
import org.apache.baremaps.tilestore.TileStore;
import org.apache.baremaps.tilestore.mbtiles.MBTilesStore;
import org.apache.baremaps.utils.SqliteUtils;
import org.apache.baremaps.vectortile.style.Style;
import org.apache.baremaps.vectortile.tilejson.TileJSON;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
Expand Down Expand Up @@ -65,22 +68,40 @@ public class MBTiles implements Callable<Integer> {
@Override
public Integer call() throws Exception {
var objectMapper = objectMapper();
var configReader = new ConfigReader();
var caffeineSpec = CaffeineSpec.parse(cache);
var datasource = SqliteUtils.createDataSource(mbtilesPath, true);

var datasource = SqliteUtils.createDataSource(mbtilesPath);
var tileStoreSupplierType = new TypeLiteral<Supplier<TileStore>>() {};
var tileStore = new MBTilesStore(datasource);
var tileCache = new TileCache(tileStore, caffeineSpec);
var tileStoreSupplier = (Supplier<TileStore>) () -> tileCache;

var styleSupplierType = new TypeLiteral<Supplier<Style>>() {};
var style = objectMapper.readValue(configReader.read(stylePath), Style.class);
var styleSupplier = (Supplier<Style>) () -> style;

var tileJSONSupplierType = new TypeLiteral<Supplier<TileJSON>>() {};
var tileJSON = objectMapper.readValue(configReader.read(tileJSONPath), TileJSON.class);
var tileJSONSupplier = (Supplier<TileJSON>) () -> tileJSON;

// Configure the application
var application =
new ResourceConfig().register(CorsFilter.class).register(ServerResources.class)
.register(newContextResolver(objectMapper)).register(new AbstractBinder() {
new ResourceConfig()
.register(CorsFilter.class)
.register(TileResource.class)
.register(StyleResource.class)
.register(TilesetResource.class)
.register(ClassPathResource.class)
.register(newContextResolver(objectMapper))
.register(new AbstractBinder() {
@Override
protected void configure() {
bind(tileJSONPath).to(Path.class).named("tileset");
bind(stylePath).to(Path.class).named("style");
bind(tileCache).to(TileStore.class);
bind(objectMapper).to(ObjectMapper.class);
bind("assets").to(String.class).named("directory");
bind("server.html").to(String.class).named("index");
bind(tileStoreSupplier).to(tileStoreSupplierType);
bind(styleSupplier).to(styleSupplierType);
bind(tileJSONSupplier).to(tileJSONSupplierType);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
import java.util.function.Supplier;
import org.apache.baremaps.cli.Options;
import org.apache.baremaps.config.ConfigReader;

import org.apache.baremaps.server.*;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.tilestore.TileCache;
import org.apache.baremaps.tilestore.TileStore;
import org.apache.baremaps.tilestore.postgres.PostgresTileStore;
import org.apache.baremaps.utils.PostgresUtils;
import org.apache.baremaps.vectortile.style.Style;
import org.apache.baremaps.vectortile.tilejson.TileJSON;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.utils.PostgresUtils;
import org.apache.baremaps.vectortile.tileset.Tileset;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
Expand Down Expand Up @@ -71,10 +70,10 @@ public class Serve implements Callable<Integer> {
public Integer call() throws Exception {
var objectMapper = objectMapper();
var configReader = new ConfigReader();
var tileset = objectMapper.readValue(configReader.read(this.tilesetPath), Tileset.class);
var caffeineSpec = CaffeineSpec.parse(cache);
var datasource = PostgresUtils.createDataSource(tileset.getDatabase());

var tileset = objectMapper.readValue(configReader.read(tilesetPath), Tileset.class);
var datasource = PostgresUtils.createDataSource(tileset.getDatabase());
var tileStoreSupplierType = new TypeLiteral<Supplier<TileStore>>() {};
var tileStore = new PostgresTileStore(datasource, tileset);
var tileCache = new TileCache(tileStore, caffeineSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,22 @@ public TileCache(TileStore tileStore, CaffeineSpec spec) {

/** {@inheritDoc} */
@Override
<<<<<<< HEAD
public ByteBuffer get(TileCoord tileCoord) throws TileStoreException {
var buffer = cache.get(tileCoord, t -> {
try {
return tileStore.get(t);
=======
public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {
return cache.get(tileCoord, t -> {
try {
var buffer = tileStore.read(t);
if (buffer == null) {
return null;
} else {
return buffer;
}
>>>>>>> 1d7ffeec (Change the TileStore interface)
} catch (TileStoreException e) {
logger.error("Unable to read the tile.", e);
return null;
Expand All @@ -68,8 +80,8 @@ public ByteBuffer get(TileCoord tileCoord) throws TileStoreException {

/** {@inheritDoc} */
@Override
public void put(TileCoord tileCoord, ByteBuffer bytes) throws TileStoreException {
tileStore.put(tileCoord, bytes);
public void write(TileCoord tileCoord, ByteBuffer bytes) throws TileStoreException {
tileStore.write(tileCoord, bytes);
cache.invalidate(tileCoord);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public TileChannel(TileStore source, TileStore target, boolean deleteEmptyTiles)
@Override
public void accept(TileCoord tileCoord) {
try {
ByteBuffer blob = source.get(tileCoord);
ByteBuffer blob = source.read(tileCoord);
if (blob != null) {
target.put(tileCoord, blob);
target.write(tileCoord, blob);
} else if (deleteEmptyTiles) {
target.delete(tileCoord);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,48 @@
public interface TileStore {

/**
* Gets the content of a tile.
* Reads the content of a tile.
*
* @param tileCoord the tile coordinate
* @return the content of the tile
* @throws TileStoreException
*/
ByteBuffer get(TileCoord tileCoord) throws TileStoreException;
ByteBuffer read(TileCoord tileCoord) throws TileStoreException;

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

/**
* Puts the content of a tile.
* Writes the content of a tile.
*
* @param tileCoord the tile coordinate
* @param blob the content of the tile
* @throws TileStoreException
*/
void put(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException;
void write(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException;

/**
* Puts the content of several tiles.
* Writes the content of several tiles.
*
* @param tileCoords the tile coordinates
* @param blobs the content of the tiles
* @throws TileStoreException
*/
default void put(List<TileCoord> tileCoords, List<ByteBuffer> blobs) throws TileStoreException {
default void write(List<TileCoord> tileCoords, List<ByteBuffer> blobs) throws TileStoreException {
for (int i = 0; i < tileCoords.size(); i++) {
put(tileCoords.get(i), blobs.get(i));
write(tileCoords.get(i), blobs.get(i));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public FileTileStore(Path path) {

/** {@inheritDoc} */
@Override
public ByteBuffer get(TileCoord tileCoord) throws TileStoreException {
public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {
try {
return ByteBuffer.wrap(Files.readAllBytes(resolve(tileCoord)));
} catch (IOException e) {
Expand All @@ -48,7 +48,7 @@ public ByteBuffer get(TileCoord tileCoord) throws TileStoreException {

/** {@inheritDoc} */
@Override
public void put(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException {
public void write(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException {
try {
var file = resolve(tileCoord);
Files.createDirectories(file.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public MBTilesStore(DataSource dataSource) {

/** {@inheritDoc} */
@Override
public ByteBuffer get(TileCoord tileCoord) throws TileStoreException {
public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(SELECT_TILE)) {
statement.setInt(1, tileCoord.z());
Expand All @@ -93,7 +93,7 @@ public ByteBuffer get(TileCoord tileCoord) throws TileStoreException {

/** {@inheritDoc} */
@Override
public void put(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException {
public void write(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(INSERT_TILE)) {
statement.setInt(1, tileCoord.z());
Expand All @@ -108,7 +108,7 @@ public void put(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException

/** {@inheritDoc} */
@Override
public void put(List<TileCoord> tileCoords, List<ByteBuffer> blobs) throws TileStoreException {
public void write(List<TileCoord> tileCoords, List<ByteBuffer> blobs) throws TileStoreException {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(INSERT_TILE)) {
for (int i = 0; i < tileCoords.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public PostgresTileStore(DataSource datasource, Tileset tileset) {

/** {@inheritDoc} */
@Override
public ByteBuffer get(TileCoord tileCoord) throws TileStoreException {
public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {
try (Connection connection = datasource.getConnection();
Statement statement = connection.createStatement();
ByteArrayOutputStream data = new ByteArrayOutputStream()) {
Expand Down Expand Up @@ -240,7 +240,7 @@ protected String tileEnvelope(TileCoord tileCoord) {

/** This operation is not supported. */
@Override
public void put(TileCoord tileCoord, ByteBuffer blob) {
public void write(TileCoord tileCoord, ByteBuffer blob) {
throw new UnsupportedOperationException("The postgis tile store is read only");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ private SqliteUtils() {}
* Create a SQLite data source.
*
* @param path the path to the SQLite database
* @param readOnly
* @return the SQLite data source
*/
public static HikariDataSource createDataSource(Path path, boolean readOnly) {
public static HikariDataSource createDataSource(Path path) {
var sqliteConfig = new SQLiteConfig();
sqliteConfig.setCacheSize(1000000);
sqliteConfig.setPageSize(65536);
sqliteConfig.setJournalMode(JournalMode.OFF);
sqliteConfig.setLockingMode(LockingMode.EXCLUSIVE);
sqliteConfig.setSynchronous(SynchronousMode.OFF);
sqliteConfig.setTempStore(TempStore.MEMORY);
sqliteConfig.setReadOnly(readOnly);

var sqliteDataSource = new SQLiteDataSource();
sqliteDataSource.setConfig(sqliteConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private TileStore sourceTileStore(Tileset tileset, DataSource datasource) {
private TileStore targetTileStore(Tileset source) throws TileStoreException, IOException {
if (mbtiles) {
Files.deleteIfExists(repository);
var dataSource = SqliteUtils.createDataSource(repository, false);
var dataSource = SqliteUtils.createDataSource(repository);
var tilesStore = new MBTilesStore(dataSource);
tilesStore.initializeDatabase();
tilesStore.writeMetadata(metadata(source));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ void readWriteDeleteTile() throws Exception {
ByteBuffer blob = ByteBuffer.wrap("tile_content".getBytes());

// Write data
tileStore.put(tileCoord, blob);
tileStore.write(tileCoord, blob);

// Read the data
ByteBuffer inputStream = tileStore.get(tileCoord);
ByteBuffer inputStream = tileStore.read(tileCoord);
assertArrayEquals(blob.array(), inputStream.array());

// Delete the data
tileStore.delete(tileCoord);
assertThrows(TileStoreException.class, () -> tileStore.get(tileCoord));
assertThrows(TileStoreException.class, () -> tileStore.read(tileCoord));
}

public abstract TileStore createTileStore() throws Exception;
Expand Down
Loading

0 comments on commit bc43970

Please sign in to comment.