Search in sources :

Example 1 with Updater

use of org.polymap.recordstore.IRecordStore.Updater in project polymap4-core by Polymap4.

the class AbstractRecordStoreTest method createRecords.

// public void tstThreaded() throws Exception {
// final int loops = 250;
// final int threads = 4;
// createRecords( loops*threads );
// 
// List<Future> results = new ArrayList();
// for (int i=0; i<threads; i++) {
// results.add( Polymap.executorService().submit( new Callable() {
// public Object call() throws Exception {
// //                    readRecords( loops );
// queryRecords( loops );
// return new Object();
// }
// } ) );
// }
// // wait for results
// for (Future result : results) {
// result.get();
// }
// }
protected void createRecords(int loops) throws Exception {
    start = System.currentTimeMillis();
    final Timer timer = new Timer();
    Updater updater = store.prepareUpdate();
    try {
        for (int i = 0; i < loops; i++) {
            TestRecord record = new TestRecord(store.newRecord());
            record.payload.put("LuceneRecordStore store = new LuceneRecordStore( new File LuceneRecordStore store = new LuceneRecordStore( new File LuceneRecordStore store = new LuceneRecordStore( new File");
            record.type.put("2");
            record.count.put(i);
            updater.store(record.state());
        }
        updater.apply();
    } catch (Exception e) {
        updater.discard();
        throw e;
    }
    log.info("Records created: " + loops + " in " + timer.elapsedTime() + "ms -> " + (double) timer.elapsedTime() / loops + "ms/loop");
}
Also used : Timer(org.polymap.core.runtime.Timer) Updater(org.polymap.recordstore.IRecordStore.Updater)

Example 2 with Updater

use of org.polymap.recordstore.IRecordStore.Updater in project polymap4-core by Polymap4.

the class RDataStore method createSchema.

@Override
public void createSchema(FeatureType schema) throws IOException {
    assert schema != null : "schema is null";
    assert schema.getName() != null : "schema.getName() is null";
    if (loadSchema(schema.getName()).isPresent()) {
        throw new IOException("Schema name already exists: " + schema.getName());
    }
    Updater tx = runningTx != null ? runningTx.updater() : store.prepareUpdate();
    try {
        String schemaContent = schemaCoder.encode(schema);
        log.debug("Created schema: " + schemaContent);
        IRecordState schemaRecord = store.newRecord().put("type", "FeatureType").put("name", schema.getName().getLocalPart()).put("qname", schema.getName().getURI()).put("content", schemaContent);
        if (schema.getName().getNamespaceURI() != null) {
            schemaRecord.put("namespace", schema.getName().getNamespaceURI());
        }
        // Configuration config = new WFSConfiguration();
        // Encoder encoder = new Encoder( config );
        // encoder.setIndenting( true );
        // encoder.setIndentSize( 4 );
        // encoder.encode( schema, GML.FeatureCollectionType, System.out );
        // encoder.setEncoding(Charset.forName( global.getCharset() ));
        tx.store(schemaRecord);
        if (runningTx == null) {
            tx.apply();
        }
    } catch (Throwable e) {
        log.debug("", e);
        if (runningTx == null) {
            tx.discard();
        }
        if (e instanceof IOException) {
            throw (IOException) e;
        } else {
            throw new IOException(e);
        }
    }
}
Also used : Updater(org.polymap.recordstore.IRecordStore.Updater) IRecordState(org.polymap.recordstore.IRecordState) IOException(java.io.IOException)

Example 3 with Updater

use of org.polymap.recordstore.IRecordStore.Updater in project polymap4-core by Polymap4.

the class RDataStore method updateSchema.

@Override
public void updateSchema(Name name, final FeatureType newSchema) throws IOException {
    assert name != null && newSchema != null;
    final Updater tx = store.prepareUpdate();
    try {
        // check modified property names
        boolean namesModified = false;
        for (PropertyDescriptor desc : newSchema.getDescriptors()) {
            // set by FeatureTypeEditor/AttributeCellModifier
            String origName = (String) desc.getUserData().get(ORIG_NAME_KEY);
            if (origName != null) {
                namesModified = true;
            }
        }
        // find deleted properties
        // XXX check complex schemas
        FeatureType schema = getSchema(name);
        final List<PropertyDescriptor> deleted = new ArrayList();
        for (PropertyDescriptor desc : schema.getDescriptors()) {
            if (newSchema.getDescriptor(desc.getName()) == null) {
                deleted.add(desc);
            }
        }
        // schema name changed or prop deleted? -> update features
        final String newName = newSchema.getName().getLocalPart();
        if (!name.getLocalPart().equals(newSchema.getName().getLocalPart()) || !deleted.isEmpty() || namesModified) {
            FeatureSource fs = getFeatureSource(name);
            fs.getFeatures().accepts(new FeatureVisitor() {

                public void visit(Feature feature) {
                    try {
                        // typeName
                        ((RFeature) feature).state.put(RFeature.TYPE_KEY, newName);
                        // List<Name> origModifiedNames = new ArrayList();
                        for (PropertyDescriptor desc : newSchema.getDescriptors()) {
                            // set by FeatureTypeEditor/AttributeCellModifier
                            String origName = (String) desc.getUserData().get(ORIG_NAME_KEY);
                            if (origName != null) {
                                RAttribute prop = (RAttribute) feature.getProperty(origName);
                                if (prop != null) {
                                    if (prop.getValue() != null) {
                                        ((RFeature) feature).state.put(desc.getName().getLocalPart(), prop.getValue());
                                    }
                                    ((RFeature) feature).state.remove(prop.key.toString());
                                }
                            }
                        }
                        // deleted attributes
                        for (PropertyDescriptor desc : deleted) {
                            // XXX check complex schemas
                            RProperty prop = (RProperty) feature.getProperty(desc.getName());
                            ((RFeature) feature).state.remove(prop.key.toString());
                        }
                        tx.store(((RFeature) feature).state);
                    } catch (Exception e) {
                        // Designing a visitor interface without Exception is not a good idea!
                        throw new RuntimeException("", e);
                    }
                }
            }, null);
        }
        // update schema record
        ResultSet rs = store.find(new SimpleQuery().setMaxResults(1).eq("type", "FeatureType").eq("name", name.getLocalPart()));
        IRecordState record = rs.get(0);
        String schemaContent = schemaCoder.encode(newSchema);
        record.put("content", schemaContent);
        record.put("name", newName);
        tx.store(record);
        log.debug("Current schema: " + schemaCoder.encode(schema));
        log.debug("Updated schema: " + schemaContent);
        tx.apply();
    } catch (Throwable e) {
        log.debug("", e);
        tx.discard();
        throw new RuntimeException(e);
    }
}
Also used : FeatureType(org.opengis.feature.type.FeatureType) FeatureSource(org.geotools.data.FeatureSource) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleQuery(org.polymap.recordstore.SimpleQuery) ArrayList(java.util.ArrayList) FeatureVisitor(org.opengis.feature.FeatureVisitor) Feature(org.opengis.feature.Feature) IOException(java.io.IOException) Updater(org.polymap.recordstore.IRecordStore.Updater) ResultSet(org.polymap.recordstore.ResultSet) IRecordState(org.polymap.recordstore.IRecordState)

Example 4 with Updater

use of org.polymap.recordstore.IRecordStore.Updater in project polymap4-core by Polymap4.

the class RDataStore method deleteSchema.

public void deleteSchema(FeatureType schema, IProgressMonitor monitor) {
    // remove features
    try {
        RFeatureStore fs = (RFeatureStore) getFeatureSource(schema.getName());
        fs.removeFeatures(Filter.INCLUDE);
    } catch (Exception e) {
        log.debug("", e);
        throw new RuntimeException(e);
    }
    // remove schema
    Updater tx = store.prepareUpdate();
    try {
        ResultSet rs = store.find(new SimpleQuery().setMaxResults(1).eq("type", "FeatureType").eq("name", schema.getName().getLocalPart()).eq("namespace", schema.getName().getNamespaceURI()));
        assert rs.count() == 1 : "Illegal number of schemas found: " + rs.count();
        tx.remove(rs.get(0));
        tx.apply();
    } catch (Throwable e) {
        log.debug("", e);
        tx.discard();
        throw new RuntimeException(e);
    }
}
Also used : SimpleQuery(org.polymap.recordstore.SimpleQuery) Updater(org.polymap.recordstore.IRecordStore.Updater) ResultSet(org.polymap.recordstore.ResultSet) IOException(java.io.IOException)

Aggregations

Updater (org.polymap.recordstore.IRecordStore.Updater)4 IOException (java.io.IOException)3 IRecordState (org.polymap.recordstore.IRecordState)2 ResultSet (org.polymap.recordstore.ResultSet)2 SimpleQuery (org.polymap.recordstore.SimpleQuery)2 ArrayList (java.util.ArrayList)1 FeatureSource (org.geotools.data.FeatureSource)1 Feature (org.opengis.feature.Feature)1 FeatureVisitor (org.opengis.feature.FeatureVisitor)1 FeatureType (org.opengis.feature.type.FeatureType)1 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)1 Timer (org.polymap.core.runtime.Timer)1