add BoomFilter to reduce the burden of database

This commit is contained in:
huoyo 2022-06-06 18:22:26 +08:00
parent 80eee8ba1a
commit 19feadfd3d
2 changed files with 64 additions and 3 deletions

View File

@ -4,9 +4,11 @@ import cn.langpy.kotime.annotation.KoListener;
import cn.langpy.kotime.handler.InvokedHandler;
import cn.langpy.kotime.model.ExceptionNode;
import cn.langpy.kotime.model.MethodNode;
import cn.langpy.kotime.util.BoomFilter;
import cn.langpy.kotime.util.Common;
import cn.langpy.kotime.util.Context;
import javax.annotation.Resource;
import java.lang.reflect.Parameter;
import java.util.logging.Logger;
@ -14,12 +16,11 @@ import java.util.logging.Logger;
public final class KoInvokedHandler implements InvokedHandler {
private static Logger log = Logger.getLogger(KoInvokedHandler.class.toString());
@Override
public void onInvoked(MethodNode current, MethodNode parent, Parameter[] names, Object[] values) {
GraphService graphService = GraphService.getInstance();
graphService.addMethodNode(parent);
graphService.addMethodNode(current);
graphService.addMethodNode(filter(parent));
graphService.addMethodNode(filter(current));
graphService.addMethodRelation(parent, current);
if (Context.getConfig().getParamAnalyse()) {
graphService.addParamAnalyse(current.getId(), names, values, current.getValue());
@ -36,4 +37,13 @@ public final class KoInvokedHandler implements InvokedHandler {
graphService.addExceptionNode(exception);
graphService.addExceptionRelation(current, exception);
}
public MethodNode filter(MethodNode currentNode) {
if (BoomFilter.exists(currentNode.getId())) {
return null;
} else {
BoomFilter.add(currentNode.getId());
return currentNode;
}
}
}

View 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;
}
}