/** Create new marshaller implementation. */ private GridBinaryMarshaller createImpl(BinaryConfiguration binCfg) { IgniteConfiguration igniteCfg = new IgniteConfiguration(); if (binCfg == null) { binCfg = new BinaryConfiguration(); binCfg.setCompactFooter(false); } igniteCfg.setBinaryConfiguration(binCfg); BinaryContext ctx = new BinaryContext(metaHnd, igniteCfg, new NullLogger()); BinaryMarshaller marsh = new BinaryMarshaller(); marsh.setContext(marshCtx); ctx.configure(marsh, igniteCfg); ctx.registerUserTypesSchema(); return new GridBinaryMarshaller(ctx); } }
/** * Resolve the class. * * @param ctx Binary context. * @param typeId Type ID. * @param clsName Class name. * @param ldr Class loaded. * @return Resovled class. */ public static Class resolveClass(BinaryContext ctx, int typeId, @Nullable String clsName, @Nullable ClassLoader ldr, boolean deserialize) { Class cls; if (typeId != GridBinaryMarshaller.UNREGISTERED_TYPE_ID) cls = ctx.descriptorForTypeId(true, typeId, ldr, deserialize).describedClass(); else { try { cls = U.forName(clsName, ldr); } catch (ClassNotFoundException e) { throw new BinaryInvalidTypeException("Failed to load the class: " + clsName, e); } // forces registering of class by type id, at least locally ctx.descriptorForClass(cls, true, false); } return cls; }
/** {@inheritDoc} */ @Override public <T> T getField(String name) { Object val; if (assignedVals != null && assignedVals.containsKey(name)) { val = assignedVals.get(name); if (val == REMOVED_FIELD_MARKER) return null; } else { ensureReadCacheInit(); int fldId = ctx.fieldId(typeId, name); val = readCache.get(fldId); } return (T)BinaryUtils.unwrapLazy(val); }
/** * @param clsName Class name. * @param ctx Binary context. */ public BinaryObjectBuilderImpl(BinaryContext ctx, String clsName) { this(ctx, ctx.typeId(clsName), ctx.userTypeName(clsName)); }
/** * Get serializer for class taking in count default one. * * @param cls Class. * @return Serializer for class or {@code null} if none exists. */ @Nullable private BinarySerializer serializerForClass(Class cls) { BinarySerializer serializer = defaultSerializer(); if (serializer == null && canUseReflectiveSerializer(cls)) serializer = new BinaryReflectiveSerializer(); return serializer; }
/** {@inheritDoc} */ @Override public BinaryType registerEnum(String typeName, Map<String, Integer> vals) { if (typeName == null || typeName.isEmpty()) throw new IllegalArgumentException("typeName"); BinaryContext ctx = marsh.context(); int typeId = ctx.typeId(typeName); ctx.updateMetadata(typeId, new BinaryMetadata(typeId, typeName, null, null, null, true, vals), false); return ctx.metadata(typeId); } }
String clsName = ((Enum)val).getDeclaringClass().getName(); int typeId = writer.context().typeId(clsName); String typeName = writer.context().userTypeName(clsName); writer.context().updateMetadata(typeId, meta, writer.failIfUnregistered()); writer.context().descriptorForClass(((Enum)val).getDeclaringClass(), false, false); writer.writeInt(c.size()); byte colType = writer.context().collectionType(c.getClass()); writer.writeInt(map.size()); writer.writeByte(writer.context().mapType(map.getClass())); int compTypeId = writer.context().typeId(((Object[])val).getClass().getComponentType().getName());
BinaryType meta = ctx.metadata(typeId); Object val = entry.getValue(); int fieldId = ctx.fieldId(typeId, name); int fieldId = ctx.fieldId(typeId, name); BinarySchemaRegistry schemaReg = ctx.schemaRegistry(typeId); affFieldName0 = ctx.affinityKeyFieldName(typeId); ctx.registerUserClassName(typeId, typeName); ctx.updateMetadata(typeId, new BinaryMetadata(typeId, typeName, fieldsMeta, affFieldName0, Collections.singleton(curSchema), false, null), writer.failIfUnregistered());
/** * @throws Exception If failed. */ @Test public void testCustomTypeRegistration() throws Exception { BinaryTypeConfiguration customType = new BinaryTypeConfiguration(Value.class.getName()); BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(customType)); BinaryContext ctx = binaryContext(marsh); int typeId = ctx.typeId(Value.class.getName()); BinaryClassDescriptor descriptor = ctx.descriptorForTypeId(true, typeId, null, false); assertEquals(Value.class, descriptor.describedClass()); assertEquals(true, descriptor.registered()); assertEquals(true, descriptor.userType()); // Custom explicit types must be registered in 'predefinedTypes' in order not to break the interoperability. Field field = ctx.getClass().getDeclaredField("predefinedTypes"); field.setAccessible(true); Map<Integer, BinaryClassDescriptor> map = (Map<Integer, BinaryClassDescriptor>)field.get(ctx); assertTrue(!map.isEmpty()); assertNotNull(map.get(typeId)); // Custom explicit types must NOT be registered in 'predefinedTypeNames'. field = ctx.getClass().getDeclaredField("predefinedTypeNames"); field.setAccessible(true); Map<String, Integer> map2 = (Map<String, Integer>)field.get(ctx); assertTrue(!map2.isEmpty()); assertNull(map2.get(ctx.userTypeName(Value.class.getName()))); }
/** {@inheritDoc} */ @Override public int typeId(String typeName) { if (binaryCtx == null) return 0; return binaryCtx.typeId(typeName); }
new BinaryContext(metaHnd, ctx.config(), ctx.log(BinaryContext.class));
/** {@inheritDoc} */ @Override public BinaryType registerEnum(String typeName, Map<String, Integer> vals) throws BinaryObjectException { A.notNullOrEmpty(typeName, "enum type name"); int typeId = binaryCtx.typeId(typeName); typeName = binaryCtx.userTypeName(typeName); BinaryUtils.validateEnumValues(typeName, vals); updateMetadata(typeId, typeName, null, null, true, vals); return binaryCtx.metadata(typeId); }
BinaryInternalMapper mapper = userTypeMapper(clsName); registered = registerUserClassName(typeId, cls.getName()); BinarySerializer serializer = serializerForClass(cls); String affFieldName = affinityFieldName(cls); desc.isEnum(), cls.isEnum() ? enumMap(cls) : null).wrap(this), false);
desc = ctx.descriptorForClass(BinaryUtils.doReadClass(in, ctx, ldr, typeId0), false, false); typeId = ctx.typeId(BinaryUtils.doReadClassName(in)); mapper = userType ? ctx.userTypeMapper(typeId) : BinaryContext.defaultMapper(); schema = BinaryUtils.hasSchema(flags) ? getOrCreateSchema() : null;
/** * Create binary marshaller. * * @return Marshaller. */ public static GridBinaryMarshaller marshaller() { BinaryContext ctx = new BinaryContext(BinaryNoopMetadataHandler.instance(), new IgniteConfiguration(), new NullLogger()); BinaryMarshaller marsh = new BinaryMarshaller(); marsh.setContext(new MarshallerContextImpl(null, null)); ctx.configure(marsh, new IgniteConfiguration()); return new GridBinaryMarshaller(ctx); }
/** * @throws Exception If failed. */ @Test public void testPredefinedTypeIds() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); BinaryContext bCtx = binaryContext(marsh); Field field = bCtx.getClass().getDeclaredField("predefinedTypeNames"); field.setAccessible(true); Map<String, Integer> map = (Map<String, Integer>)field.get(bCtx); assertTrue(!map.isEmpty()); for (Map.Entry<String, Integer> entry : map.entrySet()) { int id = entry.getValue(); if (id == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) continue; BinaryClassDescriptor desc = bCtx.descriptorForTypeId(false, entry.getValue(), null, false); assertEquals(desc.typeId(), bCtx.typeId(desc.describedClass().getName())); } }
/** * Sets {@link BinaryContext}. * <p/> * @param ctx Binary context. */ private void setBinaryContext(BinaryContext ctx, IgniteConfiguration cfg) { ctx.configure(this, cfg); impl = new GridBinaryMarshaller(ctx); }
/** * @return Target type. */ private BinaryType target() { if (target == null) { synchronized (this) { if (target == null) { if (typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID && clsName != null) typeId = ctx.typeId(clsName); target = ctx.metadata(typeId); if (target == null) throw new BinaryObjectException("Failed to get binary type details [typeId=" + typeId + ']'); } } } return target; }
/** * Perform post-write hash code update if necessary. * * @param clsName Class name. Always null if class is registered. */ public void postWriteHashCode(@Nullable String clsName) { int typeId = clsName == null ? this.typeId : ctx.typeId(clsName); BinaryIdentityResolver identity = ctx.identity(typeId); if (out.hasArray()) { // Heap. byte[] data = out.array(); BinaryObjectImpl obj = new BinaryObjectImpl(ctx, data, start); BinaryPrimitives.writeInt(data, start + GridBinaryMarshaller.HASH_CODE_POS, identity.hashCode(obj)); } else { // Offheap. long ptr = out.rawOffheapPointer(); assert ptr != 0; BinaryObjectOffheapImpl obj = new BinaryObjectOffheapImpl(ctx, ptr, start, out.capacity()); BinaryPrimitives.writeInt(ptr, start + GridBinaryMarshaller.HASH_CODE_POS, identity.hashCode(obj)); } }
/** {@inheritDoc} */ @Override public BinaryObject buildEnum(String typeName, String name) { if (typeName == null || typeName.isEmpty()) throw new IllegalArgumentException("typeName"); if (name == null || name.isEmpty()) throw new IllegalArgumentException("name"); BinaryContext ctx = marsh.context(); int typeId = ctx.typeId(typeName); BinaryMetadata metadata = ctx.metadata0(typeId); if (metadata == null) throw new BinaryObjectException( String.format("Failed to get metadata for type [typeId=%s, typeName='%s']", typeId, typeName) ); Integer ordinal = metadata.getEnumOrdinalByName(name); if (ordinal == null) throw new BinaryObjectException(String.format( "Failed to resolve enum ordinal by name [typeId=%s, typeName='%s', name='%s']", typeId, typeName, name )); return new BinaryEnumObjectImpl(ctx, typeId, null, ordinal); }