use of com.baremaps.osm.domain.Change in project baremaps by baremaps.
the class XmlChangeSpliteratorTest method tryAdvance.
@Test
void tryAdvance() throws IOException {
try (InputStream input = DATA_OSC_XML.openStream()) {
Spliterator<Change> spliterator = new OsmChangeSpliterator(input);
spliterator.forEachRemaining(fileBlock -> assertNotNull(fileBlock));
assertFalse(spliterator.tryAdvance(new HoldingConsumer<>()));
}
}
use of com.baremaps.osm.domain.Change in project baremaps by baremaps.
the class XmlChangeSpliteratorTest method forEachRemaining.
@Test
void forEachRemaining() throws IOException {
try (InputStream input = DATA_OSC_XML.openStream()) {
Spliterator<Change> spliterator = new OsmChangeSpliterator(input);
AccumulatingConsumer<Change> accumulator = new AccumulatingConsumer<>();
spliterator.forEachRemaining(accumulator);
assertEquals(accumulator.values().size(), 7);
assertEquals(accumulator.values().stream().flatMap(change -> change.getEntities().stream()).collect(Collectors.toList()).size(), 51);
}
}
use of com.baremaps.osm.domain.Change 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;
}
use of com.baremaps.osm.domain.Change in project baremaps by baremaps.
the class OsmChangeSpliterator method tryAdvance.
@Override
public boolean tryAdvance(Consumer<? super Change> consumer) {
try {
if (reader.hasNext()) {
int event = reader.next();
switch(event) {
case START_ELEMENT:
if (ELEMENT_NAME_OSMCHANGE.equals(reader.getLocalName())) {
return true;
}
Change entity = readChange();
consumer.accept(entity);
return true;
case END_DOCUMENT:
return false;
default:
return true;
}
} else {
return false;
}
} catch (XMLStreamException e) {
throw new StreamException(e);
}
}
use of com.baremaps.osm.domain.Change in project baremaps by baremaps.
the class OsmChangeSpliterator method readChange.
private Change readChange() throws XMLStreamException {
switch(reader.getLocalName()) {
case ELEMENT_NAME_CREATE:
case ELEMENT_NAME_DELETE:
case ELEMENT_NAME_MODIFY:
ChangeType type = ChangeType.valueOf(reader.getLocalName().toUpperCase());
List<Entity> elements = new ArrayList<>();
reader.nextTag();
while (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
elements.add(readElement());
reader.nextTag();
}
return new Change(type, elements);
default:
throw new StreamException("Unexpected XML element: " + reader.getLocalName());
}
}
Aggregations