Search in sources :

Example 1 with SingleClassObjectSerializationFactory

use of org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory in project osmosis by openstreetmap.

the class FileBasedSortTest method test.

/**
 * Stores a large number of items into the file-based sorter and verifies
 * that they are returned in the correct sequence. It exceeds the in-memory
 * sorting limit, but doesn't trigger additional persistence at sub-levels
 * in the merge sort to minimise file handles as this would take too much
 * time for a unit test. The item count must be greater than
 * (MAX_MEMORY_SORT_COUNT * (MAX_MERGE_SOURCE_COUNT ^
 * MAX_MEMORY_SORT_DEPTH)) to trigger intermediate persistence.
 */
@Test
public void test() {
    final long itemCount = 10000;
    // Create a new file-based sorter for the TestStoreable type.
    SingleClassObjectSerializationFactory objectFactory = new SingleClassObjectSerializationFactory(SampleStoreable.class);
    Comparator<SampleStoreable> comparator = new Comparator<SampleStoreable>() {

        @Override
        public int compare(SampleStoreable o1, SampleStoreable o2) {
            long value1 = o1.getValue();
            long value2 = o2.getValue();
            if (value1 > value2) {
                return 1;
            } else if (value1 < value2) {
                return -1;
            } else {
                return 0;
            }
        }
    };
    try (FileBasedSort<SampleStoreable> fileBasedSort = new FileBasedSort<SampleStoreable>(objectFactory, comparator, true)) {
        // Add randomly generated test values into the sorter.
        Random random = new Random();
        for (long i = 0; i < itemCount; i++) {
            fileBasedSort.add(new SampleStoreable(random.nextInt()));
        }
        // sorted correctly.
        try (ReleasableIterator<SampleStoreable> resultIterator = fileBasedSort.iterate()) {
            int lastValue = Integer.MIN_VALUE;
            while (resultIterator.hasNext()) {
                int currentValue = resultIterator.next().getValue();
                Assert.assertTrue(currentValue >= lastValue);
                lastValue = currentValue;
            }
        }
    }
}
Also used : SingleClassObjectSerializationFactory(org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory) Random(java.util.Random) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 2 with SingleClassObjectSerializationFactory

use of org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory in project osmosis by openstreetmap.

the class RelationDao method getRelationMemberHistory.

private ReleasableIterator<DbFeatureHistory<DbOrderedFeature<RelationMember>>> getRelationMemberHistory(String selectedEntityStatement, SqlParameterSource parameterSource) {
    FileBasedSort<DbFeatureHistory<DbOrderedFeature<RelationMember>>> sortingStore = new FileBasedSort<DbFeatureHistory<DbOrderedFeature<RelationMember>>>(new SingleClassObjectSerializationFactory(DbFeatureHistory.class), new DbOrderedFeatureHistoryComparator<RelationMember>(), true);
    try {
        String sql;
        SortingStoreRowMapperListener<DbFeatureHistory<DbOrderedFeature<RelationMember>>> storeListener;
        DbFeatureHistoryRowMapper<DbOrderedFeature<RelationMember>> dbFeatureHistoryRowMapper;
        DbFeatureRowMapper<RelationMember> dbFeatureRowMapper;
        DbOrderedFeatureRowMapper<RelationMember> dbOrderedFeatureRowMapper;
        RelationMemberRowMapper relationNodeRowMapper;
        ReleasableIterator<DbFeatureHistory<DbOrderedFeature<RelationMember>>> resultIterator;
        sql = "SELECT rm.relation_id AS id, rm.member_id, rm.member_role, rm.member_type, rm.version, rm.sequence_id" + " FROM " + "relation_members rm" + " INNER JOIN " + selectedEntityStatement + " t ON rm.relation_id = t.relation_id AND rm.version = t.version";
        LOG.log(Level.FINER, "Relation member history query: " + sql);
        // Sends all received data into the object store.
        storeListener = new SortingStoreRowMapperListener<DbFeatureHistory<DbOrderedFeature<RelationMember>>>(sortingStore);
        // Retrieves the version information associated with the feature.
        dbFeatureHistoryRowMapper = new DbFeatureHistoryRowMapper<DbOrderedFeature<RelationMember>>(storeListener);
        // Retrieves the sequence number associated with the feature.
        dbOrderedFeatureRowMapper = new DbOrderedFeatureRowMapper<RelationMember>(dbFeatureHistoryRowMapper);
        // Retrieves the entity information associated with the feature.
        dbFeatureRowMapper = new DbFeatureRowMapper<RelationMember>(dbOrderedFeatureRowMapper);
        // Retrieves the basic feature information.
        relationNodeRowMapper = new RelationMemberRowMapper(dbFeatureRowMapper);
        // Perform the query passing the row mapper chain to process rows in a streamy fashion.
        getNamedParamJdbcTemplate().query(sql, parameterSource, relationNodeRowMapper);
        // Open a iterator on the store that will release the store upon completion.
        resultIterator = new StoreReleasingIterator<DbFeatureHistory<DbOrderedFeature<RelationMember>>>(sortingStore.iterate(), sortingStore);
        // The store itself shouldn't be released now that it has been attached to the iterator.
        sortingStore = null;
        return resultIterator;
    } finally {
        if (sortingStore != null) {
            sortingStore.close();
        }
    }
}
Also used : SingleClassObjectSerializationFactory(org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory) DbFeatureHistory(org.openstreetmap.osmosis.core.database.DbFeatureHistory) DbOrderedFeature(org.openstreetmap.osmosis.core.database.DbOrderedFeature) RelationMember(org.openstreetmap.osmosis.core.domain.v0_6.RelationMember) FileBasedSort(org.openstreetmap.osmosis.core.sort.common.FileBasedSort)

Example 3 with SingleClassObjectSerializationFactory

use of org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory in project osmosis by openstreetmap.

the class WayDao method getWayNodeHistory.

private ReleasableIterator<DbFeatureHistory<DbOrderedFeature<WayNode>>> getWayNodeHistory(String selectedEntityStatement, SqlParameterSource parameterSource) {
    FileBasedSort<DbFeatureHistory<DbOrderedFeature<WayNode>>> sortingStore = new FileBasedSort<DbFeatureHistory<DbOrderedFeature<WayNode>>>(new SingleClassObjectSerializationFactory(DbFeatureHistory.class), new DbOrderedFeatureHistoryComparator<WayNode>(), true);
    try {
        String sql;
        SortingStoreRowMapperListener<DbFeatureHistory<DbOrderedFeature<WayNode>>> storeListener;
        DbFeatureHistoryRowMapper<DbOrderedFeature<WayNode>> dbFeatureHistoryRowMapper;
        DbFeatureRowMapper<WayNode> dbFeatureRowMapper;
        DbOrderedFeatureRowMapper<WayNode> dbOrderedFeatureRowMapper;
        WayNodeRowMapper wayNodeRowMapper;
        ReleasableIterator<DbFeatureHistory<DbOrderedFeature<WayNode>>> resultIterator;
        sql = "SELECT wn.way_id AS id, wn.node_id, wn.version, wn.sequence_id" + " FROM " + "way_nodes wn" + " INNER JOIN " + selectedEntityStatement + " t ON wn.way_id = t.way_id AND wn.version = t.version";
        LOG.log(Level.FINER, "Way node history query: " + sql);
        // Sends all received data into the object store.
        storeListener = new SortingStoreRowMapperListener<DbFeatureHistory<DbOrderedFeature<WayNode>>>(sortingStore);
        // Retrieves the version information associated with the feature.
        dbFeatureHistoryRowMapper = new DbFeatureHistoryRowMapper<DbOrderedFeature<WayNode>>(storeListener);
        // Retrieves the sequence number associated with the feature.
        dbOrderedFeatureRowMapper = new DbOrderedFeatureRowMapper<WayNode>(dbFeatureHistoryRowMapper);
        // Retrieves the entity information associated with the feature.
        dbFeatureRowMapper = new DbFeatureRowMapper<WayNode>(dbOrderedFeatureRowMapper);
        // Retrieves the basic feature information.
        wayNodeRowMapper = new WayNodeRowMapper(dbFeatureRowMapper);
        // Perform the query passing the row mapper chain to process rows in a streamy fashion.
        getNamedParamJdbcTemplate().query(sql, parameterSource, wayNodeRowMapper);
        // Open a iterator on the store that will release the store upon completion.
        resultIterator = new StoreReleasingIterator<DbFeatureHistory<DbOrderedFeature<WayNode>>>(sortingStore.iterate(), sortingStore);
        // The store itself shouldn't be released now that it has been attached to the iterator.
        sortingStore = null;
        return resultIterator;
    } finally {
        if (sortingStore != null) {
            sortingStore.close();
        }
    }
}
Also used : SingleClassObjectSerializationFactory(org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory) DbFeatureHistory(org.openstreetmap.osmosis.core.database.DbFeatureHistory) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) DbOrderedFeature(org.openstreetmap.osmosis.core.database.DbOrderedFeature) FileBasedSort(org.openstreetmap.osmosis.core.sort.common.FileBasedSort)

Example 4 with SingleClassObjectSerializationFactory

use of org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory in project osmosis by openstreetmap.

the class EntityDao method getFeaturelessEntityHistory.

private ReleasableIterator<EntityHistory<T>> getFeaturelessEntityHistory(String selectedEntityStatement, MapSqlParameterSource parameterSource) {
    FileBasedSort<EntityHistory<T>> sortingStore = new FileBasedSort<EntityHistory<T>>(new SingleClassObjectSerializationFactory(EntityHistory.class), new EntityHistoryComparator<T>(), true);
    try {
        String sql;
        SortingStoreRowMapperListener<EntityHistory<T>> storeListener;
        EntityHistoryRowMapper<T> entityHistoryRowMapper;
        RowMapperListener<CommonEntityData> entityRowMapper;
        EntityDataRowMapper entityDataRowMapper;
        ReleasableIterator<EntityHistory<T>> resultIterator;
        sql = buildFeaturelessEntityHistoryQuery(selectedEntityStatement);
        // Sends all received data into the object store.
        storeListener = new SortingStoreRowMapperListener<EntityHistory<T>>(sortingStore);
        // Retrieves the visible attribute allowing modifies to be distinguished
        // from deletes.
        entityHistoryRowMapper = new EntityHistoryRowMapper<T>(storeListener);
        // Retrieves the entity type specific columns and builds the entity objects.
        entityRowMapper = getEntityRowMapper(entityHistoryRowMapper);
        // Retrieves the common entity information.
        entityDataRowMapper = new EntityDataRowMapper(entityRowMapper, false);
        // Perform the query passing the row mapper chain to process rows in a streamy fashion.
        namedParamJdbcTemplate.query(sql, parameterSource, entityDataRowMapper);
        // Open a iterator on the store that will release the store upon completion.
        resultIterator = new StoreReleasingIterator<EntityHistory<T>>(sortingStore.iterate(), sortingStore);
        // The store itself shouldn't be released now that it has been attached to the iterator.
        sortingStore = null;
        return resultIterator;
    } finally {
        if (sortingStore != null) {
            sortingStore.close();
        }
    }
}
Also used : SingleClassObjectSerializationFactory(org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory) CommonEntityData(org.openstreetmap.osmosis.core.domain.v0_6.CommonEntityData) FileBasedSort(org.openstreetmap.osmosis.core.sort.common.FileBasedSort)

Example 5 with SingleClassObjectSerializationFactory

use of org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory in project osmosis by openstreetmap.

the class EntityDao method getFeaturelessEntity.

private ReleasableIterator<T> getFeaturelessEntity(String tablePrefix) {
    FileBasedSort<T> sortingStore;
    sortingStore = new FileBasedSort<T>(new SingleClassObjectSerializationFactory(entityMapper.getEntityClass()), new EntitySubClassComparator<T>(new EntityByTypeThenIdComparator()), true);
    try {
        String sql;
        SortingStoreRowMapperListener<T> storeListener;
        RowMapperRowCallbackListener<T> rowCallbackListener;
        ReleasableIterator<T> resultIterator;
        sql = entityMapper.getSqlSelect(tablePrefix, false, false);
        // Sends all received data into the object store.
        storeListener = new SortingStoreRowMapperListener<T>(sortingStore);
        // Converts result set rows into objects and passes them into the store.
        rowCallbackListener = new RowMapperRowCallbackListener<T>(entityMapper.getRowMapper(), storeListener);
        // Perform the query passing the row mapper chain to process rows in a streamy fashion.
        jdbcTemplate.query(sql, rowCallbackListener);
        // Open a iterator on the store that will release the store upon completion.
        resultIterator = new StoreReleasingIterator<T>(sortingStore.iterate(), sortingStore);
        // The store itself shouldn't be released now that it has been attached to the iterator.
        sortingStore = null;
        return resultIterator;
    } finally {
        if (sortingStore != null) {
            sortingStore.close();
        }
    }
}
Also used : SingleClassObjectSerializationFactory(org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory) EntityByTypeThenIdComparator(org.openstreetmap.osmosis.core.sort.v0_6.EntityByTypeThenIdComparator) EntitySubClassComparator(org.openstreetmap.osmosis.core.sort.v0_6.EntitySubClassComparator)

Aggregations

SingleClassObjectSerializationFactory (org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory)7 FileBasedSort (org.openstreetmap.osmosis.core.sort.common.FileBasedSort)5 DbFeatureHistory (org.openstreetmap.osmosis.core.database.DbFeatureHistory)3 DbOrderedFeature (org.openstreetmap.osmosis.core.database.DbOrderedFeature)3 RelationMember (org.openstreetmap.osmosis.core.domain.v0_6.RelationMember)2 Comparator (java.util.Comparator)1 Random (java.util.Random)1 Test (org.junit.Test)1 DbFeature (org.openstreetmap.osmosis.core.database.DbFeature)1 CommonEntityData (org.openstreetmap.osmosis.core.domain.v0_6.CommonEntityData)1 Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)1 WayNode (org.openstreetmap.osmosis.core.domain.v0_6.WayNode)1 EntityByTypeThenIdComparator (org.openstreetmap.osmosis.core.sort.v0_6.EntityByTypeThenIdComparator)1 EntitySubClassComparator (org.openstreetmap.osmosis.core.sort.v0_6.EntitySubClassComparator)1