X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fgraph%2FReadGraphImpl.java;h=25438ef1ef3f0d57878c32c3c03def4111c8ca0c;hb=1fb7e00de0895266e08097371c6f84e4322d6530;hp=b853ed5191e3654f5cfbddea240b33c48b77bff1;hpb=56a799c9b7d395cefb59e101cd0f7ce8d68f88e6;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java index b853ed519..25438ef1e 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java @@ -137,6 +137,7 @@ import org.simantics.db.impl.procedure.CallWrappedSingleQueryProcedure4; import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4; import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4; import org.simantics.db.impl.query.CacheEntry; +import org.simantics.db.impl.query.QueryCache; import org.simantics.db.impl.query.QueryProcessor; import org.simantics.db.impl.query.QuerySupport; import org.simantics.db.impl.query.TripleIntProcedure; @@ -283,7 +284,7 @@ public class ReadGraphImpl implements ReadGraph { } catch (ResourceNotFoundException e) { - throw new ResourceNotFoundException(e); + throw new ResourceNotFoundException(id, e); } catch (ValidationException e) { @@ -347,7 +348,7 @@ public class ReadGraphImpl implements ReadGraph { } catch (ResourceNotFoundException e) { - throw new ResourceNotFoundException(e); + throw new ResourceNotFoundException(id, e); } catch (ServiceException e) { @@ -900,83 +901,57 @@ public class ReadGraphImpl implements ReadGraph { if( relation == null) throw new IllegalArgumentException("relation can not be null"); try { - int single = processor.getSingleObject(this, subject, relation); - if(single == 0) throw new NoSingleResultException("subject=" + subject + " relation="+relation); + if (single == 0) { + if (EMPTY_RESOURCE_CHECK) { + if (!hasStatement(subject)) { + throw new EmptyResourceException("Resource " + debugString(subject)); + } + } + throw new NoSingleResultException("No single object for subject " + debugString(subject) + + " and relation " + debugString(relation), single); + } return processor.querySupport.getResource(single); - } catch (NoSingleResultException e) { - - if(EMPTY_RESOURCE_CHECK) { - if(!hasStatement(subject)) throw new EmptyResourceException("Resource " + debugString(subject)); - } - - throw new NoSingleResultException("No single object for subject " - + debugString(subject) + " and relation " - + debugString(relation), e); - + throw e; } catch (DatabaseException e) { - throw new ServiceException(e); - } - } @Override - final public Statement getSingleStatement(final Resource subject, - final Resource relation) throws NoSingleResultException, - ManyObjectsForFunctionalRelationException, ServiceException { - + final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException { assert (subject != null); assert (relation != null); - try { - Collection statements = getStatements(subject, relation); - if(statements.size() == 1) return statements.iterator().next(); - else throw new NoSingleResultException(""); - - } catch (NoSingleResultException e) { - - if(EMPTY_RESOURCE_CHECK) { - if(!hasStatement(subject)) throw new EmptyResourceException("Resource " + debugString(subject)); - } - - throw new NoSingleResultException("No single statement for subject " - + debugString(subject) + " and relation " - + debugString(relation), e); - - } catch (DatabaseException e) { - - throw new ServiceException(e); - - } - + if (statements.size() == 1) { + return statements.iterator().next(); + } else { + if (EMPTY_RESOURCE_CHECK) + if (!hasStatement(subject)) + throw new EmptyResourceException("Resource " + debugString(subject)); + throw new NoSingleResultException("No single statement for subject " + debugString(subject) + + " and relation " + debugString(relation), statements.size()); + } + } catch (ServiceException e) { + throw new ServiceException(e); + } } @Override - final public Resource getSingleType(final Resource subject) throws NoSingleResultException, - ServiceException { - + final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException { assert (subject != null); - try { - ArrayList principalTypes = (ArrayList)getPrincipalTypes(subject); - if(principalTypes.size() == 1) return principalTypes.get(0); - else throw new NoSingleResultException(""); - - } catch (NoSingleResultException e) { - - throw new NoSingleResultException(e); - + if (principalTypes.size() == 1) { + return principalTypes.get(0); + } else { + throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size()); + } } catch (ServiceException e) { - throw new ServiceException(e); - } - } @Override @@ -988,23 +963,10 @@ public class ReadGraphImpl implements ReadGraph { assert (baseType != null); try { - return syncRequest(new SingleType(subject, baseType)); - - } catch (NoSingleResultException e) { - - throw new NoSingleResultException(new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, e)); - - } catch (ServiceException e) { - - throw new ServiceException(e); - } catch (DatabaseException e) { - - throw new ServiceException(INTERNAL_ERROR_STRING, e); - + throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e); } - } @Override @@ -1142,16 +1104,11 @@ public class ReadGraphImpl implements ReadGraph { assert (relation != null); try { - Resource object = getSingleObject(subject, relation); return getValue(object); - } catch (NoSingleResultException e) { - - throw new NoSingleResultException(e); - + throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e); } catch (DoesNotContainValueException e) { - try { Layer0 L0 = processor.getL0(this); Resource object = getPossibleObject(subject, relation); @@ -1159,23 +1116,19 @@ public class ReadGraphImpl implements ReadGraph { if(isInstanceOf(object, L0.Literal)) { throw new DoesNotContainValueException(e); } else { - throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)"); + throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e); } } else { - throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value"); + throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e); } } catch (DoesNotContainValueException e2) { throw e2; } catch (DatabaseException e2) { throw new InternalException("The client failed to analyse the cause of the following exception", e); } - } catch (ServiceException e) { - throw new ServiceException(e); - - } - + } } @Override @@ -1186,16 +1139,11 @@ public class ReadGraphImpl implements ReadGraph { assert (relation != null); try { - Resource object = getSingleObject(subject, relation); return getVariantValue(object); - } catch (NoSingleResultException e) { - - throw new NoSingleResultException(e); - + throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e); } catch (DoesNotContainValueException e) { - try { Layer0 L0 = processor.getL0(this); Resource object = getPossibleObject(subject, relation); @@ -1203,23 +1151,19 @@ public class ReadGraphImpl implements ReadGraph { if(isInstanceOf(object, L0.Literal)) { throw new DoesNotContainValueException(e); } else { - throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)"); + throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e); } } else { - throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value"); + throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e); } } catch (DoesNotContainValueException e2) { throw e2; } catch (DatabaseException e2) { throw new InternalException("The client failed to analyse the cause of the following exception", e); } - } catch (ServiceException e) { - throw new ServiceException(e); - } - } @Override @@ -1230,40 +1174,23 @@ public class ReadGraphImpl implements ReadGraph { assert (relation != null); try { - Resource object = getSingleObject(subject, relation); return getValue(object, binding); - } catch (NoSingleResultException e) { - String message = ""; - try { - String subjectName = NameUtils.getSafeName(this, subject, true); String relationName = NameUtils.getSafeName(this, relation, true); message = "Subject: " + subjectName + ", Relation: " + relationName; - } catch (DatabaseException e2) { } - - throw new NoSingleResultException(message); - + throw new NoSingleResultException(message, e.getResultCount(), e); } catch (DoesNotContainValueException e) { - throw new DoesNotContainValueException(e); - } catch (ServiceException e) { - throw new ServiceException(e); - - } catch (DatabaseException e) { - - throw new ServiceException(INTERNAL_ERROR_STRING, e); - } - } @Override @@ -1949,7 +1876,7 @@ public class ReadGraphImpl implements ReadGraph { } - final AsyncProcedure NONE = new AsyncProcedure() { + final AsyncProcedure NONE = new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, Object result) { @@ -1970,35 +1897,38 @@ public class ReadGraphImpl implements ReadGraph { assert (request != null); - if (parent != null) { - - try { - return processor.queryRead(this, request, parent, null, null); - } catch (Throwable e) { - if(e instanceof DatabaseException) throw (DatabaseException)e; - else throw new DatabaseException(e); - } - - } else { - - try { - - return processor.tryQuery(this, request); - - } catch (Throwable throwable) { - - //Logger.defaultLogError("Internal read request failure", throwable); - - if (throwable instanceof DatabaseException) - throw (DatabaseException) throwable; - else - throw new DatabaseException( - "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)", - throwable); - - } + return QueryCache.resultReadEntry(this, request, parent, null, null); + + //return processor.query(this, request, parent, null, null); - } +// if (parent != null) { +// +// try { +// } catch (Throwable e) { +// if(e instanceof DatabaseException) throw (DatabaseException)e; +// else throw new DatabaseException(e); +// } +// +// } else { +// +// try { +// +// return processor.tryQuery(this, request); +// +// } catch (Throwable throwable) { +// +// //Logger.defaultLogError("Internal read request failure", throwable); +// +// if (throwable instanceof DatabaseException) +// throw (DatabaseException) throwable; +// else +// throw new DatabaseException( +// "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)", +// throwable); +// +// } +// +// } } @@ -2021,42 +1951,46 @@ public class ReadGraphImpl implements ReadGraph { ListenerBase listener = procedure != null ? getListenerBase(procedure) : null; - if (parent != null || listener != null) { - - try { - return processor.queryRead(this, request, parent, procedure, listener); - } catch (Throwable e) { - if(e instanceof DatabaseException) throw (DatabaseException)e; - else throw new DatabaseException(e); - } - - } else { - - try { - - T t = processor.tryQuery(this, request); - if(procedure != null) - procedure.execute(this, t); - - return t; - - } catch (Throwable throwable) { - - Logger.defaultLogError("Internal read request failure", throwable); - - if(procedure != null) - procedure.exception(this, throwable); - - if (throwable instanceof DatabaseException) - throw (DatabaseException) throwable; - else - throw new DatabaseException( - "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)", - throwable); - - } + return QueryCache.resultReadEntry(this, request, parent, listener, procedure); + +// return processor.query(this, request, parent, procedure, listener); + - } +// if (parent != null || listener != null) { +// +// try { +// } catch (Throwable e) { +// if(e instanceof DatabaseException) throw (DatabaseException)e; +// else throw new DatabaseException(e); +// } +// +// } else { +// +// try { +// +// T t = processor.tryQuery(this, request); +// if(procedure != null) +// procedure.execute(this, t); +// +// return t; +// +// } catch (Throwable throwable) { +// +// Logger.defaultLogError("Internal read request failure", throwable); +// +// if(procedure != null) +// procedure.exception(this, throwable); +// +// if (throwable instanceof DatabaseException) +// throw (DatabaseException) throwable; +// else +// throw new DatabaseException( +// "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)", +// throwable); +// +// } +// +// } } @@ -2145,71 +2079,73 @@ public class ReadGraphImpl implements ReadGraph { assert (request != null); - // System.out.println("syncRequest " + request + " syncParent=" + - // syncParent); - ListenerBase listener = getListenerBase(procedure); - if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) { - - Object syncParent = request; - -// final ReadGraphImpl newGraph = newSync(); - - final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( - procedure, request); - - processor.query(this, request, parent, wrapper, listener); - -// newGraph.waitAsync(syncParent); - - Throwable e = wrapper.getException(); - if (e != null) { - // The request was async - produce meaningful stack trace by - // wrapping - if (e instanceof DatabaseException) - throw (DatabaseException) e; - else - throw new DatabaseException(e); - } - - return wrapper.getResult(); - - } else { - - // System.out.println("direct call " + request ); - - // Do not set the sync state.parent for external threads - Object syncParent = request; - -// final ReadGraphImpl newGraph = newSync(); - - final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( - procedure, request); - - try { - - processor.tryQuery(this, request, wrapper); - - } catch (Throwable t) { - - wrapper.exception(this, t); - - } - - Throwable e = wrapper.getException(); - if (e != null) { - // The request was async - produce meaningful stack trace by - // wrapping - if (e instanceof DatabaseException) - throw (DatabaseException) e; - else - throw new DatabaseException(e); - } - - return wrapper.getResult(); + final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( + procedure, request); + + QueryCache.runnerAsyncReadEntry(this, request, parent, listener, wrapper); + + //processor.query(this, request, parent, wrapper, listener); + + return wrapper.getResult(); - } +// if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) { +// +//// Object syncParent = request; +// +//// final ReadGraphImpl newGraph = newSync(); +// +// +//// newGraph.waitAsync(syncParent); +// +// Throwable e = wrapper.getException(); +// if (e != null) { +// // The request was async - produce meaningful stack trace by +// // wrapping +// if (e instanceof DatabaseException) +// throw (DatabaseException) e; +// else +// throw new DatabaseException(e); +// } +// +// return wrapper.getResult(); +// +// } else { +// +// // System.out.println("direct call " + request ); +// +// // Do not set the sync state.parent for external threads +//// Object syncParent = request; +// +//// final ReadGraphImpl newGraph = newSync(); +// +// final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( +// procedure, request); +// +// try { +// +// processor.tryQuery(this, request, wrapper); +// +// } catch (Throwable t) { +// +// wrapper.exception(this, t); +// +// } +// +// Throwable e = wrapper.getException(); +// if (e != null) { +// // The request was async - produce meaningful stack trace by +// // wrapping +// if (e instanceof DatabaseException) +// throw (DatabaseException) e; +// else +// throw new DatabaseException(e); +// } +// +// return wrapper.getResult(); +// +// } } @@ -2217,40 +2153,13 @@ public class ReadGraphImpl implements ReadGraph { assert (request != null); - // System.out.println("syncRequest " + request + " syncParent=" + - // syncParent); - ListenerBase listener = getListenerBase(procedure); + assert(listener == null); - if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) { - -// final ReadGraphImpl newGraph = newSync(); - - final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( - procedure, request); - - processor.query(this, request, parent, wrapper, listener); - - } else { - - try { +// final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( +// procedure, request); -// final ReadGraphImpl newGraph = newSync(); - processor.tryQuery(this, request, procedure); -// newGraph.waitAsync(null); - waitAsyncProcedure(procedure); - - } catch (Throwable t) { - if(Development.DEVELOPMENT) { - if(Development.getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) { - t.printStackTrace(); - } - } - procedure.exception(this, t); - waitAsyncProcedure(procedure); - } - - } + QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure); } @@ -2342,7 +2251,7 @@ public class ReadGraphImpl implements ReadGraph { if (parent != null || listener != null) { - Object syncParent = request; +// Object syncParent = request; // final ReadGraphImpl newGraph = newSync(); @@ -2352,7 +2261,7 @@ public class ReadGraphImpl implements ReadGraph { } else { - Object syncParent = request; +// Object syncParent = request; // final ReadGraphImpl newGraph = newSync(); @@ -2471,7 +2380,7 @@ public class ReadGraphImpl implements ReadGraph { if (parent != null || listener != null) { - Object syncParent = request; +// Object syncParent = request; // final ReadGraphImpl newGraph = newSync(); @@ -2519,7 +2428,7 @@ public class ReadGraphImpl implements ReadGraph { if (parent != null || listener != null) { - Object syncParent = request; +// Object syncParent = request; // final ReadGraphImpl newGraph = newSync(); @@ -2529,7 +2438,7 @@ public class ReadGraphImpl implements ReadGraph { } else { - Object syncParent = request; +// Object syncParent = request; // final ReadGraphImpl newGraph = newSync(); @@ -2619,55 +2528,58 @@ public class ReadGraphImpl implements ReadGraph { assert (request != null); ListenerBase listener = getListenerBase(procedure); - + final DataContainer exception = new DataContainer(); final DataContainer result = new DataContainer(); - if (parent != null || listener != null) { - -// final ReadGraphImpl newGraph = newSync(); - - processor.query(this, request, parent, new Procedure() { + processor.query(this, request, parent, new Procedure() { - @Override - public void exception(Throwable throwable) { - exception.set(throwable); - procedure.exception(throwable); - } - - @Override - public void execute(T t) { - result.set(t); - procedure.execute(t); - } - - }, listener); - -// newGraph.waitAsync(request); - - } else { - - try { + @Override + public void exception(Throwable throwable) { + exception.set(throwable); + procedure.exception(throwable); + } - T t = processor.tryQuery(this, request); + @Override + public void execute(T t) { result.set(t); procedure.execute(t); + } - } catch (Throwable t) { - - if (t instanceof DatabaseException) { - exception.set((DatabaseException)t); - procedure.exception(exception.get()); - } else { - exception.set(new DatabaseException( - "Unexpected exception in ReadGraph.syncRequest(Read)", - t)); - procedure.exception(exception.get()); - } + }, listener); + - } - } +// if (parent != null || listener != null) { +// +//// final ReadGraphImpl newGraph = newSync(); +// +// +//// newGraph.waitAsync(request); +// +// } else { +// +// try { +// +// T t = processor.tryQuery(this, request); +// result.set(t); +// procedure.execute(t); +// +// } catch (Throwable t) { +// +// if (t instanceof DatabaseException) { +// exception.set((DatabaseException)t); +// procedure.exception(exception.get()); +// } else { +// exception.set(new DatabaseException( +// "Unexpected exception in ReadGraph.syncRequest(Read)", +// t)); +// procedure.exception(exception.get()); +// } +// +// } +// +// } Throwable t = exception.get(); if (t != null) { @@ -5480,11 +5392,12 @@ public class ReadGraphImpl implements ReadGraph { if (parent != null || listener != null) { try { - processor.queryRead(this, request, parent, procedure, - listener); - } catch (Throwable e) { + QueryCache.runnerReadEntry(this, request, parent, listener, procedure); + //processor.query(this, request, parent, procedure,listener); + } catch (DatabaseException e) { + Logger.defaultLogError(e); // This throwable has already been transferred to procedure at this point - do nothing about it - //Logger.defaultLogError("Internal error ", e); + // } } else { @@ -5589,7 +5502,12 @@ public class ReadGraphImpl implements ReadGraph { if (parent != null || listener != null) { - processor.query(this, request, parent, procedure, listener); + try { + QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure); + //processor.query(this, request, parent, procedure, listener); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + } } else { @@ -6470,7 +6388,6 @@ public class ReadGraphImpl implements ReadGraph { } } - @SuppressWarnings("unchecked") @Override public Variant getVariantValue2(Resource r, Object context) throws DatabaseException { Layer0 L0 = processor.getL0(this);