]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexSchema.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexSchema.java
diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexSchema.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexSchema.java
new file mode 100644 (file)
index 0000000..7bfa1a1
--- /dev/null
@@ -0,0 +1,105 @@
+/*******************************************************************************\r
+ * Copyright (c) 2015 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.indexing;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+import java.util.Collections;\r
+import java.util.EnumSet;\r
+import java.util.Map;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.RuntimeDatabaseException;\r
+import org.simantics.db.layer0.adapter.GenericRelation;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @since 1.20.0, 1.18.4\r
+ */\r
+class IndexSchema {\r
+\r
+    public static enum Type {\r
+        INT,\r
+        LONG,\r
+        FLOAT,\r
+        DOUBLE,\r
+        STRING,\r
+        TEXT,\r
+    }\r
+\r
+    public static final EnumSet<Type> NUMERIC_TYPES = EnumSet.of(Type.INT, Type.LONG, Type.FLOAT, Type.DOUBLE); \r
+\r
+    public final Pair<String, String>[] fields;\r
+    public final Map<String, Type> typeMap;\r
+\r
+    /**\r
+     * @param fields\r
+     * @throws IllegalArgumentException\r
+     *             if any of the specified fields has an unsupported field type.\r
+     *             Supported field types are listed in enumeration {@link Type}.\r
+     */\r
+    public IndexSchema(Pair<String, String>[] fields) {\r
+        this.fields = fields;\r
+        Map<String, Type> typeMap = new THashMap<>();\r
+        for (Pair<String, String> field : fields) {\r
+            Type type = parseType(field);\r
+            typeMap.put(field.first, type);\r
+        }\r
+        this.typeMap = Collections.unmodifiableMap(typeMap);\r
+    }\r
+\r
+    public boolean hasField(String fieldName) {\r
+        return typeMap.get(fieldName) != null;\r
+    }\r
+\r
+    private static Type parseType(Pair<String, String> field) {\r
+        switch (field.second) {\r
+        case "Int":    return Type.INT;\r
+        case "Long":   return Type.LONG;\r
+        case "Float":  return Type.FLOAT;\r
+        case "Double": return Type.DOUBLE;\r
+        case "String": return Type.STRING;\r
+        case "Text":   return Type.TEXT;\r
+        default:\r
+            throw new IllegalArgumentException("Unrecognized index field type '" + field.second + "' for field '" + field.first + "'");\r
+        }\r
+    }\r
+\r
+    public static IndexSchema readFromRelation(RequestProcessor processor, final Resource relation) {\r
+        try {\r
+            return processor.syncRequest(new UniqueRead<IndexSchema>() {\r
+                @Override\r
+                public IndexSchema perform(ReadGraph graph) throws DatabaseException {\r
+                    return readFromRelation(graph, relation);\r
+                }\r
+            });\r
+        } catch (DatabaseException e) {\r
+            throw new RuntimeDatabaseException(e);\r
+        }\r
+    }\r
+\r
+    public static IndexSchema readFromRelation(ReadGraph graph, Resource relation) throws DatabaseException {\r
+        try {\r
+            GenericRelation r = graph.adapt(relation, GenericRelation.class);\r
+            return new IndexSchema( r.getFields() );\r
+        } catch (IllegalArgumentException e) {\r
+            throw new DatabaseException(\r
+                    "Failed to read index schema for relation " + relation + ". See cause for reason.", e);\r
+        }\r
+    }\r
+\r
+}
\ No newline at end of file