Search in sources :

Example 1 with Header

use of com.baremaps.osm.domain.Header in project baremaps by baremaps.

the class ImportUpdateMonacoTest method monaco.

@Test
@Tag("integration")
void monaco() throws Exception {
    LongDataMap<Coordinate> coordinates = new LongDataOpenHashMap<>(new DataStore<>(new CoordinateDataType(), new OnHeapMemory()));
    LongDataMap<List<Long>> references = new LongDataOpenHashMap<>(new DataStore<>(new LongListDataType(), new OnHeapMemory()));
    // Import data
    new ImportService(new URI("res://monaco/monaco-210801.osm.pbf"), blobStore, coordinates, references, headerRepository, nodeRepository, wayRepository, relationRepository, 3857).call();
    assertEquals(3047l, headerRepository.selectLatest().getReplicationSequenceNumber());
    // Fix the replicationUrl so that we can update the database with local files
    headerRepository.delete(3047l);
    headerRepository.put(new Header(3047l, LocalDateTime.of(2021, 8, 01, 20, 21, 41, 0), "res://monaco/monaco-updates", "", ""));
    coordinates = new PostgresCoordinateMap(dataSource);
    references = new PostgresReferenceMap(dataSource);
    // Generate the diff and update the database
    long replicationSequenceNumber = headerRepository.selectLatest().getReplicationSequenceNumber();
    while (replicationSequenceNumber < 3075) {
        new DiffService(blobStore, coordinates, references, headerRepository, nodeRepository, wayRepository, relationRepository, 3857, 14).call();
        new UpdateService(blobStore, coordinates, references, headerRepository, nodeRepository, wayRepository, relationRepository, 3857).call();
        long nextReplicationSequenceNumber = headerRepository.selectLatest().getReplicationSequenceNumber();
        assertEquals(replicationSequenceNumber + 1, nextReplicationSequenceNumber);
        replicationSequenceNumber = nextReplicationSequenceNumber;
    }
}
Also used : LongListDataType(com.baremaps.store.type.LongListDataType) DiffService(com.baremaps.osm.repository.DiffService) URI(java.net.URI) ImportService(com.baremaps.osm.repository.ImportService) Header(com.baremaps.osm.domain.Header) UpdateService(com.baremaps.osm.repository.UpdateService) Coordinate(org.locationtech.jts.geom.Coordinate) CoordinateDataType(com.baremaps.store.type.CoordinateDataType) List(java.util.List) LongDataOpenHashMap(com.baremaps.store.LongDataOpenHashMap) OnHeapMemory(com.baremaps.store.memory.OnHeapMemory) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 2 with Header

use of com.baremaps.osm.domain.Header in project baremaps by baremaps.

the class UpdateService method call.

@Override
public Void call() throws Exception {
    Header header = headerRepository.selectLatest();
    String replicationUrl = header.getReplicationUrl();
    Long sequenceNumber = header.getReplicationSequenceNumber() + 1;
    Consumer<Entity> createGeometry = new CreateGeometryConsumer(coordinates, references);
    Consumer<Entity> reprojectGeometry = new ReprojectEntityConsumer(4326, srid);
    Consumer<Change> prepareGeometries = new ChangeEntityConsumer(createGeometry.andThen(reprojectGeometry));
    Function<Change, Change> prepareChange = consumeThenReturn(prepareGeometries);
    Consumer<Change> saveChange = new SaveChangeConsumer(nodeRepository, wayRepository, relationRepository);
    URI changeUri = resolve(replicationUrl, sequenceNumber, "osc.gz");
    Blob changeBlob = blobStore.get(changeUri);
    ProgressLogger progressLogger = new ProgressLogger(changeBlob.getContentLength(), 5000);
    try (InputStream blobInputStream = changeBlob.getInputStream();
        InputStream progressInputStream = new InputStreamProgress(blobInputStream, progressLogger);
        InputStream gzipInputStream = new GZIPInputStream(progressInputStream)) {
        new OsmChangeParser().changes(gzipInputStream).map(prepareChange).forEach(saveChange);
    }
    URI stateUri = resolve(replicationUrl, sequenceNumber, "state.txt");
    Blob stateBlob = blobStore.get(stateUri);
    try (InputStream stateInputStream = stateBlob.getInputStream()) {
        State state = new OsmStateParser().state(stateInputStream);
        headerRepository.put(new Header(state.getSequenceNumber(), state.getTimestamp(), header.getReplicationUrl(), header.getSource(), header.getWritingProgram()));
    }
    return null;
}
Also used : Entity(com.baremaps.osm.domain.Entity) CreateGeometryConsumer(com.baremaps.osm.geometry.CreateGeometryConsumer) Blob(com.baremaps.blob.Blob) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) InputStreamProgress(com.baremaps.osm.progress.InputStreamProgress) ProgressLogger(com.baremaps.osm.progress.ProgressLogger) Change(com.baremaps.osm.domain.Change) URI(java.net.URI) GZIPInputStream(java.util.zip.GZIPInputStream) ChangeEntityConsumer(com.baremaps.osm.function.ChangeEntityConsumer) Header(com.baremaps.osm.domain.Header) State(com.baremaps.osm.domain.State) ReprojectEntityConsumer(com.baremaps.osm.geometry.ReprojectEntityConsumer) OsmChangeParser(com.baremaps.osm.change.OsmChangeParser) OsmStateParser(com.baremaps.osm.state.OsmStateParser)

Example 3 with Header

use of com.baremaps.osm.domain.Header in project baremaps by baremaps.

the class ImportUpdateDataTest method data.

@Test
@Tag("integration")
void data() throws Exception {
    LongDataMap<Coordinate> coordinates = new LongDataOpenHashMap<>(new DataStore<>(new CoordinateDataType(), new OnHeapMemory()));
    LongDataMap<List<Long>> references = new LongDataOpenHashMap<>(new DataStore<>(new LongListDataType(), new OnHeapMemory()));
    // Import data
    new ImportService(new URI("res://simple/data.osm.pbf"), blobStore, coordinates, references, headerRepository, nodeRepository, wayRepository, relationRepository, 3857).call();
    headerRepository.put(new Header(0l, LocalDateTime.of(2020, 1, 1, 0, 0, 0, 0), "res://simple", "", ""));
    // Check node importation
    assertNull(nodeRepository.get(0l));
    assertNotNull(nodeRepository.get(1l));
    assertNotNull(nodeRepository.get(2l));
    assertNotNull(nodeRepository.get(3l));
    assertNull(nodeRepository.get(4l));
    // Check way importation
    assertNull(wayRepository.get(0l));
    assertNotNull(wayRepository.get(1l));
    assertNull(wayRepository.get(2l));
    // Check relation importation
    assertNull(relationRepository.get(0l));
    assertNotNull(relationRepository.get(1l));
    assertNull(relationRepository.get(2l));
    // Check node properties
    Node node = nodeRepository.get(1l);
    Assertions.assertEquals(1, node.getLon());
    Assertions.assertEquals(1, node.getLat());
    // Check way properties
    Way way = wayRepository.get(1l);
    assertNotNull(way);
    // Update the database
    new UpdateService(blobStore, new PostgresCoordinateMap(dataSource), new PostgresReferenceMap(dataSource), headerRepository, nodeRepository, wayRepository, relationRepository, 3857).call();
    // Check deletions
    assertNull(nodeRepository.get(0l));
    assertNull(nodeRepository.get(1l));
    // Check insertions
    assertNotNull(nodeRepository.get(2l));
    assertNotNull(nodeRepository.get(3l));
    assertNotNull(nodeRepository.get(4l));
}
Also used : LongListDataType(com.baremaps.store.type.LongListDataType) Node(com.baremaps.osm.domain.Node) URI(java.net.URI) Way(com.baremaps.osm.domain.Way) ImportService(com.baremaps.osm.repository.ImportService) Header(com.baremaps.osm.domain.Header) UpdateService(com.baremaps.osm.repository.UpdateService) Coordinate(org.locationtech.jts.geom.Coordinate) CoordinateDataType(com.baremaps.store.type.CoordinateDataType) List(java.util.List) LongDataOpenHashMap(com.baremaps.store.LongDataOpenHashMap) OnHeapMemory(com.baremaps.store.memory.OnHeapMemory) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 4 with Header

use of com.baremaps.osm.domain.Header in project baremaps by baremaps.

the class PostgresHeaderRepository method selectAll.

/**
 * {@inheritDoc}
 */
@Override
public List<Header> selectAll() throws RepositoryException {
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(selectLatest)) {
        try (ResultSet result = statement.executeQuery()) {
            List<Header> values = new ArrayList<>();
            while (result.next()) {
                Header value = getValue(result);
                values.add(value);
            }
            return values;
        }
    } catch (SQLException e) {
        throw new RepositoryException(e);
    }
}
Also used : Header(com.baremaps.osm.domain.Header) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) RepositoryException(com.baremaps.osm.repository.RepositoryException)

Example 5 with Header

use of com.baremaps.osm.domain.Header in project baremaps by baremaps.

the class PostgresHeaderRepository method get.

/**
 * {@inheritDoc}
 */
@Override
public List<Header> get(List<Long> keys) throws RepositoryException {
    if (keys.isEmpty()) {
        return List.of();
    }
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(selectIn)) {
        statement.setArray(1, connection.createArrayOf("int8", keys.toArray()));
        try (ResultSet result = statement.executeQuery()) {
            Map<Long, Header> values = new HashMap<>();
            while (result.next()) {
                Header value = getValue(result);
                values.put(value.getReplicationSequenceNumber(), value);
            }
            return keys.stream().map(values::get).collect(Collectors.toList());
        }
    } catch (SQLException e) {
        throw new RepositoryException(e);
    }
}
Also used : Header(com.baremaps.osm.domain.Header) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RepositoryException(com.baremaps.osm.repository.RepositoryException)

Aggregations

Header (com.baremaps.osm.domain.Header)13 URI (java.net.URI)5 RepositoryException (com.baremaps.osm.repository.RepositoryException)4 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 PGConnection (org.postgresql.PGConnection)4 ImportService (com.baremaps.osm.repository.ImportService)3 UpdateService (com.baremaps.osm.repository.UpdateService)3 LongDataOpenHashMap (com.baremaps.store.LongDataOpenHashMap)3 OnHeapMemory (com.baremaps.store.memory.OnHeapMemory)3 CoordinateDataType (com.baremaps.store.type.CoordinateDataType)3 LongListDataType (com.baremaps.store.type.LongListDataType)3 PreparedStatement (java.sql.PreparedStatement)3 LocalDateTime (java.time.LocalDateTime)3 List (java.util.List)3 Tag (org.junit.jupiter.api.Tag)3 Test (org.junit.jupiter.api.Test)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 Blob (com.baremaps.blob.Blob)2 OsmChangeParser (com.baremaps.osm.change.OsmChangeParser)2