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