1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.procore.cluster;
14 import java.io.PrintStream;
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;
24 import fi.vtt.simantics.procore.internal.SessionImplSocket;
25 import gnu.trove.map.hash.TIntIntHashMap;
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);
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();
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);
58 Assert(objectKeys[0] == ck);
60 final TIntIntHashMap predicates = new TIntIntHashMap();
61 final TIntIntHashMap countMap = new TIntIntHashMap();
63 ClusterI.ObjectProcedure<TIntIntHashMap> readObjects = new ClusterI.ObjectProcedure<TIntIntHashMap>() {
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
71 ClusterI.PredicateProcedure<TIntIntHashMap> readPredicates = new ClusterI.PredicateProcedure<TIntIntHashMap>() {
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
80 cluster.forPredicates(resourceKey, readPredicates, predicates, support);
82 Assert(predicates.size() == 1);
84 Assert(predicates.size() == AddedStatements);
85 //Assert(cluster.isComplete(fyi, resourceKey, support));
86 //Assert(cluster.getCompleteType(fyi, resourceKey, support) == ClusterI.CompleteTypeEnum.InstanceOf);
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)
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));
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));
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]);
128 out.println("Elapsed time in millseconds " + sw.elapsedMilli() + " for reading.");
130 // cluster.printDebugInfo(-1, "koss: ", support);
131 out.println("Used space consumption in bytes: " + cluster.getUsedSpace());
134 private int getResourceKey(int clusterKey, int resourceIndex)
135 throws DatabaseException {
136 return ClusterTraits.createResourceKey(clusterKey, resourceIndex);
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);
146 Stopwatch sw = new Stopwatch();
149 ClusterI cluster = ClusterImpl.make(BuiltinClusterUID, BuiltinClusterKey, support);
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)
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));
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);
170 Assert(AddedStatements <= AddedResources);
171 statementKeys[i] = getResourceKey(builtinCluster.getClusterKey(), i + 1);
173 objectKeys[i] = getResourceKey(objectCluster.getClusterKey(), i + 1);
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);
185 throw new RuntimeException("AddRelation() failed.");
186 if (null != cluster.addRelation(sKey, pKey, oKey, support))
187 throw new RuntimeException("AddRelation() failed.");
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]);
199 out.println("Elapsed time in milliseconds " + sw.elapsedMilli() + " for adding " + AddedResources + " Resources with " + AddedStatements + " statements.");
201 testReading(cluster, resourceKeys, statementKeys, objectKeys, onePredicateOn, AddedStatements, foreignClusterOn);
203 if (!(cluster instanceof ClusterBig)) {
205 ClusterI big = ((ClusterImpl)cluster).toBig(support);
207 out.println("Elapsed time in milliseconds " + sw.elapsedMilli() + " for converting to big.");
208 testReading(big, resourceKeys, statementKeys, objectKeys, onePredicateOn, AddedStatements, foreignClusterOn);
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);
226 if (!cluster.removeValue(resourceKey, support))
227 throw new RuntimeException("removeValue() failed.");
228 if (cluster.removeValue(resourceKey, support))
229 throw new RuntimeException("removeValue() failed.");
232 out.println("Elapsed time in millseconds " + sw.elapsedMilli() + " for deleting.");
233 out.println("Used space consumption in bytes: " + cluster.getUsedSpace());
237 private void testBasic()
238 throws DatabaseException {
239 final int N = AddedStatements;
240 support.setStreamOff(true);
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);
249 support.setStreamOff(false);
253 private void Assert(boolean condition)
254 throws DatabaseException {
257 DatabaseException e = new DatabaseException("Test failed!");
258 e.printStackTrace(out);
261 public static void test(Session session) {
262 TestCluster tc = new TestCluster(session);
264 System.out.println("Begin of tests.");
266 System.out.println("End of tests.");
267 } catch (Throwable t) {
268 throw new RuntimeException("Tests failed.", t);
271 public static void main(String[] args) {