/******************************************************************************* * 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.graph.ReadGraphImpl; import org.simantics.db.impl.graph.WriteGraphImpl; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.request.Read; import org.simantics.db.request.ReadExt; import org.simantics.db.request.RequestFlags; final public class ReadEntry extends CacheEntryBase> { protected Read request; public ReadEntry(Read request) { this.request = request; } @Override int makeHash() { return request.hashCode(); } @Override public Object getOriginalRequest() { return request; } @Override public void discard() { super.discard(); setResult(null); } final public Object addOrSet(AsyncReadGraph graph, Object result) { assert(assertPending()); setResult(result); setReady(); return result; } @Override final public Query getQuery() { return new Query() { @Override public void recompute(ReadGraphImpl graph) { try { T result = request.perform(graph); addOrSet(graph, result); } catch (Throwable t) { except(t); } } @Override public void removeEntry(QueryProcessor processor) { processor.cache.remove(ReadEntry.this); } @Override public int type() { if(request instanceof ReadExt) { return ((ReadExt)request).getType(); } else { return RequestFlags.INVALIDATE; } } @Override public String toString() { if(request == null) return "DISCARDED"; else return request.toString() + statusOrException; } }; } @Override public Object compute(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { ReadGraphImpl queryGraph = graph.withParent(this); try { addOrSet(queryGraph, request.perform(queryGraph)); return get(queryGraph, procedure); } catch (DatabaseException e) { except(e); return get(graph, procedure); } catch (Throwable t) { except(new DatabaseException(t)); return get(graph, procedure); } } public static void computeForEach(ReadGraphImpl graph, Read request, ReadEntry entry, AsyncProcedure procedure) throws DatabaseException { ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph; try { T result = request.perform(queryGraph); if(entry != null) entry.addOrSet(queryGraph, result); procedure.execute(graph, result); } catch (DatabaseException e) { if(entry != null) entry.except(e); procedure.exception(graph, e); } catch (Throwable t) { DatabaseException dbe = new DatabaseException(t); if(entry != null) entry.except(dbe); procedure.exception(graph, dbe); } } public Object performFromCache(ReadGraphImpl graph, AsyncProcedure procedure) { AsyncProcedure proc = (AsyncProcedure)procedure; if(proc != null) { 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 (T)getResult(); } @Override public String toString() { if(request == null) return "DISCARDED"; else return request.toString() + " - " + statusOrException; } public Object get(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { if(procedure != null) performFromCache(graph, procedure); checkAndThrow(); return getResult(); } @Override boolean isImmutable(ReadGraphImpl graph) throws DatabaseException { if(request instanceof ReadExt) { return ((ReadExt)request).isImmutable(graph); } return false; } }