Изменено кэширование
This commit is contained in:
@@ -11,10 +11,7 @@ import ru.serega6531.packmate.model.PatternType;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.repository.PatternRepository;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -24,28 +21,33 @@ public class PatternService {
|
||||
private final PatternRepository repository;
|
||||
private final StreamService streamService;
|
||||
|
||||
private Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
|
||||
private final Map<Integer, Pattern> patterns = new HashMap<>();
|
||||
private final Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
public PatternService(PatternRepository repository, @Lazy StreamService streamService) {
|
||||
this.repository = repository;
|
||||
this.streamService = streamService;
|
||||
repository.findAll().forEach(p -> patterns.put(p.getId(), p));
|
||||
log.info("Loaded {} patterns", patterns.size());
|
||||
}
|
||||
|
||||
public List<Pattern> findAll() {
|
||||
return repository.findAll();
|
||||
public Collection<Pattern> findAll() {
|
||||
return patterns.values();
|
||||
}
|
||||
|
||||
public List<Pattern> findMatching(byte[] bytes, boolean incoming) {
|
||||
String content = new String(bytes);
|
||||
|
||||
return repository.findAllByTypeEqualsOrTypeEquals(incoming ? PatternType.INPUT : PatternType.OUTPUT, PatternType.BOTH).stream()
|
||||
return patterns.values().stream()
|
||||
.filter(p -> p.getType() == (incoming ? PatternType.INPUT : PatternType.OUTPUT)
|
||||
|| p.getType() == PatternType.BOTH)
|
||||
.filter(pattern -> matches(pattern, content))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private boolean matches(Pattern pattern, String content) {
|
||||
if(pattern.isRegex()) {
|
||||
if (pattern.isRegex()) {
|
||||
final java.util.regex.Pattern regex = compilePattern(pattern);
|
||||
return regex.matcher(content).find();
|
||||
} else {
|
||||
@@ -56,7 +58,7 @@ public class PatternService {
|
||||
@Transactional
|
||||
public void deleteById(int id) {
|
||||
final Optional<Pattern> optional = repository.findById(id);
|
||||
if(optional.isPresent()) {
|
||||
if (optional.isPresent()) {
|
||||
final Pattern pattern = optional.get();
|
||||
log.info("Удален паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||
|
||||
@@ -66,12 +68,15 @@ public class PatternService {
|
||||
}
|
||||
|
||||
pattern.getMatchedStreams().clear();
|
||||
patterns.remove(pattern.getId());
|
||||
compiledPatterns.remove(pattern.getValue());
|
||||
repository.delete(pattern);
|
||||
}
|
||||
}
|
||||
|
||||
public Pattern save(Pattern pattern) {
|
||||
log.info("Добавлен новый паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||
patterns.put(pattern.getId(), pattern);
|
||||
return repository.save(pattern);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user