Search in sources :

Example 1 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryUtils method mergeMetadata.

/**
 * Merge old and new metas.
 *
 * @param oldMeta Old meta.
 * @param newMeta New meta.
 * @return New meta if old meta was null, old meta if no changes detected, merged meta otherwise.
 * @throws BinaryObjectException If merge failed due to metadata conflict.
 */
public static BinaryMetadata mergeMetadata(@Nullable BinaryMetadata oldMeta, BinaryMetadata newMeta) {
    assert newMeta != null;
    if (oldMeta == null)
        return newMeta;
    else {
        assert oldMeta.typeId() == newMeta.typeId();
        // Check type name.
        if (!F.eq(oldMeta.typeName(), newMeta.typeName())) {
            throw new BinaryObjectException("Two binary types have duplicate type ID [" + "typeId=" + oldMeta.typeId() + ", typeName1=" + oldMeta.typeName() + ", typeName2=" + newMeta.typeName() + ']');
        }
        // Check affinity field names.
        if (!F.eq(oldMeta.affinityKeyFieldName(), newMeta.affinityKeyFieldName())) {
            throw new BinaryObjectException("Binary type has different affinity key fields [" + "typeName=" + newMeta.typeName() + ", affKeyFieldName1=" + oldMeta.affinityKeyFieldName() + ", affKeyFieldName2=" + newMeta.affinityKeyFieldName() + ']');
        }
        // Check enum flag.
        if (oldMeta.isEnum() != newMeta.isEnum()) {
            if (oldMeta.isEnum())
                throw new BinaryObjectException("Binary type already registered as enum: " + newMeta.typeName());
            else
                throw new BinaryObjectException("Binary type already registered as non-enum: " + newMeta.typeName());
        }
        // Check and merge fields.
        Map<String, BinaryFieldMetadata> mergedFields;
        if (FIELDS_SORTED_ORDER)
            mergedFields = new TreeMap<>(oldMeta.fieldsMap());
        else
            mergedFields = new LinkedHashMap<>(oldMeta.fieldsMap());
        Map<String, BinaryFieldMetadata> newFields = newMeta.fieldsMap();
        boolean changed = false;
        Map<String, Integer> mergedEnumMap = null;
        if (!F.isEmpty(newMeta.enumMap())) {
            mergedEnumMap = mergeEnumValues(oldMeta.typeName(), oldMeta.enumMap(), newMeta.enumMap());
            changed = mergedEnumMap.size() > oldMeta.enumMap().size();
        }
        for (Map.Entry<String, BinaryFieldMetadata> newField : newFields.entrySet()) {
            BinaryFieldMetadata oldFieldMeta = mergedFields.put(newField.getKey(), newField.getValue());
            if (oldFieldMeta == null)
                changed = true;
            else {
                String oldFieldTypeName = fieldTypeName(oldFieldMeta.typeId());
                String newFieldTypeName = fieldTypeName(newField.getValue().typeId());
                if (!F.eq(oldFieldTypeName, newFieldTypeName)) {
                    throw new BinaryObjectException("Binary type has different field types [" + "typeName=" + oldMeta.typeName() + ", fieldName=" + newField.getKey() + ", fieldTypeName1=" + oldFieldTypeName + ", fieldTypeName2=" + newFieldTypeName + ']');
                }
            }
        }
        // Check and merge schemas.
        Collection<BinarySchema> mergedSchemas = new HashSet<>(oldMeta.schemas());
        for (BinarySchema newSchema : newMeta.schemas()) {
            if (mergedSchemas.add(newSchema))
                changed = true;
        }
        // Return either old meta if no changes detected, or new merged meta.
        return changed ? new BinaryMetadata(oldMeta.typeId(), oldMeta.typeName(), mergedFields, oldMeta.affinityKeyFieldName(), mergedSchemas, oldMeta.isEnum(), mergedEnumMap) : oldMeta;
    }
}
Also used : TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) BigInteger(java.math.BigInteger) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class JdbcBatchExecuteRequest method readBinary.

/**
 * {@inheritDoc}
 */
@Override
public void readBinary(BinaryReaderExImpl reader) throws BinaryObjectException {
    super.readBinary(reader);
    schemaName = reader.readString();
    int n = reader.readInt();
    queries = new ArrayList<>(n);
    for (int i = 0; i < n; ++i) {
        JdbcQuery qry = new JdbcQuery();
        qry.readBinary(reader);
        queries.add(qry);
    }
    try {
        if (reader.available() > 0)
            lastStreamBatch = reader.readBoolean();
    } catch (IOException e) {
        throw new BinaryObjectException(e);
    }
}
Also used : IOException(java.io.IOException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 3 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class JdbcBulkLoadBatchRequest method readBinary.

/**
 * {@inheritDoc}
 */
@Override
public void readBinary(BinaryReaderExImpl reader) throws BinaryObjectException {
    super.readBinary(reader);
    qryId = reader.readLong();
    batchIdx = reader.readInt();
    int c = reader.readInt();
    if (!isCmdValid(c))
        throw new BinaryObjectException("Invalid command: " + cmd);
    cmd = c;
    data = reader.readByteArray();
    assert data != null;
}
Also used : BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 4 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class JdbcQueryExecuteRequest method readBinary.

/**
 * {@inheritDoc}
 */
@Override
public void readBinary(BinaryReaderExImpl reader) throws BinaryObjectException {
    super.readBinary(reader);
    schemaName = reader.readString();
    pageSize = reader.readInt();
    maxRows = reader.readInt();
    sqlQry = reader.readString();
    int argsNum = reader.readInt();
    args = new Object[argsNum];
    for (int i = 0; i < argsNum; ++i) args[i] = SqlListenerUtils.readObject(reader, false);
    try {
        if (reader.available() > 0)
            stmtType = JdbcStatementType.fromOrdinal(reader.readByte());
        else
            stmtType = JdbcStatementType.ANY_STATEMENT_TYPE;
    } catch (IOException e) {
        throw new BinaryObjectException(e);
    }
}
Also used : IOException(java.io.IOException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 5 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class CacheObjectBinaryProcessorImpl method addMeta.

/**
 * {@inheritDoc}
 */
@Override
public void addMeta(final int typeId, final BinaryType newMeta) throws BinaryObjectException {
    assert newMeta != null;
    assert newMeta instanceof BinaryTypeImpl;
    BinaryMetadata newMeta0 = ((BinaryTypeImpl) newMeta).metadata();
    try {
        BinaryMetadataHolder metaHolder = metadataLocCache.get(typeId);
        BinaryMetadata oldMeta = metaHolder != null ? metaHolder.metadata() : null;
        BinaryMetadata mergedMeta = BinaryUtils.mergeMetadata(oldMeta, newMeta0);
        // metadata requested to be added is exactly the same as already presented in the cache
        if (mergedMeta == oldMeta)
            return;
        MetadataUpdateResult res = transport.requestMetadataUpdate(mergedMeta).get();
        assert res != null;
        if (res.rejected())
            throw res.error();
    } catch (IgniteCheckedException e) {
        throw new BinaryObjectException("Failed to update meta data for type: " + newMeta.typeName(), e);
    }
}
Also used : BinaryTypeImpl(org.apache.ignite.internal.binary.BinaryTypeImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Aggregations

BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)68 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)13 BinaryObject (org.apache.ignite.binary.BinaryObject)12 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)9 IOException (java.io.IOException)7 BinaryType (org.apache.ignite.binary.BinaryType)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 Test (org.junit.Test)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 TreeMap (java.util.TreeMap)5 IgniteException (org.apache.ignite.IgniteException)5 BigInteger (java.math.BigInteger)3 Time (java.sql.Time)3 Timestamp (java.sql.Timestamp)3 BinaryEnumObjectImpl (org.apache.ignite.internal.binary.BinaryEnumObjectImpl)3 BinaryTypeImpl (org.apache.ignite.internal.binary.BinaryTypeImpl)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2