Search in sources :

Example 1 with IndexedFidReader

use of org.geotoolkit.data.shapefile.fix.IndexedFidReader in project geotoolkit by Geomatys.

the class AccessManager method getFIXReader.

public IndexedFidReader getFIXReader(final RecordNumberTracker tracker) throws IOException {
    final URI url = files.getURI(ShpFileType.FIX);
    final ReadableByteChannel rbc = toClosingChannel(files.getReadChannel(url), false);
    final IndexedFidReader reader = new IndexedFidReader(url, rbc, tracker);
    readEntries.add(new AccessEntry(ShpFileType.FIX, url, reader));
    return reader;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) IndexedFidReader(org.geotoolkit.data.shapefile.fix.IndexedFidReader) URI(java.net.URI)

Example 2 with IndexedFidReader

use of org.geotoolkit.data.shapefile.fix.IndexedFidReader in project geotoolkit by Geomatys.

the class IndexedFidWriterTest method testRemoveCounting.

@Test
public void testRemoveCounting() throws Exception {
    final AccessManager locker = shpFiles.createLocker();
    IndexedFidWriter.generate(backshp.toURI().toURL());
    initWriter();
    writer.next();
    writer.remove();
    writer.next();
    writer.remove();
    writer.next();
    writer.remove();
    while (writer.hasNext()) {
        writer.next();
        writer.write();
    }
    close();
    IndexedFidReader reader = locker.getFIXReader(null);
    try {
        assertEquals(3, reader.getRemoves());
    } finally {
        reader.close();
    }
    // remove some more features
    initWriter();
    writer.next();
    writer.next();
    writer.next();
    writer.remove();
    writer.next();
    writer.remove();
    writer.next();
    writer.next();
    writer.next();
    writer.remove();
    while (writer.hasNext()) {
        writer.next();
        writer.write();
    }
    close();
    reader = locker.getFIXReader(null);
    try {
        assertEquals(6, reader.getRemoves());
    } finally {
        reader.close();
    }
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) IndexedFidReader(org.geotoolkit.data.shapefile.fix.IndexedFidReader) Test(org.junit.Test)

Example 3 with IndexedFidReader

use of org.geotoolkit.data.shapefile.fix.IndexedFidReader in project geotoolkit by Geomatys.

the class FidIndexerTest method testGenerate.

/*
     * Test method for 'org.geotoolkit.index.fid.FidIndexer.generate(URL)'
     */
@Test
public void testGenerate() throws Exception {
    final ShpFiles shpFiles = new ShpFiles(backshp.toURI().toURL());
    IndexedFidWriter.generate(shpFiles);
    final AccessManager locker = shpFiles.createLocker();
    final IndexedShapefileFeatureStore ds = new IndexedShapefileFeatureStore(backshp.toURI(), false, false, IndexType.NONE, null);
    long features = ds.getCount(new Query(ds.getNames().iterator().next()));
    final IndexedFidReader reader = locker.getFIXReader(null);
    try {
        assertEquals(features, reader.getCount());
        int i = 1;
        while (reader.hasNext()) {
            assertEquals(shpFiles.getTypeName() + "." + i, reader.next());
            assertEquals(shpFiles.getTypeName() + "." + i, i - 1, reader.currentSHXIndex());
            i++;
        }
        assertEquals(features, i - 1);
    } finally {
        reader.close();
    }
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) Query(org.geotoolkit.storage.feature.query.Query) ShpFiles(org.geotoolkit.data.shapefile.lock.ShpFiles) IndexedFidReader(org.geotoolkit.data.shapefile.fix.IndexedFidReader) Test(org.junit.Test)

Example 4 with IndexedFidReader

use of org.geotoolkit.data.shapefile.fix.IndexedFidReader in project geotoolkit by Geomatys.

the class IndexedShapefileFeatureStore method queryFidIndex.

/**
 * Uses the Fid index to quickly lookup the shp offset and the record number
 * for the list of fids
 *
 * @param fids
 *                the fids of the features to find.  If the set is sorted by alphabet the performance is likely to be better.
 * @return a list of Data objects
 */
private CloseableCollection<ShpData> queryFidIndex(final Set<String> idsSet) throws IOException {
    if (!indexUseable(FIX)) {
        return null;
    }
    final AccessManager locker = shpFiles.createLocker();
    final IndexedFidReader reader = locker.getFIXReader(null);
    final CloseableCollection<ShpData> records = new CloseableArrayList(idsSet.size());
    try {
        final ShxReader shx = locker.getSHXReader(useMemoryMappedBuffer);
        try {
            for (String fid : idsSet) {
                long recno = reader.findFid(fid);
                if (recno == -1) {
                    if (getLogger().isLoggable(Level.FINEST)) {
                        getLogger().finest("fid " + fid + " not found in index, continuing with next queried fid...");
                    }
                    continue;
                }
                try {
                    ShpData data = new ShpData((int) (recno + 1), (long) shx.getOffsetInBytes((int) recno));
                    if (getLogger().isLoggable(Level.FINEST)) {
                        getLogger().finest("fid " + fid + " found for record #" + data.getValue(0) + " at index file offset " + data.getValue(1));
                    }
                    records.add(data);
                } catch (Exception e) {
                    throw new IOException(e);
                }
            }
        } finally {
            shx.close();
        }
    } finally {
        reader.close();
    }
    return records;
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) ShpData(org.geotoolkit.data.shapefile.indexed.IndexDataReader.ShpData) ShxReader(org.geotoolkit.data.shapefile.shx.ShxReader) IOException(java.io.IOException) IndexedFidReader(org.geotoolkit.data.shapefile.fix.IndexedFidReader) MismatchedFeatureException(org.opengis.feature.MismatchedFeatureException) TreeException(org.geotoolkit.index.TreeException) DataStoreException(org.apache.sis.storage.DataStoreException) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

IndexedFidReader (org.geotoolkit.data.shapefile.fix.IndexedFidReader)4 AccessManager (org.geotoolkit.data.shapefile.lock.AccessManager)3 Test (org.junit.Test)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 UnsupportedQueryException (org.apache.sis.storage.UnsupportedQueryException)1 ShpData (org.geotoolkit.data.shapefile.indexed.IndexDataReader.ShpData)1 ShpFiles (org.geotoolkit.data.shapefile.lock.ShpFiles)1 ShxReader (org.geotoolkit.data.shapefile.shx.ShxReader)1 TreeException (org.geotoolkit.index.TreeException)1 Query (org.geotoolkit.storage.feature.query.Query)1 MismatchedFeatureException (org.opengis.feature.MismatchedFeatureException)1