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);
}
}
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);
}
}
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);
}
}
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");
}
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;
}
}
Aggregations