EntityMerger implements MultiSinkRunnableSource { public void run() { try { EntityContainerComparator comparator; EntityContainer entityContainer0 = null; EntityContainer entityContainer1 = null; comparator = new EntityContainerComparator(new EntityByTypeThenIdComparator()); postbox0.outputInitialize(); postbox1.outputInitialize(); sink.initialize(Collections.<String, Object>emptyMap()); entityContainer0 = nextOrNull(postbox0); entityContainer1 = nextOrNull(postbox1); if (entityContainer0 != null && entityContainer1 != null) { Bound bound0 = null; Bound bound1 = null; 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); } 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); } } while ( (entityContainer0 != null || postbox0.hasNext()) && (entityContainer1 != null || postbox1.hasNext())) { long comparisonResult; if (entityContainer0 == null) { entityContainer0 = postbox0.getNext(); } if (entityContainer1 == null) { entityContainer1 = postbox1.getNext(); } comparisonResult = comparator.compare(entityContainer0, entityContainer1); if (comparisonResult < 0) { sink.process(entityContainer0); entityContainer0 = null; } else if (comparisonResult > 0) { sink.process(entityContainer1); entityContainer1 = null; } else { 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 { 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 { sink.process(entityContainer1); } } else { throw new OsmosisRuntimeException( "Conflict resolution method " + conflictResolutionMethod + " is not recognized."); } entityContainer0 = null; entityContainer1 = null; } } 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(); } } EntityMerger(ConflictResolutionMethod conflictResolutionMethod, int inputBufferCapacity, 
			BoundRemovedAction boundRemovedAction); Sink getSink(int instance); int getSinkCount(); void setSink(Sink sink); void run();  }
@Test public void firstEmpty() throws Exception { File sourceFile; File expectedOutputFile; File actualOutputFile; sourceFile = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); expectedOutputFile = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); actualOutputFile = dataUtils.newFile(); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile.getPath(), "--read-empty-0.6", "--merge", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); }
@Test public void secondEmpty() throws Exception { File sourceFile; File expectedOutputFile; File actualOutputFile; sourceFile = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); expectedOutputFile = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); actualOutputFile = dataUtils.newFile(); Osmosis.run( new String[] { "-q", "--read-empty-0.6", "--read-xml-0.6", sourceFile.getPath(), "--merge", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); }
@Test public void selfMerge() throws Exception { File sourceFile1; File sourceFile2; File expectedOutputFile; File actualOutputFile; sourceFile1 = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); sourceFile2 = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); expectedOutputFile = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); actualOutputFile = dataUtils.newFile(); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile2.getPath(), "--read-xml-0.6", sourceFile1.getPath(), "--merge", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); }
@Test public void timestampConflictResolution() throws Exception { File sourceFile1; File sourceFile2; File expectedOutputFile; File actualOutputFile; sourceFile1 = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); sourceFile2 = dataUtils.createDataFile("v0_6/merge/merge-in-2-timestamp.osm"); expectedOutputFile = dataUtils.createDataFile("v0_6/merge/merge-out-timestamp.osm"); actualOutputFile = dataUtils.newFile(); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile2.getPath(), "--read-xml-0.6", sourceFile1.getPath(), "--merge", "conflictResolutionMethod=timestamp", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile1.getPath(), "--read-xml-0.6", sourceFile2.getPath(), "--merge", "conflictResolutionMethod=timestamp", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); }
@Test public void versionConflictResolution() throws Exception { File sourceFile1; File sourceFile2; File expectedOutputFile; File actualOutputFile; sourceFile1 = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); sourceFile2 = dataUtils.createDataFile("v0_6/merge/merge-in-2-version.osm"); expectedOutputFile = dataUtils.createDataFile("v0_6/merge/merge-out-version.osm"); actualOutputFile = dataUtils.newFile(); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile2.getPath(), "--read-xml-0.6", sourceFile1.getPath(), "--merge", "conflictResolutionMethod=version", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile1.getPath(), "--read-xml-0.6", sourceFile2.getPath(), "--merge", "conflictResolutionMethod=version", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); }
@Test public void secondSourceConflictResolution() throws Exception { File sourceFile1; File sourceFile2; File expectedOutputFile; File actualOutputFile; sourceFile1 = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); sourceFile2 = dataUtils.createDataFile("v0_6/merge/merge-in-2-secondSource.osm"); expectedOutputFile = dataUtils.createDataFile("v0_6/merge/merge-out-secondSource.osm"); actualOutputFile = dataUtils.newFile(); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile2.getPath(), "--read-xml-0.6", sourceFile1.getPath(), "--merge", "conflictResolutionMethod=lastSource", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); expectedOutputFile = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile1.getPath(), "--read-xml-0.6", sourceFile2.getPath(), "--merge", "conflictResolutionMethod=lastSource", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); }
@Test public void disjunctDatasets() throws Exception { File sourceFile1; File sourceFile2; File expectedOutputFile; File actualOutputFile; sourceFile1 = dataUtils.createDataFile("v0_6/merge/merge-in-1.osm"); sourceFile2 = dataUtils.createDataFile("v0_6/merge/merge-in-2-disjunct.osm"); expectedOutputFile = dataUtils.createDataFile("v0_6/merge/merge-out-disjunct.osm"); actualOutputFile = dataUtils.newFile(); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile2.getPath(), "--read-xml-0.6", sourceFile1.getPath(), "--merge", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); Osmosis.run( new String[] { "-q", "--read-xml-0.6", sourceFile1.getPath(), "--read-xml-0.6", sourceFile2.getPath(), "--merge", "conflictResolutionMethod=version", "--write-xml-0.6", actualOutputFile.getPath() } ); dataUtils.compareFiles(expectedOutputFile, actualOutputFile); }