public HollowDiffObjectCountingNode(HollowDiff diff, HollowTypeDiff topLevelTypeDiff, HollowDiffNodeIdentifier nodeId, HollowObjectTypeReadState fromState, HollowObjectTypeReadState toState) { super(diff, topLevelTypeDiff, nodeId); this.fromState = fromState; this.toState = toState; this.fromSchema = fromState == null ? emptySchema(toState.getSchema()) : fromState.getSchema(); this.toSchema = toState == null ? emptySchema(fromState.getSchema()) : toState.getSchema(); if(!fromSchema.getName().equals(toSchema.getName())) throw new IllegalArgumentException("Cannot diff between two schemas with different names: from '" + fromSchema.getName() + "' to '" + toSchema.getName() + "'"); this.unionSchema = fromSchema.findUnionSchema(toSchema); this.fieldNodes = new HollowDiffCountingNode[unionSchema.numFields()]; this.fromFieldMapping = createFieldMapping(unionSchema, fromSchema); this.toFieldMapping = createFieldMapping(unionSchema, toSchema); this.fieldRequiresMissingFieldTraversal = new boolean[unionSchema.numFields()]; this.fieldEqualOrdinalFilters = new DiffEqualOrdinalFilter[unionSchema.numFields()]; for(int i=0;i<unionSchema.numFields();i++) { int fromFieldIndex = fromSchema.getPosition(unionSchema.getFieldName(i)); int toFieldIndex = toSchema.getPosition(unionSchema.getFieldName(i)); if(unionSchema.getFieldType(i) == FieldType.REFERENCE) { HollowTypeReadState refFromState = fromFieldIndex == -1 ? null : fromSchema.getReferencedTypeState(fromFieldIndex); HollowTypeReadState refToState = toFieldIndex == -1 ? null : toSchema.getReferencedTypeState(toFieldIndex); fieldNodes[i] = getHollowDiffCountingNode(refFromState, refToState, unionSchema.getFieldName(i)); fieldEqualOrdinalFilters[i] = new DiffEqualOrdinalFilter(equalityMapping.getEqualOrdinalMap(unionSchema.getReferencedType(i))); if(refFromState == null || refToState == null || equalityMapping.requiresMissingFieldTraversal(unionSchema.getReferencedType(i))) fieldRequiresMissingFieldTraversal[i] = true; } else { HollowDiffNodeIdentifier childNodeId = new HollowDiffNodeIdentifier(nodeId, unionSchema.getFieldName(i), unionSchema.getFieldType(i).toString()); fieldNodes[i] = new HollowDiffFieldCountingNode(diff, topLevelTypeDiff, childNodeId, fromState, toState, unionSchema, i); } } }
@Test public void findsUnionSchemas() { HollowObjectSchema s1 = new HollowObjectSchema("Test", 2, "F2"); s1.addField("F1", FieldType.INT); s1.addField("F2", FieldType.LONG); HollowObjectSchema s2 = new HollowObjectSchema("Test", 2, "F2"); s2.addField("F2", FieldType.LONG); s2.addField("F3", FieldType.STRING); HollowObjectSchema unionSchema = s1.findUnionSchema(s2); Assert.assertEquals(3, unionSchema.numFields()); Assert.assertEquals("F1", unionSchema.getFieldName(0)); Assert.assertEquals(FieldType.INT, unionSchema.getFieldType(0)); Assert.assertEquals("F2", unionSchema.getFieldName(1)); Assert.assertEquals(FieldType.LONG, unionSchema.getFieldType(1)); Assert.assertEquals("F3", unionSchema.getFieldName(2)); Assert.assertEquals(FieldType.STRING, unionSchema.getFieldType(2)); Assert.assertEquals(s1.getPrimaryKey(), s2.getPrimaryKey()); Assert.assertEquals(s1.getPrimaryKey(), unionSchema.getPrimaryKey()); { HollowObjectSchema s3 = new HollowObjectSchema("Test", 2, "F3"); s3.addField("F2", FieldType.LONG); s3.addField("F3", FieldType.STRING); HollowObjectSchema u3 = s1.findUnionSchema(s3); Assert.assertNotEquals(s1.getPrimaryKey(), u3.getPrimaryKey()); Assert.assertNotEquals(s1.getPrimaryKey(), u3.getPrimaryKey()); Assert.assertNull(u3.getPrimaryKey()); } }