]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexSchema.java
a11c90df8e2a11f1ab5b831baae2c422c66ad203
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexSchema.java
1 /*******************************************************************************
2  * Copyright (c) 2015 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.indexing;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import java.util.Collections;
17 import java.util.EnumSet;
18 import java.util.Map;
19
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.RequestProcessor;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.request.UniqueRead;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.exception.RuntimeDatabaseException;
26 import org.simantics.db.layer0.adapter.GenericRelation;
27 import org.simantics.utils.datastructures.Pair;
28
29 /**
30  * @author Tuukka Lehtonen
31  * @since 1.20.0, 1.18.4
32  */
33 class IndexSchema {
34
35     public static enum Type {
36         INT,
37         LONG,
38         FLOAT,
39         DOUBLE,
40         STRING,
41         TEXT,
42     }
43
44     public static final EnumSet<Type> NUMERIC_TYPES = EnumSet.of(Type.INT, Type.LONG, Type.FLOAT, Type.DOUBLE); 
45
46     public final Pair<String, String>[] fields;
47     public final Map<String, Type> typeMap;
48
49     /**
50      * @param fields
51      * @throws IllegalArgumentException
52      *             if any of the specified fields has an unsupported field type.
53      *             Supported field types are listed in enumeration {@link Type}.
54      */
55     public IndexSchema(Pair<String, String>[] fields) {
56         this.fields = fields;
57         Map<String, Type> typeMap = new THashMap<>();
58         for (Pair<String, String> field : fields) {
59             Type type = parseType(field);
60             typeMap.put(field.first, type);
61         }
62         this.typeMap = Collections.unmodifiableMap(typeMap);
63     }
64
65     public boolean hasField(String fieldName) {
66         return typeMap.get(fieldName) != null;
67     }
68
69     private static Type parseType(Pair<String, String> field) {
70         switch (field.second) {
71         case "Int":    return Type.INT;
72         case "Long":   return Type.LONG;
73         case "Float":  return Type.FLOAT;
74         case "Double": return Type.DOUBLE;
75         case "String": return Type.STRING;
76         case "Text":   return Type.TEXT;
77         default:
78             throw new IllegalArgumentException("Unrecognized index field type '" + field.second + "' for field '" + field.first + "'");
79         }
80     }
81
82     public static IndexSchema readFromRelation(RequestProcessor processor, final Resource relation) {
83         try {
84             return processor.syncRequest(new UniqueRead<IndexSchema>() {
85                 @Override
86                 public IndexSchema perform(ReadGraph graph) throws DatabaseException {
87                     return readFromRelation(graph, relation);
88                 }
89             });
90         } catch (DatabaseException e) {
91             throw new RuntimeDatabaseException(e);
92         }
93     }
94
95     public static IndexSchema readFromRelation(ReadGraph graph, Resource relation) throws DatabaseException {
96         try {
97             GenericRelation r = graph.adapt(relation, GenericRelation.class);
98             return new IndexSchema( r.getFields() );
99         } catch (IllegalArgumentException e) {
100             throw new DatabaseException(
101                     "Failed to read index schema for relation " + relation + ". See cause for reason.", e);
102         }
103     }
104
105 }