1 /*******************************************************************************
2 * Copyright (c) 2015 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.indexing;
14 import gnu.trove.map.hash.THashMap;
16 import java.util.Collections;
17 import java.util.EnumSet;
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;
30 * @author Tuukka Lehtonen
31 * @since 1.20.0, 1.18.4
35 public static enum Type {
44 public static final EnumSet<Type> NUMERIC_TYPES = EnumSet.of(Type.INT, Type.LONG, Type.FLOAT, Type.DOUBLE);
46 public final Pair<String, String>[] fields;
47 public final Map<String, Type> typeMap;
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}.
55 public IndexSchema(Pair<String, String>[] 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);
62 this.typeMap = Collections.unmodifiableMap(typeMap);
65 public boolean hasField(String fieldName) {
66 return typeMap.get(fieldName) != null;
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;
78 throw new IllegalArgumentException("Unrecognized index field type '" + field.second + "' for field '" + field.first + "'");
82 public static IndexSchema readFromRelation(RequestProcessor processor, final Resource relation) {
84 return processor.syncRequest(new UniqueRead<IndexSchema>() {
86 public IndexSchema perform(ReadGraph graph) throws DatabaseException {
87 return readFromRelation(graph, relation);
90 } catch (DatabaseException e) {
91 throw new RuntimeDatabaseException(e);
95 public static IndexSchema readFromRelation(ReadGraph graph, Resource relation) throws DatabaseException {
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);