Search in sources :

Example 11 with GeoEntryExtractionException

use of org.codice.ddf.spatial.geocoding.GeoEntryExtractionException in project ddf by codice.

the class GeoNamesLuceneIndexer method updateIndex.

@Override
public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoEntryIndexingException, GeoEntryExtractionException, GeoNamesRemoteDownloadException {
    Directory directory;
    try {
        directory = FSDirectory.open(Paths.get(indexLocation));
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Couldn't open the directory for the index, " + indexLocation, e);
    }
    // Try-with-resources to ensure the IndexWriter always gets closed.
    try (final IndexWriter indexWriter = createIndexWriter(create, directory)) {
        final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
        final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
        final ExtractionCallback extractionCallback = new ExtractionCallback() {

            @Override
            public void extracted(final GeoEntry newEntry) throws GeoEntryIndexingException {
                try {
                    addDocument(indexWriter, newEntry, strategy);
                } catch (IOException e) {
                    throw new GeoEntryIndexingException("Error writing to the index.", e);
                }
            }

            @Override
            public void updateProgress(final int progress) {
                if (progressCallback != null) {
                    progressCallback.updateProgress(progress);
                }
            }
        };
        try {
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        } catch (GeoEntryExtractionException e) {
            // Need to roll back here before the IndexWriter is closed at the end of the try
            // block.
            indexWriter.rollback();
            throw e;
        }
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Error writing to the index.", e);
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) IndexWriter(org.apache.lucene.index.IndexWriter) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) IOException(java.io.IOException) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Aggregations

GeoEntryExtractionException (org.codice.ddf.spatial.geocoding.GeoEntryExtractionException)11 GeoEntryIndexingException (org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)7 ProgressCallback (org.codice.ddf.spatial.geocoding.ProgressCallback)6 GeoEntryExtractor (org.codice.ddf.spatial.geocoding.GeoEntryExtractor)4 Test (org.junit.Test)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 ZipInputStream (java.util.zip.ZipInputStream)3 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)3 ExtractionCallback (org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)3 GeoNamesRemoteDownloadException (org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 GeoEntryIndexer (org.codice.ddf.spatial.geocoding.GeoEntryIndexer)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ByteSource (com.google.common.io.ByteSource)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1