/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.db.impl.query; import org.simantics.db.AsyncReadGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.DebugPolicy; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.request.AsyncRead; final public class AsyncReadEntry extends CacheEntryBase> { protected AsyncRead request; AsyncReadEntry(AsyncRead request) { this.request = request; if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this); } @Override int makeHash() { return request.hashCode(); } @Override public Object getOriginalRequest() { return request; } @Override public void discard() { super.discard(); setResult(null); } final public void addOrSet(AsyncReadGraph graph, Object item) { // System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item); assert(isPending()); synchronized(this) { setResult(item); setReady(); } } public void except(AsyncReadGraph graph, Throwable t) { assert(isPending()); synchronized(this) { except(t); } } @Override final public Query getQuery() { return new Query() { @Override public void recompute(ReadGraphImpl graph) { try { request.perform(graph , new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, T result) { addOrSet(graph, result); } @Override public void exception(AsyncReadGraph graph, Throwable t) { except(t); } }); } catch (Throwable t) { except(t); } } @Override public void removeEntry(QueryProcessor qp) { qp.cache.remove(AsyncReadEntry.this); } @Override public int type() { return request.getFlags(); } @Override public String toString() { if(request == null) return "DISCARDED"; else if(isExcepted()) return request.toString() + " " + getResult(); else return request.toString() + " " + statusOrException; } }; } @Override public Object performFromCache(ReadGraphImpl graph, AsyncProcedure proc) { if(isExcepted()) { try { proc.exception(graph, (Throwable)getResult()); } catch (Throwable t) { t.printStackTrace(); } } else { try { proc.execute(graph, (T)getResult()); } catch (Throwable t) { t.printStackTrace(); } } return getResult(); } //@Override public Object compute(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { ReadGraphImpl queryGraph = graph.withParent(this); request.perform(queryGraph, new AsyncProcedure() { @Override public void execute(AsyncReadGraph returnGraph, T result) { ReadGraphImpl impl = (ReadGraphImpl)returnGraph; AsyncReadEntry.this.addOrSet(graph, result); try { procedure.execute(graph, result); } catch (Throwable t) { t.printStackTrace(); } // parentBarrier.dec(query); } @Override public void exception(AsyncReadGraph returnGraph, Throwable t) { ReadGraphImpl impl = (ReadGraphImpl)returnGraph; // AsyncReadGraph resumeGraph = finalParentGraph.newAsync(); AsyncReadEntry.this.except(graph, t); try { procedure.exception(graph, t); } catch (Throwable t2) { t2.printStackTrace(); } // parentBarrier.dec(query); } @Override public String toString() { return procedure.toString(); } }); return getResult(); } public static void computeForEach(ReadGraphImpl parentGraph, AsyncRead request, AsyncReadEntry entry, AsyncProcedure procedure) throws DatabaseException { ReadGraphImpl queryGraph = parentGraph.withParent(entry); request.perform(queryGraph, new AsyncProcedure() { @Override public void execute(AsyncReadGraph returnGraph, T result) { ReadGraphImpl impl = (ReadGraphImpl)returnGraph; if(entry != null) entry.addOrSet(parentGraph, result); try { procedure.execute(parentGraph, result); } catch (Throwable t) { t.printStackTrace(); } } @Override public void exception(AsyncReadGraph returnGraph, Throwable t) { ReadGraphImpl impl = (ReadGraphImpl)returnGraph; if(entry != null) entry.except(parentGraph, t); try { procedure.exception(parentGraph, t); } catch (Throwable t2) { t2.printStackTrace(); } } @Override public String toString() { return procedure.toString(); } }); } @Override public String toString() { if(isDiscarded()) return "DISCARDED " + request.toString(); else if(isExcepted()) return request.toString() + " " + getResult(); else return request.toString() + " " + statusOrException; } }