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