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%2FAsyncMultiReadEntry.java;h=ea88d179107c920f9f10193f727907a56d39356e;hp=9e4f49dbf9dc5ccff8aa1052c0d2eea47fef0896;hb=c4d9561b1b35a0e8e594158fbb01a9c632997808;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java index 9e4f49dbf..ea88d1791 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java @@ -1,229 +1,193 @@ -/******************************************************************************* - * 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.procedure.AsyncMultiProcedure; -import org.simantics.db.request.AsyncMultiRead; -import org.simantics.db.request.RequestFlags; - -final public class AsyncMultiReadEntry extends CacheEntryBase { - -// public ArrayList> procs = null; - - protected AsyncMultiRead request; - - public AsyncMultiReadEntry(AsyncMultiRead 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); - } - - final synchronized public void finish(AsyncReadGraph graph) { - -// new Exception("finish " + this).printStackTrace(); - - if(!isPending()) { - System.err.println("aff"); - } - - assert(isPending()); - -// ArrayList> p = null; - - synchronized(this) { - - setReady(); -// p = procs; -// procs = null; - - } - -// if(p != null) { -// ArrayList v = (ArrayList)getResult(); -// if(v != null) { -// for(AsyncMultiProcedure proc : p) { -// for(T t : v) proc.execute(graph, t); -// } -// } -// -// for(AsyncMultiProcedure proc : p) proc.finished(graph); -// } - - } - - final synchronized public void except(AsyncReadGraph graph, Throwable t) { - - assert(isPending()); - -// ArrayList> p = null; - - synchronized(this) { - - except(t); -// p = procs; -// procs = null; - - } - -// if(p != null) { -// for(AsyncMultiProcedure proc : p) proc.exception(graph, t); -// } - - } - - final synchronized public void addOrSet(Object item) { - - assert(isPending()); - - ArrayList value = (ArrayList)getResult(); - value.add((T)item); - - } - - @Override - public void clearResult(QuerySupport support) { - setResult(new ArrayList()); - } - - @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); - - try { - - request.perform(parentGraph , new AsyncMultiProcedure() { - - @Override - public void execute(AsyncReadGraph graph, T result) { - addOrSet(result); - } - - public void finished(AsyncReadGraph graph) { - finish(graph); - }; - - @Override - public void exception(AsyncReadGraph graph, Throwable t) { - except(t); - } - - }); - - } catch (Throwable t) { - except(t); - if(DebugException.DEBUG) new DebugException(t).printStackTrace(); - } - - } - - @Override - public void removeEntry(QueryProcessor processor) { - processor.asyncMultiReadMap.remove(request); - } - - @Override - public int type() { - return RequestFlags.INVALIDATE; - } - - @Override - public String toString() { - if(request == null) return "DISCARDED"; - else return request.toString() + statusOrException; - } - - }; - - } - - @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 void performFromCache(int callerThread, Object provider, -// Object procedure) { -// -// QueryProvider2 queryProvider = (QueryProvider2)provider; -// ReadGraphImpl graph = ReadGraphImpl.forFromCache(callerThread, null, new ReadGraphSupportImpl(null, queryProvider, null)); -// performFromCache(graph, provider, procedure); -// -// } - - @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.exception.DatabaseException; +import org.simantics.db.impl.BlockingAsyncMultiProcedure; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.procedure.AsyncMultiProcedure; +import org.simantics.db.request.AsyncMultiRead; +import org.simantics.db.request.RequestFlags; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final public class AsyncMultiReadEntry extends CacheEntryBase> { + + private static final Logger LOGGER = LoggerFactory.getLogger(AsyncMultiReadEntry.class); + + protected AsyncMultiRead id; + + AsyncMultiReadEntry(AsyncMultiRead 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); + } + + final synchronized public void finish(AsyncReadGraph graph) { + + assert(isPending()); + + synchronized(this) { + setReady(); + } + + } + + final synchronized public void except(AsyncReadGraph graph, Throwable t) { + + assert(isPending()); + + synchronized(this) { + except(t); + } + + } + + @SuppressWarnings("unchecked") + final synchronized public void addOrSet(Object item) { + + assert(isPending()); + + ArrayList value = (ArrayList)getResult(); + value.add((T)item); + + } + + @Override + public void clearResult(QuerySupport support) { + setResult(new ArrayList()); + } + + @Override + final public Query getQuery() { + + return new Query() { + + @Override + public void recompute(ReadGraphImpl graph) { + + try { + + BlockingAsyncMultiProcedure proc = new BlockingAsyncMultiProcedure<>(graph.asyncBarrier, graph, new AsyncMultiProcedure() { + + @Override + public void execute(AsyncReadGraph graph, T result) { + addOrSet(result); + } + + public void finished(AsyncReadGraph graph) { + finish(graph); + }; + + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + except(t); + } + + }, id); + + id.perform(graph , proc); + + proc.get(); + + } catch (Throwable t) { + + except(t); + + } + + } + + @Override + public void removeEntry(QueryProcessor processor) { + processor.cache.remove(AsyncMultiReadEntry.this); + } + + @Override + public int type() { + return RequestFlags.INVALIDATE; + } + + @Override + public String toString() { + if(id == null) return "DISCARDED"; + else return id.toString() + statusOrException; + } + + }; + + } + + @SuppressWarnings("unchecked") + @Override + public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure proc) { + + if(isExcepted()) { + + try { + proc.exception(graph, (Throwable)getResult()); + } catch (Throwable t) { + LOGGER.error("performFromCache 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 proc.execute failed", t); + } + } + + try { + proc.finished(graph); + } catch (Throwable t) { + LOGGER.error("performFromCache proc.finished failed", t); + } + + } + + return getResult(); + + } + + @Override + public String toString() { + if(id == null) return "DISCARDED"; + else return id.toString() + statusOrException; + } + + public Object compute(ReadGraphImpl graph, AsyncMultiProcedure procedure) throws DatabaseException { + return graph.processor.cache.performQuery(graph, id, this, procedure); + } + +}