@Override public boolean equalToReference(T2 candidate) { comparator2.extractKeys(candidate, candidateKeyFields, 0); for (int i = 0; i < this.comparators1.length; i++) { if (this.comparators1[i].compare(referenceKeyFields[i], candidateKeyFields[i]) != 0) { return false; } } return true; }
@Override public int compareToReference(T candidate) { comp2.setReference(candidate); return comp.compareToReference(comp2); } }
@SuppressWarnings("unchecked") public GenericPairComparator(TypeComparator<T1> comparator1, TypeComparator<T2> comparator2) { this.comparator1 = comparator1; this.comparator2 = comparator2; this.comparators1 = comparator1.getFlatComparators(); this.comparators2 = comparator2.getFlatComparators(); if(comparators1.length != comparators2.length) { throw new IllegalArgumentException("Number of key fields and comparators differ."); } int numKeys = comparators1.length; for(int i = 0; i < numKeys; i++) { this.comparators1[i] = comparators1[i].duplicate(); this.comparators2[i] = comparators2[i].duplicate(); } this.referenceKeyFields = new Object[numKeys]; this.candidateKeyFields = new Object[numKeys]; }
this.useNormKeyUninverted = !comparator.invertNormalizedKey(); if (this.comparator.supportsNormalizedKey()) { numPartialKeys = this.comparator.getFlatComparators().length; } catch (Throwable t) { numPartialKeys = 1; this.numKeyBytes = Math.min(this.comparator.getNormalizeKeyLen(), maxLen); this.normalizedKeyFullyDetermines = !this.comparator.isNormalizedKeyPrefixOnly(this.numKeyBytes);
@Test public void testNormalizedKeyReadWriter() { try { T[] data = getSortedData(); T reuse = getSortedData()[0]; TypeComparator<T> comp1 = getComparator(true); if(!comp1.supportsSerializationWithKeyNormalization()){ return; } TypeComparator<T> comp2 = comp1.duplicate(); comp2.setReference(reuse); TestOutputView out = new TestOutputView(); TestInputView in; for (T value : data) { comp1.setReference(value); comp1.writeWithKeyNormalization(value, out); in = out.getInputView(); comp1.readWithKeyDenormalization(reuse, in); assertTrue(comp1.compareToReference(comp2) == 0); } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); fail("Exception in test: " + e.getMessage()); } }
this.useNormKeyUninverted = !comparator.invertNormalizedKey(); this.numKeyBytes = this.comparator.getNormalizeKeyLen(); } else if (this.recordSize > this.segmentSize) { throw new IllegalArgumentException("This sorter works only for record lengths below the memory segment size."); } else if (!comparator.supportsSerializationWithKeyNormalization()) { throw new IllegalArgumentException("This sorter requires a comparator that supports serialization with key normalization.");
@Test @SuppressWarnings("unchecked") public void testKeyExtraction() { TypeComparator<T> comparator = getComparator(true); T[] data = getSortedData(); for (T value : data) { TypeComparator[] comparators = comparator.getFlatComparators(); Object[] extractedKeys = new Object[comparators.length]; int insertedKeys = comparator.extractKeys(value, extractedKeys, 0); assertTrue(insertedKeys == comparators.length); for (int i = 0; i < insertedKeys; i++) { // check if some keys are null, although this is not supported if (!supportsNullKeys()) { assertNotNull(extractedKeys[i]); } // compare the extracted key with itself as a basic check // if the extracted key corresponds to the comparator assertTrue(comparators[i].compare(extractedKeys[i], extractedKeys[i]) == 0); } } }
@SuppressWarnings("unchecked") public void insertOrReplace(BT record) { int hashCode = hash(buildComparator.hash(record)); int index = hashIndex(hashCode, data.length); buildComparator.setReference(record); HashEntry entry = data[index]; while (entry != null) { if (entryHashCode(entry) == hashCode && buildComparator.equalToReference((BT) entry.getValue())) { entry.setValue(record); return; } entry = entryNext(entry); } addMapping(index, hashCode, null, record); }
if (k.supportsNormalizedKey()) { if (i == 0) { inverted = k.invertNormalizedKey(); else if (k.invertNormalizedKey() != inverted) { final int len = k.getNormalizeKeyLen(); if (len < 0) { throw new RuntimeException("Comparator " + k.getClass().getName() + " specifies an invalid length for the normalized key: " + len);
@Override public int compare(IN o1, IN o2) { return comparator.compare(o1, o2); } });
@Test public void testDuplicate() { try { TypeComparator<T> comparator = getComparator(true); TypeComparator<T> clone = comparator.duplicate(); T[] data = getSortedData(); comparator.setReference(data[0]); clone.setReference(data[1]); assertTrue("Comparator duplication does not work: Altering the reference in a duplicated comparator alters the original comparator's reference.", comparator.equalToReference(data[0]) && clone.equalToReference(data[1])); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Override @SuppressWarnings("unchecked") public K getKey(IN value) throws Exception { comparator.extractKeys(value, keyArray, 0); return (K) keyArray[0]; } }
@Test public void testEqualityWithReference() { try { TypeSerializer<T> serializer = createSerializer(); TypeComparator<T> comparator = getComparator(true); TypeComparator<T> comparator2 = getComparator(true); T[] data = getSortedData(); for (T d : data) { comparator.setReference(d); // Make a copy to compare T copy = serializer.copy(d, serializer.createInstance()); // And then test equalTo and compareToReference method of comparator assertTrue(comparator.equalToReference(d)); comparator2.setReference(copy); assertTrue(comparator.compareToReference(comparator2) == 0); } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); fail("Exception in test: " + e.getMessage()); } }
@Override public void putNormalizedKey(T value, MemorySegment target, int offset, int numBytes) { int i = 0; for (; i < this.numLeadingNormalizableKeys && numBytes > 0; i++) { int len = this.normalizedKeyLengths[i]; len = numBytes >= len ? len : numBytes; this.comparators[i].putNormalizedKey(accessField(keyFields[i], value), target, offset, len); numBytes -= len; offset += len; } }
@Override public int hash(T record) { if (record != null) { return wrappedComparator.hash(record); } else { return 0; } }
public SameTypePairComparator(TypeComparator<T> comp) { this.comp = comp; comp2 = comp.duplicate(); }
this.comparator.setReference(this.lookahead); this.valuesIterator.next = this.lookahead; this.lookahead = null; while (true) { if (currentPosition < input.size() && (next = this.input.get(currentPosition++)) != null) { if (!this.comparator.equalToReference(next)) { this.comparator.setReference(next); this.valuesIterator.next = next; this.valuesIterator.iteratorAvailable = true; this.comparator.setReference(first); this.valuesIterator = new ValuesIterator(first, serializer); return true;
private int getNormKeyLen(boolean halfLength, T[] data, TypeComparator<T> comparator) throws Exception { // Same as in the NormalizedKeySorter int keyLen = Math.min(comparator.getNormalizeKeyLen(), DEFAULT_MAX_NORMALIZED_KEY_LEN); if (keyLen < comparator.getNormalizeKeyLen()) { assertTrue(comparator.isNormalizedKeyPrefixOnly(keyLen)); } if (halfLength) { keyLen = keyLen / 2; assertTrue(comparator.isNormalizedKeyPrefixOnly(keyLen)); } return keyLen; }
public NullAwareComparator(TypeComparator<T> wrappedComparator, boolean order) { this.wrappedComparator = wrappedComparator; this.order = order; this.flatFields = wrappedComparator.getFlatComparators().length; }
@Override public int compareToReference(TypeComparator<T> referencedComparator) { NullAwareComparator otherComparator = (NullAwareComparator) referencedComparator; boolean otherNullReference = otherComparator.nullReference; // both values are null -> equality if (nullReference && otherNullReference) { return 0; } // first value is null -> inequality // but order is considered else if (nullReference) { return order ? 1 : -1; } // second value is null -> inequality // but order is considered else if (otherNullReference) { return order ? -1 : 1; } // no null values else { return wrappedComparator.compareToReference(otherComparator.wrappedComparator); } }