]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryCacheBase.java
1 package org.simantics.db.impl.query;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.concurrent.atomic.AtomicBoolean;
6
7 import org.simantics.db.AsyncReadGraph;
8 import org.simantics.db.RelationInfo;
9 import org.simantics.db.common.utils.Logger;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.impl.DebugPolicy;
12 import org.simantics.db.impl.graph.ReadGraphImpl;
13 import org.simantics.db.impl.procedure.InternalProcedure;
14 import org.simantics.db.procedure.AsyncMultiProcedure;
15 import org.simantics.db.procedure.AsyncProcedure;
16 import org.simantics.db.procedure.Listener;
17 import org.simantics.db.procedure.ListenerBase;
18 import org.simantics.db.request.AsyncMultiRead;
19 import org.simantics.db.request.AsyncRead;
20 import org.simantics.db.request.ExternalRead;
21 import org.simantics.db.request.MultiRead;
22 import org.simantics.db.request.Read;
23
24 import gnu.trove.map.hash.THashMap;
25 import gnu.trove.map.hash.TObjectIntHashMap;
26
27 public class QueryCacheBase {
28
29         // Statistics
30         final int THREADS;
31         final public int  THREAD_MASK;
32         int                                             hits                  = 0;
33         int                                             misses                = 0;
34         int                                             updates               = 0;
35         public int                                              size                  = 0;
36         
37         volatile public boolean dirty = false;
38         public boolean                                         collecting            = false;
39
40         final protected THashMap<String, URIToResource>                      uRIToResourceMap;
41         final protected THashMap<String, NamespaceIndex>                     namespaceIndexMap;
42         final protected DoubleKeyQueryHashMap<IntProcedure>                     objectsMap;
43         final protected DoubleKeyQueryHashMap<TripleIntProcedure>               assertedStatementsMap;
44         final protected DoubleKeyQueryHashMap<IntProcedure>                     directObjectsMap;
45         final protected DoubleKeyQueryHashMap<TripleIntProcedure>               statementsMap;
46         final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         typesMap;
47         final protected UnaryQueryHashMap<IntProcedure>                      principalTypesMap;
48         final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         predicatesMap;
49         final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         superTypesMap;
50         final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         typeHierarchyMap;
51         final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         superRelationsMap;
52         
53         final protected UnaryQueryHashMap<IntProcedure>                      orderedSetMap;
54         final protected UnaryQueryHashMap<IntProcedure>                      assertedPredicatesMap;
55         final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         directPredicatesMap;
56         final protected UnaryQueryHashMap<IntProcedure>                      directSuperRelationsMap;
57         
58         final protected UnaryQueryHashMap<InternalProcedure<RelationInfo>>   relationInfoQueryMap;
59         final protected UnaryQueryHashMap<InternalProcedure<byte[]>>         valueQueryMap;
60         
61         final protected StableHashMap<AsyncRead, AsyncReadEntry>                  asyncReadEntryMap; 
62         final protected StableHashMap<Read, ReadEntry>                            readEntryMap;
63         final protected StableHashMap<MultiRead, MultiReadEntry>                  multiReadEntryMap; 
64         final protected StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>        asyncMultiReadEntryMap; 
65         final protected StableHashMap<ExternalRead, ExternalReadEntry>            externalReadEntryMap; 
66
67         final THashMap<CacheEntry, ArrayList<ListenerEntry>>       listeners;
68
69         final public QuerySupport                               querySupport;
70
71         public QueryCacheBase(QuerySupport querySupport, int threads) {
72                 
73                 THREADS = threads;
74                 THREAD_MASK = threads - 1;
75
76                 this.querySupport = querySupport;
77                 directPredicatesMap = new UnaryQueryHashMap();
78                 directSuperRelationsMap = new UnaryQueryHashMap();
79                 valueQueryMap = new UnaryQueryHashMap();
80                 principalTypesMap = new UnaryQueryHashMap();
81                 uRIToResourceMap = new THashMap<String, URIToResource>();
82                 namespaceIndexMap = new THashMap<String, NamespaceIndex>();
83                 relationInfoQueryMap = new UnaryQueryHashMap();
84                 typeHierarchyMap = new UnaryQueryHashMap();
85                 superTypesMap = new UnaryQueryHashMap();
86                 superRelationsMap = new UnaryQueryHashMap();
87                 typesMap = new UnaryQueryHashMap();
88                 objectsMap = new DoubleKeyQueryHashMap();
89                 orderedSetMap = new UnaryQueryHashMap();
90                 predicatesMap = new UnaryQueryHashMap();
91                 statementsMap = new DoubleKeyQueryHashMap();
92                 directObjectsMap = new DoubleKeyQueryHashMap();
93                 assertedPredicatesMap = new UnaryQueryHashMap();
94                 assertedStatementsMap = new DoubleKeyQueryHashMap();
95                 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>(); 
96                 readEntryMap = new StableHashMap<Read, ReadEntry>();
97                 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>(); 
98                 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>(); 
99                 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>(); 
100                 listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
101         }
102         
103         public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
104
105                 AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
106                 AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
107
108                 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
109
110                 try {
111                         
112                         query.perform(queryGraph, new AsyncProcedure<T>() {
113
114                                 @Override
115                                 public void execute(AsyncReadGraph returnGraph, T result) {
116                                         ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
117                                         entry.addOrSet(parentGraph, result);
118                                         try {
119                                                 procedure.execute(parentGraph, result);
120                                         } catch (Throwable t) {
121                                                 t.printStackTrace();
122                                         }
123 //                                      parentBarrier.dec(query);
124                                 }
125
126                                 @Override
127                                 public void exception(AsyncReadGraph returnGraph, Throwable t) {
128                                         ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
129 //                                      AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
130                                         entry.except(parentGraph, t);
131                                         try {
132                                                 procedure.exception(parentGraph, t);
133                                         } catch (Throwable t2) {
134                                                 t2.printStackTrace();
135                                         }
136 //                                      parentBarrier.dec(query);
137                                 }
138
139                                 @Override
140                                 public String toString() {
141                                         return procedure.toString();
142                                 }
143
144                         });
145
146                 } catch (Throwable t) {
147
148                         entry.except(t);
149                         try {
150                                 procedure.exception(parentGraph, t);
151                         } catch (Throwable t2) {
152                                 t2.printStackTrace();
153                         }
154 //                      parentBarrier.dec(query);
155
156                 }
157                 
158                 return null;
159                 
160         }
161
162 //      public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
163 //
164 //              ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
165 //
166 //              ReadEntry entry = (ReadEntry)entry_;
167 //
168 //              try {
169 //
170 //                      T result = (T)query.perform(queryGraph);
171 //                      entry.addOrSet(queryGraph, result);
172 //
173 //                      return (T)entry.get(parentGraph, procedure_);
174 //
175 //              }  catch (Throwable t) {
176 //
177 //                      entry.except(t);
178 //                      return (T)entry.get(parentGraph, procedure_);
179 //
180 //              }
181 //              
182 //      }
183
184         public <T> Object performQuery(ReadGraphImpl parentGraph, final ExternalRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
185
186                 ExternalReadEntry entry = (ExternalReadEntry)entry_;
187                 AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
188                 
189                 try {
190
191                         query.register(parentGraph, new Listener<T>() {
192
193                                 AtomicBoolean used = new AtomicBoolean(false);
194
195                                 @Override
196                                 public void execute(T result) {
197                                         
198                                         // Just for safety
199                                         if(entry.isDiscarded()) return;
200                                         if(entry.isExcepted()) entry.setPending();
201                                         
202                                         if(used.compareAndSet(false, true)) {
203                                                 entry.addOrSet(parentGraph.processor, result);
204                                                 procedure.execute(parentGraph, result);
205                                         } else {
206                                                 entry.queue(result);
207                                                 parentGraph.processor.updatePrimitive(query);
208                                         }
209                                         
210                                 }
211
212                                 @Override
213                                 public void exception(Throwable t) {
214                                         
215                                         entry.except(t);
216
217                                         if(used.compareAndSet(false, true)) {
218                                                 procedure.exception(parentGraph, t);
219                                         } else {
220 //                                              entry.queue(result);
221                                                 parentGraph.processor.updatePrimitive(query);
222                                         }
223                                         
224                                 }
225
226                                 @Override
227                                 public String toString() {
228                                         return procedure.toString();
229                                 }
230
231                                 @Override
232                                 public boolean isDisposed() {
233                                         return entry.isDiscarded() || !parentGraph.processor.isBound(entry);
234                                 }
235
236                         });
237                         
238                         return entry.getResult();
239
240                 } catch (Throwable t) {
241
242                         entry.except(t);
243                         procedure.exception(parentGraph, t);
244                         return entry.getResult();
245
246                 }
247                 
248         }
249
250         public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
251
252                 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
253
254                 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
255                 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
256
257                 try {
258
259                         query.perform(queryGraph, new AsyncMultiProcedure<T>() {
260
261                                 @Override
262                                 public void execute(AsyncReadGraph graph, T result) {
263                                         ReadGraphImpl impl = (ReadGraphImpl)graph;
264                                         entry.addOrSet(result);
265                                         try {
266                                                 procedure.execute(parentGraph, result);
267                                         } catch (Throwable t) {
268                                                 t.printStackTrace();
269                                         }
270                                 }
271
272                                 @Override
273                                 public void finished(AsyncReadGraph graph) {
274                                         ReadGraphImpl impl = (ReadGraphImpl)graph;
275                                         entry.finish(parentGraph);
276                                         try {
277                                                 procedure.finished(parentGraph);
278                                         } catch (Throwable t) {
279                                                 t.printStackTrace();
280                                         }
281                                 }
282
283                                 @Override
284                                 public void exception(AsyncReadGraph graph, Throwable t) {
285                                         ReadGraphImpl impl = (ReadGraphImpl)graph;
286                                         entry.except(parentGraph, t);
287                                         try {
288                                                 procedure.exception(parentGraph, t);
289                                         } catch (Throwable t2) {
290                                                 t2.printStackTrace();
291                                         }
292                                 }
293
294                         });
295                         
296                         return entry.getResult();
297
298                 } catch (Throwable t) {
299
300                         entry.except(t);
301                         try {
302                                 procedure.exception(parentGraph, t);
303                         } catch (Throwable t2) {
304                                 t2.printStackTrace();
305                         }
306                         
307                         return entry.getResult();
308                         
309                 }
310                 
311         }
312
313         public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
314
315                 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
316
317                 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
318                 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
319
320                 try {
321
322                         query.perform(queryGraph, new AsyncMultiProcedure<T>() {
323
324                                 @Override
325                                 public void execute(AsyncReadGraph graph, T result) {
326                                         ReadGraphImpl impl = (ReadGraphImpl)graph;
327                                         entry.addOrSet(result);
328                                         try {
329                                                 procedure.execute(parentGraph, result);
330                                         } catch (Throwable t) {
331                                                 t.printStackTrace();
332                                         }
333                                 }
334
335                                 @Override
336                                 public void finished(AsyncReadGraph graph) {
337                                         ReadGraphImpl impl = (ReadGraphImpl)graph;
338                                         entry.finish(parentGraph);
339                                         try {
340                                                 procedure.finished(parentGraph);
341                                         } catch (Throwable t) {
342                                                 t.printStackTrace();
343                                         }
344                                 }
345
346                                 @Override
347                                 public void exception(AsyncReadGraph graph, Throwable t) {
348                                         ReadGraphImpl impl = (ReadGraphImpl)graph;
349                                         entry.except(parentGraph, t);
350                                         try {
351                                                 procedure.exception(parentGraph, t);
352                                         } catch (Throwable t2) {
353                                                 t2.printStackTrace();
354                                         }
355                                 }
356
357                         });
358                         
359                         return entry.getResult();
360
361                 } catch (Throwable t) {
362
363                         entry.except(t);
364                         try {
365                                 procedure.exception(parentGraph, t);
366                         } catch (Throwable t2) {
367                                 t2.printStackTrace();
368                         }
369                         
370                         return entry.getResult();
371                         
372                 }
373                 
374         }
375         
376         synchronized public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
377
378                 if (parent != null && !inferred) {
379                         try {
380                                 if(!child.isImmutable(graph))
381                                         child.addParent(parent);
382                         } catch (DatabaseException e) {
383                                 Logger.defaultLogError(e);
384                         }
385                         if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
386                 }
387
388                 if (listener != null) {
389                         return registerListener(child, listener, procedure);
390                 } else {
391                         return null;
392                 }
393
394         }
395         
396         public ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
397
398                 assert (entry != null);
399
400                 if (base.isDisposed())
401                         return null;
402
403                 return addListener(entry, base, procedure);
404
405         }
406
407         protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
408                 entry.setLastKnown(result);
409         }
410
411         private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
412
413                 assert (entry != null);
414                 assert (procedure != null);
415
416                 ArrayList<ListenerEntry> list = listeners.get(entry);
417                 if (list == null) {
418                         list = new ArrayList<ListenerEntry>(1);
419                         listeners.put(entry, list);
420                 }
421
422                 ListenerEntry result = new ListenerEntry(entry, base, procedure);
423                 int currentIndex = list.indexOf(result);
424                 // There was already a listener
425                 if(currentIndex > -1) {
426                         ListenerEntry current = list.get(currentIndex);
427                         if(!current.base.isDisposed()) return null;
428                         list.set(currentIndex, result);
429                 } else {
430                         list.add(result);
431                 }
432
433                 if(DebugPolicy.LISTENER) {
434                         new Exception().printStackTrace();
435                         System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
436                 }
437
438                 return result;
439
440         }
441         
442         
443         public Collection<CacheEntry> getRootList() {
444
445                 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
446
447                 for (Object e : valueQueryMap.values()) {
448                         result.add((CacheEntry) e);
449                 }
450                 for (Object e : directPredicatesMap.values()) {
451                         result.add((CacheEntry) e);
452                 }
453                 for (Object e : directSuperRelationsMap.values()) {
454                         result.add((CacheEntry) e);
455                 }
456                 for (Object e : objectsMap.values()) {
457                         result.add((CacheEntry) e);
458                 }
459                 for (Object e : directObjectsMap.values()) {
460                         result.add((CacheEntry) e);
461                 }
462                 for (Object e : principalTypesMap.values()) {
463                         result.add((CacheEntry) e);
464                 }
465                 for (Object e : superRelationsMap.values()) {
466                         result.add((CacheEntry) e);
467                 }
468                 for (Object e : superTypesMap.values()) {
469                         result.add((CacheEntry) e);
470                 }
471                 for (Object e : typesMap.values()) {
472                         result.add((CacheEntry) e);
473                 }
474                 for (Object e : objectsMap.values()) {
475                         result.add((CacheEntry) e);
476                 }
477                 for (Object e : assertedStatementsMap.values()) {
478                         result.add((CacheEntry) e);
479                 }
480                 for (Object e : readEntryMap.values()) {
481                         if(e instanceof CacheEntry) {
482                                 result.add((CacheEntry) e);
483                         } else {
484                                 System.err.println("e=" + e);
485                         }
486                 }
487                 for (Object e : asyncReadEntryMap.values()) {
488                         if(e instanceof CacheEntry) {
489                                 result.add((CacheEntry) e);
490                         } else {
491                                 System.err.println("e=" + e);
492                         }
493                 }
494                 for (Object e : externalReadEntryMap.values()) {
495                         result.add((CacheEntry) e);
496                 }
497                 for (Object e : orderedSetMap.values()) {
498                         result.add((CacheEntry) e);
499                 }
500
501                 return result;
502
503         }
504
505         public int calculateCurrentSize() {
506                 
507                 int realSize = 0;
508                 
509                 realSize += directPredicatesMap.size();
510                 realSize += directSuperRelationsMap.size();
511                 realSize += principalTypesMap.size();
512                 realSize += uRIToResourceMap.size();
513                 realSize += namespaceIndexMap.size();
514                 
515                 realSize += relationInfoQueryMap.size();
516                 realSize += superTypesMap.size();
517                 realSize += typeHierarchyMap.size();
518                 realSize += superRelationsMap.size();
519                 realSize += typesMap.size();
520                 
521                 realSize += valueQueryMap.size();
522                 realSize += directObjectsMap.size();
523                 realSize += objectsMap.size();
524                 realSize += orderedSetMap.size();
525                 realSize += predicatesMap.size();
526                 
527                 realSize += statementsMap.size();
528                 realSize += assertedPredicatesMap.size();
529                 realSize += assertedStatementsMap.size();
530                 realSize += externalReadEntryMap.size();
531                 realSize += asyncReadEntryMap.size();
532                 
533                 realSize += readEntryMap.size();
534                 realSize += asyncMultiReadEntryMap.size();
535                 realSize += multiReadEntryMap.size();
536                 
537                 return realSize;
538                 
539         }
540         
541         CacheCollectionResult allCaches(CacheCollectionResult result) {
542
543                 int level = Integer.MAX_VALUE;
544                 directPredicatesMap.values(level, result);
545                 directSuperRelationsMap.values(level, result);
546                 principalTypesMap.values(level, result);
547                 for(CacheEntryBase e : uRIToResourceMap.values())
548                         if(e.getLevel() <= level)
549                                 result.add(e);
550                 for(CacheEntryBase e : namespaceIndexMap.values())
551                         if(e.getLevel() <= level)
552                                 result.add(e);
553                 
554                 relationInfoQueryMap.values(level, result);
555                 superTypesMap.values(level, result);
556                 typeHierarchyMap.values(level, result);
557                 superRelationsMap.values(level, result);
558                 typesMap.values(level, result);
559
560                 valueQueryMap.values(level, result);
561                 directObjectsMap.values(level, result);
562                 objectsMap.values(level, result);
563                 orderedSetMap.values(level, result);
564                 predicatesMap.values(level, result);
565
566                 statementsMap.values(level, result);
567                 assertedPredicatesMap.values(level, result);
568                 assertedStatementsMap.values(level, result);
569                 externalReadEntryMap.values(level, result);
570                 asyncReadEntryMap.values(level, result);
571                 
572                 readEntryMap.values(level, result);
573                 asyncMultiReadEntryMap.values(level, result);
574                 multiReadEntryMap.values(level, result);
575
576                 return result;
577                 
578         }
579         
580         public void scanPending() {
581
582                 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
583
584                 entries.addAll(directPredicatesMap.values());
585                 entries.addAll(directSuperRelationsMap.values());
586                 entries.addAll(principalTypesMap.values());
587                 entries.addAll(uRIToResourceMap.values());
588                 entries.addAll(namespaceIndexMap.values());
589                 entries.addAll(relationInfoQueryMap.values());
590                 entries.addAll(superTypesMap.values());
591                 entries.addAll(superRelationsMap.values());
592                 entries.addAll(typesMap.values());
593                 entries.addAll(valueQueryMap.values());
594                 entries.addAll(directObjectsMap.values());
595                 entries.addAll(objectsMap.values());
596                 entries.addAll(orderedSetMap.values());
597                 entries.addAll(predicatesMap.values());
598                 entries.addAll(orderedSetMap.values());
599                 entries.addAll(statementsMap.values());
600                 //                      entries.addAll(assertedObjectsMap.values());
601                 entries.addAll(assertedPredicatesMap.values());
602                 entries.addAll(assertedStatementsMap.values());
603                 entries.addAll(externalReadEntryMap.values());
604                 entries.addAll(asyncReadEntryMap.values());
605                 entries.addAll(externalReadEntryMap.values());
606                 entries.addAll(readEntryMap.values());
607                 entries.addAll(asyncMultiReadEntryMap.values());
608                 entries.addAll(multiReadEntryMap.values());
609                 entries.addAll(readEntryMap.values());
610                 System.out.println(entries.size() + " entries.");
611                 for(Object e : entries) {
612                         if(e instanceof CacheEntry) {
613                                 CacheEntry en = (CacheEntry)e;
614                                 if(en.isPending()) System.out.println("pending " + e);
615                                 if(en.isExcepted()) System.out.println("excepted " + e);
616                                 if(en.isDiscarded()) System.out.println("discarded " + e);
617                                 if(en.isRefuted()) System.out.println("refuted " + e);
618                                 if(en.isFresh()) System.out.println("fresh " + e);
619                         } else {
620                                 //System.out.println("Unknown object " + e);
621                         }
622                 }
623         }
624         
625         public static void waitPending(CacheEntry entry) throws DatabaseException {
626                 
627                 int counter = 0;
628                 while(entry.isPending()) {
629                         try {
630                                 Thread.sleep(1);
631                                 counter++;
632                                 if(counter > 1000) {
633                                         CacheEntryBase base = ((CacheEntryBase)entry);
634 //                                      if(base.created != null) {
635 //                                              System.err.println("created:");
636 //                                              base.created.printStackTrace();
637 //                                      }
638 //                                      if(base.performed != null) {
639 //                                              System.err.println("performed:");
640 //                                              base.performed.printStackTrace();
641 //                                      }
642 //                                      if(base.ready != null) {
643 //                                              System.err.println("ready:");
644 //                                              base.ready.printStackTrace();
645 //                                      }
646                                         System.err.println("asd");
647                                         //base.getQuery().recompute(null, null, entry);
648                                 }
649                         } catch (InterruptedException e) {
650                         }
651                 }
652
653         }
654         
655         //////////////////////////////////////
656         
657         static public Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
658                 synchronized(processor.cache.objectsMap) {
659                         return processor.cache.objectsMap.values(r1);
660                 }
661         }
662         
663         static public Collection<Objects> entriesObjects(QueryProcessor processor) {
664                 synchronized(processor.cache.objectsMap) {
665                         return processor.cache.objectsMap.values();
666                 }
667         }
668         
669         static public Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
670                 synchronized(processor.cache.directPredicatesMap) {
671                         return processor.cache.directPredicatesMap.values();
672                 }
673         }
674
675         final static Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
676                 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
677                 return hash.values(r1);
678         }
679         
680         final static Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
681                 return processor.cache.statementsMap.values(r1);
682         }
683
684         final static Types entryTypes(final QueryProcessor processor, final int r) {
685                 return (Types)processor.cache.typesMap.get(r);
686         }
687
688         final static PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
689                 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
690         }
691
692         final static OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
693                 return (OrderedSet)processor.cache.orderedSetMap.get(r);
694         }
695         
696         final static ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
697                 return (ValueQuery)processor.cache.valueQueryMap.get(r);
698         }
699
700         final static DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
701                 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
702         }
703
704         public final static ReadEntry entryRead(final QueryProcessor processor, final Read request) {
705                 return (ReadEntry)processor.cache.readEntryMap.get(request);
706         }
707
708         public final static MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
709                 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
710         }
711
712         public final static AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
713                 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
714         }
715
716         public final static AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
717                 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
718         }
719
720         final protected static long keyR2(long r1, long r2) {
721         long result = (r1<<32) | (r2 & 0xffffffffL); 
722         return result;
723     }
724         
725         final protected static <T> T id(T o) {
726                 return o;
727         }
728
729     final protected static int keyR(int r) {
730         return r;
731     }
732
733     final protected static String keyID(String id) {
734         return id;
735     }
736         
737     protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
738
739                 @Override
740                 public void execute(ReadGraphImpl graph, IntSet result) {
741                 }
742
743                 @Override
744                 public void exception(ReadGraphImpl graph, Throwable throwable) {
745                 }
746                 
747         }; 
748
749     protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
750
751                 @Override
752                 public void execute(ReadGraphImpl graph, byte[] bytes) {
753                 }
754
755                 @Override
756                 public void exception(ReadGraphImpl graph, Throwable throwable) {
757                 }
758                 
759         }; 
760
761     protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
762
763                 @Override
764                 public void execute(ReadGraphImpl graph, Integer i) {
765                 }
766
767                 @Override
768                 public void exception(ReadGraphImpl graph, Throwable throwable) {
769                 }
770                 
771         }; 
772
773
774     protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
775
776                 @Override
777                 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
778                 }
779
780                 @Override
781                 public void exception(ReadGraphImpl graph, Throwable throwable) {
782                 }
783                 
784         }; 
785
786
787     protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
788
789                 @Override
790                 public void execute(ReadGraphImpl graph, RelationInfo i) {
791                 }
792
793                 @Override
794                 public void exception(ReadGraphImpl graph, Throwable throwable) {
795                 }
796                 
797         }; 
798
799         
800         protected static IntProcedure emptyIntProcedure = new IntProcedure() {
801                 
802                 @Override
803                 public void finished(ReadGraphImpl graph) {
804                 }
805                 
806                 @Override
807                 public void execute(ReadGraphImpl graph, int i) {
808                 }
809                 
810                 @Override
811                 public void exception(ReadGraphImpl graph, Throwable throwable) {
812                 }
813         }; 
814         
815         protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
816
817                 @Override
818                 public void execute(ReadGraphImpl graph, int s, int p, int o) {
819                 }
820
821                 @Override
822                 public void finished(ReadGraphImpl graph) {
823                 }
824
825                 @Override
826                 public void exception(ReadGraphImpl graph, Throwable throwable) {
827                 }
828                 
829         }; 
830
831     protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
832
833                 @Override
834                 public void execute(AsyncReadGraph graph, Object result) {
835                 }
836
837                 @Override
838                 public void exception(AsyncReadGraph graph, Throwable throwable) {
839                 }
840                 
841         }; 
842         
843     protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
844
845                 @Override
846                 public void execute(AsyncReadGraph graph, Object result) {
847                 }
848
849                 @Override
850                 public void finished(AsyncReadGraph graph) {
851                 }
852
853                 @Override
854                 public void exception(AsyncReadGraph graph, Throwable throwable) {
855                 }
856
857                 
858         }; 
859
860         protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
861     protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
862     protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
863     protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
864     protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
865     protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
866
867     protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
868     protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
869     protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
870     protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
871     protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
872     protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
873     
874     protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
875     protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
876
877     protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
878     
879     protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
880     protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
881     protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
882
883     protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
884     protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
885     protected static AsyncMultiProcedure emptyProcedureMultiReadEntry = emptyAsyncMultiProcedure;
886     protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
887     protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
888         
889     static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
890         
891         private AsyncProcedure<T> procedure;
892         private T result = null;
893         private Throwable throwable = null;
894         
895         AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
896                 this.procedure = procedure;
897         }
898
899                 @Override
900                 public void execute(AsyncReadGraph graph, T result) {
901                         if(procedure != null) procedure.execute(graph, result);
902                         this.result = result;
903                 }
904
905                 @Override
906                 public void exception(AsyncReadGraph graph, Throwable throwable) {
907                         if(procedure != null) procedure.exception(graph, throwable);
908                         this.throwable = throwable;
909                 }
910                 
911                 public T get() throws DatabaseException {
912                         if(throwable != null) {
913                                 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
914                                 else throw new DatabaseException(throwable);
915                         } else {
916                                 return result;
917                         }
918                 }
919                 
920     }
921
922     
923     static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
924         
925         private InternalProcedure<T> procedure;
926         private T result = null;
927         private Throwable throwable = null;
928         
929         InternalProcedureWrapper(InternalProcedure<T> procedure) {
930                 this.procedure = procedure;
931         }
932
933                 @Override
934                 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
935                         if(procedure != null) procedure.execute(graph, result);
936                         this.result = result;
937                 }
938
939                 @Override
940                 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
941                         if(procedure != null) procedure.exception(graph, throwable);
942                         this.throwable = throwable;
943                 }
944                 
945                 public T get() throws DatabaseException {
946                         if(throwable != null) {
947                                 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
948                                 else throw new DatabaseException(throwable);
949                         } else {
950                                 return result;
951                         }
952                 }
953                 
954     }
955     
956     static class IntSetWrapper implements IntProcedure {
957         
958         private IntProcedure procedure;
959         private IntSet result = new IntSet();
960         private Throwable throwable = null;
961         
962         IntSetWrapper(IntProcedure procedure) {
963                 this.procedure = procedure;
964         }
965
966                 @Override
967                 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
968                         if(procedure != null) procedure.execute(graph, i);
969                         result.add(i);
970                 }
971
972                 @Override
973                 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
974                         if(procedure != null) procedure.exception(graph, throwable);
975                         this.throwable = throwable;
976                 }
977                 
978                 @Override
979                 public void finished(ReadGraphImpl graph) throws DatabaseException {
980                         if(procedure != null) procedure.finished(graph);
981                 }
982                 
983                 public IntSet get() throws DatabaseException {
984                         if(throwable != null) {
985                                 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
986                                 else throw new DatabaseException(throwable);
987                         } else {
988                                 return result;
989                         }
990                 }
991
992     }
993
994     static class TripleIntProcedureWrapper implements TripleIntProcedure {
995         
996         private TripleIntProcedure procedure;
997         private IntArray result = new IntArray();
998         private Throwable throwable = null;
999         
1000         TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1001                 this.procedure = procedure;
1002         }
1003
1004                 @Override
1005                 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1006                         if(procedure != null) procedure.execute(graph, i1, i2, i3);
1007                         result.add(i1);
1008                         result.add(i2);
1009                         result.add(i3);
1010                 }
1011
1012                 @Override
1013                 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1014                         if(procedure != null) procedure.exception(graph, throwable);
1015                         this.throwable = throwable;
1016                 }
1017                 
1018                 @Override
1019                 public void finished(ReadGraphImpl graph) throws DatabaseException {
1020                         if(procedure != null) procedure.finished(graph);
1021                 }
1022                 
1023                 public IntArray get() throws DatabaseException {
1024                         if(throwable != null) {
1025                                 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1026                                 else throw new DatabaseException(throwable);
1027                         } else {
1028                                 return result;
1029                         }
1030                 }
1031
1032     }
1033
1034     public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1035         AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1036         QueryCache.runnerReadEntry(graph, r, parent, listener, wrap);
1037         return wrap.get();
1038     }
1039
1040     public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<byte[]> procedure) throws DatabaseException {
1041         InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(procedure);
1042         QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1043         return wrap.get();
1044     }
1045
1046     public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
1047         InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(procedure);
1048         QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1049         return wrap.get();
1050     }
1051
1052     public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1053         InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1054         QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1055         return wrap.get();
1056     }
1057
1058     public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1059         InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1060         QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1061         return wrap.get();
1062     }
1063     
1064     public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1065         InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1066         QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1067         return wrap.get();
1068     }
1069
1070     public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1071         InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1072         QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1073         return wrap.get();
1074     }
1075
1076     public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1077         InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1078         QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1079         return wrap.get();
1080     }
1081
1082     public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
1083         TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(procedure);
1084         QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
1085         return wrap.get();
1086     }
1087
1088 }
1089