X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.indexing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Findexing%2FIndexSchema.java;fp=bundles%2Forg.simantics.db.indexing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Findexing%2FIndexSchema.java;h=a11c90df8e2a11f1ab5b831baae2c422c66ad203;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=7bfa1a1ec87370412f7adf0c12e4b06e7e3d1803;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git 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 index 7bfa1a1ec..a11c90df8 100644 --- 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 @@ -1,105 +1,105 @@ -/******************************************************************************* - * 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 gnu.trove.map.hash.THashMap; - -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.layer0.adapter.GenericRelation; -import org.simantics.utils.datastructures.Pair; - -/** - * @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 NUMERIC_TYPES = EnumSet.of(Type.INT, Type.LONG, Type.FLOAT, Type.DOUBLE); - - public final Pair[] fields; - public final Map 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[] fields) { - this.fields = fields; - Map typeMap = new THashMap<>(); - for (Pair 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 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() { - @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 DatabaseException( - "Failed to read index schema for relation " + relation + ". See cause for reason.", e); - } - } - +/******************************************************************************* + * 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 gnu.trove.map.hash.THashMap; + +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.layer0.adapter.GenericRelation; +import org.simantics.utils.datastructures.Pair; + +/** + * @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 NUMERIC_TYPES = EnumSet.of(Type.INT, Type.LONG, Type.FLOAT, Type.DOUBLE); + + public final Pair[] fields; + public final Map 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[] fields) { + this.fields = fields; + Map typeMap = new THashMap<>(); + for (Pair 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 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() { + @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 DatabaseException( + "Failed to read index schema for relation " + relation + ". See cause for reason.", e); + } + } + } \ No newline at end of file