Skip to content

Commit

Permalink
IGNITE-16318 Fix write empty binary object (#10021)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taras Ledkov committed May 18, 2022
1 parent be45101 commit f22b520
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
Expand Up @@ -292,11 +292,9 @@ public void postWrite(boolean userType, boolean registered) {
useCompactFooter = false;
}

int finalSchemaId;
int offset;

if (fieldCnt != 0) {
finalSchemaId = schemaId;
offset = out.position() - start;

// Write the schema.
Expand All @@ -318,16 +316,13 @@ else if (offsetByteCnt == BinaryUtils.OFFSET_2)
}
else {
if (rawOffPos != 0) {
finalSchemaId = 0;
offset = rawOffPos - start;

// If there is no schema, we are free to write raw offset to schema offset.
flags |= BinaryUtils.FLAG_HAS_RAW;
}
else {
finalSchemaId = 0;
offset = 0;
}
else
offset = GridBinaryMarshaller.DFLT_HDR_LEN;
}

// Actual write.
Expand All @@ -341,7 +336,7 @@ else if (offsetByteCnt == BinaryUtils.OFFSET_2)
out.unsafeWriteInt(registered ? typeId : GridBinaryMarshaller.UNREGISTERED_TYPE_ID);
out.unsafePosition(start + GridBinaryMarshaller.TOTAL_LEN_POS);
out.unsafeWriteInt(retPos - start);
out.unsafeWriteInt(finalSchemaId);
out.unsafeWriteInt(schemaId);
out.unsafeWriteInt(offset);

out.unsafePosition(retPos);
Expand Down
Expand Up @@ -1178,6 +1178,36 @@ public void rebuildObjectWithCollections() throws Exception {
}
}

/** */
@Test
public void emptyObjectBuilder() throws IgniteCheckedException {
BinaryObject emptyBinObj = builder("test_type").build();

BinaryObjectBuilder bob2 = emptyBinObj.toBuilder();

// Check any field is null at the empty object.
assertNull(bob2.getField("a"));

// Modify empty object: add field.
BinaryObjectImpl o1 = (BinaryObjectImpl)bob2.setField("a", 1).build();
BinaryObjectImpl o2 = (BinaryObjectImpl)builder("test_type").setField("a", 1).build();

// Check that modified empty object and the new object with the sanme field are equals.
assertEquals(o1.schemaId(), o2.schemaId());
assertEquals(o1, o2);
}

/** */
@Test
public void emptyObjectBinarylizable() throws IgniteCheckedException {
BinaryMarshaller m = binaryMarshaller();

BinaryObjectBuilder bob = marshal(new ObjectRaw(), m).toBuilder();

// Check any field is null at the empty object.
assertNull(bob.getField("a"));
}

/**
*
*/
Expand Down Expand Up @@ -4155,6 +4185,66 @@ protected BinaryMarshaller binaryMarshaller(
return marsh;
}

/**
* @return Binary object builder.
*/
protected BinaryObjectBuilder builder(
String typeName
) throws IgniteCheckedException {
return builder(typeName, null, null, null, null, null);
}

/**
* @return Binary object builder.
*/
protected BinaryObjectBuilder builder(
String typeName,
BinaryNameMapper nameMapper,
BinaryIdMapper mapper,
BinarySerializer serializer,
Collection<BinaryTypeConfiguration> cfgs,
Collection<String> excludedClasses
) throws IgniteCheckedException {
IgniteConfiguration iCfg = new IgniteConfiguration();

BinaryConfiguration bCfg = new BinaryConfiguration();

bCfg.setNameMapper(nameMapper);
bCfg.setIdMapper(mapper);
bCfg.setSerializer(serializer);
bCfg.setCompactFooter(compactFooter());

bCfg.setTypeConfigurations(cfgs);

iCfg.setBinaryConfiguration(bCfg);
iCfg.setClientMode(false);
iCfg.setDiscoverySpi(new TcpDiscoverySpi() {
@Override public void sendCustomEvent(DiscoverySpiCustomMessage msg) throws IgniteException {
//No-op.
}
});
iCfg.setSystemViewExporterSpi(new JmxSystemViewExporterSpi());

BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), iCfg, new NullLogger());

BinaryMarshaller marsh = new BinaryMarshaller();

MarshallerContextTestImpl marshCtx = new MarshallerContextTestImpl(null, excludedClasses);

GridTestKernalContext kernCtx = new GridTestKernalContext(log, iCfg);

kernCtx.add(new GridSystemViewManager(kernCtx));
kernCtx.add(new GridDiscoveryManager(kernCtx));

marshCtx.onMarshallerProcessorStarted(kernCtx, null);

marsh.setContext(marshCtx);

marsh.setBinaryContext(ctx, iCfg);

return new BinaryObjectBuilderImpl(ctx, typeName);
}

/**
* @param exp Expected.
* @param act Actual.
Expand Down

0 comments on commit f22b520

Please sign in to comment.