]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/HierarchicalNames.java
Multiple reader thread support for db client
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / performance / read / HierarchicalNames.java
1 package org.simantics.db.tests.performance.read;
2
3 import java.io.IOException;
4 import java.io.UTFDataFormatException;
5 import java.nio.charset.Charset;
6 import java.util.Set;
7 import java.util.Vector;
8
9 import org.simantics.databoard.Bindings;
10 import org.simantics.databoard.Datatypes;
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.binding.impl.StringBindingDefault;
13 import org.simantics.databoard.serialization.SerializationException;
14 import org.simantics.databoard.serialization.Serializer;
15 import org.simantics.databoard.serialization.impl.ModifiedUTF8StringSerializer;
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.RelationInfo;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.VirtualGraph;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.WriteOnlyGraph;
24 import org.simantics.db.common.request.AsyncReadRequest;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.common.request.WriteOnlyResultRequest;
27 import org.simantics.db.common.request.WriteResultRequest;
28 import org.simantics.db.exception.AssumptionException;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.procedure.AsyncMultiProcedure;
31 import org.simantics.db.procedure.AsyncProcedure;
32 import org.simantics.db.procedure.SyncContextProcedure;
33 import org.simantics.db.procedure.SyncMultiProcedure;
34 import org.simantics.db.procedure.SyncProcedure;
35 import org.simantics.db.request.AsyncRead;
36 import org.simantics.db.request.Read;
37 import org.simantics.db.service.ClusterBuilder;
38 import org.simantics.db.service.ClusterBuilder.ResourceHandle;
39 import org.simantics.db.service.ClusterBuilder.StatementHandle;
40 import org.simantics.db.service.DirectQuerySupport;
41 import org.simantics.db.service.QueryControl;
42 import org.simantics.db.service.SerialisationSupport;
43 import org.simantics.db.tests.performance.read.HierarchicalNames.FastStringSerializer.FastStringSerializer2;
44 import org.simantics.layer0.Layer0;
45 import org.simantics.utils.datastructures.Pair;
46
47 /*
48  * The model contains:
49  * -2M resources
50  * -1M string literals
51  * -6M statements
52  */
53 public class HierarchicalNames {
54
55         public static class FastStringSerializer extends ModifiedUTF8StringSerializer {
56
57                 public static final Charset UTF8 = Charset.forName("utf-8");
58
59                 public static FastStringSerializer INSTANCE = new FastStringSerializer();
60                 
61                 public FastStringSerializer() {
62                         super(FastStringBinding.INSTANCE);
63                 }
64
65                 static byte[] writeUTF(String str) throws IOException {
66                         
67                         int strlen = str.length();
68                         int utflen = 0;
69                         int c, count = 0;
70
71                         /* use charAt instead of copying String to char array */
72                         for (int i = 0; i < strlen; i++) {
73                                 c = str.charAt(i);
74                                 if ((c >= 0x0001) && (c <= 0x007F)) {
75                                         utflen++;
76                                 } else if (c > 0x07FF) {
77                                         utflen += 3;
78                                 } else {
79                                         utflen += 2;
80                                 }
81                         }
82
83                         if (utflen > 65535)
84                                 throw new UTFDataFormatException(
85                                                 "encoded string too long: " + utflen + " bytes");
86
87                         byte[] bytearr = new byte[utflen+2];
88
89                         bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
90                         bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);  
91
92                         int i=0;
93                         for (i=0; i<strlen; i++) {
94                                 c = str.charAt(i);
95                                 if (!((c >= 0x0001) && (c <= 0x007F))) break;
96                                 bytearr[count++] = (byte) c;
97                         }
98
99                         for (;i < strlen; i++){
100                                 c = str.charAt(i);
101                                 if ((c >= 0x0001) && (c <= 0x007F)) {
102                                         bytearr[count++] = (byte) c;
103
104                                 } else if (c > 0x07FF) {
105                                         bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
106                                         bytearr[count++] = (byte) (0x80 | ((c >>  6) & 0x3F));
107                                         bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
108                                 } else {
109                                         bytearr[count++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
110                                         bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
111                                 }
112                         }
113                         return bytearr;
114                         
115                 }
116
117                 public static class FastStringSerializer2 extends ModifiedUTF8StringSerializer {
118
119                         public static final Charset UTF8 = Charset.forName("utf-8");
120
121                         public static FastStringSerializer2 INSTANCE = new FastStringSerializer2();
122
123                         public FastStringSerializer2() {
124                                 super(FastStringBinding.INSTANCE);
125                         }
126
127                         static byte[] writeUTF(String str) throws IOException {
128
129                                 int strlen = str.length();
130                                 int utflen = 0;
131                                 int c = 0;
132
133                                 /* use charAt instead of copying String to char array */
134                                 for (int i = 0; i < strlen; i++) {
135                                         c = str.charAt(i);
136                                         if ((c >= 0x0001) && (c <= 0x007F)) {
137                                                 utflen++;
138                                         } else if (c > 0x07FF) {
139                                                 utflen += 3;
140                                         } else {
141                                                 utflen += 2;
142                                         }
143                                 }
144
145                                 if (utflen > 65535)
146                                         throw new UTFDataFormatException(
147                                                         "encoded string too long: " + utflen + " bytes");
148
149                                 int byteIndex = 0;
150                                 byte[] bytearr;
151
152                                 if(utflen < 0x80) {
153                                         bytearr = new byte[utflen+1];
154                                         bytearr[byteIndex++] = ((byte)utflen);
155                                 }
156                                 else {
157                                         utflen -= 0x80;
158                                         if(utflen < 0x4000) {
159                                                 bytearr = new byte[utflen+2];
160                                                 bytearr[byteIndex++] = (byte)( ((utflen&0x3f) | 0x80) );
161                                                 bytearr[byteIndex++] = (byte)( (utflen>>>6) );
162                                         }
163                                         else {
164                                                 utflen -= 0x4000;
165                                                 if(utflen < 0x200000) {
166                                                         bytearr = new byte[utflen+3];
167                                                         bytearr[byteIndex++] = (byte)( ((utflen&0x1f) | 0xc0) );
168                                                         bytearr[byteIndex++] = (byte)( ((utflen>>>5)&0xff) );
169                                                         bytearr[byteIndex++] = (byte)( ((utflen>>>13)&0xff) );  
170                                                 }
171                                                 else {
172                                                         utflen -= 0x200000;
173                                                         if(utflen < 0x10000000) {
174                                                                 bytearr = new byte[utflen+4];
175                                                                 bytearr[byteIndex++] = (byte)( ((utflen&0x0f) | 0xe0) );
176                                                                 bytearr[byteIndex++] = (byte)( ((utflen>>>4)&0xff) );
177                                                                 bytearr[byteIndex++] = (byte)( ((utflen>>>12)&0xff) );  
178                                                                 bytearr[byteIndex++] = (byte)( ((utflen>>>20)&0xff) );
179                                                         }
180                                                         else {
181                                                                 utflen -= 0x10000000;
182                                                                 bytearr = new byte[utflen+5];
183                                                                 bytearr[byteIndex++] = (byte)( ((utflen&0x07) | 0xf0) );
184                                                                 bytearr[byteIndex++] = (byte)( ((utflen>>>3)&0xff) );
185                                                                 bytearr[byteIndex++] = (byte)( ((utflen>>>11)&0xff) );  
186                                                                 bytearr[byteIndex++] = (byte)( ((utflen>>>19)&0xff) );
187                                                                 bytearr[byteIndex++] = (byte)( ((utflen>>>27)&0xff) );
188                                                         }
189                                                 }                               
190                                         }
191                                 }       
192
193
194                                 int i=0;
195                                 for (i=0; i<strlen; i++) {
196                                         c = str.charAt(i);
197                                         if (!((c >= 0x0001) && (c <= 0x007F))) break;
198                                         bytearr[byteIndex++] = (byte)(c);
199                                 }
200
201                                 for (;i < strlen; i++){
202                                         c = str.charAt(i);
203                                         if ((c >= 0x0001) && (c <= 0x007F)) {
204                                                 bytearr[byteIndex++] = (byte)( c );
205                                         } else if (c > 0x07FF) {
206                                                 bytearr[byteIndex++] = (byte)(0xE0 | ((c >> 12) & 0x0F));
207                                                 bytearr[byteIndex++] = (byte)(0x80 | ((c >>  6) & 0x3F));
208                                                 bytearr[byteIndex++] = (byte)(0x80 | ((c >>  0) & 0x3F));
209                                         } else {
210                                                 bytearr[byteIndex++] = (byte)(0xC0 | ((c >>  6) & 0x1F));
211                                                 bytearr[byteIndex++] = (byte)(0x80 | ((c >>  0) & 0x3F));
212                                         }
213                                 }
214
215                                 return bytearr;
216
217                         }
218
219                         @Override
220                         public byte[] serialize(Object obj) throws IOException {
221                                 try {
222                                         return writeUTF((String)obj);
223                                 } catch (IOException e) {
224                                         throw new SerializationException();
225                                 }
226                                 
227                         }
228                         
229                 }
230                 
231                 
232                 @Override
233                 public byte[] serialize(Object obj) throws IOException {
234                         try {
235                                 return writeUTF((String)obj);
236                         } catch (IOException e) {
237                                 throw new SerializationException();
238                         }
239                         
240                 }
241
242 //          private static byte bytearr[] = new byte[80];
243 //          private static char chararr[] = new char[80];
244                 
245             public final static String readUTF(byte[] bytearr) throws IOException {
246
247                 return "name";
248                 
249 //              int utflen = (bytearr[0] << 8) + bytearr[1];
250 //              
251 //              char[] chararr = new char[utflen*2];
252 //
253 //              int c, char2, char3;
254 //              int count = 2;
255 //              int chararr_count=0;
256 //
257 ////            in.readFully(bytearr, 0, utflen);
258 //
259 //              while (count < utflen + 2) {
260 //                  c = (int) bytearr[count] & 0xff;      
261 //                  if (c > 127) break;
262 //                  count++;
263 //                  chararr[chararr_count++]=(char)c;
264 //              }
265 //
266 //              while (count < utflen) {
267 //                  c = (int) bytearr[count] & 0xff;
268 //                  switch (c >> 4) {
269 //                      case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
270 //                          /* 0xxxxxxx*/
271 //                          count++;
272 //                          chararr[chararr_count++]=(char)c;
273 //                          break;
274 //                      case 12: case 13:
275 //                          /* 110x xxxx   10xx xxxx*/
276 //                          count += 2;
277 //                          if (count > utflen)
278 //                              throw new UTFDataFormatException(
279 //                                  "malformed input: partial character at end");
280 //                          char2 = (int) bytearr[count-1];
281 //                          if ((char2 & 0xC0) != 0x80)
282 //                              throw new UTFDataFormatException(
283 //                                  "malformed input around byte " + count); 
284 //                          chararr[chararr_count++]=(char)(((c & 0x1F) << 6) | 
285 //                                                          (char2 & 0x3F));  
286 //                          break;
287 //                      case 14:
288 //                          /* 1110 xxxx  10xx xxxx  10xx xxxx */
289 //                          count += 3;
290 //                          if (count > utflen)
291 //                              throw new UTFDataFormatException(
292 //                                  "malformed input: partial character at end");
293 //                          char2 = (int) bytearr[count-2];
294 //                          char3 = (int) bytearr[count-1];
295 //                          if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
296 //                              throw new UTFDataFormatException(
297 //                                  "malformed input around byte " + (count-1));
298 //                          chararr[chararr_count++]=(char)(((c     & 0x0F) << 12) |
299 //                                                          ((char2 & 0x3F) << 6)  |
300 //                                                          ((char3 & 0x3F) << 0));
301 //                          break;
302 //                      default:
303 //                          /* 10xx xxxx,  1111 xxxx */
304 //                          throw new UTFDataFormatException(
305 //                              "malformed input around byte " + count);
306 //                  }
307 //              }
308 //              if(chararr_count == 2*utflen) {
309 //                      // The number of chars produced may be less than utflen
310 //                      return new String(chararr);
311 //              } else {
312 //                      // The number of chars produced may be less than utflen
313 //                      return new String(chararr, 0, chararr_count);
314 //              }
315                 
316             }
317                 
318                 @Override
319                 public Object deserialize(byte[] data) throws IOException {
320                         return readUTF(data);
321                         //return new String(data, UTF8);
322                 }
323                 
324         }
325         
326         public static class FastStringBinding extends StringBindingDefault {
327                 
328                 public static FastStringBinding INSTANCE = new FastStringBinding(); 
329                 
330                 public FastStringBinding() {
331                         super(Datatypes.STRING);
332                 }
333
334                 @Override
335                 public Serializer serializer() {
336                         return FastStringSerializer.INSTANCE;
337                 }
338                 
339         }
340         
341         public static WriteOnlyResultRequest<Resource> writeOnly(final String name, final int[] sizes) throws DatabaseException {
342                 return writeOnly(name, sizes, null);
343         }
344
345         public static WriteOnlyResultRequest<Resource> writeOnly(final String name, final int[] sizes, VirtualGraph graph) throws DatabaseException {
346
347                 return new WriteOnlyResultRequest<Resource>(graph) {
348
349                         @Override
350                         public Resource perform(WriteOnlyGraph graph) throws DatabaseException {
351
352                                 //Binding binding = Bindings.STRING;
353 //                              Serializer serializer = Bindings.STRING.serializer();
354                                 Serializer serializer = FastStringSerializer2.INSTANCE;
355                                 Session session = graph.getService(Session.class);
356                                 Layer0 b = Layer0.getInstance(session);
357                                 
358                                 graph.flushCluster();
359
360                                 ClusterBuilder builder = graph.getService(ClusterBuilder.class);
361                                 SerialisationSupport ss = graph.getService(SerialisationSupport.class);
362                                 
363                                 ResourceHandle hasNameR = builder.resource(b.HasName);
364                                 ResourceHandle nameOfR = builder.resource(b.NameOf);
365                                 ResourceHandle consistsOfR = builder.resource(b.ConsistsOf);
366                                 ResourceHandle partOfR = builder.resource(b.PartOf);
367                                 ResourceHandle instanceOfR = builder.resource(b.InstanceOf);
368                                 ResourceHandle libraryR = builder.resource(b.Library);
369                                 ResourceHandle ontologyR = builder.resource(b.Ontology);
370                                 ResourceHandle stringR = builder.resource(b.String);
371                                 
372                                 StatementHandle instanceOf = builder.newStatement(instanceOfR, libraryR);
373                                 StatementHandle instanceOf2 = builder.newStatement(instanceOfR, ontologyR);
374                                 StatementHandle instanceOfString = builder.newStatement(instanceOfR, stringR);
375
376                                 ResourceHandle root = builder.newResource();
377                                 root.addStatement(instanceOf);
378
379                                 ResourceHandle rootLiteral = builder.newResource();
380                                 rootLiteral.addStatement(instanceOfString);
381                                 rootLiteral.addStatement(nameOfR, root);
382                                 rootLiteral.addValue(name, serializer);
383                                 root.addStatement(hasNameR, rootLiteral);
384
385 //                              System.out.println("root: " + root.resource());
386 //                              System.out.println("literal: " + rootLiteral.resource());
387
388                                 //              graph.addLiteral(root, b.HasName, b.NameOf, b.String, name, binding);
389                                 //              graph.claim(root, b.InstanceOf, null, b.Library);
390
391                                 StatementHandle rootPart = builder.newStatement(partOfR, root);
392
393                                 for(int i=0;i<sizes[0];i++) {
394
395                                         //                      long duration2 = System.nanoTime() - start;
396                                         //                      System.err.println("Writetime: " + 1e-9*duration2);
397
398                                         builder.newCluster();
399                                         
400                                         ResourceHandle level1 = builder.newResource();
401                                         ResourceHandle level1Literal = builder.newResource();
402                                         
403                                         level1Literal.addStatement(instanceOfString);
404                                         level1.addStatement(hasNameR, level1Literal);
405                                         level1Literal.addStatement(nameOfR, level1);
406                                         root.addStatement(consistsOfR, level1);
407                                         level1Literal.addValue(name, serializer);
408                                         level1.addStatement(instanceOf);
409                                         level1.addStatement(rootPart);
410
411                                         StatementHandle level1Part = builder.newStatement(partOfR, level1);
412
413                                         //                      Resource level1 = graph.newResource();
414                                         //                      graph.addLiteral(level1, b.HasName, b.NameOf, b.String, name, binding);
415                                         //                      graph.claim(level1, b.InstanceOf, null, b.Library);
416                                         //                      graph.claim(level1, b.PartOf, b.ConsistsOf, root);
417
418                                         for(int j=0;j<sizes[1];j++) {
419
420                                                 ResourceHandle level2 = builder.newResource();
421                                                 ResourceHandle level2Literal = builder.newResource();
422
423                                                 level2.addStatement(hasNameR, level2Literal);
424                                                 level2.addStatement(instanceOf);
425                                                 level2.addStatement(level1Part);
426                                                 level1.addStatement(consistsOfR, level2);
427
428                                                 level2Literal.addStatement(instanceOfString);
429                                                 level2Literal.addStatement(nameOfR, level2);
430                                                 level2Literal.addValue(name, serializer);
431
432                                                 StatementHandle level2Part = builder.newStatement(partOfR, level2);
433
434                                                 //                              Resource level2 = graph.newResource();
435                                                 //                              graph.addLiteral(level2, b.HasName, b.NameOf, b.String, name, binding);
436                                                 //                              graph.claim(level2, b.InstanceOf, null, b.Library);
437                                                 //                              graph.claim(level2, b.PartOf, b.ConsistsOf, level1);
438
439                                                 for(int k=0;k<sizes[2];k++) {
440
441                                                         ResourceHandle level3 = builder.newResource();
442                                                         ResourceHandle level3Literal = builder.newResource();
443                                                         
444                                                         level3.addStatement(hasNameR, level3Literal);
445                                                         level3.addStatement(instanceOf2);
446                                                         level3.addStatement(level2Part);
447                                                         level2.addStatement(consistsOfR, level3);
448                                                         
449                                                         level3Literal.addStatement(instanceOfString);
450                                                         level3Literal.addStatement(nameOfR, level3);
451                                                         level3Literal.addValue(name, serializer);
452
453                                                 }
454
455                                         }
456
457                                 }
458
459                                 //              long duration = System.nanoTime() - start;
460                                 //              System.err.println("Writetime: " + 1e-9*duration);
461
462                                 return root.resource(ss);
463                 
464                         }
465                         
466                 };
467
468         }
469
470         static int tot = 0;
471         static int quick = 0;
472         
473         public static WriteOnlyResultRequest<Resource> writeOnly2(final String name, final int[] sizes) throws DatabaseException {
474                 return writeOnly2(name, sizes, null);
475         }
476         
477         public static WriteOnlyResultRequest<Resource> writeOnly2(final String name, final int[] sizes, VirtualGraph graph) throws DatabaseException {
478
479                 return new WriteOnlyResultRequest<Resource>(graph) {
480
481                         @Override
482                         public Resource perform(WriteOnlyGraph graph) throws DatabaseException {
483
484                                 Binding binding = Bindings.STRING;
485
486                 Session session = graph.getService(Session.class);
487                                 Layer0 b = Layer0.getInstance(session);
488
489                                 Resource root = graph.newResource();
490                                 graph.addLiteral(root, b.HasName, b.NameOf, b.String, name, binding);
491                                 graph.claim(root, b.InstanceOf, null, b.Library);
492
493                                 for(int i=0;i<sizes[0];i++) {
494
495                                         Resource level1 = graph.newResource();
496                                         graph.addLiteral(level1, b.HasName, b.NameOf, b.String, name, binding);
497                                         graph.claim(level1, b.InstanceOf, null, b.Library);
498                                         graph.claim(level1, b.PartOf, b.ConsistsOf, root);
499
500                                         for(int j=0;j<sizes[1];j++) {
501
502                                                 Resource level2 = graph.newResource();
503                                                 graph.addLiteral(level2, b.HasName, b.NameOf, b.String, name, binding);
504                                                 graph.claim(level2, b.InstanceOf, null, b.Library);
505                                                 graph.claim(level2, b.PartOf, b.ConsistsOf, level1);
506
507                                                 for(int k=0;k<sizes[2];k++) {
508
509                                                         Resource level3 = graph.newResource();
510                                                         graph.addLiteral(level3, b.HasName, b.NameOf, b.String, name, binding);
511                                                         graph.claim(level3, b.InstanceOf, null, b.Library);
512                                                         graph.claim(level3, b.PartOf, b.ConsistsOf, level2);
513
514                                                 }
515
516                                         }
517
518                                 }
519
520                                 return root;
521
522                         }
523                         
524                 };
525
526         }
527
528         public static WriteResultRequest<Resource> write(final String name, final int[] sizes) throws DatabaseException {
529                 return write(name, sizes, null);
530         }
531         
532         public static WriteResultRequest<Resource> write(final String name, final int[] sizes, VirtualGraph graph) throws DatabaseException {
533
534                 return new WriteResultRequest<Resource>(graph) {
535
536                         @Override
537                         public Resource perform(WriteGraph graph) throws DatabaseException {
538
539                                 Binding binding = Bindings.STRING;
540
541                 Session session = graph.getService(Session.class);
542                                 Layer0 b = Layer0.getInstance(session);
543
544                                 Resource root = graph.newResource();
545                                 graph.addLiteral(root, b.HasName, b.NameOf, b.String, name, binding);
546                                 graph.claim(root, b.InstanceOf, null, b.Library);
547
548                                 for(int i=0;i<sizes[0];i++) {
549
550                                         Resource level1 = graph.newResource();
551                                         graph.addLiteral(level1, b.HasName, b.NameOf, b.String, name, binding);
552                                         graph.claim(level1, b.InstanceOf, null, b.Library);
553                                         graph.claim(level1, b.PartOf, b.ConsistsOf, root);
554
555                                         for(int j=0;j<sizes[1];j++) {
556
557                                                 Resource level2 = graph.newResource();
558                                                 graph.addLiteral(level2, b.HasName, b.NameOf, b.String, name, binding);
559                                                 graph.claim(level2, b.InstanceOf, null, b.Library);
560                                                 graph.claim(level2, b.PartOf, b.ConsistsOf, level1);
561
562                                                 for(int k=0;k<sizes[2];k++) {
563
564                                                         Resource level3 = graph.newResource();
565                                                         graph.addLiteral(level3, b.HasName, b.NameOf, b.String, name, binding);
566                                                         graph.claim(level3, b.InstanceOf, null, b.Library);
567                                                         graph.claim(level3, b.PartOf, b.ConsistsOf, level2);
568
569                                                 }
570
571                                         }
572
573                                 }
574
575                                 return root;
576
577                         }
578                         
579                 };
580
581         }
582
583         public static WriteResultRequest<Resource> write2(final String name, final int[] sizes) throws DatabaseException {
584                 return write(name, sizes, null);
585         }
586         
587         public static WriteResultRequest<Resource> write2(final String name, final int[] sizes, VirtualGraph graph) throws DatabaseException {
588
589                 return new WriteResultRequest<Resource>(graph) {
590
591                         @Override
592                         public Resource perform(WriteGraph graph) throws DatabaseException {
593
594                                 Binding binding = Bindings.STRING;
595
596                 Session session = graph.getService(Session.class);
597                                 Layer0 b = Layer0.getInstance(session);
598
599                                 Resource root = graph.newResource();
600                                 graph.claimLiteral(root, b.HasName, name, binding);
601                                 graph.claim(root, b.InstanceOf, null, b.Library);
602
603                                 for(int i=0;i<sizes[0];i++) {
604
605                                         Resource level1 = graph.newResource();
606                                         graph.claimLiteral(level1, b.HasName, name, binding);
607                                         graph.claim(level1, b.InstanceOf, null, b.Library);
608                                         graph.claim(level1, b.PartOf, root);
609
610                                         for(int j=0;j<sizes[1];j++) {
611
612                                                 Resource level2 = graph.newResource();
613                                                 graph.claimLiteral(level2, b.HasName, name, binding);
614                                                 graph.claim(level2, b.InstanceOf, null, b.Library);
615                                                 graph.claim(level2, b.PartOf, level1);
616
617                                                 for(int k=0;k<sizes[2];k++) {
618
619                                                         Resource level3 = graph.newResource();
620                                                         graph.claimLiteral(level3, b.HasName, name, binding);
621                                                         graph.claim(level3, b.InstanceOf, null, b.Library);
622                                                         graph.claim(level3, b.PartOf, level2);
623
624                                                 }
625
626                                         }
627
628                                 }
629
630                                 return root;
631
632                         }
633                         
634                 };
635
636         }
637
638         public static Read<Object> readSync(final Resource resource) throws DatabaseException {
639                 
640                 return new ReadRequest() {
641                         
642                         private void readChildren(ReadGraph graph, Resource resource) throws DatabaseException {
643 //                              System.err.println("child=" + resource);
644                                 Layer0 L0 = Layer0.getInstance(graph);
645                                 String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);
646 //                              System.err.println("name=" + name);
647                                 for(Resource child : graph.getObjects(resource, L0.ConsistsOf)) {
648                                         readChildren(graph, child);
649                                 }
650                         }
651                         
652                         @Override
653                         public void run(ReadGraph graph) throws DatabaseException {
654                                 readChildren(graph, resource);
655                         }
656                         
657                 };
658                 
659         }
660         
661         public static Read<Object> readSync2(final Resource resource) throws DatabaseException {
662                 
663                 return new ReadRequest() {
664                         
665                         private void readChildren(ReadGraph graph, Resource resource) throws DatabaseException {
666                                 Layer0 L0 = Layer0.getInstance(graph);
667                                 for(Resource child : graph.getObjects(resource, L0.ConsistsOf)) {
668                                         graph.getPossibleRelatedValue(child, L0.HasName);
669                                         readChildren(graph, child);
670                                 }
671                         }
672                         
673                         @Override
674                         public void run(ReadGraph graph) throws DatabaseException {
675                                 readChildren(graph, resource);
676                         }
677                         
678                 };
679                 
680         }
681
682
683         final private static boolean VALIDATE = false;
684
685         public static Vector<String> validation = new Vector<String>();
686         public static Vector<String> criteria = new Vector<String>();
687         
688         public static void validate() throws AssumptionException {
689                 if(VALIDATE) if(!validation.equals(criteria)) {
690                         for(String s : validation) System.err.println("-'" + s + "'");
691                         throw new AssumptionException("");
692                 }
693         }
694         
695         public static AsyncRead<Object> readAsync(final Resource resource) {
696                 
697                 if(VALIDATE) {
698                         for(int i=0;i<244*64*64;i++) criteria.add("name");
699                         validation.clear();
700                 }
701
702                 class Process {
703
704                         final SyncMultiProcedure<Resource> structure;
705                         final SyncProcedure<String> names;
706                         
707                         Process(ReadGraph graph, Resource resource) throws DatabaseException {
708
709                                 final Layer0 L0 = Layer0.getInstance(graph);
710                                 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
711                                 final QueryControl control = graph.getService(QueryControl.class);
712                                 
713                                 names = dqs.compilePossibleRelatedValue(graph, L0.HasName, new SyncProcedure<String>() {
714
715                                         @Override
716                                         public void execute(ReadGraph graph, String name) {
717                                                 if(VALIDATE) validation.add(name);
718 //                                              System.err.println("af=" + name);
719                                         }
720
721                                         @Override
722                                         public void exception(ReadGraph graph, Throwable throwable) {
723                                                 throwable.printStackTrace();
724                                         }
725
726                                 });
727                                 
728                                 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new SyncMultiProcedure<Resource>() {
729
730                                         @Override
731                                         public void execute(ReadGraph graph, Resource child) {
732                                                 //if(control.scheduleByCluster(graph, child, this)) {
733                                                         dqs.forEachObjectCompiled(graph, child, structure);
734                                                         dqs.forPossibleRelatedValueCompiled(graph, child, names);
735                                                 //}
736                                         }
737
738                                         @Override
739                                         public void finished(ReadGraph graph) {
740                                         }
741
742                                         @Override
743                                         public void exception(ReadGraph graph, Throwable throwable) {
744                                                 throwable.printStackTrace();
745                                         }
746
747                                 });
748
749                                 dqs.forEachObjectCompiled(graph, resource, structure);
750                                 dqs.forPossibleRelatedValueCompiled(graph, resource, names);
751                                 
752                         }
753                         
754                 }
755                 
756                 return new AsyncReadRequest() {
757                         
758                         @Override
759                         public void run(AsyncReadGraph graph) {
760
761                                 try {
762                                         new Process(graph, resource);
763                                 } catch (DatabaseException e) {
764                                         e.printStackTrace();
765                                 }
766
767                         }
768                         
769                 };
770
771         }
772
773         public static void readAsyncLoop2(AsyncReadGraph graph, Layer0 L0, Resource resource, AsyncMultiProcedure<Resource> procedure, AsyncProcedure<String> procedure2) {
774                 graph.forEachObject(resource, L0.ConsistsOf, procedure);
775                 graph.forPossibleRelatedValue(resource, L0.HasName, FastStringBinding.INSTANCE, procedure2);
776         }
777
778         public static AsyncReadRequest readAsync2(final Resource resource) {
779
780                 return new AsyncReadRequest() {
781
782                         @Override
783                         public void run(AsyncReadGraph graph) {
784
785                                 final Layer0 L0 = Layer0.getInstance(graph);
786                                 
787                                 try {
788
789                                         final AsyncProcedure<String> names = new AsyncProcedure<String>() {
790
791                                                 @Override
792                                                 public void execute(AsyncReadGraph graph, String name) {
793                                                 }
794
795                                                 @Override
796                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
797                                                         throwable.printStackTrace();
798                                                 }
799
800                                         };
801
802                                         readAsyncLoop2(graph, L0, resource, new AsyncMultiProcedure<Resource>() {
803
804                                                 @Override
805                                                 public void execute(AsyncReadGraph graph, Resource child) {
806                                                         readAsyncLoop2(graph, L0, child, this, names);
807                                                 }
808
809                                                 @Override
810                                                 public void finished(AsyncReadGraph graph) {
811                                                 }
812
813                                                 @Override
814                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
815                                                         throwable.printStackTrace();
816                                                 }
817
818                                         }, names);
819
820                                 } catch (Exception e) {
821
822                                         e.printStackTrace();
823
824                                 }
825
826                         }
827                         
828                 };
829
830         }
831         
832         static int names = 0;
833         
834         public static void readAsyncTypesLoop(final DirectQuerySupport dqs, final Layer0 L0, AsyncReadGraph graph, final Resource resource, final RelationInfo consistsOf, final AsyncMultiProcedure<Resource> procedure, final RelationInfo name, final Serializer serializer, final AsyncProcedure<String> procedure2) {
835                 
836                 dqs.forPossibleDirectType(graph, resource, dqs, new SyncContextProcedure<DirectQuerySupport, Resource>() {
837
838                         @Override
839                         public void execute(ReadGraph graph, DirectQuerySupport dqs, Resource type) {
840                                 
841 //                              System.err.println("affa");
842                                 
843                                 try {
844                                         graph.syncRequest(new TypeSetAndString(type), new SyncProcedure<Pair<Set<Resource>, String>>() {
845
846                                                 @Override
847                                                 public void execute(ReadGraph graph, Pair<Set<Resource>, String> typeInfo) {
848                                                         
849                                                         Set<Resource> types = typeInfo.first;
850                                                         if(types.contains(L0.Ontology)) {
851                                                                 
852 //                                                      dqs.forPossibleRelatedValue(graph, resource, name, serializer, new AsyncProcedure<String>() {
853 //
854 //                                                              @Override
855 //                                                              public void execute(AsyncReadGraph graph, String result) {
856 ////                                                                    if(names++ % 1000 == 0) System.err.println("names=" + names + "(" + result + ")");
857 //                                                              }
858 //
859 //                                                              @Override
860 //                                                              public void exception(AsyncReadGraph graph, Throwable throwable) {
861 //                                                              }
862 //                                                              
863 //                                                      });
864                                                                 
865                                                         } else if (types.contains(L0.Library)) {
866                                                                 //dqs.forEachObject(graph, resource, consistsOf, procedure);
867                                                         }
868                                                         
869                                                 }
870
871                                                 @Override
872                                                 public void exception(ReadGraph graph, Throwable throwable) {
873                                                 }
874                                                 
875                                         });
876                                 } catch (DatabaseException e) {
877                                         e.printStackTrace();
878                                 }
879                                 
880                         }
881
882                         @Override
883                         public void exception(ReadGraph graph, Throwable throwable) {
884                         }
885
886                 });
887                 
888                 
889         }
890
891         public static Read<Object> readAsyncTypes(final Resource resource) {
892
893                 return new ReadRequest() {
894                         
895                         @Override
896                         public void run(ReadGraph graph) {
897
898                                 final Layer0 L0 = Layer0.getInstance(graph);
899
900                                 try {
901
902 //                                      graph.syncRequest(new TypeSetAndString(L0.Library), new TransientCacheListener<Pair<Set<Resource>, String>>());
903 //                                      graph.syncRequest(new TypeSetAndString(L0.Ontology), new TransientCacheListener<Pair<Set<Resource>, String>>());
904
905                                         final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
906                                         final RelationInfo consistsOf = graph.syncRequest(new AsyncRead<RelationInfo>() {
907
908                                                 @Override
909                                                 public void perform(AsyncReadGraph graph, AsyncProcedure<RelationInfo> procedure) {
910                                                         try {
911                                                                 RelationInfo ri = dqs.getRelationInfo(graph, L0.ConsistsOf);
912                                                                 procedure.execute(graph, ri);
913                                                         } catch (DatabaseException e) {
914                                                                 procedure.exception(graph, e);
915                                                         }
916                                                 }
917
918                                 @Override
919                                     public int threadHash() {
920                                         return hashCode();
921                                     }
922
923                                                 @Override
924                                                 public int getFlags() {
925                                                         return 0;
926                                                 }
927
928                                         });
929                                         final RelationInfo name = graph.syncRequest(new AsyncRead<RelationInfo>() {
930
931                                                 @Override
932                                                 public void perform(AsyncReadGraph graph, AsyncProcedure<RelationInfo> procedure) {
933                                                         try {
934                                                                 RelationInfo ri = dqs.getRelationInfo(graph, L0.HasName);
935                                                                 procedure.execute(graph, ri);
936                                                         } catch (DatabaseException e) {
937                                                                 procedure.exception(graph, e);
938                                                         }
939                                                 }
940
941                                 @Override
942                                     public int threadHash() {
943                                         return hashCode();
944                                     }
945
946                                                 @Override
947                                                 public int getFlags() {
948                                                         return 0;
949                                                 }
950
951                                         });
952
953                                         final Serializer serializer = FastStringSerializer.INSTANCE;//Bindings.getSerializerUnchecked( Bindings.STRING ); 
954
955                                         final AsyncProcedure<String> names = new AsyncProcedure<String>() {
956
957                                                 @Override
958                                                 public void execute(AsyncReadGraph graph, String name) {
959 //                                                      System.out.println("exec: " + name);
960                                                 }
961
962                                                 @Override
963                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
964                                                         throwable.printStackTrace();
965                                                 }
966
967                                         };
968
969 //                                      readAsyncLoop(dqs, graph, resource, new AsyncMultiProcedure<Resource>() {
970 //
971 //                                              @Override
972 //                                              public void execute(AsyncReadGraph graph, Resource child) {
973 //                                                      readAsyncTypesLoop(dqs, L0, graph, child, consistsOf, this, name, serializer, names);
974 //                                              }
975 //
976 //                                              @Override
977 //                                              public void finished(AsyncReadGraph graph) {
978 //                                              }
979 //
980 //                                              @Override
981 //                                              public void exception(AsyncReadGraph graph, Throwable throwable) {
982 //                                                      throwable.printStackTrace();
983 //                                              }
984 //
985 //                                      }, name, serializer, names);
986
987                                 } catch (Exception e) {
988
989                                         e.printStackTrace();
990
991                                 }
992
993                         }
994                         
995                 };
996
997         }
998         
999         
1000 }