Search in sources :

Example 1 with IRecordState

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

the class CacheStatistics method layerStoreSize.

public long layerStoreSize(String layer) {
    try {
        SimpleQuery query = new SimpleQuery();
        query.setMaxResults(Integer.MAX_VALUE);
        query.eq(CachedTile.TYPE.layerId.name(), layer);
        long result = 0;
        try (ResultSet resultSet = cache.store.find(query)) {
            for (IRecordState state : resultSet) {
                result += new CachedTile(state, null).filesize.get();
            }
        }
        return result;
    } catch (Exception e) {
        return -1;
    }
}
Also used : SimpleQuery(org.polymap.recordstore.SimpleQuery) ResultSet(org.polymap.recordstore.ResultSet) IRecordState(org.polymap.recordstore.IRecordState)

Example 2 with IRecordState

use of org.polymap.recordstore.IRecordState 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 IRecordState

use of org.polymap.recordstore.IRecordState 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 IRecordState

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

the class RDataStore method getNames.

@Override
public List<Name> getNames() throws IOException {
    // see #loadSchema()
    try {
        ResultSet rs = RDataStore.this.store.find(new SimpleQuery().setMaxResults(1000).eq("type", "FeatureType"));
        HashSet result = new HashSet(rs.count() * 2);
        for (IRecordState entry : rs) {
            String namespace = entry.get("namespace");
            String localpart = entry.get("name");
            Name name = namespace == null ? new NameImpl(namespace, localpart) : new NameImpl(localpart);
            if (!result.add(name)) {
                throw new IllegalStateException("Name already loaded: " + name);
            }
        }
        return ImmutableList.copyOf(result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : NameImpl(org.geotools.feature.NameImpl) SimpleQuery(org.polymap.recordstore.SimpleQuery) ResultSet(org.polymap.recordstore.ResultSet) IRecordState(org.polymap.recordstore.IRecordState) IOException(java.io.IOException) HashSet(java.util.HashSet) Name(org.opengis.feature.type.Name)

Example 5 with IRecordState

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

the class Cache304 method get.

/**
 * @param request
 * @param layers
 * @param props The processor properties for this layer.
 * @return The cached tile or null.
 */
public CachedTile get(PipelineProcessorSite site, GetMapRequest request) {
    try {
        // keep store and queue stable; prevent race between removing
        // Command from queue and writing to store
        lock.readLock().lock();
        // search the store
        RecordQuery query = buildQuery(site, request);
        query.setMaxResults(2);
        ResultSet resultSet = store.find(query);
        if (resultSet.count() > 1) {
            log.warn("More than one tile for query: " + request);
        }
        List<CachedTile> result = new ArrayList();
        for (IRecordState state : resultSet) {
            result.add(new CachedTile(state, dataDir));
        }
        // search the queue
        updateQueue.adaptCacheResult(result, query);
        if (result.size() > 1) {
            log.warn("More than one tile in result: " + result.size());
        }
        if (!result.isEmpty()) {
            CachedTile cachedTile = result.get(0);
            long now = System.currentTimeMillis();
            if (!cachedTile.dataExists()) {
                log.warn("Tile data file lost for: " + cachedTile.filename.get());
                return null;
            }
            // done in the last accessTimeRasterMillis
            if ((cachedTile.lastAccessed.get() + accessTimeRasterMillis) < now) {
                cachedTile.lastAccessed.put(now);
                updateQueue.push(new CacheUpdateQueue.TouchCommand(cachedTile));
                updater.reSchedule();
            }
            statistics.incLayerCounter(site.layerId.get(), false);
            return cachedTile;
        } else {
            statistics.incLayerCounter(site.layerId.get(), true);
            return null;
        }
    } catch (Exception e) {
        log.error("", e);
        return null;
    } finally {
        lock.readLock().unlock();
    }
}
Also used : ResultSet(org.polymap.recordstore.ResultSet) ArrayList(java.util.ArrayList) IRecordState(org.polymap.recordstore.IRecordState) RecordQuery(org.polymap.recordstore.RecordQuery) IOException(java.io.IOException)

Aggregations

IRecordState (org.polymap.recordstore.IRecordState)10 IOException (java.io.IOException)8 ResultSet (org.polymap.recordstore.ResultSet)8 SimpleQuery (org.polymap.recordstore.SimpleQuery)5 Timer (org.polymap.core.runtime.Timer)4 FeatureType (org.opengis.feature.type.FeatureType)3 RecordQuery (org.polymap.recordstore.RecordQuery)3 ArrayList (java.util.ArrayList)2 Feature (org.opengis.feature.Feature)2 Updater (org.polymap.recordstore.IRecordStore.Updater)2 LuceneRecordQuery (org.polymap.recordstore.lucene.LuceneRecordQuery)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FeatureSource (org.geotools.data.FeatureSource)1 NameImpl (org.geotools.feature.NameImpl)1 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)1 FeatureVisitor (org.opengis.feature.FeatureVisitor)1 Name (org.opengis.feature.type.Name)1 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)1 ExcludeFilter (org.opengis.filter.ExcludeFilter)1