Убраны пересекающиеся матчи
This commit is contained in:
@@ -1,24 +1,19 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.security.crypto.codec.Hex;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.serega6531.packmate.model.FoundPattern;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
||||
import ru.serega6531.packmate.model.enums.PatternSearchType;
|
||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
|
||||
import ru.serega6531.packmate.repository.PatternRepository;
|
||||
import ru.serega6531.packmate.utils.Bytes;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -30,7 +25,6 @@ public class PatternService {
|
||||
private final StreamSubscriptionService subscriptionService;
|
||||
|
||||
private final Map<Integer, Pattern> patterns = new HashMap<>();
|
||||
private final Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
public PatternService(PatternRepository repository,
|
||||
@@ -53,75 +47,11 @@ public class PatternService {
|
||||
}
|
||||
|
||||
public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) {
|
||||
return patterns.values().stream()
|
||||
final List<Pattern> list = patterns.values().stream()
|
||||
.filter(p -> p.getDirectionType() == (incoming ? PatternDirectionType.INPUT : PatternDirectionType.OUTPUT)
|
||||
|| p.getDirectionType() == PatternDirectionType.BOTH)
|
||||
.map(pattern -> match(pattern, bytes))
|
||||
.flatMap(List::stream)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private List<FoundPattern> match(Pattern pattern, byte[] bytes) {
|
||||
List<FoundPattern> found = new ArrayList<>();
|
||||
|
||||
if (pattern.getSearchType() == PatternSearchType.REGEX) {
|
||||
String content = new String(bytes);
|
||||
final java.util.regex.Pattern regex = compilePattern(pattern);
|
||||
final Matcher matcher = regex.matcher(content);
|
||||
int startPos = 0;
|
||||
|
||||
while (matcher.find(startPos)) {
|
||||
found.add(FoundPattern.builder()
|
||||
.patternId(pattern.getId())
|
||||
.startPosition(matcher.start())
|
||||
.endPosition(matcher.end() - 1)
|
||||
.build());
|
||||
startPos = matcher.end();
|
||||
}
|
||||
|
||||
return found;
|
||||
} else if (pattern.getSearchType() == PatternSearchType.SUBSTRING) {
|
||||
String content = new String(bytes);
|
||||
int startSearch = 0;
|
||||
final String value = pattern.getValue();
|
||||
|
||||
while (true) {
|
||||
int start = StringUtils.indexOfIgnoreCase(content, value, startSearch);
|
||||
|
||||
if (start == -1) {
|
||||
return found;
|
||||
}
|
||||
|
||||
int end = start + value.length() - 1;
|
||||
found.add(FoundPattern.builder()
|
||||
.patternId(pattern.getId())
|
||||
.startPosition(start)
|
||||
.endPosition(end)
|
||||
.build());
|
||||
|
||||
startSearch = end + 1;
|
||||
}
|
||||
} else { // SUBBYTES
|
||||
int startSearch = 0;
|
||||
final byte[] value = Hex.decode(pattern.getValue());
|
||||
|
||||
while (true) {
|
||||
int start = Bytes.indexOf(bytes, value, startSearch, bytes.length);
|
||||
|
||||
if (start == -1) {
|
||||
return found;
|
||||
}
|
||||
|
||||
int end = start + value.length - 1;
|
||||
found.add(FoundPattern.builder()
|
||||
.patternId(pattern.getId())
|
||||
.startPosition(start)
|
||||
.endPosition(end)
|
||||
.build());
|
||||
|
||||
startSearch = end + 1;
|
||||
}
|
||||
}
|
||||
.collect(Collectors.toList());
|
||||
return new PatternMatcher(bytes, list).findMatches();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -138,7 +68,6 @@ public class PatternService {
|
||||
|
||||
pattern.getMatchedStreams().clear();
|
||||
patterns.remove(id);
|
||||
compiledPatterns.remove(pattern.getValue());
|
||||
repository.delete(pattern);
|
||||
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.DELETE_PATTERN, id));
|
||||
}
|
||||
@@ -152,8 +81,4 @@ public class PatternService {
|
||||
return saved;
|
||||
}
|
||||
|
||||
private java.util.regex.Pattern compilePattern(Pattern pattern) {
|
||||
return compiledPatterns.computeIfAbsent(pattern.getValue(), java.util.regex.Pattern::compile);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user