Commit 45cda665 authored by ma yanling's avatar ma yanling
Browse files

project commit

parent ad2fb30a
Pipeline #2354 failed with stages
in 0 seconds
package cn.hutool.bloomfilter.filter;
import cn.hutool.bloomfilter.BloomFilter;
import cn.hutool.bloomfilter.bitMap.BitMap;
import cn.hutool.bloomfilter.bitMap.IntMap;
import cn.hutool.bloomfilter.bitMap.LongMap;
/**
* 抽象Bloom过滤器
*
* @author loolly
*
*/
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;
/**
* 构造
*
* @param maxValue 最大值
* @param machineNum 机器位数
*/
public AbstractFilter(long maxValue, int machineNum) {
init(maxValue, machineNum);
}
/**
* 构造32位
*
* @param maxValue 最大值
*/
public AbstractFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
/**
* 初始化
*
* @param maxValue 最大值
* @param machineNum 机器位数
*/
public void init(long maxValue, int machineNum) {
this.size = maxValue;
switch (machineNum) {
case BitMap.MACHINE32:
bm = new IntMap((int) (size / machineNum));
break;
case BitMap.MACHINE64:
bm = new LongMap((int) (size / machineNum));
break;
default:
throw new RuntimeException("Error Machine number!");
}
}
@Override
public boolean contains(String str) {
return bm.contains(Math.abs(hash(str)));
}
@Override
public boolean add(String str) {
final long hash = Math.abs(hash(str));
if (bm.contains(hash)) {
return false;
}
bm.add(hash);
return true;
}
/**
* 自定义Hash方法
*
* @param str 字符串
* @return HashCode
*/
public abstract long hash(String str);
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
/**
* 默认Bloom过滤器,使用Java自带的Hash算法
*
* @author loolly
*/
public class DefaultFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public DefaultFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public DefaultFilter(long maxValue, int machineNumber) {
super(maxValue, machineNumber, HashUtil::javaDefaultHash);
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class ELFFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public ELFFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public ELFFilter(long maxValue, int machineNumber) {
super(maxValue, machineNumber, HashUtil::elfHash);
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class FNVFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public FNVFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public FNVFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::fnvHash);
}
}
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;
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class HfFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public HfFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public HfFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::hfHash);
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class HfIpFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public HfIpFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public HfIpFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::hfIpHash);
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class JSFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public JSFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public JSFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::jsHash);
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class PJWFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public PJWFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public PJWFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::pjwHash);
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class RSFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public RSFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public RSFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::rsHash);
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class SDBMFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public SDBMFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public SDBMFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::sdbmHash);
}
}
package cn.hutool.bloomfilter.filter;
import cn.hutool.core.util.HashUtil;
public class TianlFilter extends FuncFilter {
private static final long serialVersionUID = 1L;
public TianlFilter(long maxValue) {
this(maxValue, DEFAULT_MACHINE_NUM);
}
public TianlFilter(long maxValue, int machineNum) {
super(maxValue, machineNum, HashUtil::tianlHash);
}
}
/**
* 各种Hash算法的过滤器实现
*
* @author looly
*
*/
package cn.hutool.bloomfilter.filter;
\ No newline at end of file
/**
* 布隆过滤,提供一些Hash算法的布隆过滤
*
* @author looly
*
*/
package cn.hutool.bloomfilter;
\ No newline at end of file
package cn.hutool.bloomfilter;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.bloomfilter.bitMap.IntMap;
import cn.hutool.bloomfilter.bitMap.LongMap;
public class BitMapBloomFilterTest {
@Test
public void filterTest() {
BitMapBloomFilter filter = new BitMapBloomFilter(10);
filter.add("123");
filter.add("abc");
filter.add("ddd");
Assert.assertTrue(filter.contains("abc"));
Assert.assertTrue(filter.contains("ddd"));
Assert.assertTrue(filter.contains("123"));
}
@Test
@Ignore
public void testIntMap(){
IntMap intMap = new IntMap();
for (int i = 0 ; i < 32; i++) {
intMap.add(i);
}
intMap.remove(30);
for (int i = 0; i < 32; i++) {
System.out.println(i + "是否存在-->" + intMap.contains(i));
}
}
@Test
@Ignore
public void testLongMap(){
LongMap longMap = new LongMap();
for (int i = 0 ; i < 64; i++) {
longMap.add(i);
}
longMap.remove(30);
for (int i = 0; i < 64; i++) {
System.out.println(i + "是否存在-->" + longMap.contains(i));
}
}
}
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.8.19</version>
</parent>
<artifactId>hutool-bom</artifactId>
<name>${project.artifactId}</name>
<description>提供丰富的Java工具方法,此模块为Hutool所有模块汇总,最终形式为拆分开的多个jar包,可以通过exclude方式排除不需要的模块</description>
<url>https://github.com/looly/hutool</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-aop</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-bloomFilter</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-cache</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-db</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-dfa</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-extra</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-log</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-script</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-setting</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-system</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-cron</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-json</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-poi</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-socket</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-jwt</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-aop</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-bloomFilter</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-cache</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-db</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-dfa</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-extra</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-log</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-script</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-setting</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-system</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-cron</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-json</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-poi</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-socket</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-jwt</artifactId>
</dependency>
</dependencies>
</project>
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.8.19</version>
</parent>
<artifactId>hutool-cache</artifactId>
<name>${project.artifactId}</name>
<description>Hutool 缓存</description>
<properties>
<Automatic-Module-Name>cn.hutool.cache</Automatic-Module-Name>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>
package cn.hutool.cache;
import cn.hutool.cache.impl.CacheObj;
import cn.hutool.core.lang.func.Func0;
import java.io.Serializable;
import java.util.Iterator;
/**
* 缓存接口
*
* @param <K> 键类型
* @param <V> 值类型
* @author Looly, jodd
*/
public interface Cache<K, V> extends Iterable<V>, Serializable {
/**
* 返回缓存容量,{@code 0}表示无大小限制
*
* @return 返回缓存容量,{@code 0}表示无大小限制
*/
int capacity();
/**
* 缓存失效时长, {@code 0} 表示没有设置,单位毫秒
*
* @return 缓存失效时长, {@code 0} 表示没有设置,单位毫秒
*/
long timeout();
/**
* 将对象加入到缓存,使用默认失效时长
*
* @param key 键
* @param object 缓存的对象
* @see Cache#put(Object, Object, long)
*/
void put(K key, V object);
/**
* 将对象加入到缓存,使用指定失效时长<br>
* 如果缓存空间满了,{@link #prune()} 将被调用以获得空间来存放新对象
*
* @param key 键
* @param object 缓存的对象
* @param timeout 失效时长,单位毫秒
*/
void put(K key, V object, long timeout);
/**
* 从缓存中获得对象,当对象不在缓存中或已经过期返回{@code null}
* <p>
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回{@code null},否则返回值。
* <p>
* 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。
*
* @param key 键
* @return 键对应的对象
* @see #get(Object, boolean)
*/
default V get(K key) {
return get(key, true);
}
/**
* 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象
* <p>
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回{@code null},否则返回值。
* <p>
* 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。
*
* @param key 键
* @param supplier 如果不存在回调方法,用于生产值对象
* @return 值对象
*/
default V get(K key, Func0<V> supplier) {
return get(key, true, supplier);
}
/**
* 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象
* <p>
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回{@code null},否则返回值。
* <p>
* 每次调用此方法会可选是否刷新最后访问时间,{@code true}表示会重新计算超时时间。
*
* @param key 键
* @param isUpdateLastAccess 是否更新最后访问时间,即重新计算超时时间。
* @param supplier 如果不存在回调方法,用于生产值对象
* @return 值对象
*/
V get(K key, boolean isUpdateLastAccess, Func0<V> supplier);
/**
* 从缓存中获得对象,当对象不在缓存中或已经过期返回{@code null}
* <p>
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回{@code null},否则返回值。
* <p>
* 每次调用此方法会可选是否刷新最后访问时间,{@code true}表示会重新计算超时时间。
*
* @param key 键
* @param isUpdateLastAccess 是否更新最后访问时间,即重新计算超时时间。
* @return 键对应的对象
*/
V get(K key, boolean isUpdateLastAccess);
/**
* 返回包含键和值得迭代器
*
* @return 缓存对象迭代器
* @since 4.0.10
*/
Iterator<CacheObj<K, V>> cacheObjIterator();
/**
* 从缓存中清理过期对象,清理策略取决于具体实现
*
* @return 清理的缓存对象个数
*/
int prune();
/**
* 缓存是否已满,仅用于有空间限制的缓存对象
*
* @return 缓存是否已满,仅用于有空间限制的缓存对象
*/
boolean isFull();
/**
* 从缓存中移除对象
*
* @param key 键
*/
void remove(K key);
/**
* 清空缓存
*/
void clear();
/**
* 缓存的对象数量
*
* @return 缓存的对象数量
*/
int size();
/**
* 缓存是否为空
*
* @return 缓存是否为空
*/
boolean isEmpty();
/**
* 是否包含key
*
* @param key KEY
* @return 是否包含key
*/
boolean containsKey(K key);
/**
* 设置监听
*
* @param listener 监听
* @return this
* @since 5.5.2
*/
default Cache<K, V> setListener(CacheListener<K, V> listener){
return this;
}
}
package cn.hutool.cache;
/**
* 缓存监听,用于实现缓存操作时的回调监听,例如缓存对象的移除事件等
*
* @param <K> 缓存键
* @param <V> 缓存值
* @author looly
* @since 5.5.2
*/
public interface CacheListener<K, V> {
/**
* 对象移除回调
*
* @param key 键
* @param cachedObject 被缓存的对象
*/
void onRemove(K key, V cachedObject);
}
package cn.hutool.cache;
import cn.hutool.cache.impl.FIFOCache;
import cn.hutool.cache.impl.LFUCache;
import cn.hutool.cache.impl.LRUCache;
import cn.hutool.cache.impl.NoCache;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.cache.impl.WeakCache;
/**
* 缓存工具类
* @author Looly
*@since 3.0.1
*/
public class CacheUtil {
/**
* 创建FIFO(first in first out) 先进先出缓存.
*
* @param <K> Key类型
* @param <V> Value类型
* @param capacity 容量
* @param timeout 过期时长,单位:毫秒
* @return {@link FIFOCache}
*/
public static <K, V> FIFOCache<K, V> newFIFOCache(int capacity, long timeout){
return new FIFOCache<>(capacity, timeout);
}
/**
* 创建FIFO(first in first out) 先进先出缓存.
*
* @param <K> Key类型
* @param <V> Value类型
* @param capacity 容量
* @return {@link FIFOCache}
*/
public static <K, V> FIFOCache<K, V> newFIFOCache(int capacity){
return new FIFOCache<>(capacity);
}
/**
* 创建LFU(least frequently used) 最少使用率缓存.
*
* @param <K> Key类型
* @param <V> Value类型
* @param capacity 容量
* @param timeout 过期时长,单位:毫秒
* @return {@link LFUCache}
*/
public static <K, V> LFUCache<K, V> newLFUCache(int capacity, long timeout){
return new LFUCache<>(capacity, timeout);
}
/**
* 创建LFU(least frequently used) 最少使用率缓存.
*
* @param <K> Key类型
* @param <V> Value类型
* @param capacity 容量
* @return {@link LFUCache}
*/
public static <K, V> LFUCache<K, V> newLFUCache(int capacity){
return new LFUCache<>(capacity);
}
/**
* 创建LRU (least recently used)最近最久未使用缓存.
*
* @param <K> Key类型
* @param <V> Value类型
* @param capacity 容量
* @param timeout 过期时长,单位:毫秒
* @return {@link LRUCache}
*/
public static <K, V> LRUCache<K, V> newLRUCache(int capacity, long timeout){
return new LRUCache<>(capacity, timeout);
}
/**
* 创建LRU (least recently used)最近最久未使用缓存.
*
* @param <K> Key类型
* @param <V> Value类型
* @param capacity 容量
* @return {@link LRUCache}
*/
public static <K, V> LRUCache<K, V> newLRUCache(int capacity){
return new LRUCache<>(capacity);
}
/**
* 创建定时缓存.
*
* @param <K> Key类型
* @param <V> Value类型
* @param timeout 过期时长,单位:毫秒
* @return {@link TimedCache}
*/
public static <K, V> TimedCache<K, V> newTimedCache(long timeout){
return new TimedCache<>(timeout);
}
/**
* 创建弱引用缓存.
*
* @param <K> Key类型
* @param <V> Value类型
* @param timeout 过期时长,单位:毫秒
* @return {@link WeakCache}
* @since 3.0.7
*/
public static <K, V> WeakCache<K, V> newWeakCache(long timeout){
return new WeakCache<>(timeout);
}
/**
* 创建无缓存实现.
*
* @param <K> Key类型
* @param <V> Value类型
* @return {@link NoCache}
*/
public static <K, V> NoCache<K, V> newNoCache(){
return new NoCache<>();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment