Package org.geotools.data

Examples of org.geotools.data.VersioningDataStore


     */
    SimpleFeature getLastSynchronizationRecord(QName typeName) throws IOException {
        FeatureIterator<SimpleFeature> fi = null;
        try {
            // get the versioning data store
            VersioningDataStore ds = core.getVersioningStore();

            // gather the record from the synch history table
            DefaultQuery q = new DefaultQuery();
            q.setFilter(ff.equal(ff.property("table_name"), ff.literal(typeName.getLocalPart()),
                    true));
            q.setSortBy(new SortBy[] { ff.sort("central_revision", SortOrder.DESCENDING),
                    ff.sort("local_revision", SortOrder.DESCENDING)});
            q.setMaxFeatures(1);
            fi = ds.getFeatureSource(SYNCH_HISTORY).getFeatures(q).features();

            if (fi.hasNext()) {
                return fi.next();
            } else {
                return null;
View Full Code Here


            throw new GSSException("Could not locate typeName: " + typeName,
                    GSSExceptionCode.InvalidParameterValue, TYPE_NAME);
        }

        // get the versioning data store
        VersioningDataStore ds = core.getVersioningStore();

        // check the table is actually synch-ed
        DefaultQuery q = new DefaultQuery();
        q.setFilter(ff.equal(ff.property("table_name"), ff.literal(fti.getName()), true));
        int count = ds.getFeatureSource(SYNCH_TABLES).getCount(q);
        if (count == 0) {
            throw new GSSException(fti.getName() + " is not a synchronized layer",
                    GSSExceptionCode.InvalidParameterValue, TYPE_NAME);
        }
    }
View Full Code Here

            }

            // make sure all of the changes are applied in one hit, or none
            // very important, make sure all versioning writes use the same transaction or they
            // will deadlock each other
            VersioningDataStore ds = core.getVersioningStore();
           
            // see if there is anything at all to do, if both sides have no changes there
            // is no point eating away a revision number (this avoid the local revision number to
            // skyrocket for nothing if there are frequent synchronisations)
            String tableName = request.getTypeName().getLocalPart();
            VersioningFeatureStore fs = (VersioningFeatureStore) ds.getFeatureSource(tableName);
            FeatureStore history = (FeatureStore) ds.getFeatureSource(SYNCH_HISTORY);
            PropertyIsEqualTo ftSyncRecord = ff.equals(ff.property("table_name"), ff.literal(tableName));
            TransactionType changes = request.getTransaction();
            int changesCount = core.countChanges(changes);
            // ... if we have no changes from remote
            if(changesCount == 0) {
                // ... and we have no changes locally
                String lastLocalRevisionId = lastLocalRevision != -1 ? String.valueOf(lastLocalRevision) : "FIRST";
                if(fs.getLog(lastLocalRevisionId, "LAST", null, null, 1).size() == 0) {
                    // add a new record without the need to grab a new local revision
                    // (if necessary, that is, if at least the Central revision changed or if
                    // we don't have a synch history at all)
                    long newCentralRevision = request.getToVersion();
                    if(lastCentralRevision != newCentralRevision || record == null) {
                        SimpleFeatureType hft = (SimpleFeatureType) history.getSchema();
                        SimpleFeature f = SimpleFeatureBuilder.build(hft, new Object[] { tableName,
                                lastLocalRevision, newCentralRevision }, null);
                        history.addFeatures(DataUtilities.collection(f));
                    }
                   
                    // ... let's just return directly, no need to store or do anything
                    return new PostDiffResponseType();
                }
            }
           
            // setup the commit message and author
            transaction.putProperty(VersioningDataStore.AUTHOR, "gss");
            transaction.putProperty(VersioningDataStore.MESSAGE, "Applying " + changesCount
                    + " changes coming from Central on layer '" + tableName + "'");

            // grab the feature stores and bind them all to the same transaction
            VersioningFeatureStore conflicts = (VersioningFeatureStore) ds
                    .getFeatureSource(SYNCH_CONFLICTS);
            conflicts.setTransaction(transaction);
            history.setTransaction(transaction);
            fs.setTransaction(transaction);
View Full Code Here

                            "the latest PostDiff synchronisation: " + lastPostRevision,
                        GSSExceptionCode.InvalidParameterValue, FROM_VERSION);
            }

            // ok, we need to find what revisions we have to jump over (the synch ones)
            VersioningDataStore ds = core.getVersioningStore();

            // gather all records in the synch history that happened after the requested revision
            DefaultQuery q = new DefaultQuery();
            String tableName = request.getTypeName().getLocalPart();
            Filter tableFilter = ff.equal(ff.property("table_name"), ff.literal(tableName), true);
            Filter revisionFilter = ff.greater(ff.property("local_revision"), ff.literal(request
                    .getFromVersion()));
            q.setFilter(ff.and(tableFilter, revisionFilter));
            q.setSortBy(new SortBy[] { ff.sort("local_revision", SortOrder.ASCENDING) });
            fi = ds.getFeatureSource(SYNCH_HISTORY).getFeatures(q).features();

            // build a list so that taking elements pair-wise we get the intervals we need to query
            // (we won't sent local changes happened after the last PostDiff as there is no way
            // to know if they would conflict with Central or not)
            List<Long> intervals = new ArrayList<Long>();
            intervals.add(request.getFromVersion());
            while (fi.hasNext()) {
                intervals.add((Long) fi.next().getAttribute("local_revision"));
            }
            fi.close();

            TransactionType transaction;
            if(intervals.size() > 1) {
                // gather the ids of the features still under conflict, we don't want to load their
                // diffs
                Filter nonConflictingFilter = getFidConflictFilter(tableName,
                        getActiveConflicts(tableName));
   
                // gather all of the diff readers for the non conflicting features
                VersioningFeatureSource fs = (VersioningFeatureSource) ds.getFeatureSource(tableName);
                FeatureDiffReader[] readers = new FeatureDiffReader[intervals.size() - 1];
                for (int i = 1; i < intervals.size(); i++) {
                    // mind we need to skip the actual synch points, so we subtract 1
                    // from the revision number
                    String fromVersion = String.valueOf(intervals.get(i - 1));
View Full Code Here

    /**
     * Returns all the active conflicts for the specified table
     */
    FeatureCollection<SimpleFeatureType, SimpleFeature> getActiveConflicts(String tableName)
            throws IOException {
        VersioningDataStore ds = core.getVersioningStore();
        VersioningFeatureSource conflicts = (VersioningFeatureSource) ds
                .getFeatureSource(SYNCH_CONFLICTS);

        Filter unresolved = ff.equals(ff.property("state"), ff.literal(String.valueOf('c')));
        Filter tableFilter = ff.equals(ff.property("table_name"), ff.literal(tableName));
        return conflicts.getFeatures(ff.and(unresolved, tableFilter));
View Full Code Here

    /**
     * Returns the clean merges occurred at the specified revision
     */
    FeatureCollection<SimpleFeatureType, SimpleFeature> getCleanMerges(String tableName,
            long revision) throws IOException {
        VersioningDataStore ds = core.getVersioningStore();
        VersioningFeatureSource conflicts = (VersioningFeatureSource) ds
                .getFeatureSource(SYNCH_CONFLICTS);

        Filter unresolved = ff.equals(ff.property("state"), ff.literal(String.valueOf('m')));
        Filter tableFilter = ff.equals(ff.property("table_name"), ff.literal(tableName));
        Filter version = ff.equals(ff.property("local_revision"), ff.literal(revision));
View Full Code Here

        Document dom = dom(response);
        checkOws10Exception(dom, "InvalidParameterValue", "fromVersion");
    }

    public void testEmptyRepeated() throws Exception {
        VersioningDataStore synch = (VersioningDataStore) getCatalog().getDataStoreByName("synch").getDataStore(null);
        VersioningFeatureSource restricted = (VersioningFeatureSource) synch.getFeatureSource("restricted");
        long revisionStart = getLastRevision(restricted);
       
        MockHttpServletResponse response = postAsServletResponse(root(true),
                loadTextResource("PostDiffEmpty.xml"));
        checkPostDiffSuccessResponse(response);
View Full Code Here

        MockHttpServletResponse response = postAsServletResponse(root(true),
                loadTextResource("PostDiffEmptyTransaction.xml"));
        checkPostDiffSuccessResponse(response);

        // check the lasts known Central revision is updated
        VersioningDataStore synch = (VersioningDataStore) getCatalog().getDataStoreByName("synch")
                .getDataStore(null);
        FeatureSource<SimpleFeatureType, SimpleFeature> fs = synch.getFeatureSource(SYNCH_HISTORY);
        FeatureCollection fc = fs.getFeatures(ff.equals(ff.property("table_name"), ff
                .literal("restricted")));
        FeatureIterator fi = fc.features();
        SimpleFeature f = (SimpleFeature) fi.next();
        fi.close();
View Full Code Here

        FeatureIterator<SimpleFeature> fi = null;
        FeatureIterator<SimpleFeature> li = null;
        try {
            // grab the layers to be synchronised
            VersioningDataStore ds = core.getVersioningStore();
            FeatureSource<SimpleFeatureType, SimpleFeature> outstanding = ds
                    .getFeatureSource(SYNCH_OUTSTANDING);
            DefaultQuery q = new DefaultQuery(SYNCH_OUTSTANDING);
            q.setSortBy(new SortBy[] { ff.sort("last_synchronization", SortOrder.ASCENDING) });

            LOGGER.info("Performing scheduled synchronisation");

            fi = outstanding.getFeatures(q).features();

            // the set of units we failed to synchronize with. The problem might be a connection
            // timeout, we don't really want to multiply the timeout by the number of layers
            Set<Integer> unitBlacklist = new HashSet<Integer>();

            while (fi.hasNext()) {
                // extract relevant attributes
                SimpleFeature layer = fi.next();
                int unitId = (Integer) layer.getAttribute("unit_id");
                int tableId = (Integer) layer.getAttribute("table_id");
                String unitName = (String) layer.getAttribute("unit_name");
                String tableName = (String) layer.getAttribute("table_name");
                String address = (String) layer.getAttribute("unit_address");
                String user = (String) layer.getAttribute("synch_user");
                String password = (String) layer.getAttribute("synch_password");
                Long getDiffCentralRevision = (Long) layer.getAttribute("getdiff_central_revision");
                Long lastUnitRevision = (Long) layer.getAttribute("last_unit_revision");

                // avoid the unit that already failed this run, we'll try next run
                if (unitBlacklist.contains(unitId)) {
                    LOGGER.log(Level.INFO, "Unit " + unitName + " is blacklisted "
                            + "for this run, skipping " + tableName);
                    continue;
                }

                Transaction transaction = null;
                try {
                    // build the transaction with the proper author and commit message
                    transaction = new DefaultTransaction();

                    // get the last central revision the client knows about
                    GSSClient client = getClient(address, user, password);
                    QName layerName = getLayerName(tableName);
                    long clientCentralRevision = client.getCentralRevision(layerName);

                    // compute the diff that we have to send the client. Notice that we have
                    // to skip over the local change occurred when we last performed a GetDiff
                    // against the client
                    VersioningFeatureStore fs = (VersioningFeatureStore) ds
                            .getFeatureSource(tableName);
                    fs.setTransaction(transaction);
                    String fromRevision = clientCentralRevision == -1 ? "FIRST" : String
                            .valueOf(clientCentralRevision);
                    TransactionType centralChanges;
                    LOGGER.log(Level.INFO, "About to compute PostDiff changes. Last central revision known to client " + clientCentralRevision + ", last GetDiff central revision " + getDiffCentralRevision);
                    if (getDiffCentralRevision == null || clientCentralRevision >= getDiffCentralRevision) {
                        // either first time or we don't need to make jumps
                        LOGGER.log(Level.INFO, "First PostDiff or clientRevion same as the last central one, computing diff from " + fromRevision +  " to LAST");
                        FeatureDiffReader fdr = fs.getDifferences(fromRevision, "LAST", null, null);
                        centralChanges = new VersioningTransactionConverter().convert(fdr,
                                TransactionType.class);
                    } else  {
                        // we need to jump over the last local changes
                        String before = String.valueOf(getDiffCentralRevision - 1);
                        String after = String.valueOf(getDiffCentralRevision);
                        LOGGER.log(Level.INFO, "Client revision lower than the server one, computing diff from " + fromRevision +  " to " + before + " and merging with diffs from " + after + " to LAST");
                        FeatureDiffReader fdr1 = fs.getDifferences(fromRevision, before, null, null);
                        FeatureDiffReader fdr2 = fs.getDifferences(after, "LAST", null, null);
                        FeatureDiffReader[] fdr = new FeatureDiffReader[] { fdr1, fdr2 };
                        centralChanges = new VersioningTransactionConverter().convert(fdr,
                                TransactionType.class);
                    }

                    // what is the latest change on this layer? (worst case it's the last GetDiff
                    // from this Unit)
                    long lastCentralRevision = clientCentralRevision;
                    li = fs.getLog("LAST", fromRevision, null, null, 1).features();
                    if (li.hasNext()) {
                        lastCentralRevision = (Long) li.next().getAttribute("revision");
                    }
                    li.close();

                    // finally run the PostDiff
                    PostDiffType postDiff = new PostDiffType();
                    postDiff.setTypeName(layerName);
                    postDiff.setFromVersion(clientCentralRevision);
                    postDiff.setToVersion(lastCentralRevision);
                    postDiff.setTransaction(centralChanges);
                    client.postDiff(postDiff);

                    // grab the changes from the client and apply them locally
                    GetDiffType getDiff = new GetDiffType();
                    getDiff.setFromVersion(lastUnitRevision == null ? -1 : lastUnitRevision);
                    getDiff.setTypeName(layerName);
                    GetDiffResponseType gdr = client.getDiff(getDiff);
                    TransactionType unitChanges = gdr.getTransaction();
                    core.applyChanges(unitChanges, fs);
                   
                    // mark down this layer as succesfully synchronised
                    FeatureStore<SimpleFeatureType, SimpleFeature> tuMetadata = (FeatureStore<SimpleFeatureType, SimpleFeature>) ds
                            .getFeatureSource(SYNCH_UNIT_TABLES);
                    tuMetadata.setTransaction(transaction);
                    SimpleFeatureType tuSchema = tuMetadata.getSchema();
                    int unitChangeCount = core.countChanges(unitChanges);
                    int centralChangeCount = core.countChanges(centralChanges);
                    if (unitChangeCount == 0 && centralChangeCount == 0) {
                        // just update the last_synch marker, as nothing else happened and
                        // this way we can avoid eating away central revision number (which
                        // might go up very rapidly otherwise)
                        AttributeDescriptor[] atts = new AttributeDescriptor[] { tuSchema
                                .getDescriptor("last_synchronization") };
                        Object[] values = new Object[] { new Date() };
                        Filter filter = ff.and(ff.equals(ff.property("table_id"), ff
                                .literal(tableId)), ff.equals(ff.property("unit_id"), ff
                                .literal(unitId)));
                        tuMetadata.modifyFeatures(atts, values, filter);
                    } else {
                        AttributeDescriptor[] atts = new AttributeDescriptor[] {
                                tuSchema.getDescriptor("last_synchronization"),
                                tuSchema.getDescriptor("getdiff_central_revision"),
                                tuSchema.getDescriptor("last_unit_revision") };
                        Object[] values = new Object[] { new Date(),
                                Long.parseLong(fs.getVersion()), gdr.getToVersion() };
                        Filter filter = ff.and(ff.equals(ff.property("table_id"), ff
                                .literal(tableId)), ff.equals(ff.property("unit_id"), ff
                                .literal(unitId)));
                        tuMetadata.modifyFeatures(atts, values, filter);
                    }

                    // mark the unit as succeffully updated
                    updateUnitStatus(ds, transaction, unitId, false);
                   
                    // the the commit log
                    transaction.putProperty(VersioningDataStore.AUTHOR, "gss");
                    transaction.putProperty(VersioningDataStore.MESSAGE, "Synchronizing with Unit '"
                            + unitName + "' on table '" + tableName + "': " + centralChangeCount
                            + " changes sent and " + unitChangeCount + " changes received");

                    // close up
                    transaction.commit();
                    LOGGER.log(Level.INFO, "Successfull synchronisation of table " + tableName
                            + " for unit " + unitName + "(" + centralChangeCount
                            + " changes sent to the Unit, " + unitChangeCount
                            + " change incoming from the Unit)");
                } catch (Exception e) {
                    LOGGER.log(Level.SEVERE, "Synchronisation of table " + tableName + " for unit "
                            + unitName + " failed", e);

                    // rollback all current changes
                    transaction.rollback();

                    // if anything at all went bad mark the layer synch as failed
                    FeatureStore<SimpleFeatureType, SimpleFeature> tuMetadata = (FeatureStore<SimpleFeatureType, SimpleFeature>) ds
                            .getFeatureSource(SYNCH_UNIT_TABLES);
                    SimpleFeatureType tuSchema = tuMetadata.getSchema();
                    AttributeDescriptor[] atts = new AttributeDescriptor[] { tuSchema
                            .getDescriptor("last_failure"), };
                    Object[] values = new Object[] { new Date() };
View Full Code Here

   
    @Override
    protected void setUpInternal() throws Exception {
        super.setUpInternal();
       
        VersioningDataStore synch = (VersioningDataStore) getCatalog().getDataStoreByName("synch").getDataStore(null);
        FeatureStore<SimpleFeatureType, SimpleFeature> fs = (FeatureStore<SimpleFeatureType, SimpleFeature>) synch.getFeatureSource(SYNCH_HISTORY);
        SimpleFeatureBuilder fb = new SimpleFeatureBuilder(fs.getSchema());
        // three synchs occurred on this layer
        fs.addFeatures(DataUtilities.collection(fb.buildFeature(null, new Object[] {"roads", 150, 160})));
        fs.addFeatures(DataUtilities.collection(fb.buildFeature(null, new Object[] {"roads", 182, 210})));
        fs.addFeatures(DataUtilities.collection(fb.buildFeature(null, new Object[] {"roads", 193, 340})));
View Full Code Here

TOP

Related Classes of org.geotools.data.VersioningDataStore

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.