]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/TestCluster.java b/bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/TestCluster.java
new file mode 100644 (file)
index 0000000..341af38
--- /dev/null
@@ -0,0 +1,274 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.procore.cluster;\r
+\r
+import java.io.PrintStream;\r
+\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.impl.ClusterBase;\r
+import org.simantics.db.impl.ClusterI;\r
+import org.simantics.db.impl.ClusterI.CompleteTypeEnum;\r
+import org.simantics.db.impl.ClusterTranslator;\r
+import org.simantics.db.service.ClusterUID;\r
+\r
+import fi.vtt.simantics.procore.internal.SessionImplSocket;\r
+import gnu.trove.map.hash.TIntIntHashMap;\r
+\r
+final public class TestCluster {\r
+    private static final boolean DEBUG = false;\r
+    private static final int AddedResources = 100; //ClusterTraits.getMaxNumberOfResources();\r
+    private static final int AddedStatements = 100; // Must be less than or equal to number of resources.\r
+    private static final int ValueCapacity = 5;\r
+    private final ClusterBase builtinCluster;\r
+    private final ClusterUID BuiltinClusterUID = ClusterUID.Builtin;\r
+    private final int BuiltinClusterKey;\r
+    private final ClusterBase foreignCluster;\r
+    private final ClusterUID ForeignClusterUID = ClusterUID.make(/*ClusterUID.Builtin.first*/0, ClusterUID.Builtin.second+1);\r
+    private final ClusterBase objectCluster;\r
+    private final ClusterUID ObjectClusterUID = ClusterUID.make(/*ClusterUID.Builtin.first*/0, ClusterUID.Builtin.second+2);\r
+    private final ClusterTranslator support;\r
+    PrintStream out = System.out;\r
+    TestCluster(Session session) {\r
+        support = ((SessionImplSocket)session).clusterTranslator;\r
+        builtinCluster = support.getClusterByClusterUIDOrMake(BuiltinClusterUID);\r
+        BuiltinClusterKey = builtinCluster.getClusterKey();\r
+        foreignCluster = support.getClusterByClusterUIDOrMake(ForeignClusterUID);\r
+        objectCluster = support.getClusterByClusterUIDOrMake(ObjectClusterUID);\r
+    }\r
+    private void testReading(ClusterI cluster, int[] resourceKeys, int[] statementKeys, int[] objectKeys, boolean onePredicateOn, int AddedStatements, boolean foreignClusterOn)\r
+    throws DatabaseException {\r
+        Stopwatch sw = new Stopwatch();\r
+        sw.start();\r
+        for (int i = 0; i < AddedResources; ++i) {\r
+            int resourceKey = resourceKeys[i];\r
+            CompleteTypeEnum ct = cluster.getCompleteType(resourceKey, support);\r
+            if (ct != CompleteTypeEnum.NotComplete) {\r
+                int ck = cluster.getCompleteObjectKey(resourceKey, support);\r
+                Assert(0 != ck);\r
+                Assert(objectKeys[0] == ck);\r
+            }\r
+            final TIntIntHashMap predicates = new TIntIntHashMap();\r
+            final TIntIntHashMap countMap = new TIntIntHashMap();\r
+            countMap.clear();\r
+            ClusterI.ObjectProcedure<TIntIntHashMap> readObjects = new ClusterI.ObjectProcedure<TIntIntHashMap>() {\r
+                @Override\r
+                public boolean execute(final TIntIntHashMap objects, final int object) {\r
+                    int i = objects.get(object);\r
+                    objects.put(object, ++i);\r
+                    return false; // continue looping\r
+                }\r
+            };\r
+            ClusterI.PredicateProcedure<TIntIntHashMap> readPredicates = new ClusterI.PredicateProcedure<TIntIntHashMap>() {\r
+                @Override\r
+                public boolean execute(TIntIntHashMap set, int predicateKey, int objectIndex) {\r
+                    set.put(predicateKey, objectIndex);\r
+                    int i = countMap.get(predicateKey);\r
+                    countMap.put(predicateKey, ++i);\r
+                    return false; // continue looping\r
+                }\r
+            };\r
+            cluster.forPredicates(resourceKey, readPredicates, predicates, support);\r
+            if (onePredicateOn)\r
+                Assert(predicates.size() == 1);\r
+            else {\r
+                Assert(predicates.size() == AddedStatements);\r
+                //Assert(cluster.isComplete(fyi, resourceKey, support));\r
+                //Assert(cluster.getCompleteType(fyi, resourceKey, support) == ClusterI.CompleteTypeEnum.InstanceOf);\r
+            }\r
+            for (int j = 0; j < AddedStatements; ++j) {\r
+                int pKey = onePredicateOn ? resourceKey : statementKeys[j];\r
+                if (onePredicateOn && foreignClusterOn)\r
+                    pKey = statementKeys[0];\r
+                Assert(predicates.contains(pKey));\r
+                Assert(countMap.get(pKey) == 1);\r
+                int oIndex = predicates.get(pKey);\r
+                if (AddedStatements < 3)\r
+                    Assert(oIndex == 0);\r
+                TIntIntHashMap objects = new TIntIntHashMap();\r
+                TIntIntHashMap objects2 = new TIntIntHashMap();\r
+                cluster.forObjects(resourceKey, pKey, oIndex, readObjects, objects, support);\r
+                cluster.forObjects(resourceKey, pKey, readObjects, objects2, support);\r
+                Assert(objects.size() == objects2.size());\r
+                if (onePredicateOn) {\r
+                    Assert(objects.size() == AddedStatements);\r
+                    for (int k = 0; k < AddedStatements; ++k) {\r
+                        int oKey = objectKeys[k];\r
+                        Assert(objects.contains(oKey));\r
+                        Assert(1 == objects.get(oKey));\r
+                        Assert(objects2.contains(oKey));\r
+                        Assert(1 == objects2.get(oKey));\r
+                    }\r
+                } else {\r
+                    Assert(objects.size() == 1);\r
+                    int oKey = objectKeys[j];\r
+                    Assert(objects.contains(oKey));\r
+                    Assert(1 == objects.get(oKey));\r
+                    Assert(objects2.contains(oKey));\r
+                    Assert(1 == objects2.get(oKey));\r
+                }\r
+            }\r
+            if (!cluster.hasValue(resourceKey, support))\r
+                throw new RuntimeException("hasValue() failed for resource key=" + resourceKey);\r
+            byte[] data = cluster.getValue(resourceKey, support);\r
+            Assert(data.length == ValueCapacity);\r
+            for (int l = 0; l < ValueCapacity; ++l)\r
+                Assert((byte) l == data[l]);\r
+        }\r
+        sw.stop();\r
+        out.println("Elapsed time in millseconds " + sw.elapsedMilli() + " for reading.");\r
+        // cluster.check();\r
+        // cluster.printDebugInfo(-1, "koss: ", support);\r
+        out.println("Used space consumption in bytes: " + cluster.getUsedSpace());\r
+\r
+    }\r
+    private int getResourceKey(int clusterKey, int resourceIndex)\r
+    throws DatabaseException {\r
+        return ClusterTraits.createResourceKey(clusterKey, resourceIndex);\r
+    }\r
+    private boolean clusterTest(boolean onePredicateOn, boolean foreignClusterOn, int AddedStatements)\r
+    throws DatabaseException {\r
+        String testName = "Cluster ";\r
+        out.println("********************************************");\r
+        String eo = onePredicateOn ? "on" : "off";\r
+        String fco = foreignClusterOn ? "on" : "off";\r
+        out.println(testName + " test with one predicate " + eo + ", foreign cluster " + fco);\r
+\r
+        Stopwatch sw = new Stopwatch();\r
+        sw.start();\r
+\r
+        ClusterI cluster = ClusterImpl.make(BuiltinClusterUID, BuiltinClusterKey, support);\r
+        if (DEBUG)\r
+            System.out.println("cluster key=" + cluster.getClusterKey() + " id=" + cluster.getClusterId() + " uid=" + cluster.getClusterUID());\r
+        byte[] value = new byte[ValueCapacity];\r
+        for (int i = 0; i < ValueCapacity; ++i)\r
+            value[i] = (byte) i;\r
+\r
+        Assert(AddedResources <= ClusterTraits.getMaxNumberOfResources());\r
+        int[] resourceKeys = new int[AddedResources];\r
+        for (int i = 0; i < AddedResources; ++i) {\r
+            resourceKeys[i] = cluster.createResource(support);\r
+            Assert(ClusterTraits.getResourceIndexFromResourceKey(resourceKeys[i]) == i + 1);\r
+            Assert(!cluster.isComplete(resourceKeys[i], support));\r
+        }\r
+        int[] statementKeys = new int[AddedStatements];\r
+        int[] objectKeys = new int[AddedStatements];\r
+        for (int i = 0; i < AddedStatements; ++i) {\r
+            if (foreignClusterOn) {\r
+                Assert(AddedStatements <= ClusterTraits.getMaxNumberOfResources());\r
+                statementKeys[i] = getResourceKey(foreignCluster.getClusterKey(), i + 1);\r
+            } else {\r
+                Assert(AddedStatements <= AddedResources);\r
+                statementKeys[i] = getResourceKey(builtinCluster.getClusterKey(), i + 1);\r
+            }\r
+            objectKeys[i] = getResourceKey(objectCluster.getClusterKey(), i + 1);\r
+        }\r
+        for (int i = 0; i < AddedResources; ++i) {\r
+            final int resourceKey = resourceKeys[i];\r
+            for (int j = 0; j < AddedStatements; ++j) {\r
+                int sKey = resourceKey;\r
+                int pKey = onePredicateOn ? resourceKey : statementKeys[j];\r
+                if (onePredicateOn && foreignClusterOn)\r
+                    pKey = statementKeys[0];\r
+                int oKey = objectKeys[j];\r
+                cluster = cluster.addRelation(sKey, pKey, oKey, support);\r
+                if (null == cluster)\r
+                    throw new RuntimeException("AddRelation() failed.");\r
+                if (null != cluster.addRelation(sKey, pKey, oKey, support))\r
+                    throw new RuntimeException("AddRelation() failed.");\r
+            }\r
+            cluster = cluster.setValue(resourceKey, value, value.length, support);\r
+            if (!cluster.hasValue(resourceKey, support))\r
+                throw new RuntimeException("AddRelation() failed.");\r
+            byte[] data = cluster.getValue(resourceKey, support);\r
+            Assert(data.length == value.length);\r
+            for (int l = 0; l < value.length; ++l)\r
+                Assert((byte) l == data[l]);\r
+        }\r
+\r
+        sw.stop();\r
+        out.println("Elapsed time in milliseconds " + sw.elapsedMilli() + " for adding " + AddedResources + " Resources with " + AddedStatements + " statements.");\r
+\r
+        testReading(cluster, resourceKeys, statementKeys, objectKeys, onePredicateOn, AddedStatements, foreignClusterOn);\r
+\r
+        if (!(cluster instanceof ClusterBig)) {\r
+            sw.restart();\r
+            ClusterI big = ((ClusterImpl)cluster).toBig(support);\r
+            sw.stop();\r
+            out.println("Elapsed time in milliseconds " + sw.elapsedMilli() + " for converting to big.");\r
+            testReading(big, resourceKeys, statementKeys, objectKeys, onePredicateOn, AddedStatements, foreignClusterOn);\r
+        }\r
+\r
+        sw.restart();\r
+        for (int i = 0; i < AddedResources; ++i) {\r
+            int resourceKey = resourceKeys[i];\r
+            for (int j = 0; j < AddedStatements; ++j) {\r
+                int sKey = resourceKey;\r
+                int pKey = onePredicateOn ? resourceKey : statementKeys[j];\r
+                if (onePredicateOn && foreignClusterOn)\r
+                    pKey = statementKeys[0];\r
+                int oKey = objectKeys[j];\r
+                if (!cluster.removeRelation(sKey, pKey, oKey, support))\r
+                    throw new RuntimeException("RemoveRelation() failed.");\r
+                if (cluster.removeRelation(sKey, pKey, oKey, support))\r
+                    throw new RuntimeException("RemoveRelation() failed.");\r
+                cluster.denyRelation(sKey, pKey, oKey, support);\r
+            }\r
+            if (!cluster.removeValue(resourceKey, support))\r
+                throw new RuntimeException("removeValue() failed.");\r
+            if (cluster.removeValue(resourceKey, support))\r
+                throw new RuntimeException("removeValue() failed.");\r
+        }\r
+        sw.stop();\r
+        out.println("Elapsed time in millseconds " + sw.elapsedMilli() + " for deleting.");\r
+        out.println("Used space consumption in bytes: " + cluster.getUsedSpace());\r
+        return false; // ok\r
+    }\r
+\r
+    private void testBasic()\r
+    throws DatabaseException {\r
+        final int N = AddedStatements;\r
+        support.setStreamOff(true);\r
+        try {\r
+            for (int i = N; i < N + 1; ++i) {\r
+                clusterTest(false, false, i);\r
+                clusterTest(false, true, i);\r
+                clusterTest(true, false, i);\r
+                clusterTest(true, true, i);\r
+            }\r
+        } finally {\r
+            support.setStreamOff(false);\r
+        }\r
+    }\r
+\r
+    private void Assert(boolean condition)\r
+    throws DatabaseException {\r
+        if (condition)\r
+            return;\r
+        DatabaseException e = new DatabaseException("Test failed!");\r
+        e.printStackTrace(out);\r
+        throw e;\r
+    }\r
+    public static void test(Session session) {\r
+        TestCluster tc = new TestCluster(session);\r
+        try {\r
+            System.out.println("Begin of tests.");\r
+            tc.testBasic();\r
+            System.out.println("End of tests.");\r
+        } catch (Throwable t) {\r
+            throw new RuntimeException("Tests failed.", t);\r
+        }\r
+    }\r
+    public static void main(String[] args) {\r
+        test(null);\r
+    }\r
+}\r