use of org.openstreetmap.osmosis.core.sort.v0_6.EntityContainerComparator in project osmosis by openstreetmap.
the class EntityMerger method run.
/**
* {@inheritDoc}
*/
public void run() {
try {
EntityContainerComparator comparator;
EntityContainer entityContainer0 = null;
EntityContainer entityContainer1 = null;
// Create a comparator for comparing two entities by type and identifier.
comparator = new EntityContainerComparator(new EntityByTypeThenIdComparator());
// We can't get meaningful data from the initialize data on the
// input streams, so pass empty meta data to the sink and discard
// the input meta data.
postbox0.outputInitialize();
postbox1.outputInitialize();
sink.initialize(Collections.<String, Object>emptyMap());
// BEGIN bound special handling
// If there is a bound, it's going to be the first object
// in a properly sorted stream
entityContainer0 = nextOrNull(postbox0);
entityContainer1 = nextOrNull(postbox1);
// on both streams - no data implies no bound
if (entityContainer0 != null && entityContainer1 != null) {
Bound bound0 = null;
Bound bound1 = null;
// If there are any bounds upstream, eat them up
if (entityContainer0.getEntity().getType() == EntityType.Bound) {
bound0 = (Bound) entityContainer0.getEntity();
entityContainer0 = nextOrNull(postbox0);
}
if (entityContainer1.getEntity().getType() == EntityType.Bound) {
bound1 = (Bound) entityContainer1.getEntity();
entityContainer1 = nextOrNull(postbox1);
}
// to be smaller than the actual data, which is bad)
if (bound0 != null && bound1 != null) {
sink.process(new BoundContainer(bound0.union(bound1)));
} else if ((bound0 != null && bound1 == null) || (bound0 == null && bound1 != null)) {
handleBoundRemoved(bound0 == null);
}
}
// We continue in the comparison loop while both sources still have data.
while ((entityContainer0 != null || postbox0.hasNext()) && (entityContainer1 != null || postbox1.hasNext())) {
long comparisonResult;
// Get the next input data where required.
if (entityContainer0 == null) {
entityContainer0 = postbox0.getNext();
}
if (entityContainer1 == null) {
entityContainer1 = postbox1.getNext();
}
// Compare the two entities.
comparisonResult = comparator.compare(entityContainer0, entityContainer1);
if (comparisonResult < 0) {
// Entity 0 doesn't exist on the other source and can be
// sent straight through.
sink.process(entityContainer0);
entityContainer0 = null;
} else if (comparisonResult > 0) {
// Entity 1 doesn't exist on the other source and can be
// sent straight through.
sink.process(entityContainer1);
entityContainer1 = null;
} else {
// The entity exists on both sources so we must resolve the conflict.
if (conflictResolutionMethod.equals(ConflictResolutionMethod.Timestamp)) {
int timestampComparisonResult;
timestampComparisonResult = entityContainer0.getEntity().getTimestamp().compareTo(entityContainer1.getEntity().getTimestamp());
if (timestampComparisonResult < 0) {
sink.process(entityContainer1);
} else if (timestampComparisonResult > 0) {
sink.process(entityContainer0);
} else {
// If both have identical timestamps, use the second source.
sink.process(entityContainer1);
}
} else if (conflictResolutionMethod.equals(ConflictResolutionMethod.LatestSource)) {
sink.process(entityContainer1);
} else if (conflictResolutionMethod.equals(ConflictResolutionMethod.Version)) {
int version0 = entityContainer0.getEntity().getVersion();
int version1 = entityContainer1.getEntity().getVersion();
if (version0 < version1) {
sink.process(entityContainer1);
} else if (version0 > version1) {
sink.process(entityContainer0);
} else {
// If both have identical versions, use the second source.
sink.process(entityContainer1);
}
} else {
throw new OsmosisRuntimeException("Conflict resolution method " + conflictResolutionMethod + " is not recognized.");
}
entityContainer0 = null;
entityContainer1 = null;
}
}
// Any remaining entities on either source can be sent straight through.
while (entityContainer0 != null || postbox0.hasNext()) {
if (entityContainer0 == null) {
entityContainer0 = postbox0.getNext();
}
sink.process(entityContainer0);
entityContainer0 = null;
}
while (entityContainer1 != null || postbox1.hasNext()) {
if (entityContainer1 == null) {
entityContainer1 = postbox1.getNext();
}
sink.process(entityContainer1);
entityContainer1 = null;
}
sink.complete();
postbox0.outputComplete();
postbox1.outputComplete();
} finally {
sink.close();
postbox0.outputRelease();
postbox1.outputRelease();
}
}
use of org.openstreetmap.osmosis.core.sort.v0_6.EntityContainerComparator in project osmosis by openstreetmap.
the class ChangeApplier method run.
/**
* Processes the input sources and sends the updated data stream to the
* sink.
*/
public void run() {
try {
EntityContainerComparator comparator;
EntityContainer base = null;
ChangeContainer change = null;
Map<String, Object> metaData;
// Create a comparator for comparing two entities by type and identifier.
comparator = new EntityContainerComparator(new EntityByTypeThenIdComparator());
// Initialise the pipeline with a combination of the metadata from
// both inputs. The change stream metadata will be applied second
// and will override any values with the same key.
metaData = new HashMap<String, Object>();
metaData.putAll(basePostbox.outputInitialize());
metaData.putAll(changePostbox.outputInitialize());
sink.initialize(metaData);
// We continue in the comparison loop while both sources still have data.
while ((base != null || basePostbox.hasNext()) && (change != null || changePostbox.hasNext())) {
int comparisonResult;
// Get the next input data where required.
if (base == null) {
base = basePostbox.getNext();
}
if (change == null) {
change = changePostbox.getNext();
}
// Compare the two sources.
comparisonResult = comparator.compare(base, change.getEntityContainer());
if (comparisonResult < 0) {
processBaseOnlyEntity(base);
base = null;
} else if (comparisonResult > 0) {
processChangeOnlyEntity(change);
change = null;
} else {
processBothSourceEntity(base, change);
base = null;
change = null;
}
}
// Any remaining "base" entities are unmodified.
while (base != null || basePostbox.hasNext()) {
if (base == null) {
base = basePostbox.getNext();
}
processBaseOnlyEntity(base);
base = null;
}
// Process any remaining "change" entities.
while (change != null || changePostbox.hasNext()) {
if (change == null) {
change = changePostbox.getNext();
}
processChangeOnlyEntity(change);
change = null;
}
sink.complete();
basePostbox.outputComplete();
changePostbox.outputComplete();
} finally {
sink.close();
basePostbox.outputRelease();
changePostbox.outputRelease();
}
}
use of org.openstreetmap.osmosis.core.sort.v0_6.EntityContainerComparator in project osmosis by openstreetmap.
the class ChangeDeriver method run.
/**
* Processes the input sources and sends the changes to the change sink.
*/
public void run() {
try {
EntityContainerComparator comparator;
EntityContainer fromEntityContainer = null;
EntityContainer toEntityContainer = null;
TimestampSetter timestampSetter;
// Create a comparator for comparing two entities by type and identifier.
comparator = new EntityContainerComparator(new EntityByTypeThenIdComparator());
// Create an object for setting the current timestamp on entities being deleted.
timestampSetter = new TimestampSetter();
// We can't get meaningful data from the initialize data on the
// input streams, so pass empty meta data to the sink and discard
// the input meta data.
fromPostbox.outputInitialize();
toPostbox.outputInitialize();
changeSink.initialize(Collections.<String, Object>emptyMap());
// We continue in the comparison loop while both sources still have data.
while ((fromEntityContainer != null || fromPostbox.hasNext()) && (toEntityContainer != null || toPostbox.hasNext())) {
int comparisonResult;
// Get the next input data where required.
if (fromEntityContainer == null) {
fromEntityContainer = fromPostbox.getNext();
}
if (toEntityContainer == null) {
toEntityContainer = toPostbox.getNext();
}
// Compare the two sources.
comparisonResult = comparator.compare(fromEntityContainer, toEntityContainer);
if (comparisonResult < 0) {
// The from entity doesn't exist on the to source therefore
// has been deleted. We don't know when the entity was
// deleted so set the delete time to the current time.
changeSink.process(new ChangeContainer(timestampSetter.updateTimestamp(fromEntityContainer), ChangeAction.Delete));
fromEntityContainer = null;
} else if (comparisonResult > 0) {
// The to entity doesn't exist on the from source therefore has
// been created.
changeSink.process(new ChangeContainer(toEntityContainer, ChangeAction.Create));
toEntityContainer = null;
} else {
// entity has been modified.
if (!fromEntityContainer.getEntity().equals(toEntityContainer.getEntity())) {
changeSink.process(new ChangeContainer(toEntityContainer, ChangeAction.Modify));
}
fromEntityContainer = null;
toEntityContainer = null;
}
}
// Any remaining "from" entities are deletes.
while (fromEntityContainer != null || fromPostbox.hasNext()) {
if (fromEntityContainer == null) {
fromEntityContainer = fromPostbox.getNext();
}
// The from entity doesn't exist on the to source therefore
// has been deleted. We don't know when the entity was
// deleted so set the delete time to the current time.
changeSink.process(new ChangeContainer(timestampSetter.updateTimestamp(fromEntityContainer), ChangeAction.Delete));
fromEntityContainer = null;
}
// Any remaining "to" entities are creates.
while (toEntityContainer != null || toPostbox.hasNext()) {
if (toEntityContainer == null) {
toEntityContainer = toPostbox.getNext();
}
changeSink.process(new ChangeContainer(toEntityContainer, ChangeAction.Create));
toEntityContainer = null;
}
changeSink.complete();
fromPostbox.outputComplete();
toPostbox.outputComplete();
} finally {
changeSink.close();
fromPostbox.outputRelease();
toPostbox.outputRelease();
}
}
use of org.openstreetmap.osmosis.core.sort.v0_6.EntityContainerComparator in project osmosis by openstreetmap.
the class ChangeMerger method run.
/**
* {@inheritDoc}
*/
public void run() {
try {
EntityContainerComparator comparator;
ChangeContainer changeContainer0 = null;
ChangeContainer changeContainer1 = null;
// Create a comparator for comparing two entities by type and identifier.
comparator = new EntityContainerComparator(new EntityByTypeThenIdThenVersionComparator());
// We can't get meaningful data from the initialize data on the
// input streams, so pass empty meta data to the sink and discard
// the input meta data.
postbox0.outputInitialize();
postbox1.outputInitialize();
changeSink.initialize(Collections.<String, Object>emptyMap());
// We continue in the comparison loop while both sources still have data.
while ((changeContainer0 != null || postbox0.hasNext()) && (changeContainer1 != null || postbox1.hasNext())) {
long comparisonResult;
// Get the next input data where required.
if (changeContainer0 == null) {
changeContainer0 = postbox0.getNext();
}
if (changeContainer1 == null) {
changeContainer1 = postbox1.getNext();
}
// Compare the two entities.
comparisonResult = comparator.compare(changeContainer0.getEntityContainer(), changeContainer1.getEntityContainer());
if (comparisonResult < 0) {
// Entity 0 doesn't exist on the other source and can be
// sent straight through.
changeSink.process(changeContainer0);
changeContainer0 = null;
} else if (comparisonResult > 0) {
// Entity 1 doesn't exist on the other source and can be
// sent straight through.
changeSink.process(changeContainer1);
changeContainer1 = null;
} else {
// The entity exists on both sources so we must resolve the conflict.
if (conflictResolutionMethod.equals(ConflictResolutionMethod.Timestamp)) {
int timestampComparisonResult;
timestampComparisonResult = changeContainer0.getEntityContainer().getEntity().getTimestamp().compareTo(changeContainer1.getEntityContainer().getEntity().getTimestamp());
if (timestampComparisonResult < 0) {
changeSink.process(changeContainer1);
} else if (timestampComparisonResult > 0) {
changeSink.process(changeContainer0);
} else {
// If both have identical timestamps, use the second source.
changeSink.process(changeContainer1);
}
} else if (conflictResolutionMethod.equals(ConflictResolutionMethod.LatestSource)) {
changeSink.process(changeContainer1);
} else if (conflictResolutionMethod.equals(ConflictResolutionMethod.Version)) {
int version0 = changeContainer0.getEntityContainer().getEntity().getVersion();
int version1 = changeContainer1.getEntityContainer().getEntity().getVersion();
if (version0 < version1) {
changeSink.process(changeContainer1);
} else if (version0 > version1) {
changeSink.process(changeContainer0);
} else {
// If both have identical versions, use the second source.
changeSink.process(changeContainer1);
}
} else {
throw new OsmosisRuntimeException("Conflict resolution method " + conflictResolutionMethod + " is not recognized.");
}
changeContainer0 = null;
changeContainer1 = null;
}
}
// Any remaining entities on either source can be sent straight through.
while (changeContainer0 != null || postbox0.hasNext()) {
if (changeContainer0 == null) {
changeContainer0 = postbox0.getNext();
}
changeSink.process(changeContainer0);
changeContainer0 = null;
}
while (changeContainer1 != null || postbox1.hasNext()) {
if (changeContainer1 == null) {
changeContainer1 = postbox1.getNext();
}
changeSink.process(changeContainer1);
changeContainer1 = null;
}
changeSink.complete();
postbox0.outputComplete();
postbox1.outputComplete();
} finally {
changeSink.close();
postbox0.outputRelease();
postbox1.outputRelease();
}
}
use of org.openstreetmap.osmosis.core.sort.v0_6.EntityContainerComparator in project osmosis by openstreetmap.
the class CorePluginLoader method loadTaskFactories.
/**
* {@inheritDoc}
*/
@Override
public Map<String, TaskManagerFactory> loadTaskFactories() {
Map<String, TaskManagerFactory> factoryMap;
EntitySorterFactory entitySorterFactory06;
ChangeSorterFactory changeSorterFactory06;
factoryMap = new HashMap<String, TaskManagerFactory>();
// Configure factories that require additional information.
entitySorterFactory06 = new EntitySorterFactory();
entitySorterFactory06.registerComparator("TypeThenId", new EntityContainerComparator(new EntityByTypeThenIdComparator()), true);
changeSorterFactory06 = new ChangeSorterFactory();
changeSorterFactory06.registerComparator("streamable", new ChangeForStreamableApplierComparator(), true);
changeSorterFactory06.registerComparator("seekable", new ChangeForSeekableApplierComparator(), false);
// Register factories.
factoryMap.put("sort", entitySorterFactory06);
factoryMap.put("s", entitySorterFactory06);
factoryMap.put("sort-change", changeSorterFactory06);
factoryMap.put("sc", changeSorterFactory06);
factoryMap.put("write-null", new NullWriterFactory());
factoryMap.put("wn", new NullWriterFactory());
factoryMap.put("write-null-change", new NullChangeWriterFactory());
factoryMap.put("wnc", new NullChangeWriterFactory());
factoryMap.put("buffer", new EntityBufferFactory());
factoryMap.put("b", new EntityBufferFactory());
factoryMap.put("buffer-change", new ChangeBufferFactory());
factoryMap.put("bc", new ChangeBufferFactory());
factoryMap.put("report-entity", new EntityReporterFactory());
factoryMap.put("re", new EntityReporterFactory());
factoryMap.put("report-integrity", new IntegrityReporterFactory());
factoryMap.put("ri", new IntegrityReporterFactory());
factoryMap.put("log-progress", new EntityProgressLoggerFactory());
factoryMap.put("lp", new EntityProgressLoggerFactory());
factoryMap.put("log-progress-change", new ChangeProgressLoggerFactory());
factoryMap.put("lpc", new ChangeProgressLoggerFactory());
factoryMap.put("tee", new EntityTeeFactory());
factoryMap.put("t", new EntityTeeFactory());
factoryMap.put("tee-change", new ChangeTeeFactory());
factoryMap.put("tc", new ChangeTeeFactory());
factoryMap.put("read-empty", new EmptyReaderFactory());
factoryMap.put("rem", new EmptyReaderFactory());
factoryMap.put("read-empty-change", new EmptyChangeReaderFactory());
factoryMap.put("remc", new EmptyChangeReaderFactory());
factoryMap.put("compute-bounding-box", new BoundComputerFactory());
factoryMap.put("cbb", new BoundComputerFactory());
factoryMap.put("set-bounding-box", new BoundSetterFactory());
factoryMap.put("sbb", new BoundSetterFactory());
factoryMap.put("sort-0.6", entitySorterFactory06);
factoryMap.put("sort-change-0.6", changeSorterFactory06);
factoryMap.put("write-null-0.6", new NullWriterFactory());
factoryMap.put("write-null-change-0.6", new NullChangeWriterFactory());
factoryMap.put("buffer-0.6", new EntityBufferFactory());
factoryMap.put("buffer-change-0.6", new ChangeBufferFactory());
factoryMap.put("report-entity-0.6", new EntityReporterFactory());
factoryMap.put("report-integrity-0.6", new IntegrityReporterFactory());
factoryMap.put("log-progress-0.6", new EntityProgressLoggerFactory());
factoryMap.put("log-progress-change-0.6", new ChangeProgressLoggerFactory());
factoryMap.put("tee-0.6", new EntityTeeFactory());
factoryMap.put("tee-change-0.6", new ChangeTeeFactory());
factoryMap.put("read-empty-0.6", new EmptyReaderFactory());
factoryMap.put("read-empty-change-0.6", new EmptyChangeReaderFactory());
factoryMap.put("tag-sort-0.6", new TagSorterFactory());
factoryMap.put("tag-sort-change-0.6", new ChangeTagSorterFactory());
factoryMap.put("compute-bounding-box-0.6", new BoundComputerFactory());
factoryMap.put("set-bounding-box-0.6", new BoundSetterFactory());
return factoryMap;
}
Aggregations