1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
15 import org.simantics.db.procedure.AsyncProcedure;
\r
16 import org.simantics.db.procedure.ListenerBase;
\r
17 import org.simantics.db.procedure.Procedure;
\r
20 final public class DirectStatements /*extends CollectionBinaryQuery<TripleIntProcedure>*/ {
\r
23 public DirectStatements(final int r1, final int r2) {
\r
27 public static DirectStatements newInstance(final int r1, final int r2) {
\r
28 return new DirectStatements(r1, r2);
\r
31 final static DirectStatements entry(final QueryProcessor provider, final int r1, final int r2) {
\r
33 return (DirectStatements)provider.statementsMap[provider.resourceThread(r1)].get(id(r1,r2));
\r
37 final static Collection<DirectStatements> entries(final QueryProcessor processor, final int r1) {
\r
38 BinaryQueryHashMap<TripleIntProcedure> hash = processor.statementsMap[processor.resourceThread(r1)];
\r
39 return hash.values(r1);
\r
43 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<org.simantics.db.DirectStatements> procedure, boolean ignoreVirtual) {
\r
47 org.simantics.db.DirectStatements result = provider.querySupport.getStatements(graph, r, provider, ignoreVirtual);
\r
48 procedure.execute(graph, result);
\r
52 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final Procedure<org.simantics.db.DirectStatements> procedure) {
\r
58 org.simantics.db.DirectStatements result = provider.querySupport.getStatements(graph, r, provider, true);
\r
59 procedure.execute(result);
\r
61 // provider.querySupport.getStatements(graph, r, procedure);
\r
63 } catch (Throwable t) {
\r
65 t.printStackTrace();
\r
73 public BinaryQuery<TripleIntProcedure> getEntry(int callerThread, QueryProcessor provider) {
\r
74 return provider.statementsMap[callerThread].get(id);
\r
78 public void putEntry(int callerThread, QueryProcessor provider) {
\r
79 provider.statementsMap[callerThread].put(id, this);
\r
83 final public void removeEntry(int callerThread, QueryProcessor provider) {
\r
84 provider.statementsMap[callerThread].remove(id);
\r
87 final private void forSingleAssertion(ReadGraphImpl graph, final QueryProcessor queryProvider, final TripleIntProcedure procedure) {
\r
89 final AtomicInteger results = new AtomicInteger();
\r
91 PrincipalTypes.queryEach(graph, r1(), queryProvider, this, null, new SyncIntProcedure() {
\r
94 public void run(ReadGraphImpl graph) {
\r
96 int rc = results.get();
\r
100 procedure.exception(graph, (Throwable)getResult());
\r
104 // No result or single result
\r
105 if(rc == 0 || rc == 1) {
\r
107 finish(graph, queryProvider);
\r
108 procedure.finished(graph);
\r
112 ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1());
\r
114 procedure.exception(graph, exception);
\r
123 public void execute(ReadGraphImpl graph, int type) {
\r
127 AssertedStatements.queryEach(graph, type, r2(), queryProvider, DirectStatements.this, null, new TripleIntProcedureAdapter() {
\r
130 public void execute(ReadGraphImpl graph, int s, int p, int o) {
\r
132 int event = results.getAndIncrement();
\r
136 procedure.execute(graph, s, p, o);
\r
143 public void finished(ReadGraphImpl graph) {
\r
150 public void exception(ReadGraphImpl graph, Throwable t) {
\r
162 public void finished(ReadGraphImpl graph) {
\r
172 // Search for one statement
\r
173 final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final TripleIntProcedure procedure, final boolean store) {
\r
177 boolean found = provider.querySupport.getObjects(graph, r1(), r2(), new IntProcedure() {
\r
180 public void execute(ReadGraphImpl graph, int i) {
\r
181 addOrSet(r1(), r2(), i);
\r
185 public void exception(ReadGraphImpl graph, Throwable t) {
\r
186 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
\r
190 public void finished(ReadGraphImpl graph) {
\r
195 // If functional relation was found there is no need to check assertions
\r
198 final IntArray array = (IntArray)getResult();
\r
199 if(array.size() > 3) {
\r
200 Throwable exception = new ManyObjectsForFunctionalRelationException("Functional relation " + r2() + " has multiple objects " + Arrays.toString(array.toArray()) + " for subject " + r1());
\r
202 procedure.exception(graph, exception);
\r
205 procedure.execute(graph, array.data[0], array.data[1], array.data[2]);
\r
206 finish(graph, provider);
\r
207 procedure.finished(graph);
\r
212 // Check for assertions
\r
213 forSingleAssertion(graph, provider, procedure);
\r
218 final AtomicBoolean found = new AtomicBoolean(false);
\r
220 DirectPredicates.queryEach(graph, r1(), provider, DirectStatements.this, null, new SyncIntProcedure() {
\r
223 public void run(ReadGraphImpl graph) {
\r
225 if(found.get()) return;
\r
227 // Check for assertions
\r
228 forSingleAssertion(graph, provider, procedure);
\r
233 public void execute(ReadGraphImpl graph, final int pred) {
\r
235 if(found.get()) return;
\r
241 DirectObjects.queryEach(graph, r1(), pred, provider, DirectStatements.this, null, new IntProcedure() {
\r
244 public void execute(ReadGraphImpl graph, int i) {
\r
246 if(found.compareAndSet(false, true)) {
\r
248 addOrSet(r1(), pred, i);
\r
249 finish(graph, provider);
\r
250 procedure.execute(graph, r1(), pred, i);
\r
251 procedure.finished(graph);
\r
255 ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1());
\r
257 procedure.exception(graph, exception);
\r
264 public void finished(ReadGraphImpl graph) {
\r
271 public void exception(ReadGraphImpl graph, Throwable t) {
\r
272 procedure.exception(graph, t);
\r
281 SuperRelations.queryEach(graph, pred, provider, DirectStatements.this, null, new InternalProcedure<IntSet>() {
\r
284 public void execute(ReadGraphImpl graph, IntSet result) {
\r
291 if(result.contains(r2())) {
\r
295 DirectObjects.queryEach(graph, r1(), pred, provider, DirectStatements.this, null, new IntProcedure() {
\r
298 public void execute(ReadGraphImpl graph, int i) {
\r
300 if(found.compareAndSet(false, true)) {
\r
302 addOrSet(r1(), pred, i);
\r
303 finish(graph, provider);
\r
304 procedure.execute(graph, r1(), pred, i);
\r
305 procedure.finished(graph);
\r
309 ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1());
\r
311 procedure.exception(graph, exception);
\r
318 public void finished(ReadGraphImpl graph) {
\r
325 public void exception(ReadGraphImpl graph, Throwable t) {
\r
326 procedure.exception(graph, t);
\r
338 public void exception(ReadGraphImpl graph, Throwable t) {
\r
339 procedure.exception(graph, t);
\r
349 public void finished(ReadGraphImpl graph) {
\r
361 final private void forAssertions(ReadGraphImpl graph, final QueryProcessor queryProvider, final TripleIntProcedure procedure) {
\r
363 PrincipalTypes.queryEach(graph, r1(), queryProvider, this, null, new SyncIntProcedure() {
\r
366 public void run(ReadGraphImpl graph) {
\r
368 finish(graph, queryProvider);
\r
369 procedure.finished(graph);
\r
373 TripleIntProcedure proc = new TripleIntProcedureAdapter() {
\r
376 public void execute(ReadGraphImpl graph, int s, int p, int o) {
\r
378 procedure.execute(graph, s, p, o);
\r
382 public void finished(ReadGraphImpl graph) {
\r
387 public void exception(ReadGraphImpl graph, Throwable t) {
\r
388 procedure.exception(graph, t);
\r
394 public void execute(ReadGraphImpl graph, int type) {
\r
398 AssertedStatements.queryEach(graph, type, r2(), queryProvider, DirectStatements.this, null, proc);
\r
403 public void finished(ReadGraphImpl graph) {
\r
412 final public void computeNotFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, RelationInfo ri, final TripleIntProcedure procedure, final boolean store) {
\r
416 provider.querySupport.getObjects(graph, r1(), r2(), new IntProcedure() {
\r
419 public void execute(ReadGraphImpl graph, int i) {
\r
420 addOrSet(r1(), r2(), i);
\r
424 public void exception(ReadGraphImpl graph, Throwable t) {
\r
425 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
\r
426 procedure.exception(graph, t);
\r
430 public void finished(ReadGraphImpl graph) {
\r
435 final IntArray value = (IntArray)getResult();
\r
436 for(int i=0;i<value.size();i+=3) {
\r
437 procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
\r
440 forAssertions(graph, provider, procedure);
\r
444 DirectPredicates.queryEach(graph, r1(), provider, DirectStatements.this, null, new SyncIntProcedure() {
\r
447 public void run(ReadGraphImpl graph) {
\r
449 forAssertions(graph, provider, procedure);
\r
454 public void execute(ReadGraphImpl graph, final int pred) {
\r
456 // System.out.println("directpredicates execute " + pred);
\r
462 DirectObjects.queryEach(graph, r1(), pred, provider, DirectStatements.this, null, new IntProcedure() {
\r
465 public void execute(ReadGraphImpl graph, int i) {
\r
466 // System.out.println("pred=" + pred + " object=" + i);
\r
467 addOrSet(r1(), pred, i);
\r
468 procedure.execute(graph, r1(), pred, i);
\r
472 public void finished(ReadGraphImpl graph) {
\r
477 public void exception(ReadGraphImpl graph, Throwable t) {
\r
478 procedure.exception(graph, t);
\r
487 SuperRelations.queryEach(graph, pred, provider, DirectStatements.this, null, new InternalProcedure<IntSet>() {
\r
490 public void execute(ReadGraphImpl graph, IntSet result) {
\r
492 if(result.contains(r2())) {
\r
496 DirectObjects.queryEach(graph, r1(), pred, provider, DirectStatements.this, null, new IntProcedure() {
\r
499 public void execute(ReadGraphImpl graph, int i) {
\r
501 // if(pred == 65548)
\r
502 // System.out.println("s=" + r1() + "p=" + pred + " o=" + i);
\r
504 addOrSet(r1(), pred, i);
\r
505 procedure.execute(graph, r1(), pred, i);
\r
510 public void finished(ReadGraphImpl graph) {
\r
515 public void exception(ReadGraphImpl graph, Throwable t) {
\r
516 procedure.exception(graph, t);
\r
528 public void exception(ReadGraphImpl graph, Throwable t) {
\r
529 procedure.exception(graph, t);
\r
539 public void finished(ReadGraphImpl graph) {
\r
551 public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, final boolean store) {
\r
553 RelationInfoQuery.queryEach(graph, r2(), provider, this, null, new InternalProcedure<RelationInfo>() {
\r
556 public void execute(ReadGraphImpl graph, final RelationInfo ri) {
\r
558 provider.querySupport.ensureLoaded(graph, r1(), r2());
\r
559 if(ri.isFunctional) {
\r
560 computeFunctionalIndex(graph, provider, ri, procedure, store);
\r
562 computeNotFunctionalIndex(graph, provider, ri, procedure, store);
\r
568 public void exception(ReadGraphImpl graph, Throwable t) {
\r
569 procedure.exception(graph, t);
\r
577 public String toString() {
\r
578 return "Statements[" + r1() + " - " + r2() + "]";
\r
581 final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
\r
583 assert(isPending());
\r
585 synchronized(this) {
\r
591 synchronized public void addOrSet(int s, int p, int o) {
\r
593 assert(isPending());
\r
595 IntArray value = (IntArray)getResult();
\r
603 public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
\r
607 if(handleException(graph, procedure)) return;
\r
609 final IntArray value = (IntArray)getResult();
\r
610 for(int i=0;i<value.size();i+=3) {
\r
611 procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
\r
614 procedure.finished(graph);
\r
619 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
\r
621 final Semaphore s = new Semaphore(0);
\r
623 computeForEach(graph, provider, new TripleIntProcedureAdapter() {
\r
626 public void finished(ReadGraphImpl graph) {
\r
631 public void exception(ReadGraphImpl graph, Throwable t) {
\r
632 new Error("Error in recompute.", t).printStackTrace();
\r
638 while(!s.tryAcquire()) {
\r
639 provider.resume(graph);
\r
645 public int type() {
\r
646 return RequestFlags.IMMEDIATE_UPDATE;
\r