Search in sources :

Example 16 with Timer

use of org.polymap.core.runtime.Timer in project polymap4-core by Polymap4.

the class ImageCacheProcessor method getMapRequest.

@Override
public void getMapRequest(GetMapRequest request, ProcessorContext context) throws Exception {
    Timer timer = new Timer();
    CachedTile cachedTile = cache.get(site, request);
    // cached
    if (cachedTile != null) {
        // check If-Modified-Since
        long modifiedSince = request.getIfModifiedSince();
        long lastModified = cachedTile.lastModified.get();
        if (modifiedSince > 0 && lastModified > modifiedSince) {
            log.info("### CACHE: 304! :) -- " + timer.elapsedTime() + "ms");
            context.sendResponse(EncodedImageResponse.NOT_MODIFIED);
            context.sendResponse(ProcessorResponse.EOP);
        } else // in cache but modified
        {
            byte[] data = cachedTile.data.get();
            log.info("### CACHE: Hit (" + data.length + " bytes) -- " + timer.elapsedTime() + "ms");
            EncodedImageResponse response = new EncodedImageResponse(data, data.length);
            response.setLastModified(cachedTile.lastModified.get());
            response.setExpires(cachedTile.expires.get());
            context.sendResponse(response);
            context.sendResponse(ProcessorResponse.EOP);
        }
    } else // not in cache -> send request down the pipeline
    {
        log.info("### CACHE: Miss -- " + timer.elapsedTime() + "ms");
        ByteArrayOutputStream cacheBuf = new ByteArrayOutputStream(128 * 1024);
        context.put("cacheBuf", cacheBuf);
        context.put("request", request);
        context.put("created", System.currentTimeMillis());
        context.sendRequest(request);
    }
}
Also used : Timer(org.polymap.core.runtime.Timer) EncodedImageResponse(org.polymap.core.data.image.EncodedImageResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with Timer

use of org.polymap.core.runtime.Timer in project polymap4-core by Polymap4.

the class LuceneQueryDialect method getFeatureStates.

@Override
public PostProcessResultSet getFeatureStates(RFeatureStore fs, final Query query) throws IOException {
    try {
        Timer timer = new Timer();
        // transform query
        final Transformer transformer = new Transformer();
        RecordQuery rsQuery = transformer.transform(fs, query);
        // field selector
        final String[] propNames = query.getPropertyNames();
        if (propNames != null) {
            rsQuery.setFieldSelector(new IRecordFieldSelector() {

                private Map<String, Boolean> keys = new HashMap(64);

                @Override
                public boolean test(String key) {
                    Boolean accepted = keys.get(key);
                    if (accepted == null) {
                        keys.put(key, accepted = Boolean.FALSE);
                        for (String propName : propNames) {
                            // XXX real field names and additional fields are not known here
                            if (key.startsWith(propName)) {
                                keys.put(key, accepted = Boolean.TRUE);
                                break;
                            }
                        }
                    }
                    return accepted;
                }
            });
        }
        final ResultSet results = rs(fs).find(rsQuery);
        log.debug("    non-processed results: " + results.count() + " ( " + timer.elapsedTime() + "ms)");
        return new PostProcessResultSet() {

            private boolean hasProcessing = !transformer.postProcess.isEmpty();

            private Filter filter = query.getFilter();

            @Override
            public Iterator<IRecordState> iterator() {
                return results.iterator();
            }

            @Override
            public boolean hasPostProcessing() {
                return hasProcessing;
            }

            @Override
            public boolean postProcess(Feature feature) {
                return hasProcessing ? filter.evaluate(feature) : true;
            }

            @Override
            public int size() {
                return results.count();
            }
        };
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) IRecordFieldSelector(org.polymap.recordstore.IRecordFieldSelector) Feature(org.opengis.feature.Feature) RFeature(org.polymap.core.data.rs.RFeature) IOException(java.io.IOException) Timer(org.polymap.core.runtime.Timer) IncludeFilter(org.opengis.filter.IncludeFilter) ExcludeFilter(org.opengis.filter.ExcludeFilter) Filter(org.opengis.filter.Filter) ResultSet(org.polymap.recordstore.ResultSet) IRecordState(org.polymap.recordstore.IRecordState) RecordQuery(org.polymap.recordstore.RecordQuery) LuceneRecordQuery(org.polymap.recordstore.lucene.LuceneRecordQuery)

Example 18 with Timer

use of org.polymap.core.runtime.Timer in project polymap4-core by Polymap4.

the class LuceneQueryDialect method getBounds.

@Override
public ReferencedEnvelope getBounds(RFeatureStore fs, Query query) throws IOException {
    Timer timer = new Timer();
    FeatureType schema = fs.getSchema();
    assert schema.getGeometryDescriptor() != null : "Schema has no Geometry: " + schema;
    String geomName = schema.getGeometryDescriptor().getLocalName();
    // type/name query
    // XXX handle postProcess
    Transformer transformer = new Transformer();
    RecordQuery rsQuery = transformer.transform(fs, query);
    rsQuery.setMaxResults(1);
    try {
        // MinX
        String fieldName = geomName + GeometryValueCoder.FIELD_MINX;
        rsQuery.sort(fieldName, RecordQuery.ASC, Double.class);
        ResultSet resultSet = rs(fs).find(rsQuery);
        if (resultSet.count() == 0) {
            return ReferencedEnvelope.EVERYTHING;
        }
        IRecordState record = resultSet.get(0);
        double minX = record.get(fieldName);
        // MaxX
        fieldName = geomName + GeometryValueCoder.FIELD_MAXX;
        rsQuery.sort(fieldName, RecordQuery.DESC, Double.class);
        resultSet = rs(fs).find(rsQuery);
        double maxX = resultSet.get(0).get(fieldName);
        // MinY
        fieldName = geomName + GeometryValueCoder.FIELD_MINY;
        rsQuery.sort(fieldName, RecordQuery.ASC, Double.class);
        resultSet = rs(fs).find(rsQuery);
        double minY = resultSet.get(0).get(fieldName);
        // MaxX
        fieldName = geomName + GeometryValueCoder.FIELD_MAXY;
        rsQuery.sort(fieldName, RecordQuery.DESC, Double.class);
        resultSet = rs(fs).find(rsQuery);
        double maxY = resultSet.get(0).get(fieldName);
        log.info("Bounds: ... (" + timer.elapsedTime() + "ms)");
        return new ReferencedEnvelope(minX, maxX, minY, maxY, schema.getCoordinateReferenceSystem());
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : FeatureType(org.opengis.feature.type.FeatureType) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Timer(org.polymap.core.runtime.Timer) ResultSet(org.polymap.recordstore.ResultSet) IRecordState(org.polymap.recordstore.IRecordState) IOException(java.io.IOException) RecordQuery(org.polymap.recordstore.RecordQuery) LuceneRecordQuery(org.polymap.recordstore.lucene.LuceneRecordQuery) IOException(java.io.IOException)

Example 19 with Timer

use of org.polymap.core.runtime.Timer in project polymap4-core by Polymap4.

the class Soft2Cache method test.

public static void test(Cache<Integer, byte[]> cache) {
    System.out.println("\n*** " + cache.getClass().getSimpleName() + " ****************************");
    Timer timer = new Timer();
    int loops = 10000000;
    Random random = new Random(0);
    CacheLoader2<Integer, byte[]> loader = key -> new byte[1024];
    for (int i = 0; i < loops; i++) {
        double gausian = Math.abs(random.nextGaussian());
        Integer key = (int) (gausian * 40000);
        assert cache.get(key, loader) != null;
    }
    for (byte[] value : cache.values()) {
        assert value != null;
    }
    long time = timer.elapsedTime();
    Runtime rt = Runtime.getRuntime();
    System.out.println("Mem: total:" + byteCountToDisplaySize(rt.totalMemory()) + " / free: " + byteCountToDisplaySize(rt.freeMemory()));
    System.out.println("Cache size: " + cache.size() + " -> " + (cache.size() / 1000) + "MB");
    System.out.println("Loops: " + loops + " in " + time + "ms -> " + (1000f * loops / time) + "/s");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileUtils.byteCountToDisplaySize(org.apache.commons.io.FileUtils.byteCountToDisplaySize) Timer(org.polymap.core.runtime.Timer) Iterator(java.util.Iterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Random(java.util.Random) Function(java.util.function.Function) ReferenceQueue(java.lang.ref.ReferenceQueue) ConcurrentMap(java.util.concurrent.ConcurrentMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Log(org.apache.commons.logging.Log) SoftReference(java.lang.ref.SoftReference) LogFactory(org.apache.commons.logging.LogFactory) Timer(org.polymap.core.runtime.Timer) Random(java.util.Random)

Example 20 with Timer

use of org.polymap.core.runtime.Timer in project polymap4-core by Polymap4.

the class AbstractLoginDialog method handle.

public void handle(final Callback[] callbacks) throws IOException {
    this.callbackArray = callbacks;
    final Display display = Display.getDefault();
    display.syncExec(new Runnable() {

        public void run() {
            isCancelled = false;
            setBlockOnOpen(false);
            open();
            final Button okButton = getButton(IDialogConstants.OK_ID);
            okButton.setText("Login");
            okButton.addSelectionListener(new SelectionAdapter() {

                public void widgetSelected(final SelectionEvent event) {
                    processCallbacks = true;
                }
            });
            final Button cancel = getButton(IDialogConstants.CANCEL_ID);
            cancel.addSelectionListener(new SelectionAdapter() {

                public void widgetSelected(final SelectionEvent event) {
                    isCancelled = true;
                    processCallbacks = true;
                }
            });
        }
    });
    try {
        // Works for now.
        ModalContext.setAllowReadAndDispatch(true);
        ModalContext.run(new IRunnableWithProgress() {

            public void run(final IProgressMonitor monitor) {
                Timer start = new Timer();
                // (in the loginSucceeded event).
                while (!processCallbacks) {
                    // prevent deadlock in UIThread
                    if (start.elapsedTime() > 60000) {
                        System.out.println("No login. Refreshing...");
                        // XXX
                        System.err.println("No: new PolymapWorkbench.Terminator().schedule();");
                        return;
                    }
                    try {
                        Thread.sleep(100);
                    } catch (final Exception e) {
                    // do nothing
                    }
                }
                processCallbacks = false;
                // Call the adapter to handle the callbacks
                if (!isCancelled())
                    internalHandle();
            }
        }, true, new NullProgressMonitor(), Display.getDefault());
    } catch (final Exception e) {
        final IOException ioe = new IOException();
        ioe.initCause(e);
        throw ioe;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Timer(org.polymap.core.runtime.Timer) Button(org.eclipse.swt.widgets.Button) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IOException(java.io.IOException) IOException(java.io.IOException) Display(org.eclipse.swt.widgets.Display) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Aggregations

Timer (org.polymap.core.runtime.Timer)24 IOException (java.io.IOException)4 IRecordState (org.polymap.recordstore.IRecordState)4 ResultSet (org.polymap.recordstore.ResultSet)4 Random (java.util.Random)3 Iterator (java.util.Iterator)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)2 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)2 GridEnvelope (org.opengis.coverage.grid.GridEnvelope)2 Filter (org.opengis.filter.Filter)2 RecordQuery (org.polymap.recordstore.RecordQuery)2 SimpleQuery (org.polymap.recordstore.SimpleQuery)2 LuceneRecordQuery (org.polymap.recordstore.lucene.LuceneRecordQuery)2 Auth (io.milton.http.Auth)1 Request (io.milton.http.Request)1 Response (io.milton.http.Response)1 RenderedImage (java.awt.image.RenderedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1