Search in sources :

Example 1 with QueryEntityClassProperty

use of org.apache.ignite.internal.processors.cache.query.QueryEntityClassProperty in project ignite by apache.

the class QueryEntity method convert.

/**
 * @param desc Type descriptor.
 * @return Type metadata.
 */
private static QueryEntity convert(QueryEntityTypeDescriptor desc) {
    QueryEntity entity = new QueryEntity();
    // Key and val types.
    entity.setKeyType(desc.keyClass().getName());
    entity.setValueType(desc.valueClass().getName());
    for (QueryEntityClassProperty prop : desc.properties().values()) entity.addQueryField(prop.fullName(), U.box(prop.type()).getName(), prop.alias());
    entity.setKeyFields(desc.keyProperties());
    QueryIndex txtIdx = null;
    Collection<QueryIndex> idxs = new ArrayList<>();
    for (Map.Entry<String, GridQueryIndexDescriptor> idxEntry : desc.indexes().entrySet()) {
        GridQueryIndexDescriptor idx = idxEntry.getValue();
        if (idx.type() == QueryIndexType.FULLTEXT) {
            assert txtIdx == null;
            txtIdx = new QueryIndex();
            txtIdx.setIndexType(QueryIndexType.FULLTEXT);
            txtIdx.setFieldNames(idx.fields(), true);
            txtIdx.setName(idxEntry.getKey());
        } else {
            QueryIndex sortedIdx = new QueryIndex();
            sortedIdx.setIndexType(idx.type());
            LinkedHashMap<String, Boolean> fields = new LinkedHashMap<>();
            for (String f : idx.fields()) fields.put(f, !idx.descending(f));
            sortedIdx.setFields(fields);
            sortedIdx.setName(idxEntry.getKey());
            sortedIdx.setInlineSize(idx.inlineSize());
            idxs.add(sortedIdx);
        }
    }
    if (desc.valueTextIndex()) {
        if (txtIdx == null) {
            txtIdx = new QueryIndex();
            txtIdx.setIndexType(QueryIndexType.FULLTEXT);
            txtIdx.setFieldNames(Arrays.asList(QueryUtils.VAL_FIELD_NAME), true);
        } else
            txtIdx.getFields().put(QueryUtils.VAL_FIELD_NAME, true);
    }
    if (txtIdx != null)
        idxs.add(txtIdx);
    if (!F.isEmpty(idxs))
        entity.setIndexes(idxs);
    if (!F.isEmpty(desc.notNullFields()))
        entity.setNotNullFields(desc.notNullFields());
    if (!F.isEmpty(desc.fieldsPrecision()))
        entity.setFieldsPrecision(desc.fieldsPrecision());
    if (!F.isEmpty(desc.fieldsScale()))
        entity.setFieldsScale(desc.fieldsScale());
    return entity;
}
Also used : ArrayList(java.util.ArrayList) QueryEntityClassProperty(org.apache.ignite.internal.processors.cache.query.QueryEntityClassProperty) GridQueryIndexDescriptor(org.apache.ignite.internal.processors.query.GridQueryIndexDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with QueryEntityClassProperty

use of org.apache.ignite.internal.processors.cache.query.QueryEntityClassProperty in project ignite by apache.

the class QueryEntity method processAnnotationsInClass.

/**
 * Process annotations for class.
 *
 * @param key If given class relates to key.
 * @param cls Class.
 * @param type Type descriptor.
 * @param parent Parent in case of embeddable.
 */
private static void processAnnotationsInClass(boolean key, Class<?> cls, QueryEntityTypeDescriptor type, @Nullable QueryEntityClassProperty parent) {
    if (U.isJdk(cls) || QueryUtils.isGeometryClass(cls)) {
        if (parent == null && !key && QueryUtils.isSqlType(cls)) {
            // We have to index primitive _val.
            String idxName = cls.getSimpleName() + "_" + QueryUtils.VAL_FIELD_NAME + "_idx";
            type.addIndex(idxName, QueryUtils.isGeometryClass(cls) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED, QueryIndex.DFLT_INLINE_SIZE);
            type.addFieldToIndex(idxName, QueryUtils.VAL_FIELD_NAME, 0, false);
        }
        return;
    }
    if (parent != null && parent.knowsClass(cls))
        throw new CacheException("Recursive reference found in type: " + cls.getName());
    if (parent == null) {
        // Check class annotation at top level only.
        QueryTextField txtAnnCls = cls.getAnnotation(QueryTextField.class);
        if (txtAnnCls != null)
            type.valueTextIndex(true);
        QueryGroupIndex grpIdx = cls.getAnnotation(QueryGroupIndex.class);
        if (grpIdx != null)
            type.addIndex(grpIdx.name(), QueryIndexType.SORTED, grpIdx.inlineSize());
        QueryGroupIndex.List grpIdxList = cls.getAnnotation(QueryGroupIndex.List.class);
        if (grpIdxList != null && !F.isEmpty(grpIdxList.value())) {
            for (QueryGroupIndex idx : grpIdxList.value()) type.addIndex(idx.name(), QueryIndexType.SORTED, idx.inlineSize());
        }
    }
    for (Class<?> c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) {
        for (Field field : c.getDeclaredFields()) {
            QuerySqlField sqlAnn = field.getAnnotation(QuerySqlField.class);
            QueryTextField txtAnn = field.getAnnotation(QueryTextField.class);
            if (sqlAnn != null || txtAnn != null) {
                QueryEntityClassProperty prop = new QueryEntityClassProperty(field);
                prop.parent(parent);
                // Add parent property before its possible nested properties so that
                // resulting parent column comes before columns corresponding to those
                // nested properties in the resulting table - that way nested
                // properties override will happen properly (first parent, then children).
                type.addProperty(prop, sqlAnn, key, true);
                processAnnotation(key, sqlAnn, txtAnn, cls, c, field.getType(), prop, type);
            }
        }
    }
}
Also used : QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) QueryField(org.apache.ignite.internal.processors.query.QueryField) Field(java.lang.reflect.Field) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QueryGroupIndex(org.apache.ignite.cache.query.annotations.QueryGroupIndex) CacheException(javax.cache.CacheException) QueryEntityClassProperty(org.apache.ignite.internal.processors.cache.query.QueryEntityClassProperty)

Aggregations

QueryEntityClassProperty (org.apache.ignite.internal.processors.cache.query.QueryEntityClassProperty)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CacheException (javax.cache.CacheException)1 QueryGroupIndex (org.apache.ignite.cache.query.annotations.QueryGroupIndex)1 QuerySqlField (org.apache.ignite.cache.query.annotations.QuerySqlField)1 QueryTextField (org.apache.ignite.cache.query.annotations.QueryTextField)1 GridQueryIndexDescriptor (org.apache.ignite.internal.processors.query.GridQueryIndexDescriptor)1 QueryField (org.apache.ignite.internal.processors.query.QueryField)1