Search in sources :

Example 1 with BinaryType

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

the class BinaryObjectExImpl method toString.

/**
     * @param ctx Reader context.
     * @param handles Handles for already traversed objects.
     * @return String representation.
     */
private String toString(BinaryReaderHandles ctx, IdentityHashMap<BinaryObject, Integer> handles) {
    int idHash = System.identityHashCode(this);
    int hash = hashCode();
    BinaryType meta;
    try {
        meta = rawType();
    } catch (BinaryObjectException ignore) {
        meta = null;
    }
    if (meta == null || !S.INCLUDE_SENSITIVE)
        return S.toString(S.INCLUDE_SENSITIVE ? BinaryObject.class.getSimpleName() : "BinaryObject", "idHash", idHash, false, "hash", hash, false, "typeId", typeId(), true);
    handles.put(this, idHash);
    SB buf = new SB(meta.typeName());
    if (meta.fieldNames() != null) {
        buf.a(" [idHash=").a(idHash).a(", hash=").a(hash);
        for (String name : meta.fieldNames()) {
            Object val = field(ctx, name);
            buf.a(", ").a(name).a('=');
            appendValue(val, buf, ctx, handles);
        }
        buf.a(']');
    }
    return buf.toString();
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 2 with BinaryType

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

the class BinaryCachingMetadataHandler method addMeta.

/** {@inheritDoc} */
@Override
public synchronized void addMeta(int typeId, BinaryType type) throws BinaryObjectException {
    synchronized (this) {
        BinaryType oldType = metas.put(typeId, type);
        if (oldType != null) {
            BinaryMetadata oldMeta = ((BinaryTypeImpl) oldType).metadata();
            BinaryMetadata newMeta = ((BinaryTypeImpl) type).metadata();
            BinaryMetadata mergedMeta = BinaryUtils.mergeMetadata(oldMeta, newMeta);
            BinaryType mergedType = mergedMeta.wrap(((BinaryTypeImpl) oldType).context());
            metas.put(typeId, mergedType);
        }
    }
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType)

Example 3 with BinaryType

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

the class BinaryObjectBuilderImpl method serializeTo.

/**
     * @param writer Writer.
     * @param serializer Serializer.
     */
@SuppressWarnings("ResultOfMethodCallIgnored")
void serializeTo(BinaryWriterExImpl writer, BinaryBuilderSerializer serializer) {
    try {
        writer.preWrite(registeredType ? null : clsNameToWrite);
        Set<Integer> remainsFlds = null;
        BinaryType meta = ctx.metadata(typeId);
        Map<String, BinaryFieldMetadata> fieldsMeta = null;
        if (reader != null && BinaryUtils.hasSchema(flags)) {
            BinarySchema schema = reader.schema();
            Map<Integer, Object> assignedFldsById;
            if (assignedVals != null) {
                assignedFldsById = U.newHashMap(assignedVals.size());
                for (Map.Entry<String, Object> entry : assignedVals.entrySet()) {
                    String name = entry.getKey();
                    Object val = entry.getValue();
                    int fieldId = ctx.fieldId(typeId, name);
                    assignedFldsById.put(fieldId, val);
                    if (val != REMOVED_FIELD_MARKER)
                        fieldsMeta = checkMetadata(meta, fieldsMeta, val, name, fieldId);
                }
                remainsFlds = assignedFldsById.keySet();
            } else
                assignedFldsById = Collections.emptyMap();
            // Get footer details.
            int fieldIdLen = BinaryUtils.fieldIdLength(flags);
            int fieldOffsetLen = BinaryUtils.fieldOffsetLength(flags);
            IgniteBiTuple<Integer, Integer> footer = BinaryUtils.footerAbsolute(reader, start);
            int footerPos = footer.get1();
            int footerEnd = footer.get2();
            // Get raw position.
            int rawPos = BinaryUtils.rawOffsetAbsolute(reader, start);
            // Position reader on data.
            reader.position(start + hdrLen);
            int idx = 0;
            while (reader.position() < rawPos) {
                int fieldId = schema.fieldId(idx++);
                int fieldLen = fieldPositionAndLength(footerPos, footerEnd, rawPos, fieldIdLen, fieldOffsetLen).get2();
                // Position where reader will be placed afterwards.
                int postPos = reader.position() + fieldLen;
                footerPos += fieldIdLen + fieldOffsetLen;
                if (assignedFldsById.containsKey(fieldId)) {
                    Object assignedVal = assignedFldsById.remove(fieldId);
                    if (assignedVal != REMOVED_FIELD_MARKER) {
                        writer.writeFieldId(fieldId);
                        serializer.writeValue(writer, assignedVal);
                    }
                } else {
                    int type = fieldLen != 0 ? reader.readByte(0) : 0;
                    if (fieldLen != 0 && !BinaryUtils.isPlainArrayType(type) && BinaryUtils.isPlainType(type)) {
                        writer.writeFieldId(fieldId);
                        writer.write(reader.array(), reader.position(), fieldLen);
                    } else {
                        writer.writeFieldId(fieldId);
                        Object val;
                        if (fieldLen == 0)
                            val = null;
                        else if (readCache == null) {
                            val = reader.parseValue();
                            assert reader.position() == postPos;
                        } else
                            val = readCache.get(fieldId);
                        serializer.writeValue(writer, val);
                    }
                }
                reader.position(postPos);
            }
        }
        if (assignedVals != null && (remainsFlds == null || !remainsFlds.isEmpty())) {
            for (Map.Entry<String, Object> entry : assignedVals.entrySet()) {
                Object val = entry.getValue();
                if (val == REMOVED_FIELD_MARKER)
                    continue;
                String name = entry.getKey();
                int fieldId = ctx.fieldId(typeId, name);
                if (remainsFlds != null && !remainsFlds.contains(fieldId))
                    continue;
                writer.writeFieldId(fieldId);
                serializer.writeValue(writer, val);
                if (reader == null)
                    // Metadata has already been checked.
                    fieldsMeta = checkMetadata(meta, fieldsMeta, val, name, fieldId);
            }
        }
        if (reader != null) {
            // Write raw data if any.
            int rawOff = BinaryUtils.rawOffsetAbsolute(reader, start);
            int footerStart = BinaryUtils.footerStartAbsolute(reader, start);
            if (rawOff < footerStart) {
                writer.rawWriter();
                writer.write(reader.array(), rawOff, footerStart - rawOff);
            }
            // Shift reader to the end of the object.
            reader.position(start + BinaryUtils.length(reader, start));
        }
        //noinspection NumberEquality
        writer.postWrite(true, registeredType);
        // Update metadata if needed.
        int schemaId = writer.schemaId();
        BinarySchemaRegistry schemaReg = ctx.schemaRegistry(typeId);
        if (schemaReg.schema(schemaId) == null) {
            String typeName = this.typeName;
            if (typeName == null) {
                assert meta != null;
                typeName = meta.typeName();
            }
            BinarySchema curSchema = writer.currentSchema();
            ctx.updateMetadata(typeId, new BinaryMetadata(typeId, typeName, fieldsMeta, ctx.affinityKeyFieldName(typeId), Collections.singleton(curSchema), false, null));
            schemaReg.addSchema(curSchema.schemaId(), curSchema);
        }
        // Update hash code after schema is written.
        writer.postWriteHashCode(registeredType ? null : clsNameToWrite);
    } finally {
        writer.popSchema();
    }
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) BinarySchemaRegistry(org.apache.ignite.internal.binary.BinarySchemaRegistry) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) BinaryObject(org.apache.ignite.binary.BinaryObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 4 with BinaryType

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

the class GridDefaultBinaryMappersBinaryMetaDataSelfTest method testReflection.

/**
     * @throws Exception If failed.
     */
public void testReflection() throws Exception {
    BinaryType meta = binaries().type(TestObject1.class);
    assertNotNull(meta);
    assertEquals(expectedTypeName(TestObject1.class.getName()), meta.typeName());
    Collection<String> fields = meta.fieldNames();
    assertEquals(7, fields.size());
    assertTrue(fields.contains("intVal"));
    assertTrue(fields.contains("strVal"));
    assertTrue(fields.contains("arrVal"));
    assertTrue(fields.contains("obj1Val"));
    assertTrue(fields.contains("obj2Val"));
    assertTrue(fields.contains("decVal"));
    assertTrue(fields.contains("decArrVal"));
    assertEquals("int", meta.fieldTypeName("intVal"));
    assertEquals("String", meta.fieldTypeName("strVal"));
    assertEquals("byte[]", meta.fieldTypeName("arrVal"));
    assertEquals("Object", meta.fieldTypeName("obj1Val"));
    assertEquals("Object", meta.fieldTypeName("obj2Val"));
    assertEquals("decimal", meta.fieldTypeName("decVal"));
    assertEquals("decimal[]", meta.fieldTypeName("decArrVal"));
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType)

Example 5 with BinaryType

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

the class GridDefaultBinaryMappersBinaryMetaDataSelfTest method testGetAll.

/**
     * @throws Exception If failed.
     */
public void testGetAll() throws Exception {
    binaries().toBinary(new TestObject2());
    Collection<BinaryType> metas = binaries().types();
    assertEquals(2, metas.size());
    for (BinaryType meta : metas) {
        Collection<String> fields;
        if (expectedTypeName(TestObject1.class.getName()).equals(meta.typeName())) {
            fields = meta.fieldNames();
            assertEquals(7, fields.size());
            assertTrue(fields.contains("intVal"));
            assertTrue(fields.contains("strVal"));
            assertTrue(fields.contains("arrVal"));
            assertTrue(fields.contains("obj1Val"));
            assertTrue(fields.contains("obj2Val"));
            assertTrue(fields.contains("decVal"));
            assertTrue(fields.contains("decArrVal"));
            assertEquals("int", meta.fieldTypeName("intVal"));
            assertEquals("String", meta.fieldTypeName("strVal"));
            assertEquals("byte[]", meta.fieldTypeName("arrVal"));
            assertEquals("Object", meta.fieldTypeName("obj1Val"));
            assertEquals("Object", meta.fieldTypeName("obj2Val"));
            assertEquals("decimal", meta.fieldTypeName("decVal"));
            assertEquals("decimal[]", meta.fieldTypeName("decArrVal"));
        } else if (expectedTypeName(TestObject2.class.getName()).equals(meta.typeName())) {
            fields = meta.fieldNames();
            assertEquals(7, fields.size());
            assertTrue(fields.contains("boolVal"));
            assertTrue(fields.contains("dateVal"));
            assertTrue(fields.contains("uuidArrVal"));
            assertTrue(fields.contains("objVal"));
            assertTrue(fields.contains("mapVal"));
            assertTrue(fields.contains("decVal"));
            assertTrue(fields.contains("decArrVal"));
            assertEquals("boolean", meta.fieldTypeName("boolVal"));
            assertEquals("Date", meta.fieldTypeName("dateVal"));
            assertEquals("UUID[]", meta.fieldTypeName("uuidArrVal"));
            assertEquals("Object", meta.fieldTypeName("objVal"));
            assertEquals("Map", meta.fieldTypeName("mapVal"));
            assertEquals("decimal", meta.fieldTypeName("decVal"));
            assertEquals("decimal[]", meta.fieldTypeName("decArrVal"));
        } else
            assert false : meta.typeName();
    }
    grid().cache(DEFAULT_CACHE_NAME).put(new AffinityKey<>(1, 1), 1);
    metas = binaries().types();
    assertEquals(3, metas.size());
    for (BinaryType meta : metas) {
        if (AffinityKey.class.getSimpleName().equals(meta.typeName())) {
            assertEquals("affKey", meta.affinityKeyFieldName());
            return;
        }
    }
    fail("Failed to find metadata for AffinityKey");
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

Aggregations

BinaryType (org.apache.ignite.binary.BinaryType)24 BinaryObject (org.apache.ignite.binary.BinaryObject)8 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)4 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BinaryField (org.apache.ignite.binary.BinaryField)3 BinaryObjectEx (org.apache.ignite.internal.binary.BinaryObjectEx)3 BinaryObjectBuilderImpl (org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl)3 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)3 HashSet (java.util.HashSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Ignite (org.apache.ignite.Ignite)2 IgniteBinary (org.apache.ignite.IgniteBinary)2 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)2 SB (org.apache.ignite.internal.util.typedef.internal.SB)2 ConcurrentHashSet (org.eclipse.jetty.util.ConcurrentHashSet)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1