]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/TestCluster.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / TestCluster.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.procore.cluster;
13
14 import java.io.PrintStream;
15
16 import org.simantics.db.Session;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.ClusterBase;
19 import org.simantics.db.impl.ClusterI;
20 import org.simantics.db.impl.ClusterI.CompleteTypeEnum;
21 import org.simantics.db.impl.ClusterTranslator;
22 import org.simantics.db.service.ClusterUID;
23
24 import fi.vtt.simantics.procore.internal.SessionImplSocket;
25 import gnu.trove.map.hash.TIntIntHashMap;
26
27 final public class TestCluster {
28     private static final boolean DEBUG = false;
29     private static final int AddedResources = 100; //ClusterTraits.getMaxNumberOfResources();
30     private static final int AddedStatements = 100; // Must be less than or equal to number of resources.
31     private static final int ValueCapacity = 5;
32     private final ClusterBase builtinCluster;
33     private final ClusterUID BuiltinClusterUID = ClusterUID.Builtin;
34     private final int BuiltinClusterKey;
35     private final ClusterBase foreignCluster;
36     private final ClusterUID ForeignClusterUID = ClusterUID.make(/*ClusterUID.Builtin.first*/0, ClusterUID.Builtin.second+1);
37     private final ClusterBase objectCluster;
38     private final ClusterUID ObjectClusterUID = ClusterUID.make(/*ClusterUID.Builtin.first*/0, ClusterUID.Builtin.second+2);
39     private final ClusterTranslator support;
40     PrintStream out = System.out;
41     TestCluster(Session session) {
42         support = ((SessionImplSocket)session).clusterTranslator;
43         builtinCluster = support.getClusterByClusterUIDOrMake(BuiltinClusterUID);
44         BuiltinClusterKey = builtinCluster.getClusterKey();
45         foreignCluster = support.getClusterByClusterUIDOrMake(ForeignClusterUID);
46         objectCluster = support.getClusterByClusterUIDOrMake(ObjectClusterUID);
47     }
48     private void testReading(ClusterI cluster, int[] resourceKeys, int[] statementKeys, int[] objectKeys, boolean onePredicateOn, int AddedStatements, boolean foreignClusterOn)
49     throws DatabaseException {
50         Stopwatch sw = new Stopwatch();
51         sw.start();
52         for (int i = 0; i < AddedResources; ++i) {
53             int resourceKey = resourceKeys[i];
54             CompleteTypeEnum ct = cluster.getCompleteType(resourceKey, support);
55             if (ct != CompleteTypeEnum.NotComplete) {
56                 int ck = cluster.getCompleteObjectKey(resourceKey, support);
57                 Assert(0 != ck);
58                 Assert(objectKeys[0] == ck);
59             }
60             final TIntIntHashMap predicates = new TIntIntHashMap();
61             final TIntIntHashMap countMap = new TIntIntHashMap();
62             countMap.clear();
63             ClusterI.ObjectProcedure<TIntIntHashMap> readObjects = new ClusterI.ObjectProcedure<TIntIntHashMap>() {
64                 @Override
65                 public boolean execute(final TIntIntHashMap objects, final int object) {
66                     int i = objects.get(object);
67                     objects.put(object, ++i);
68                     return false; // continue looping
69                 }
70             };
71             ClusterI.PredicateProcedure<TIntIntHashMap> readPredicates = new ClusterI.PredicateProcedure<TIntIntHashMap>() {
72                 @Override
73                 public boolean execute(TIntIntHashMap set, int predicateKey, int objectIndex) {
74                     set.put(predicateKey, objectIndex);
75                     int i = countMap.get(predicateKey);
76                     countMap.put(predicateKey, ++i);
77                     return false; // continue looping
78                 }
79             };
80             cluster.forPredicates(resourceKey, readPredicates, predicates, support);
81             if (onePredicateOn)
82                 Assert(predicates.size() == 1);
83             else {
84                 Assert(predicates.size() == AddedStatements);
85                 //Assert(cluster.isComplete(fyi, resourceKey, support));
86                 //Assert(cluster.getCompleteType(fyi, resourceKey, support) == ClusterI.CompleteTypeEnum.InstanceOf);
87             }
88             for (int j = 0; j < AddedStatements; ++j) {
89                 int pKey = onePredicateOn ? resourceKey : statementKeys[j];
90                 if (onePredicateOn && foreignClusterOn)
91                     pKey = statementKeys[0];
92                 Assert(predicates.contains(pKey));
93                 Assert(countMap.get(pKey) == 1);
94                 int oIndex = predicates.get(pKey);
95                 if (AddedStatements < 3)
96                     Assert(oIndex == 0);
97                 TIntIntHashMap objects = new TIntIntHashMap();
98                 TIntIntHashMap objects2 = new TIntIntHashMap();
99                 cluster.forObjects(resourceKey, pKey, oIndex, readObjects, objects, support);
100                 cluster.forObjects(resourceKey, pKey, readObjects, objects2, support);
101                 Assert(objects.size() == objects2.size());
102                 if (onePredicateOn) {
103                     Assert(objects.size() == AddedStatements);
104                     for (int k = 0; k < AddedStatements; ++k) {
105                         int oKey = objectKeys[k];
106                         Assert(objects.contains(oKey));
107                         Assert(1 == objects.get(oKey));
108                         Assert(objects2.contains(oKey));
109                         Assert(1 == objects2.get(oKey));
110                     }
111                 } else {
112                     Assert(objects.size() == 1);
113                     int oKey = objectKeys[j];
114                     Assert(objects.contains(oKey));
115                     Assert(1 == objects.get(oKey));
116                     Assert(objects2.contains(oKey));
117                     Assert(1 == objects2.get(oKey));
118                 }
119             }
120             if (!cluster.hasValue(resourceKey, support))
121                 throw new RuntimeException("hasValue() failed for resource key=" + resourceKey);
122             byte[] data = cluster.getValue(resourceKey, support);
123             Assert(data.length == ValueCapacity);
124             for (int l = 0; l < ValueCapacity; ++l)
125                 Assert((byte) l == data[l]);
126         }
127         sw.stop();
128         out.println("Elapsed time in millseconds " + sw.elapsedMilli() + " for reading.");
129         // cluster.check();
130         // cluster.printDebugInfo(-1, "koss: ", support);
131         out.println("Used space consumption in bytes: " + cluster.getUsedSpace());
132
133     }
134     private int getResourceKey(int clusterKey, int resourceIndex)
135     throws DatabaseException {
136         return ClusterTraits.createResourceKey(clusterKey, resourceIndex);
137     }
138     private boolean clusterTest(boolean onePredicateOn, boolean foreignClusterOn, int AddedStatements)
139     throws DatabaseException {
140         String testName = "Cluster ";
141         out.println("********************************************");
142         String eo = onePredicateOn ? "on" : "off";
143         String fco = foreignClusterOn ? "on" : "off";
144         out.println(testName + " test with one predicate " + eo + ", foreign cluster " + fco);
145
146         Stopwatch sw = new Stopwatch();
147         sw.start();
148
149         ClusterI cluster = ClusterImpl.make(BuiltinClusterUID, BuiltinClusterKey, support);
150         if (DEBUG)
151             System.out.println("cluster key=" + cluster.getClusterKey() + " id=" + cluster.getClusterId() + " uid=" + cluster.getClusterUID());
152         byte[] value = new byte[ValueCapacity];
153         for (int i = 0; i < ValueCapacity; ++i)
154             value[i] = (byte) i;
155
156         Assert(AddedResources <= ClusterTraits.getMaxNumberOfResources());
157         int[] resourceKeys = new int[AddedResources];
158         for (int i = 0; i < AddedResources; ++i) {
159             resourceKeys[i] = cluster.createResource(support);
160             Assert(ClusterTraits.getResourceIndexFromResourceKey(resourceKeys[i]) == i + 1);
161             Assert(!cluster.isComplete(resourceKeys[i], support));
162         }
163         int[] statementKeys = new int[AddedStatements];
164         int[] objectKeys = new int[AddedStatements];
165         for (int i = 0; i < AddedStatements; ++i) {
166             if (foreignClusterOn) {
167                 Assert(AddedStatements <= ClusterTraits.getMaxNumberOfResources());
168                 statementKeys[i] = getResourceKey(foreignCluster.getClusterKey(), i + 1);
169             } else {
170                 Assert(AddedStatements <= AddedResources);
171                 statementKeys[i] = getResourceKey(builtinCluster.getClusterKey(), i + 1);
172             }
173             objectKeys[i] = getResourceKey(objectCluster.getClusterKey(), i + 1);
174         }
175         for (int i = 0; i < AddedResources; ++i) {
176             final int resourceKey = resourceKeys[i];
177             for (int j = 0; j < AddedStatements; ++j) {
178                 int sKey = resourceKey;
179                 int pKey = onePredicateOn ? resourceKey : statementKeys[j];
180                 if (onePredicateOn && foreignClusterOn)
181                     pKey = statementKeys[0];
182                 int oKey = objectKeys[j];
183                 cluster = cluster.addRelation(sKey, pKey, oKey, support);
184                 if (null == cluster)
185                     throw new RuntimeException("AddRelation() failed.");
186                 if (null != cluster.addRelation(sKey, pKey, oKey, support))
187                     throw new RuntimeException("AddRelation() failed.");
188             }
189             cluster = cluster.setValue(resourceKey, value, value.length, support);
190             if (!cluster.hasValue(resourceKey, support))
191                 throw new RuntimeException("AddRelation() failed.");
192             byte[] data = cluster.getValue(resourceKey, support);
193             Assert(data.length == value.length);
194             for (int l = 0; l < value.length; ++l)
195                 Assert((byte) l == data[l]);
196         }
197
198         sw.stop();
199         out.println("Elapsed time in milliseconds " + sw.elapsedMilli() + " for adding " + AddedResources + " Resources with " + AddedStatements + " statements.");
200
201         testReading(cluster, resourceKeys, statementKeys, objectKeys, onePredicateOn, AddedStatements, foreignClusterOn);
202
203         if (!(cluster instanceof ClusterBig)) {
204             sw.restart();
205             ClusterI big = ((ClusterImpl)cluster).toBig(support);
206             sw.stop();
207             out.println("Elapsed time in milliseconds " + sw.elapsedMilli() + " for converting to big.");
208             testReading(big, resourceKeys, statementKeys, objectKeys, onePredicateOn, AddedStatements, foreignClusterOn);
209         }
210
211         sw.restart();
212         for (int i = 0; i < AddedResources; ++i) {
213             int resourceKey = resourceKeys[i];
214             for (int j = 0; j < AddedStatements; ++j) {
215                 int sKey = resourceKey;
216                 int pKey = onePredicateOn ? resourceKey : statementKeys[j];
217                 if (onePredicateOn && foreignClusterOn)
218                     pKey = statementKeys[0];
219                 int oKey = objectKeys[j];
220                 if (!cluster.removeRelation(sKey, pKey, oKey, support))
221                     throw new RuntimeException("RemoveRelation() failed.");
222                 if (cluster.removeRelation(sKey, pKey, oKey, support))
223                     throw new RuntimeException("RemoveRelation() failed.");
224                 cluster.denyRelation(sKey, pKey, oKey, support);
225             }
226             if (!cluster.removeValue(resourceKey, support))
227                 throw new RuntimeException("removeValue() failed.");
228             if (cluster.removeValue(resourceKey, support))
229                 throw new RuntimeException("removeValue() failed.");
230         }
231         sw.stop();
232         out.println("Elapsed time in millseconds " + sw.elapsedMilli() + " for deleting.");
233         out.println("Used space consumption in bytes: " + cluster.getUsedSpace());
234         return false; // ok
235     }
236
237     private void testBasic()
238     throws DatabaseException {
239         final int N = AddedStatements;
240         support.setStreamOff(true);
241         try {
242             for (int i = N; i < N + 1; ++i) {
243                 clusterTest(false, false, i);
244                 clusterTest(false, true, i);
245                 clusterTest(true, false, i);
246                 clusterTest(true, true, i);
247             }
248         } finally {
249             support.setStreamOff(false);
250         }
251     }
252
253     private void Assert(boolean condition)
254     throws DatabaseException {
255         if (condition)
256             return;
257         DatabaseException e = new DatabaseException("Test failed!");
258         e.printStackTrace(out);
259         throw e;
260     }
261     public static void test(Session session) {
262         TestCluster tc = new TestCluster(session);
263         try {
264             System.out.println("Begin of tests.");
265             tc.testBasic();
266             System.out.println("End of tests.");
267         } catch (Throwable t) {
268             throw new RuntimeException("Tests failed.", t);
269         }
270     }
271     public static void main(String[] args) {
272         test(null);
273     }
274 }