X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FMultiReadEntry.java;h=77e3b8d0fc0bd592b9b85c9ea49d34b3b64bcb79;hp=a9f9958c7e5137be1746ad8d9abdcb375c0a45d2;hb=c4d9561b1b35a0e8e594158fbb01a9c632997808;hpb=969bd23cab98a79ca9101af33334000879fb60c5 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 index a9f9958c7..77e3b8d0f 100644 --- 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 @@ -1,240 +1,213 @@ -/******************************************************************************* - * 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; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2018 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.ReadGraph; +import org.simantics.db.common.exception.DebugException; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.procedure.AsyncMultiProcedure; +import org.simantics.db.procedure.SyncMultiProcedure; +import org.simantics.db.request.MultiRead; +import org.simantics.db.request.RequestFlags; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class MultiReadEntry extends CacheEntryBase> { + + private static final Logger LOGGER = LoggerFactory.getLogger(MultiReadEntry.class); + + protected MultiRead id; + + MultiReadEntry(MultiRead request) { + this.id = request; + } + + @Override + int makeHash() { + return id.hashCode(); + } + + @Override + public Object getOriginalRequest() { + return id; + } + + @Override + public void discard() { + super.discard(); + id = null; + setResult(null); + } + + synchronized public void finish(ReadGraph graph) { + + assert(isPending()); + + synchronized(this) { + setReady(); + } + + } + + @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) { + + try { + + id.perform(graph , new SyncMultiProcedure() { + + @Override + public void execute(ReadGraph graph, T result) { + addOrSet(result); + } + + public void finished(ReadGraph graph) { + finish(graph); + }; + + @Override + public void exception(ReadGraph graph, Throwable t) { + except(t); + } + + }); + + } catch (Throwable t) { + except(t); + if(DebugException.DEBUG) new DebugException(t).printStackTrace(); + } + + } + + @Override + public void removeEntry(QueryProcessor processor) { + processor.cache.remove(MultiReadEntry.this); + } + + @Override + public int type() { + return RequestFlags.INVALIDATE; + } + + @Override + public String toString() { + if(id == null) return "DISCARDED"; + else return id.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) { + LOGGER.error("performFromCache proc.exception failed", t); + } +// parentBarrier.dec(); + + } else { + + final ArrayList values = (ArrayList)getResult(); + for(T value : values) { + try { + proc.execute(graph, value); + } catch (Throwable t) { + LOGGER.error("performFromCache proc.execute failed", t); + } + } + + try { + proc.finished(graph); + } catch (Throwable t) { + LOGGER.error("performFromCache proc.finished failed", t); + } +// parentBarrier.dec(); + + } + + } + + @Override + public Object performFromCache(ReadGraphImpl graph, SyncMultiProcedure proc) { + + if(isExcepted()) { + + try { + proc.exception(graph, (Throwable)getResult()); + } catch (Throwable t) { + LOGGER.error("performFromCache(Sync) proc.exception failed", t); + } + + } else { + + final ArrayList values = (ArrayList)getResult(); + for(T value : values) { + try { + proc.execute(graph, value); + } catch (Throwable t) { + LOGGER.error("performFromCache(Sync) proc.execute failed", t); + } + } + + try { + proc.finished(graph); + } catch (Throwable t) { + LOGGER.error("performFromCache(Sync) proc.finished failed", t); + } + + } + + return null; + + } + + @Override + public String toString() { + if(id == null) return "DISCARDED"; + else return id.toString() + statusOrException; + } + + public Object compute(ReadGraphImpl graph, SyncMultiProcedure procedure) throws DatabaseException { + return graph.processor.cache.performQuery(graph, id, this, procedure); + } + +}