use of org.apache.ignite.internal.binary.BinaryArray 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() && BinaryEnumObjectImpl.class.isAssignableFrom(newVal.getClass().getComponentType()))
newFldTypeId = GridBinaryMarshaller.ENUM_ARR;
else if (newVal.getClass().isArray() && BinaryObject.class.isAssignableFrom(newVal.getClass().getComponentType()))
newFldTypeId = GridBinaryMarshaller.OBJ_ARR;
else if (newVal instanceof BinaryEnumArray)
newFldTypeId = GridBinaryMarshaller.ENUM_ARR;
else if (newVal instanceof BinaryArray)
newFldTypeId = 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) && !oldFldTypeName.equals(BinaryUtils.fieldTypeName(GridBinaryMarshaller.OBJ))) {
throw new BinaryObjectException("Wrong value has been set [" + "typeName=" + (typeName == null ? meta.typeName() : typeName) + ", fieldName=" + name + ", fieldType=" + oldFldTypeName + ", assignedValueType=" + newFldTypeName + ']');
}
}
return fieldsMeta;
}
use of org.apache.ignite.internal.binary.BinaryArray in project ignite by apache.
the class CacheObjectBinaryProcessorImpl method marshalToBinary.
/**
* {@inheritDoc}
*/
@Override
public Object marshalToBinary(@Nullable Object obj, boolean failIfUnregistered) throws BinaryObjectException {
if (obj == null)
return null;
if (BinaryUtils.isBinaryType(obj.getClass()))
return obj;
if (obj instanceof Object[]) {
Object[] arr = (Object[]) obj;
Object[] pArr = new Object[arr.length];
for (int i = 0; i < arr.length; i++) pArr[i] = marshalToBinary(arr[i], failIfUnregistered);
if (!BinaryArray.useBinaryArrays())
return pArr;
Class<?> compCls = obj.getClass().getComponentType();
boolean isBinaryArr = BinaryObject.class.isAssignableFrom(compCls);
String compClsName = isBinaryArr ? Object.class.getName() : compCls.getName();
// In case of interface or multidimensional array rely on class name.
// Interfaces and array not registered as binary types.
BinaryClassDescriptor desc = binaryCtx.descriptorForClass(compCls);
if (compCls.isEnum() || compCls == BinaryEnumObjectImpl.class) {
return new BinaryEnumArray(binaryCtx, desc.registered() ? desc.typeId() : GridBinaryMarshaller.UNREGISTERED_TYPE_ID, compClsName, pArr);
} else {
return new BinaryArray(binaryCtx, desc.registered() ? desc.typeId() : GridBinaryMarshaller.UNREGISTERED_TYPE_ID, compClsName, pArr);
}
}
if (obj instanceof IgniteBiTuple) {
IgniteBiTuple tup = (IgniteBiTuple) obj;
if (obj instanceof T2)
return new T2<>(marshalToBinary(tup.get1(), failIfUnregistered), marshalToBinary(tup.get2(), failIfUnregistered));
return new IgniteBiTuple<>(marshalToBinary(tup.get1(), failIfUnregistered), marshalToBinary(tup.get2(), failIfUnregistered));
}
{
Collection<Object> pCol = BinaryUtils.newKnownCollection(obj);
if (pCol != null) {
Collection<?> col = (Collection<?>) obj;
for (Object item : col) pCol.add(marshalToBinary(item, failIfUnregistered));
return (pCol instanceof MutableSingletonList) ? U.convertToSingletonList(pCol) : pCol;
}
}
{
Map<Object, Object> pMap = BinaryUtils.newKnownMap(obj);
if (pMap != null) {
Map<?, ?> map = (Map<?, ?>) obj;
for (Map.Entry<?, ?> e : map.entrySet()) pMap.put(marshalToBinary(e.getKey(), failIfUnregistered), marshalToBinary(e.getValue(), failIfUnregistered));
return pMap;
}
}
if (obj instanceof Map.Entry) {
Map.Entry<?, ?> e = (Map.Entry<?, ?>) obj;
return new GridMapEntry<>(marshalToBinary(e.getKey(), failIfUnregistered), marshalToBinary(e.getValue(), failIfUnregistered));
}
if (binaryMarsh.mustDeserialize(obj))
// No need to go through marshal-unmarshal because result will be the same as initial object.
return obj;
byte[] arr = binaryMarsh.marshal(obj, failIfUnregistered);
assert arr.length > 0;
Object obj0 = binaryMarsh.unmarshal(arr, null);
// Possible if a class has writeObject method.
if (obj0 instanceof BinaryObjectImpl)
((BinaryObjectImpl) obj0).detachAllowed(true);
return obj0;
}
use of org.apache.ignite.internal.binary.BinaryArray in project ignite by apache.
the class H2Utils method bindObject.
/**
* Binds object to prepared statement.
*
* @param stmt SQL statement.
* @param idx Index.
* @param obj Value to store.
* @throws IgniteCheckedException If failed.
*/
private static void bindObject(PreparedStatement stmt, int idx, @Nullable Object obj) throws IgniteCheckedException {
try {
if (obj == null)
stmt.setNull(idx, Types.VARCHAR);
else if (obj instanceof BigInteger)
stmt.setObject(idx, obj, Types.JAVA_OBJECT);
else if (obj instanceof BigDecimal)
stmt.setObject(idx, obj, Types.DECIMAL);
else if (obj instanceof BinaryArray)
stmt.setObject(idx, BinaryUtils.rawArrayFromBinary(obj));
else
stmt.setObject(idx, obj);
} catch (SQLException e) {
throw new IgniteCheckedException("Failed to bind parameter [idx=" + idx + ", obj=" + obj + ", stmt=" + stmt + ']', e);
}
}
Aggregations