org.apache.hadoop.hive.ql.exec.vector.expressions.CuckooSetLong Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hive.ql.exec.vector.expressions;
import java.util.Arrays;
import java.util.Random;
/**
* A high-performance set implementation used to support fast set membership testing,
* using Cuckoo hashing. This is used to support fast tests of the form
*
* column IN ( >> 32) ^ y)) & 0xFFFFFFFF);
x = (x + salt[0]) + (x << 12);
x = (x ^ salt[1]) ^ (x >> 19);
x = (x + salt[2]) + (x << 5);
x = (x + salt[3]) ^ (x << 9);
x = (x + salt[4]) + (x << 3);
x = (x ^ salt[5]) ^ (x >> 16);
// Return value modulo n but always in the positive range (0..n-1).
// And with the mask to zero the sign bit to make the input to mod positive
// so the output will definitely be positive.
return (x & 0x7FFFFFFF) % n;
}
/**
* basic modular hash function
*/
private int h2(long x) {
// Return value modulo n but always in the positive range (0..n-1).
// Since n is prime, this gives good spread for numbers that are multiples
// of one billion, which is important since timestamps internally
// are stored as a number of nanoseconds, and the fractional seconds
// part is often 0.
return (((int) (x % n)) + n) % n;
}
/**
* In case of rehash, hash function h2 is changed by updating the
* entries in the salt array with new random values.
*/
private void updateHashSalt() {
for (int i = 0; i != 6; i++) {
salt[i] = gen.nextInt(0x7FFFFFFF);
}
}
private void rehash() {
rehashCount++;
if (rehashCount > 20) {
throw new RuntimeException("Too many rehashes");
}
updateHashSalt();
// Save original values
if (prev1 == null) {
prev1 = t1;
prev2 = t2;
}
t1 = new long[n];
t2 = new long[n];
Arrays.fill(t1, blank);
Arrays.fill(t2, blank);
for (Long v : prev1) {
if (v != blank) {
long x = tryInsert(v);
if (x != blank) {
rehash();
return;
}
}
}
for (Long v : prev2) {
if (v != blank) {
long x = tryInsert(v);
if (x != blank) {
rehash();
return;
}
}
}
// We succeeded in adding all the values, so
// clear the previous values recorded.
prev1 = null;
prev2 = null;
}
}