X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FMultiReadEntry.java;fp=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FMultiReadEntry.java;h=a9f9958c7e5137be1746ad8d9abdcb375c0a45d2;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java new file mode 100644 index 000000000..a9f9958c7 --- /dev/null +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java @@ -0,0 +1,240 @@ +/******************************************************************************* + * 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 java.util.ArrayList; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.common.exception.DebugException; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier; +import org.simantics.db.procedure.AsyncMultiProcedure; +import org.simantics.db.request.MultiRead; +import org.simantics.db.request.RequestFlags; +import org.simantics.utils.datastructures.Pair; + +final public class MultiReadEntry extends CacheEntryBase { + +// public ArrayList, AsyncBarrier>> procs; + + protected MultiRead request; + + public MultiReadEntry(MultiRead request) { + this.request = request; + } + + @Override + int makeHash() { + return request.hashCode(); + } + + @Override + public Object getOriginalRequest() { + return request; + } + + @Override + public void discard() { + super.discard(); + request = null; + setResult(null); + } + + synchronized public void finish(AsyncReadGraph graph) { + + assert(isPending()); + + ArrayList, AsyncBarrier>> p = null; + + synchronized(this) { + + setReady(); + +// p = procs; +// procs = null; + + } + +// if(p != null) { +// ArrayList v = (ArrayList)getResult(); +// if(v != null) { +// for(Pair, AsyncBarrier> pair : p) { +// for(T t : v) pair.first.execute(graph, t); +// } +// } +// for(Pair, AsyncBarrier> pair : p) { +// pair.first.finished(graph); +// pair.second.dec(); +// } +// } + + } + + @Override + final public void clearResult(QuerySupport support) { + setResult(new ArrayList()); + } + + final synchronized public void addOrSet(Object item) { + + assert(isPending()); + + ArrayList value = (ArrayList)getResult(); + value.add((T)item); + + } + + @Override + final public Query getQuery() { + + return new Query() { + + @Override + public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) { + + QueryProcessor qp = (QueryProcessor)provider; + + final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); +// parentGraph.state.barrier.inc(); + + try { + + request.perform(parentGraph , new AsyncMultiProcedure() { + + @Override + public void execute(AsyncReadGraph graph, T result) { + addOrSet(result); +// parentGraph.state.barrier.dec(); + } + + public void finished(AsyncReadGraph graph) { + finish(graph); +// parentGraph.state.barrier.dec(); + }; + + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + except(t); +// parentGraph.state.barrier.dec(); + } + + }); + +// parentGraph.waitAsync(request); + + } catch (Throwable t) { + except(t); +// parentGraph.state.barrier.dec(); + if(DebugException.DEBUG) new DebugException(t).printStackTrace(); + } + + } + + @Override + public void removeEntry(QueryProcessor processor) { + processor.multiReadMap.remove(request); + } + + @Override + public int type() { + return RequestFlags.INVALIDATE; + } + + @Override + public String toString() { + if(request == null) return "DISCARDED"; + else return request.toString() + statusOrException; + } + + }; + + } + + public void performFromCache(AsyncReadGraph graph, Object provider, Object procedure) { + + AsyncMultiProcedure proc = (AsyncMultiProcedure)procedure; + + if(isExcepted()) { + + try { + proc.exception(graph, (Throwable)getResult()); + } catch (Throwable t) { + t.printStackTrace(); + } +// parentBarrier.dec(); + + } else { + + final ArrayList values = (ArrayList)getResult(); + for(T value : values) { + try { + proc.execute(graph, value); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + try { + proc.finished(graph); + } catch (Throwable t) { + t.printStackTrace(); + } +// parentBarrier.dec(); + + } + + } + + @Override + public void performFromCache(ReadGraphImpl graph, Object provider, + Object procedure) { + + final AsyncMultiProcedure proc = (AsyncMultiProcedure)procedure; + + if(isExcepted()) { + + try { + proc.exception(graph, (Throwable)getResult()); + } catch (Throwable t) { + t.printStackTrace(); + } + + } else { + + final ArrayList values = (ArrayList)getResult(); + for(T value : values) { + try { + proc.execute(graph, value); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + try { + proc.finished(graph); + } catch (Throwable t) { + t.printStackTrace(); + } + + } + + + + } + + @Override + public String toString() { + if(request == null) return "DISCARDED"; + else return request.toString() + statusOrException; + } + +}