public HollowEffigy effigy(HollowDataAccess dataAccess, String typeName, int ordinal) { if(ordinal == -1) return null; HollowTypeDataAccess typeState = dataAccess.getTypeDataAccess(typeName, ordinal); if(typeState == null) return null; if(typeState instanceof HollowObjectTypeDataAccess) { return new HollowEffigy(this, (HollowObjectTypeDataAccess) typeState, ordinal); } else if(typeState instanceof HollowCollectionTypeDataAccess) { return new HollowEffigy(this, (HollowCollectionTypeDataAccess) typeState, ordinal); } else if(typeState instanceof HollowMapTypeDataAccess){ return new HollowEffigy(this, (HollowMapTypeDataAccess) typeState, ordinal); } throw new IllegalArgumentException("I don't know how to effigize a " + typeState.getClass()); }
private void addUnmatchedElements(List<EffigyFieldPair> fieldPairs, BitSet pairedFromIndices, BitSet pairedToIndices) { for(int i=0;i<from.getFields().size();i++) { if(!pairedFromIndices.get(i)) fieldPairs.add(new EffigyFieldPair(from.getFields().get(i), null, i, -1)); } for(int i=0;i<to.getFields().size();i++) { if(!pairedToIndices.get(i)) fieldPairs.add(new EffigyFieldPair(null, to.getFields().get(i), -1, i)); } }
public Field(String fieldName, HollowEffigy value) { this(fieldName, value.getObjectType(), value); }
private HollowEffigy list(HollowEffigy... elements) { HollowEffigy list = new HollowEffigy("list"); for(HollowEffigy element : elements) { list.add(new Field("element", element)); } return list; }
private boolean rowIsExactMatch(HollowDiffViewRow row) { EffigyFieldPair fieldPair = row.getFieldPair(); if(fieldPair.getFrom() == null || fieldPair.getTo() == null || fieldPair.isLeafNode()) return false; HollowEffigy fromEffigy = (HollowEffigy)fieldPair.getFrom().getValue(); HollowEffigy toEffigy = (HollowEffigy)fieldPair.getTo().getValue(); if(fromEffigy == null || toEffigy == null) return false; return exactRecordMatcher.isExactMatch(fromEffigy.getDataAccess(), fromEffigy.getOrdinal(), toEffigy.getDataAccess(), toEffigy.getOrdinal()); }
if(from.getFields().size() == 0) { for(int i=0;i<to.getFields().size();i++) fieldPairs.add(new EffigyFieldPair(null, to.getFields().get(i), -1, i)); return fieldPairs; } else if(to.getFields().size() == 0) { for(int i=0;i<from.getFields().size();i++) fieldPairs.add(new EffigyFieldPair(from.getFields().get(i), null, i, -1)); toFieldPathIndexes[i] = matchHint.getFieldPathIndex(to.getDataAccess().getDataAccess(), i); fromFieldPathIndexes[i] = matchHint.getFieldPathIndex(from.getDataAccess().getDataAccess(), i); int hashedToFieldIndexes[] = new int[HashCodes.hashTableSize(to.getFields().size())]; Arrays.fill(hashedToFieldIndexes, -1); for(int i=0;i<to.getFields().size();i++) { HollowEffigy comparisonEffigy = getComparisonEffigy((HollowEffigy) to.getFields().get(i).getValue()); int hash = hashCode(comparisonEffigy, toFieldPathIndexes); hash &= hashedToFieldIndexes.length-1; BitSet matchedToElements = new BitSet(to.getFields().size()); BitSet matchedFromElements = new BitSet(from.getFields().size()); for(int i=0;i<from.getFields().size();i++) { HollowEffigy fromEffigy = getComparisonEffigy((HollowEffigy) from.getFields().get(i).getValue()); int hash = hashCode(fromEffigy, fromFieldPathIndexes); hash &= hashedToFieldIndexes.length-1; int toIdx = hashedToFieldIndexes[hash]; if(!matchedToElements.get(toIdx)) {
public static List<EffigyFieldPair> pair(HollowEffigy from, HollowEffigy to, Map<String, PrimaryKey> matchHints) { if(from == null || to == null) return new HollowEffigyNullPartnerPairer(from, to).pair(); if(from.getDataAccess() == null) return new HollowEffigyObjectPairer(from, to).pair(); HollowSchema schema = from.getDataAccess().getSchema(); switch(schema.getSchemaType()) { case OBJECT: return new HollowEffigyObjectPairer(from, to).pair(); case MAP: String keyType = ((HollowMapSchema)schema).getKeyType(); return new HollowEffigyMapPairer(from, to, matchHints.get(keyType)).pair(); case LIST: case SET: String elementType = ((HollowCollectionSchema)schema).getElementType(); return new HollowEffigyCollectionPairer(from, to, matchHints.get(elementType)).pair(); } throw new IllegalArgumentException("I don't know how to pair fields for type " + schema.getName() + "(" + schema.getSchemaType() + ")"); } }
private HollowEffigy element(String field1, int field2, float field3) { HollowEffigy eff = new HollowEffigy("element"); eff.add(new Field("field1", "STRING", field1)); eff.add(new Field("field2", "INT", field2)); eff.add(new Field("field3", "FLOAT", field3)); return eff; } }
private boolean fieldsAreEqual(HollowEffigy fromElement, HollowEffigy toElement, int[] fromFieldPath, int[] toFieldPath) { HollowObjectTypeDataAccess fromDataAccess = (HollowObjectTypeDataAccess) fromElement.getDataAccess(); int fromOrdinal = fromElement.getOrdinal(); HollowObjectTypeDataAccess toDataAccess = (HollowObjectTypeDataAccess) toElement.getDataAccess(); int toOrdinal = toElement.getOrdinal(); HollowObjectSchema fromSchema = fromDataAccess.getSchema(); HollowObjectSchema toSchema = toDataAccess.getSchema(); for (int i = 0; i < fromFieldPath.length - 1; i++) { int fromFieldPosition = fromFieldPath[i]; int toFieldPosition = toFieldPath[i]; fromOrdinal = fromDataAccess.readOrdinal(fromOrdinal, fromFieldPosition); toOrdinal = toDataAccess.readOrdinal(toOrdinal, toFieldPosition); fromDataAccess = (HollowObjectTypeDataAccess) fromDataAccess.getDataAccess().getTypeDataAccess(fromSchema.getReferencedType(fromFieldPosition)); toDataAccess = (HollowObjectTypeDataAccess) toDataAccess.getDataAccess().getTypeDataAccess(toSchema.getReferencedType(toFieldPosition)); fromSchema = fromDataAccess.getSchema(); toSchema = toDataAccess.getSchema(); } return HollowReadFieldUtils.fieldsAreEqual(fromDataAccess, fromOrdinal, fromFieldPath[fromFieldPath.length-1], toDataAccess, toOrdinal, toFieldPath[toFieldPath.length-1]); }
private List<Field> createMapFields(HollowEffigy effigy) { List<Field> fields = new ArrayList<Field>(); HollowMapTypeDataAccess typeDataAccess = (HollowMapTypeDataAccess)effigy.dataAccess; HollowMapSchema schema = typeDataAccess.getSchema(); HollowMapEntryOrdinalIterator iter = typeDataAccess.ordinalIterator(effigy.ordinal); while(iter.next()) { HollowEffigy entryEffigy = new HollowEffigy("Map.Entry"); entryEffigy.add(new Field("key", effigy(typeDataAccess.getDataAccess(), schema.getKeyType(), iter.getKey()))); entryEffigy.add(new Field("value", effigy(typeDataAccess.getDataAccess(), schema.getValueType(), iter.getValue()))); fields.add(new Field("entry", "Map.Entry", entryEffigy)); } return fields; }
@Override public List<EffigyFieldPair> pair() { List<EffigyFieldPair> pairs = new ArrayList<EffigyFieldPair>(); if(from != null) { for(int i=0;i<from.getFields().size();i++) { pairs.add(new EffigyFieldPair(from.getFields().get(i), null, i, -1)); } } else if(to != null) { for(int i=0;i<to.getFields().size();i++) { pairs.add(new EffigyFieldPair(null, to.getFields().get(i), -1, i)); } } return pairs; }
private int fieldHashCode(HollowEffigy element, int[] fieldPath) { HollowObjectTypeDataAccess dataAccess = (HollowObjectTypeDataAccess) element.getDataAccess(); HollowObjectSchema schema = dataAccess.getSchema(); int ordinal = element.getOrdinal(); for (int i = 0; i < fieldPath.length - 1; i++) { int fieldPosition = fieldPath[i]; ordinal = dataAccess.readOrdinal(ordinal, fieldPosition); dataAccess = (HollowObjectTypeDataAccess) dataAccess.getDataAccess().getTypeDataAccess(schema.getReferencedType(fieldPosition)); schema = dataAccess.getSchema(); } int fieldHash = HollowReadFieldUtils.fieldHashCode(dataAccess, ordinal, fieldPath[fieldPath.length-1]); return HashCodes.hashInt(fieldHash); }
public Field getField(HollowEffigy effigy, String fieldName) { for(Field field : effigy.getFields()) { if(field.getFieldName().equals(fieldName)) return field; } return null; }
@Override protected HollowEffigy getComparisonEffigy(HollowEffigy effigy) { return (HollowEffigy) effigy.getFields().get(0).getValue(); }
List<EffigyFieldPair> fieldPairs = new ArrayList<EffigyFieldPair>(); BitSet pairedFromIndices = new BitSet(from.getFields().size()); BitSet pairedToIndices = new BitSet(to.getFields().size()); int maxPairs = Math.min(from.getFields().size(), to.getFields().size()); continue; fieldPairs.add(new EffigyFieldPair(from.getFields().get(fromIndex), to.getFields().get(toIndex), fromIndex, toIndex)); pairedFromIndices.set(fromIndex); pairedToIndices.set(toIndex);
public long[] pair(final BitSet pairedFromIndices, final BitSet pairedToIndices, final int maxDiff) { final long diffMatrixElements[] = new long[from.getFields().size() * to.getFields().size()]; int matrixElementIdx = 0; for(int i=0;i<from.getFields().size();i++) { final int fromIdx = i; if(pairedFromIndices.get(fromIdx)) { for(int j=0;j<to.getFields().size();j++) { diffMatrixElements[matrixElementIdx++] = getDiffMatrixElement(fromIdx, j, MAX_MATRIX_ELEMENT_FIELD_VALUE); } } else { HollowEffigy fromElement = getComparisonEffigy((HollowEffigy) from.getFields().get(fromIdx).getValue()); HollowEffigyDiffRecord diffRecord = new HollowEffigyDiffRecord(fromElement); for(int j=0;j<to.getFields().size();j++) { if(pairedToIndices.get(j)) { diffMatrixElements[matrixElementIdx++] = getDiffMatrixElement(fromIdx, j, MAX_MATRIX_ELEMENT_FIELD_VALUE); } else { HollowEffigy toElement = getComparisonEffigy((HollowEffigy) to.getFields().get(j).getValue()); int diffScore = diffRecord.calculateDiff(toElement, maxDiff); diffMatrixElements[matrixElementIdx++] = getDiffMatrixElement(fromIdx, j, diffScore); } } } } return diffMatrixElements; }
@Override public List<EffigyFieldPair> pair() { List<EffigyFieldPair> fieldPairs = new ArrayList<EffigyFieldPair>(); for(Field fromField : from.getFields()) { fieldPairs.add(new EffigyFieldPair(fromField, getField(to, fromField.getFieldName()), -1, -1)); } for(Field toField : to.getFields()) { Field fromField = getField(from, toField.getFieldName()); if(fromField == null) fieldPairs.add(new EffigyFieldPair(null, toField, -1, -1)); } return fieldPairs; }
private void traverseOriginalFields(HollowEffigy effigy) { for(Field field : effigy.getFields()) { if(field.isLeafNode()) { FieldDiffCount fieldCount = map.get(field); if(fieldCount == null) { fieldCount = new FieldDiffCount(); map.put(field, fieldCount); } fieldCount.incrementOriginalCount(); totalOriginalFieldCount++; } else { traverseOriginalFields((HollowEffigy) field.getValue()); } } }
public void traverseComparisonFields(HollowEffigy comparison, int maxDiff) { for(Field field : comparison.getFields()) { if(field.isLeafNode()) { FieldDiffCount fieldCount = map.get(field); if(fieldCount == null) { if(simDiffCount.diffCount+1 >= maxDiff) { simDiffCount.diffCount++; return; } fieldCount = new FieldDiffCount(); map.put(field, fieldCount); } if(fieldCount.incrementComparisonCount(runId)) { if(++simDiffCount.diffCount >= maxDiff) return; } else { simDiffCount.simCount++; } } else { traverseComparisonFields((HollowEffigy) field.getValue(), maxDiff); if(simDiffCount.diffCount >= maxDiff) return; } } }
private void assertPair(EffigyFieldPair pair, String expectedFromField1, String expectedToField1) { if(expectedFromField1 != null) { HollowEffigy element = (HollowEffigy) pair.getFrom().getValue(); Assert.assertEquals(expectedFromField1, element.getFields().get(0).getValue()); } else { Assert.assertNull(pair.getFrom()); } if(expectedToField1 != null) { HollowEffigy element = (HollowEffigy) pair.getTo().getValue(); Assert.assertEquals(expectedToField1, element.getFields().get(0).getValue()); } else { Assert.assertNull(pair.getTo()); } }