mirror of
https://gitee.com/huoyo/ko-time.git
synced 2025-12-06 16:58:26 +08:00
add BoomFilter to reduce the burden of database
This commit is contained in:
parent
80eee8ba1a
commit
19feadfd3d
@ -4,9 +4,11 @@ import cn.langpy.kotime.annotation.KoListener;
|
|||||||
import cn.langpy.kotime.handler.InvokedHandler;
|
import cn.langpy.kotime.handler.InvokedHandler;
|
||||||
import cn.langpy.kotime.model.ExceptionNode;
|
import cn.langpy.kotime.model.ExceptionNode;
|
||||||
import cn.langpy.kotime.model.MethodNode;
|
import cn.langpy.kotime.model.MethodNode;
|
||||||
|
import cn.langpy.kotime.util.BoomFilter;
|
||||||
import cn.langpy.kotime.util.Common;
|
import cn.langpy.kotime.util.Common;
|
||||||
import cn.langpy.kotime.util.Context;
|
import cn.langpy.kotime.util.Context;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.lang.reflect.Parameter;
|
import java.lang.reflect.Parameter;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -14,12 +16,11 @@ import java.util.logging.Logger;
|
|||||||
public final class KoInvokedHandler implements InvokedHandler {
|
public final class KoInvokedHandler implements InvokedHandler {
|
||||||
private static Logger log = Logger.getLogger(KoInvokedHandler.class.toString());
|
private static Logger log = Logger.getLogger(KoInvokedHandler.class.toString());
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInvoked(MethodNode current, MethodNode parent, Parameter[] names, Object[] values) {
|
public void onInvoked(MethodNode current, MethodNode parent, Parameter[] names, Object[] values) {
|
||||||
GraphService graphService = GraphService.getInstance();
|
GraphService graphService = GraphService.getInstance();
|
||||||
graphService.addMethodNode(parent);
|
graphService.addMethodNode(filter(parent));
|
||||||
graphService.addMethodNode(current);
|
graphService.addMethodNode(filter(current));
|
||||||
graphService.addMethodRelation(parent, current);
|
graphService.addMethodRelation(parent, current);
|
||||||
if (Context.getConfig().getParamAnalyse()) {
|
if (Context.getConfig().getParamAnalyse()) {
|
||||||
graphService.addParamAnalyse(current.getId(), names, values, current.getValue());
|
graphService.addParamAnalyse(current.getId(), names, values, current.getValue());
|
||||||
@ -36,4 +37,13 @@ public final class KoInvokedHandler implements InvokedHandler {
|
|||||||
graphService.addExceptionNode(exception);
|
graphService.addExceptionNode(exception);
|
||||||
graphService.addExceptionRelation(current, exception);
|
graphService.addExceptionRelation(current, exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MethodNode filter(MethodNode currentNode) {
|
||||||
|
if (BoomFilter.exists(currentNode.getId())) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
BoomFilter.add(currentNode.getId());
|
||||||
|
return currentNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
51
src/main/java/cn/langpy/kotime/util/BoomFilter.java
Normal file
51
src/main/java/cn/langpy/kotime/util/BoomFilter.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package cn.langpy.kotime.util;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.BitSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class BoomFilter {
|
||||||
|
private static Logger log = Logger.getLogger(BoomFilter.class.toString());
|
||||||
|
|
||||||
|
private final static int size = 1000000;
|
||||||
|
private final static BitSet bits = new BitSet(size);
|
||||||
|
private final static int[] hashSeeds = new int[]{3, 113, 919, 2203, 4013, 6011};
|
||||||
|
|
||||||
|
private final static List<BiFunction<String, Integer, Integer>> hashFuncs = new ArrayList<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
for (int i = 0; i < hashSeeds.length; i++) {
|
||||||
|
hashFuncs.add((v, s) -> hash(v, s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void add(String value) {
|
||||||
|
for (int i = 0; i < hashFuncs.size(); i++) {
|
||||||
|
BiFunction<String, Integer, Integer> hashFunc = hashFuncs.get(i);
|
||||||
|
int hashSeed = hashSeeds[i];
|
||||||
|
Integer bitHash = hashFunc.apply(value, hashSeed);
|
||||||
|
bits.set(bitHash, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean exists(String value) {
|
||||||
|
for (int i = 0; i < hashFuncs.size(); i++) {
|
||||||
|
BiFunction<String, Integer, Integer> hashFunc = hashFuncs.get(i);
|
||||||
|
int hashSeed = hashSeeds[i];
|
||||||
|
Integer bitHash = hashFunc.apply(value, hashSeed);
|
||||||
|
boolean isTrue = bits.get(bitHash);
|
||||||
|
if (isTrue == false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int hash(String value, int seed) {
|
||||||
|
return Math.abs(value.hashCode() * seed + 33) % size;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user