Skip to content

Commit

Permalink
Fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Jun 19, 2023
1 parent 450804d commit 710c39f
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 33 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 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
50 changes: 36 additions & 14 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,23 @@
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,24 +69,42 @@ 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() {
@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);
}
});
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("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);
}
});

var httpService = new HttpJerseyRouterBuilder().buildBlockingStreaming(application);
var serverContext = HttpServers.forPort(port).listenBlockingStreamingAndAwait(httpService);
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 @@ -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 @@ -13,7 +13,6 @@
package org.apache.baremaps.tilestore.mbtiles;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -56,14 +55,14 @@ public MBTilesStore createTileStore() throws Exception {
void readWriteMetadata() throws Exception {
MBTilesStore tileStore = createTileStore();
Map<String, String> metadata = tileStore.readMetadata();
assertTrue(metadata.size() == 0);
assertEquals(0, metadata.size());

Map<String, String> m1 = new HashMap<>();
m1.put("test", "test");
tileStore.writeMetadata(m1);

Map<String, String> m2 = tileStore.readMetadata();
assertTrue(m2.size() == 1);
assertEquals(m2.get("test"), "test");
assertEquals(1, m2.size());
assertEquals("test", m2.get("test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Response getTile(@PathParam("z") int z, @PathParam("x") int x, @PathParam
TileCoord tileCoord = new TileCoord(x, y, z);
try {
TileStore tileStore = tileStoreSupplier.get();
ByteBuffer blob = tileStore.read(tileCoord);
ByteBuffer blob = tileStore.get(tileCoord);
if (blob != null) {
byte[] bytes = new byte[blob.remaining()];
blob.get(bytes);
Expand Down

0 comments on commit 710c39f

Please sign in to comment.