Search in sources :

Example 1 with Transaction

use of org.geotools.data.Transaction in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testAddFeaturesOnASeparateBranch.

@Test
public void testAddFeaturesOnASeparateBranch() throws Exception {
    final String branchName = "addtest";
    final Ref branchRef = geogig.command(BranchCreateOp.class).setName(branchName).call();
    dataStore.setHead(branchName);
    FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
    collection = DataUtilities.collection(Arrays.asList((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3));
    Transaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    assertSame(tx, points.getTransaction());
    try {
        List<FeatureId> addedFeatures = points.addFeatures(collection);
        assertNotNull(addedFeatures);
        assertEquals(3, addedFeatures.size());
        // assert transaction isolation
        assertEquals(3, points.getFeatures().size());
        assertEquals(0, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
        tx.commit();
        assertEquals(3, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
}
Also used : FeatureId(org.opengis.filter.identity.FeatureId) Ref(org.locationtech.geogig.api.Ref) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) Test(org.junit.Test)

Example 2 with Transaction

use of org.geotools.data.Transaction in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testUseProvidedFIDSupported.

@Test
public void testUseProvidedFIDSupported() throws Exception {
    assertTrue(points.getQueryCapabilities().isUseProvidedFIDSupported());
    FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
    collection = DataUtilities.collection(Arrays.asList((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3));
    Transaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    try {
        List<FeatureId> newFids = points.addFeatures(collection);
        assertNotNull(newFids);
        assertEquals(3, newFids.size());
        FeatureId fid1 = newFids.get(0);
        FeatureId fid2 = newFids.get(1);
        FeatureId fid3 = newFids.get(2);
        // new ids should have been generated...
        assertFalse(idP1.equals(fid1.getID()));
        assertFalse(idP1.equals(fid1.getID()));
        assertFalse(idP1.equals(fid1.getID()));
        // now force the use of provided feature ids
        points1.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE);
        points2.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE);
        points3.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE);
        List<FeatureId> providedFids = points.addFeatures(collection);
        assertNotNull(providedFids);
        assertEquals(3, providedFids.size());
        FeatureId fid11 = providedFids.get(0);
        FeatureId fid21 = providedFids.get(1);
        FeatureId fid31 = providedFids.get(2);
        // ids should match provided
        assertEquals(idP1, fid11.getID());
        assertEquals(idP2, fid21.getID());
        assertEquals(idP3, fid31.getID());
        tx.commit();
        assertEquals(1, points.getFeatures(ff.id(Collections.singleton(fid1))).size());
        assertEquals(1, points.getFeatures(ff.id(Collections.singleton(fid2))).size());
        assertEquals(1, points.getFeatures(ff.id(Collections.singleton(fid3))).size());
        assertEquals(1, points.getFeatures(ff.id(Collections.singleton(fid11))).size());
        assertEquals(1, points.getFeatures(ff.id(Collections.singleton(fid21))).size());
        assertEquals(1, points.getFeatures(ff.id(Collections.singleton(fid31))).size());
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
}
Also used : FeatureId(org.opengis.filter.identity.FeatureId) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) Test(org.junit.Test)

Example 3 with Transaction

use of org.geotools.data.Transaction in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testModifyFeatures.

@Test
public void testModifyFeatures() throws Exception {
    // add features circumventing FeatureStore.addFeatures to keep the test
    // independent of the addFeatures functionality
    insertAndAdd(lines1, lines2, lines3, points1, points2, points3);
    geogig.command(CommitOp.class).call();
    Id filter = ff.id(Collections.singleton(ff.featureId(idP1)));
    Transaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    try {
        // initial value
        SimpleFeature initial = points.getFeatures(filter).features().next();
        assertEquals("StringProp1_1", initial.getAttribute("sp"));
        // modify
        points.modifyFeatures("sp", "modified", filter);
        // modified value before commit
        SimpleFeature modified = points.getFeatures(filter).features().next();
        assertEquals("modified", modified.getAttribute("sp"));
        // unmodified value before commit on another store instance (tx isolation)
        assertEquals("StringProp1_1", dataStore.getFeatureSource(pointsTypeName).getFeatures(filter).features().next().getAttribute("sp"));
        tx.commit();
        // modified value after commit on another store instance
        assertEquals("modified", dataStore.getFeatureSource(pointsTypeName).getFeatures(filter).features().next().getAttribute("sp"));
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
    points.setTransaction(Transaction.AUTO_COMMIT);
    SimpleFeature modified = points.getFeatures(filter).features().next();
    assertEquals("modified", modified.getAttribute("sp"));
}
Also used : Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) ResourceId(org.opengis.filter.identity.ResourceId) ObjectId(org.locationtech.geogig.api.ObjectId) Id(org.opengis.filter.Id) FeatureId(org.opengis.filter.identity.FeatureId) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 4 with Transaction

use of org.geotools.data.Transaction in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testAddFeatures.

@Test
public void testAddFeatures() throws Exception {
    FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
    collection = DataUtilities.collection(Arrays.asList((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3));
    try {
        points.addFeatures(collection);
        fail("Expected UnsupportedOperationException on AUTO_COMMIT");
    } catch (UnsupportedOperationException e) {
        assertTrue(e.getMessage().contains("AUTO_COMMIT"));
    }
    Transaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    assertSame(tx, points.getTransaction());
    try {
        List<FeatureId> addedFeatures = points.addFeatures(collection);
        assertNotNull(addedFeatures);
        assertEquals(3, addedFeatures.size());
        for (FeatureId id : addedFeatures) {
            assertFalse(id instanceof ResourceId);
            assertNotNull(id.getFeatureVersion());
        }
        // assert transaction isolation
        assertEquals(3, points.getFeatures().size());
        assertEquals(0, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
        tx.commit();
        assertEquals(3, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
}
Also used : FeatureId(org.opengis.filter.identity.FeatureId) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) ResourceId(org.opengis.filter.identity.ResourceId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) Test(org.junit.Test)

Example 5 with Transaction

use of org.geotools.data.Transaction in project GeoGig by boundlessgeo.

the class ExportDiffOp method _call.

/**
     * Executes the export operation using the parameters that have been specified.
     * 
     * @return a FeatureCollection with the specified features
     */
@Override
protected SimpleFeatureStore _call() {
    final SimpleFeatureStore targetStore = getTargetStore();
    final String refspec = old ? oldRef : newRef;
    final RevTree rootTree = resolveRootTree(refspec);
    final NodeRef typeTreeRef = resolTypeTreeRef(refspec, path, rootTree);
    final ObjectId defaultMetadataId = typeTreeRef.getMetadataId();
    final ProgressListener progressListener = getProgressListener();
    progressListener.started();
    progressListener.setDescription("Exporting diffs for path '" + path + "'... ");
    FeatureCollection<SimpleFeatureType, SimpleFeature> asFeatureCollection = new BaseFeatureCollection<SimpleFeatureType, SimpleFeature>() {

        @Override
        public FeatureIterator<SimpleFeature> features() {
            Iterator<DiffEntry> diffs = command(DiffOp.class).setOldVersion(oldRef).setNewVersion(newRef).setFilter(path).call();
            final Iterator<SimpleFeature> plainFeatures = getFeatures(diffs, old, stagingDatabase(), defaultMetadataId, progressListener);
            Iterator<Optional<Feature>> transformed = Iterators.transform(plainFeatures, ExportDiffOp.this.function);
            Iterator<SimpleFeature> filtered = Iterators.filter(Iterators.transform(transformed, new Function<Optional<Feature>, SimpleFeature>() {

                @Override
                public SimpleFeature apply(Optional<Feature> input) {
                    return (SimpleFeature) (input.isPresent() ? input.get() : null);
                }
            }), Predicates.notNull());
            return new DelegateFeatureIterator<SimpleFeature>(filtered);
        }
    };
    // add the feature collection to the feature store
    final Transaction transaction;
    if (transactional) {
        transaction = new DefaultTransaction("create");
    } else {
        transaction = Transaction.AUTO_COMMIT;
    }
    try {
        targetStore.setTransaction(transaction);
        try {
            targetStore.addFeatures(asFeatureCollection);
            transaction.commit();
        } catch (final Exception e) {
            if (transactional) {
                transaction.rollback();
            }
            Throwables.propagateIfInstanceOf(e, GeoToolsOpException.class);
            throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
        } finally {
            transaction.close();
        }
    } catch (IOException e) {
        throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
    }
    progressListener.complete();
    return targetStore;
}
Also used : DelegateFeatureIterator(org.geotools.feature.collection.DelegateFeatureIterator) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) IOException(java.io.IOException) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) IOException(java.io.IOException) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) BaseFeatureCollection(org.geotools.feature.collection.BaseFeatureCollection) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Aggregations

Transaction (org.geotools.data.Transaction)11 DefaultTransaction (org.geotools.data.DefaultTransaction)9 SimpleFeature (org.opengis.feature.simple.SimpleFeature)8 Test (org.junit.Test)7 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)7 FeatureId (org.opengis.filter.identity.FeatureId)6 ObjectId (org.locationtech.geogig.api.ObjectId)5 ResourceId (org.opengis.filter.identity.ResourceId)3 Function (com.google.common.base.Function)2 Optional (com.google.common.base.Optional)2 IOException (java.io.IOException)2 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)2 BaseFeatureCollection (org.geotools.feature.collection.BaseFeatureCollection)2 DelegateFeatureIterator (org.geotools.feature.collection.DelegateFeatureIterator)2 NodeRef (org.locationtech.geogig.api.NodeRef)2 ProgressListener (org.locationtech.geogig.api.ProgressListener)2 RevFeature (org.locationtech.geogig.api.RevFeature)2 RevTree (org.locationtech.geogig.api.RevTree)2 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)2 Feature (org.opengis.feature.Feature)2