add FuncFilter

This commit is contained in:
Looly 2022-03-27 21:15:49 +08:00
parent 328d54d986
commit 7385fe937d
14 changed files with 134 additions and 132 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.0 (2022-03-26)
# 5.8.0 (2022-03-27)
### ❌不兼容特性
* 【db 】 【不向下兼容 】增加MongoDB4.x支持返回MongoClient变更pr#568@Gitee
@ -46,6 +46,7 @@
* 【core 】 CompareUtil增加comparingIndexedpr#585@Gitee
* 【db 】 DruidDataSource构建时支持自定义参数issue#I4ZKCW@Gitee
* 【poi 】 ExcelWriter增加addImg重载issue#2218@Github
* 【bloomFilter】 增加FuncFilter
### 🐞Bug修复
* 【core 】 修复ObjectUtil.hasNull传入null返回true的问题pr#555@Gitee

View File

@ -14,9 +14,11 @@ import cn.hutool.bloomfilter.bitMap.LongMap;
public abstract class AbstractFilter implements BloomFilter {
private static final long serialVersionUID = 1L;
protected static int DEFAULT_MACHINE_NUM = BitMap.MACHINE32;
private BitMap bm = null;
protected long size = 0;
protected long size;
/**
* 构造
@ -34,7 +36,7 @@ public abstract class AbstractFilter implements BloomFilter {
* @param maxValue 最大值
*/
public AbstractFilter(long maxValue) {
this(maxValue, BitMap.MACHINE32);
this(maxValue, DEFAULT_MACHINE_NUM);
}
/**
@ -80,4 +82,4 @@ public abstract class AbstractFilter implements BloomFilter {
* @return HashCode
*/
public abstract long hash(String str);
}
}

View File

@ -7,19 +7,14 @@ import cn.hutool.core.util.HashUtil;
*
* @author loolly
*/
public class DefaultFilter extends AbstractFilter {
public class DefaultFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public DefaultFilter(long maxValue, int machineNumber) {
super(maxValue, machineNumber);
}
public DefaultFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
return HashUtil.javaDefaultHash(str) % size;
public DefaultFilter(long maxValue, int machineNumber) {
super(maxValue, machineNumber, HashUtil::javaDefaultHash);
}
}

View File

@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class ELFFilter extends AbstractFilter {
public class ELFFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public ELFFilter(long maxValue, int machineNumber) {
super(maxValue, machineNumber);
}
public ELFFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
return HashUtil.elfHash(str) % size;
public ELFFilter(long maxValue, int machineNumber) {
super(maxValue, machineNumber, HashUtil::elfHash);
}
}

View File

@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class FNVFilter extends AbstractFilter {
public class FNVFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public FNVFilter(long maxValue, int machineNum) {
super(maxValue, machineNum);
}
public FNVFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
return HashUtil.fnvHash(str);
public FNVFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::fnvHash);
}
}

View File

@ -0,0 +1,42 @@
package cn.hutool.bloomfilter.filter;
import cn.hutool.bloomfilter.BloomFilter;
import java.util.function.Function;
/**
* 基于Hash函数方法的{@link BloomFilter}
*
* @author looly
* @since 5.8.0
*/
public class FuncFilter extends AbstractFilter {
private static final long serialVersionUID = 1L;
private final Function<String, Number> hashFunc;
/**
* 构造
*
* @param maxValue 最大值
* @param hashFunc Hash函数
*/
public FuncFilter(long maxValue, Function<String, Number> hashFunc) {
this(maxValue, DEFAULT_MACHINE_NUM, hashFunc);
}
/**
* @param maxValue 最大值
* @param machineNum 机器位数
* @param hashFunc Hash函数
*/
public FuncFilter(long maxValue, int machineNum, Function<String, Number> hashFunc) {
super(maxValue, machineNum);
this.hashFunc = hashFunc;
}
@Override
public long hash(String str) {
return hashFunc.apply(str).longValue() % size;
}
}

View File

@ -1,31 +1,16 @@
package cn.hutool.bloomfilter.filter;
public class HfFilter extends AbstractFilter {
import cn.hutool.core.util.HashUtil;
public class HfFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public HfFilter(long maxValue, int machineNum) {
super(maxValue, machineNum);
}
public HfFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
int length = str.length() ;
long hash = 0;
for (int i = 0; i < length; i++) {
hash += str.charAt(i) * 3 * i;
}
if (hash < 0) {
hash = -hash;
}
return hash % size;
public HfFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::hfHash);
}
}

View File

@ -1,24 +1,15 @@
package cn.hutool.bloomfilter.filter;
public class HfIpFilter extends AbstractFilter {
import cn.hutool.core.util.HashUtil;
public class HfIpFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public HfIpFilter(long maxValue, int machineNum) {
super(maxValue, machineNum);
}
public HfIpFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
int length = str.length();
long hash = 0;
for (int i = 0; i < length; i++) {
hash += str.charAt(i % 4) ^ str.charAt(i);
}
return hash % size;
public HfIpFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::hfIpHash);
}
}

View File

@ -1,30 +1,15 @@
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class JSFilter extends AbstractFilter {
public class JSFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public JSFilter(long maxValue, int machineNum) {
super(maxValue, machineNum);
}
public JSFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
int hash = 1315423911;
for (int i = 0; i < str.length(); i++) {
hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
}
if(hash<0) {
hash*=-1 ;
}
return hash % size;
public JSFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::jsHash);
}
}

View File

@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class PJWFilter extends AbstractFilter {
public class PJWFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public PJWFilter(long maxValue, int machineNum) {
super(maxValue, machineNum);
}
public PJWFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
return HashUtil.pjwHash(str) % size;
public PJWFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::pjwHash);
}
}

View File

@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class RSFilter extends AbstractFilter {
public class RSFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public RSFilter(long maxValue, int machineNum) {
super(maxValue, machineNum);
}
public RSFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
return HashUtil.rsHash(str) % size;
public RSFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::rsHash);
}
}

View File

@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class SDBMFilter extends AbstractFilter {
public class SDBMFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public SDBMFilter(long maxValue, int machineNum) {
super(maxValue, machineNum);
}
public SDBMFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
return HashUtil.sdbmHash(str) % size;
public SDBMFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::sdbmHash);
}
}

View File

@ -2,21 +2,14 @@ package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class TianlFilter extends AbstractFilter {
public class TianlFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public TianlFilter(long maxValue, int machineNum) {
super(maxValue, machineNum);
}
public TianlFilter(long maxValue) {
super(maxValue);
this(maxValue, DEFAULT_MACHINE_NUM);
}
@Override
public long hash(String str) {
return HashUtil.tianlHash(str) % size;
public TianlFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::tianlHash);
}
}

View File

@ -226,7 +226,7 @@ public class HashUtil {
hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
}
return hash & 0x7FFFFFFF;
return Math.abs(hash) & 0x7FFFFFFF;
}
/**
@ -576,7 +576,7 @@ public class HashUtil {
* @return hash值long[0]低位long[1]高位
*/
public static long[] metroHash128(byte[] data, long seed) {
return MetroHash.hash128(data,seed).getLongArray();
return MetroHash.hash128(data, seed).getLongArray();
}
/**
@ -588,4 +588,42 @@ public class HashUtil {
public static long[] metroHash128(byte[] data) {
return MetroHash.hash128(data).getLongArray();
}
/**
* HF Hash算法
*
* @param data 字符串
* @return hash结果
* @since 5.8.0
*/
public static long hfHash(String data) {
int length = data.length();
long hash = 0;
for (int i = 0; i < length; i++) {
hash += (long) data.charAt(i) * 3 * i;
}
if (hash < 0) {
hash = -hash;
}
return hash;
}
/**
* HFIP Hash算法
*
* @param data 字符串
* @return hash结果
* @since 5.8.0
*/
public static long hfIpHash(String data) {
int length = data.length();
long hash = 0;
for (int i = 0; i < length; i++) {
hash += data.charAt(i % 4) ^ data.charAt(i);
}
return hash;
}
}