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=8a8b18b7dda5641113207102d8c43f54c5608716;hb=c4d9561b1b35a0e8e594158fbb01a9c632997808;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 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 8a8b18b7d..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,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * 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 @@ -14,93 +14,64 @@ 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.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 { +final public class AsyncMultiReadEntry extends CacheEntryBase> { -// public ArrayList> procs = null; + private static final Logger LOGGER = LoggerFactory.getLogger(AsyncMultiReadEntry.class); - protected AsyncMultiRead request; + protected AsyncMultiRead id; - public AsyncMultiReadEntry(AsyncMultiRead request) { - this.request = request; + AsyncMultiReadEntry(AsyncMultiRead request) { + this.id = request; } @Override int makeHash() { - return request.hashCode(); + return id.hashCode(); } @Override public Object getOriginalRequest() { - return request; + return id; } @Override public void discard() { super.discard(); - request = null; + id = 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) { + @SuppressWarnings("unchecked") + final synchronized public void addOrSet(Object item) { assert(isPending()); @@ -120,42 +91,43 @@ final public class AsyncMultiReadEntry extends CacheEntryBase { return new Query() { @Override - public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) { - - QueryProcessor qp = (QueryProcessor)provider; - - final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); + public void recompute(ReadGraphImpl graph) { try { - request.perform(parentGraph , new AsyncMultiProcedure() { + 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); - } + @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); - if(DebugException.DEBUG) new DebugException(t).printStackTrace(); - } + + except(t); + + } } @Override public void removeEntry(QueryProcessor processor) { - processor.asyncMultiReadMap.remove(request); + processor.cache.remove(AsyncMultiReadEntry.this); } @Override @@ -165,65 +137,57 @@ final public class AsyncMultiReadEntry extends CacheEntryBase { @Override public String toString() { - if(request == null) return "DISCARDED"; - else return request.toString() + statusOrException; + if(id == null) return "DISCARDED"; + else return id.toString() + statusOrException; } }; } + @SuppressWarnings("unchecked") @Override - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { - - final AsyncMultiProcedure proc = (AsyncMultiProcedure)procedure; + public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure proc) { if(isExcepted()) { try { proc.exception(graph, (Throwable)getResult()); } catch (Throwable t) { - t.printStackTrace(); + 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) { - t.printStackTrace(); + LOGGER.error("performFromCache proc.execute failed", t); } } try { proc.finished(graph); } catch (Throwable t) { - t.printStackTrace(); + LOGGER.error("performFromCache proc.finished failed", t); } } - + return getResult(); } - -// @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; + 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); } }