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