Search in sources :

Example 1 with BinaryEnumObjectImpl

use of org.apache.ignite.internal.binary.BinaryEnumObjectImpl in project ignite by apache.

the class GridCacheReplicatedPreloadSelfTest method testDeployment.

/**
     * @throws Exception If test failed.
     */
public void testDeployment() throws Exception {
    // TODO GG-11141.
    if (true)
        return;
    preloadMode = SYNC;
    try {
        Ignite g1 = startGrid(1);
        Ignite g2 = startGrid(2);
        IgniteCache<Integer, Object> cache1 = g1.cache(DEFAULT_CACHE_NAME);
        IgniteCache<Integer, Object> cache2 = g2.cache(DEFAULT_CACHE_NAME);
        ClassLoader ldr = grid(1).configuration().getClassLoader();
        Object v1 = ldr.loadClass("org.apache.ignite.tests.p2p.CacheDeploymentTestValue3").newInstance();
        cache1.put(1, v1);
        info("Stored value in cache1 [v=" + v1 + ", ldr=" + v1.getClass().getClassLoader() + ']');
        Object v2 = cache2.get(1);
        info("Read value from cache2 [v=" + v2 + ", ldr=" + v2.getClass().getClassLoader() + ']');
        assert v2 != null;
        assert v2.toString().equals(v1.toString());
        assert !v2.getClass().getClassLoader().equals(getClass().getClassLoader());
        assert v2.getClass().getClassLoader().getClass().getName().contains("GridDeploymentClassLoader") || grid(2).configuration().getMarshaller() instanceof BinaryMarshaller;
        Object e1 = ldr.loadClass("org.apache.ignite.tests.p2p.CacheDeploymentTestEnumValue").getEnumConstants()[0];
        cache1.put(2, e1);
        Object e2 = cache2.get(2);
        if (g1.configuration().getMarshaller() instanceof BinaryMarshaller) {
            BinaryObject enumObj = (BinaryObject) cache2.withKeepBinary().get(2);
            assertEquals(0, enumObj.enumOrdinal());
            assertTrue(enumObj.type().isEnum());
            assertTrue(enumObj instanceof BinaryEnumObjectImpl);
        }
        assert e2 != null;
        assert e2.toString().equals(e1.toString());
        assert !e2.getClass().getClassLoader().equals(getClass().getClassLoader());
        assert e2.getClass().getClassLoader().getClass().getName().contains("GridDeploymentClassLoader") || grid(2).configuration().getMarshaller() instanceof BinaryMarshaller;
        stopGrid(1);
        Ignite g3 = startGrid(3);
        IgniteCache<Integer, Object> cache3 = g3.cache(DEFAULT_CACHE_NAME);
        Object v3 = cache3.localPeek(1, CachePeekMode.ONHEAP);
        assert v3 != null;
        info("Read value from cache3 [v=" + v3 + ", ldr=" + v3.getClass().getClassLoader() + ']');
        assert v3 != null;
        assert v3.toString().equals(v1.toString());
        assert !v3.getClass().getClassLoader().equals(getClass().getClassLoader());
        assert v3.getClass().getClassLoader().getClass().getName().contains("GridDeploymentClassLoader") || grid(3).configuration().getMarshaller() instanceof BinaryMarshaller;
    } finally {
        stopAllGrids();
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl)

Example 2 with BinaryEnumObjectImpl

use of org.apache.ignite.internal.binary.BinaryEnumObjectImpl in project ignite by apache.

the class CacheAbstractJdbcStore method fillParameter.

/**
     * Sets the value of the designated parameter using the given object.
     *
     * @param stmt Prepare statement.
     * @param idx Index for parameters.
     * @param field Field descriptor.
     * @param fieldVal Field value.
     * @throws CacheException If failed to set statement parameter.
     */
protected void fillParameter(PreparedStatement stmt, int idx, JdbcTypeField field, @Nullable Object fieldVal) throws CacheException {
    try {
        if (fieldVal != null) {
            if (field.getJavaFieldType() == UUID.class) {
                switch(field.getDatabaseFieldType()) {
                    case Types.BINARY:
                        fieldVal = U.uuidToBytes((UUID) fieldVal);
                        break;
                    case Types.CHAR:
                    case Types.VARCHAR:
                        fieldVal = fieldVal.toString();
                        break;
                    default:
                }
            } else if (field.getJavaFieldType().isEnum()) {
                if (fieldVal instanceof Enum) {
                    Enum val = (Enum) fieldVal;
                    fieldVal = NUMERIC_TYPES.contains(field.getDatabaseFieldType()) ? val.ordinal() : val.name();
                } else if (fieldVal instanceof BinaryEnumObjectImpl) {
                    BinaryEnumObjectImpl val = (BinaryEnumObjectImpl) fieldVal;
                    fieldVal = val.enumOrdinal();
                }
            }
            stmt.setObject(idx, fieldVal);
        } else
            stmt.setNull(idx, field.getDatabaseFieldType());
    } catch (SQLException e) {
        throw new CacheException("Failed to set statement parameter name: " + field.getDatabaseFieldName(), e);
    }
}
Also used : SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) UUID(java.util.UUID)

Example 3 with BinaryEnumObjectImpl

use of org.apache.ignite.internal.binary.BinaryEnumObjectImpl in project ignite by apache.

the class CacheObjectBinaryProcessorImpl method buildEnum.

/** {@inheritDoc} */
@Override
public BinaryObject buildEnum(String typeName, String name) throws BinaryObjectException {
    A.notNullOrEmpty(typeName, "enum type name");
    A.notNullOrEmpty(name, "enum name");
    int typeId = binaryCtx.typeId(typeName);
    BinaryMetadata metadata = metadata0(typeId);
    if (metadata == null)
        throw new BinaryObjectException("Failed to get metadata for type [typeId=" + typeId + ", typeName='" + typeName + "']");
    Integer ordinal = metadata.getEnumOrdinalByName(name);
    typeName = binaryCtx.userTypeName(typeName);
    if (ordinal == null)
        throw new BinaryObjectException("Failed to resolve enum ordinal by name [typeId=" + typeId + ", typeName='" + typeName + "', name='" + name + "']");
    return new BinaryEnumObjectImpl(binaryCtx, typeId, null, ordinal);
}
Also used : BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 4 with BinaryEnumObjectImpl

use of org.apache.ignite.internal.binary.BinaryEnumObjectImpl in project ignite by apache.

the class BinaryObjectBuilderImpl method checkMetadata.

/**
     * Checks metadata when a BinaryObject is being serialized.
     *
     * @param meta Current metadata.
     * @param fieldsMeta Map holding metadata information that has to be updated.
     * @param newVal Field value being serialized.
     * @param name Field name.
     * @param fieldId Field ID.
     */
private Map<String, BinaryFieldMetadata> checkMetadata(BinaryType meta, Map<String, BinaryFieldMetadata> fieldsMeta, Object newVal, String name, int fieldId) {
    String oldFldTypeName = meta == null ? null : meta.fieldTypeName(name);
    boolean nullFieldVal = false;
    int newFldTypeId;
    if (newVal instanceof BinaryValueWithType) {
        newFldTypeId = ((BinaryValueWithType) newVal).typeId();
        if (((BinaryValueWithType) newVal).value() == null)
            nullFieldVal = true;
    } else // Detect Enum and Enum array type.
    if (newVal instanceof BinaryEnumObjectImpl)
        newFldTypeId = GridBinaryMarshaller.ENUM;
    else if (newVal.getClass().isArray() && newVal.getClass().getComponentType() == BinaryObject.class) {
        BinaryObject[] arr = (BinaryObject[]) newVal;
        newFldTypeId = arr.length > 0 && arr[0] instanceof BinaryEnumObjectImpl ? GridBinaryMarshaller.ENUM_ARR : GridBinaryMarshaller.OBJ_ARR;
    } else
        newFldTypeId = BinaryUtils.typeByClass(newVal.getClass());
    if (oldFldTypeName == null) {
        // It's a new field, we have to add it to metadata.
        if (fieldsMeta == null) {
            if (BinaryUtils.FIELDS_SORTED_ORDER)
                fieldsMeta = new TreeMap<>();
            else
                fieldsMeta = new LinkedHashMap<>();
        }
        fieldsMeta.put(name, new BinaryFieldMetadata(newFldTypeId, fieldId));
    } else if (!nullFieldVal) {
        String newFldTypeName = BinaryUtils.fieldTypeName(newFldTypeId);
        if (!F.eq(newFldTypeName, oldFldTypeName)) {
            throw new BinaryObjectException("Wrong value has been set [" + "typeName=" + (typeName == null ? meta.typeName() : typeName) + ", fieldName=" + name + ", fieldType=" + oldFldTypeName + ", assignedValueType=" + newFldTypeName + ']');
        }
    }
    return fieldsMeta;
}
Also used : BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) TreeMap(java.util.TreeMap) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with BinaryEnumObjectImpl

use of org.apache.ignite.internal.binary.BinaryEnumObjectImpl in project ignite by apache.

the class CacheEnumOperationsAbstractTest method assertBinaryEnum.

/**
     * @param cache Cache.
     * @param key Key.
     * @param expVal Expected value.
     */
private static void assertBinaryEnum(IgniteCache<Object, Object> cache, int key, TestEnum expVal) {
    Marshaller marsh = ((IgniteCacheProxy) cache).context().marshaller();
    if (marsh instanceof BinaryMarshaller) {
        BinaryObject enumObj = (BinaryObject) cache.withKeepBinary().get(key);
        assertEquals(expVal.ordinal(), enumObj.enumOrdinal());
        assertTrue(enumObj.type().isEnum());
        assertTrue(enumObj instanceof BinaryEnumObjectImpl);
    }
}
Also used : BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) Marshaller(org.apache.ignite.marshaller.Marshaller) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl)

Aggregations

BinaryEnumObjectImpl (org.apache.ignite.internal.binary.BinaryEnumObjectImpl)5 BinaryObject (org.apache.ignite.binary.BinaryObject)3 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)2 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)2 SQLException (java.sql.SQLException)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1 UUID (java.util.UUID)1 CacheException (javax.cache.CacheException)1 Ignite (org.apache.ignite.Ignite)1 BinaryFieldMetadata (org.apache.ignite.internal.binary.BinaryFieldMetadata)1 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)1 Marshaller (org.apache.ignite.marshaller.Marshaller)1