mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add FuncFilter
This commit is contained in:
parent
328d54d986
commit
7385fe937d
@ -2,7 +2,7 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.0 (2022-03-26)
|
# 5.8.0 (2022-03-27)
|
||||||
|
|
||||||
### ❌不兼容特性
|
### ❌不兼容特性
|
||||||
* 【db 】 【不向下兼容 】增加MongoDB4.x支持返回MongoClient变更(pr#568@Gitee)
|
* 【db 】 【不向下兼容 】增加MongoDB4.x支持返回MongoClient变更(pr#568@Gitee)
|
||||||
@ -46,6 +46,7 @@
|
|||||||
* 【core 】 CompareUtil增加comparingIndexed(pr#585@Gitee)
|
* 【core 】 CompareUtil增加comparingIndexed(pr#585@Gitee)
|
||||||
* 【db 】 DruidDataSource构建时支持自定义参数(issue#I4ZKCW@Gitee)
|
* 【db 】 DruidDataSource构建时支持自定义参数(issue#I4ZKCW@Gitee)
|
||||||
* 【poi 】 ExcelWriter增加addImg重载(issue#2218@Github)
|
* 【poi 】 ExcelWriter增加addImg重载(issue#2218@Github)
|
||||||
|
* 【bloomFilter】 增加FuncFilter
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复ObjectUtil.hasNull传入null返回true的问题(pr#555@Gitee)
|
* 【core 】 修复ObjectUtil.hasNull传入null返回true的问题(pr#555@Gitee)
|
||||||
|
@ -14,9 +14,11 @@ import cn.hutool.bloomfilter.bitMap.LongMap;
|
|||||||
public abstract class AbstractFilter implements BloomFilter {
|
public abstract class AbstractFilter implements BloomFilter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
protected static int DEFAULT_MACHINE_NUM = BitMap.MACHINE32;
|
||||||
|
|
||||||
private BitMap bm = null;
|
private BitMap bm = null;
|
||||||
|
|
||||||
protected long size = 0;
|
protected long size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
@ -34,7 +36,7 @@ public abstract class AbstractFilter implements BloomFilter {
|
|||||||
* @param maxValue 最大值
|
* @param maxValue 最大值
|
||||||
*/
|
*/
|
||||||
public AbstractFilter(long maxValue) {
|
public AbstractFilter(long maxValue) {
|
||||||
this(maxValue, BitMap.MACHINE32);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,19 +7,14 @@ import cn.hutool.core.util.HashUtil;
|
|||||||
*
|
*
|
||||||
* @author loolly
|
* @author loolly
|
||||||
*/
|
*/
|
||||||
public class DefaultFilter extends AbstractFilter {
|
public class DefaultFilter extends FuncFilter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public DefaultFilter(long maxValue, int machineNumber) {
|
|
||||||
super(maxValue, machineNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultFilter(long maxValue) {
|
public DefaultFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public DefaultFilter(long maxValue, int machineNumber) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNumber, HashUtil::javaDefaultHash);
|
||||||
return HashUtil.javaDefaultHash(str) % size;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
|
|||||||
|
|
||||||
import cn.hutool.core.util.HashUtil;
|
import cn.hutool.core.util.HashUtil;
|
||||||
|
|
||||||
public class ELFFilter extends AbstractFilter {
|
public class ELFFilter extends FuncFilter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public ELFFilter(long maxValue, int machineNumber) {
|
|
||||||
super(maxValue, machineNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ELFFilter(long maxValue) {
|
public ELFFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public ELFFilter(long maxValue, int machineNumber) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNumber, HashUtil::elfHash);
|
||||||
return HashUtil.elfHash(str) % size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
|
|||||||
|
|
||||||
import cn.hutool.core.util.HashUtil;
|
import cn.hutool.core.util.HashUtil;
|
||||||
|
|
||||||
public class FNVFilter extends AbstractFilter {
|
public class FNVFilter extends FuncFilter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public FNVFilter(long maxValue, int machineNum) {
|
|
||||||
super(maxValue, machineNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FNVFilter(long maxValue) {
|
public FNVFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public FNVFilter(long maxValue, int machineNum) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNum, HashUtil::fnvHash);
|
||||||
return HashUtil.fnvHash(str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,31 +1,16 @@
|
|||||||
package cn.hutool.bloomfilter.filter;
|
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;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public HfFilter(long maxValue, int machineNum) {
|
|
||||||
super(maxValue, machineNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HfFilter(long maxValue) {
|
public HfFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public HfFilter(long maxValue, int machineNum) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNum, HashUtil::hfHash);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,15 @@
|
|||||||
package cn.hutool.bloomfilter.filter;
|
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;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public HfIpFilter(long maxValue, int machineNum) {
|
|
||||||
super(maxValue, machineNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HfIpFilter(long maxValue) {
|
public HfIpFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public HfIpFilter(long maxValue, int machineNum) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNum, HashUtil::hfIpHash);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,15 @@
|
|||||||
package cn.hutool.bloomfilter.filter;
|
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;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public JSFilter(long maxValue, int machineNum) {
|
|
||||||
super(maxValue, machineNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSFilter(long maxValue) {
|
public JSFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public JSFilter(long maxValue, int machineNum) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNum, HashUtil::jsHash);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
|
|||||||
|
|
||||||
import cn.hutool.core.util.HashUtil;
|
import cn.hutool.core.util.HashUtil;
|
||||||
|
|
||||||
public class PJWFilter extends AbstractFilter {
|
public class PJWFilter extends FuncFilter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public PJWFilter(long maxValue, int machineNum) {
|
|
||||||
super(maxValue, machineNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PJWFilter(long maxValue) {
|
public PJWFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public PJWFilter(long maxValue, int machineNum) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNum, HashUtil::pjwHash);
|
||||||
return HashUtil.pjwHash(str) % size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
|
|||||||
|
|
||||||
import cn.hutool.core.util.HashUtil;
|
import cn.hutool.core.util.HashUtil;
|
||||||
|
|
||||||
public class RSFilter extends AbstractFilter {
|
public class RSFilter extends FuncFilter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public RSFilter(long maxValue, int machineNum) {
|
|
||||||
super(maxValue, machineNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RSFilter(long maxValue) {
|
public RSFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public RSFilter(long maxValue, int machineNum) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNum, HashUtil::rsHash);
|
||||||
return HashUtil.rsHash(str) % size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,14 @@ package cn.hutool.bloomfilter.filter;
|
|||||||
|
|
||||||
import cn.hutool.core.util.HashUtil;
|
import cn.hutool.core.util.HashUtil;
|
||||||
|
|
||||||
public class SDBMFilter extends AbstractFilter {
|
public class SDBMFilter extends FuncFilter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public SDBMFilter(long maxValue, int machineNum) {
|
|
||||||
super(maxValue, machineNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SDBMFilter(long maxValue) {
|
public SDBMFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public SDBMFilter(long maxValue, int machineNum) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNum, HashUtil::sdbmHash);
|
||||||
return HashUtil.sdbmHash(str) % size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,21 +2,14 @@ package cn.hutool.bloomfilter.filter;
|
|||||||
|
|
||||||
import cn.hutool.core.util.HashUtil;
|
import cn.hutool.core.util.HashUtil;
|
||||||
|
|
||||||
|
public class TianlFilter extends FuncFilter {
|
||||||
public class TianlFilter extends AbstractFilter {
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public TianlFilter(long maxValue, int machineNum) {
|
|
||||||
super(maxValue, machineNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TianlFilter(long maxValue) {
|
public TianlFilter(long maxValue) {
|
||||||
super(maxValue);
|
this(maxValue, DEFAULT_MACHINE_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public TianlFilter(long maxValue, int machineNum) {
|
||||||
public long hash(String str) {
|
super(maxValue, machineNum, HashUtil::tianlHash);
|
||||||
return HashUtil.tianlHash(str) % size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,7 @@ public class HashUtil {
|
|||||||
hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
|
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]:高位
|
* @return hash值,long[0]:低位,long[1]:高位
|
||||||
*/
|
*/
|
||||||
public static long[] metroHash128(byte[] data, long seed) {
|
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) {
|
public static long[] metroHash128(byte[] data) {
|
||||||
return MetroHash.hash128(data).getLongArray();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user