--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.scalability;
+
+import java.util.Collection;
+
+import org.junit.Test;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteOnlyGraph;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.db.tests.common.Configuration;
+import org.simantics.layer0.Layer0;
+
+public class LargeImportTest extends ExistingDatabaseTest {
+
+ private static final int OUTER_INSTANCE_COUNT = Configuration.get().importOuterCount; // these go to the root library
+ private static final int INNER_INSTANCE_COUNT = Configuration.get().importInnerCount; // these are for each outer instance
+ private static Layer0 l0;
+
+ /**
+ * Creates new type and new instance of it. Then tries to find the instance and the type, and verifies that inheritance is correct.
+ * @throws Exception
+ */
+ @Test
+ public void testImport() throws Exception{
+ Session session = getSession();
+ final Resource rootLib = getSession().getRootLibrary();
+ final String random = getRandomString();
+ final String newTypeName = "New Type " + random;
+ session.syncRequest(new TestReadRequest() {
+ @Override
+ public void run(ReadGraph g) throws Exception {
+ l0 = Layer0.getInstance(g);
+ assert(null != l0.InstanceOf);
+ assert(null != l0.Type);
+ assert(null != l0.Inherits);
+ assert(null != l0.SupertypeOf);
+ assert(null != l0.HasName);
+ assert(null != l0.ConsistsOf);
+ }
+ });
+ session.syncRequest(new WriteOnlyQuery() {
+ @Override
+ public void run(WriteOnlyGraph g) throws Throwable {
+ Resource newType = g.newResource();
+ g.claim(newType, l0.InstanceOf, null, l0.Type);
+ g.claim(newType, l0.Inherits, l0.SupertypeOf, l0.Type);
+ g.claimValue(newType, newTypeName, Bindings.STRING);
+ g.claim(rootLib, l0.ConsistsOf, l0.PartOf, newType);
+ for (int i=0; i<OUTER_INSTANCE_COUNT; ++i) {
+ //System.out.println("Write outer=" + i);
+ Resource outerInstance = g.newResource();
+ g.claim(outerInstance, l0.InstanceOf, null, newType);
+ String outerInstanceName = random+(i+1);
+ g.claimValue(outerInstance, outerInstanceName, Bindings.STRING);
+ g.claim(rootLib, l0.ConsistsOf, l0.PartOf, outerInstance);
+ for (int j=0; j<INNER_INSTANCE_COUNT; ++j) {
+ Resource innerInstance = g.newResource();
+ g.claim(innerInstance, l0.InstanceOf, null, newType);
+ String innerInstanceName = ""+(j+1);
+ g.claimValue(innerInstance, innerInstanceName, Bindings.STRING);
+ g.claim(outerInstance, l0.ConsistsOf, l0.PartOf, innerInstance);
+ }
+// if (((flush++)%10000)==0)
+// g.flushCluster();
+ }
+ }
+ });
+
+ checkException();
+
+ session.syncRequest(new TestReadRequest() {
+ @Override
+ public void run(ReadGraph g) throws Exception {
+ Layer0 l0 =Layer0.getInstance(g);
+ Collection<Resource> resources = g.getObjects(rootLib, l0.ConsistsOf);
+ Resource newType = null;
+ Resource newInstance[] = new Resource[OUTER_INSTANCE_COUNT];
+ for (Resource r : resources) {
+ String name = g.getPossibleValue(r, Bindings.STRING);
+ if (null == name)
+ continue;
+ if (name.equals(newTypeName)) {
+ newType = r;
+ }
+ if (!name.startsWith(random, 0))
+ continue;
+ name = name.substring(random.length());
+ int index = 0;
+ try {
+ index = Integer.parseInt(name);
+ } catch (NumberFormatException e) {
+ continue; // not a number
+ }
+ if (index > 0 && index < OUTER_INSTANCE_COUNT+1) {
+ newInstance[index-1] = r;
+ }
+ }
+ if (newType == null)
+ throw new Exception("Could not find created type");
+ for (int i=0; i<OUTER_INSTANCE_COUNT; ++i) {
+ //System.out.println("Read outer=" + i);
+ if (newInstance[i] == null)
+ throw new Exception("Could not find created instance");
+ if (!g.isInstanceOf(newInstance[i], l0.Type))
+ throw new Exception("Created resource is not an instance of Type");
+ if (!g.isInstanceOf(newInstance[i], newType))
+ throw new Exception("Created resource is not an instance of created type");
+ resources = g.getObjects(newInstance[i], l0.ConsistsOf);
+ if (resources.size() != INNER_INSTANCE_COUNT)
+ fail("Inner instance count does not match." +
+ " Read count=" + resources.size() +
+ ", expected=" + INNER_INSTANCE_COUNT);
+ for (Resource r : resources) {
+ if (!g.isInstanceOf(r, l0.Type))
+ throw new Exception("Created resource is not an instance of Type");
+ if (!g.isInstanceOf(r, newType))
+ throw new Exception("Created resource is not an instance of created type");
+ String name = g.getPossibleValue(r, Bindings.STRING);
+ if (null == name)
+ fail("Inner instance name is not correct.");
+ Integer.parseInt(name);
+ }
+ }
+ }
+ });
+
+ checkException();
+
+ }
+}