private int hashCode(HollowEffigy element, int[][] fieldPathIndexes) { int hash = 0; for(int i=0;i<fieldPathIndexes.length;i++) { hash = hash * 31; hash ^= fieldHashCode(element, fieldPathIndexes[i]); } return hash; }
private boolean recordsMatch(HollowEffigy fromElement, HollowEffigy toElement, int[][] fromFieldPathIndexes, int[][] toFieldPathIndexes) { for(int i=0;i<fromFieldPathIndexes.length;i++) { if(!fieldsAreEqual(fromElement, toElement, fromFieldPathIndexes[i], toFieldPathIndexes[i])) return false; } return true; }
long diffMatrixElements[] = pair(pairedFromIndices, pairedToIndices, maxDiffBackoff[i]); break; int diffScore = getDiffScore(matrixElement); int fromIndex = getFromIndex(matrixElement); int toIndex = getToIndex(matrixElement); addUnmatchedElements(fieldPairs, pairedFromIndices, pairedToIndices);
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() + ")"); } }
HollowEffigy comparisonEffigy = getComparisonEffigy((HollowEffigy) to.getFields().get(i).getValue()); int hash = hashCode(comparisonEffigy, toFieldPathIndexes); hash &= hashedToFieldIndexes.length-1; while(hashedToFieldIndexes[hash] != -1) { HollowEffigy fromEffigy = getComparisonEffigy((HollowEffigy) from.getFields().get(i).getValue()); int hash = hashCode(fromEffigy, fromFieldPathIndexes); hash &= hashedToFieldIndexes.length-1; while(hashedToFieldIndexes[hash] != -1) { int toIdx = hashedToFieldIndexes[hash]; if(!matchedToElements.get(toIdx)) { HollowEffigy toEffigy = getComparisonEffigy((HollowEffigy) to.getFields().get(toIdx).getValue()); if(recordsMatch(fromEffigy, toEffigy, fromFieldPathIndexes, toFieldPathIndexes)) { fieldPairs.add(new EffigyFieldPair(from.getFields().get(i), to.getFields().get(toIdx), i, toIdx)); addUnmatchedElements(fieldPairs, matchedFromElements, matchedToElements);
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; }
@Test public void test() { HollowEffigy list1 = list( element("1", 1, 1), element("2", 2, 2), element("3", 3, 3), element("4", 4, 4) ); HollowEffigy list2 = list( element("3", 103, 103), element("1", 2, 1), element("2", 102, 2), element("5", 5, 5), element("1", 1, 1) ); HollowEffigyCollectionPairer pairer = new HollowEffigyCollectionPairer(list1, list2, null); List<EffigyFieldPair> pairs = pairer.pair(); Assert.assertEquals(6, pairs.size()); assertPair(pairs.get(0), "1", "1"); assertPair(pairs.get(1), "2", "2"); assertPair(pairs.get(2), "3", "3"); assertPair(pairs.get(3), "4", null); assertPair(pairs.get(4), null, "1"); assertPair(pairs.get(5), null, "5"); }