Commit 848bf99c authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Refactor logic of using matcher which was successful

parent 42b5fa55
...@@ -286,28 +286,37 @@ class RuleImportService : IntentService("RuleImportService") { ...@@ -286,28 +286,37 @@ class RuleImportService : IntentService("RuleImportService") {
var ruleCount = 0 var ruleCount = 0
val sourceId = source.id val sourceId = source.id
BufferedReader(InputStreamReader(stream)).useLines { lines -> BufferedReader(InputStreamReader(stream)).useLines { lines ->
var validParsings = 0 var remainingMatcher:Matcher? = null
var hostsOfRemainingMatcher:MutableList<DnsRule>? = null
lines.forEach { _line -> lines.forEach { _line ->
val line = _line.trim() val line = _line.trim()
if (!isAborted) { if (!isAborted) {
if (parsers.isNotEmpty() && !line.startsWith("#") && !line.startsWith("!") && !line.isBlank()) { if (parsers.isNotEmpty() && !line.startsWith("#") && !line.startsWith("!") && !line.isBlank()) {
lineCount++ lineCount++
val iterator = parsers.iterator() if(remainingMatcher != null) {
for ((matcher, hosts) in iterator) { if (remainingMatcher!!.reset(line).matches()) {
if (matcher.reset(line).matches()) { val rule = processLine(remainingMatcher!!, sourceId, source.whitelistSource)
validParsings++ if (rule != null) hostsOfRemainingMatcher!!.add(rule.apply {
val rule = processLine(matcher, sourceId, source.whitelistSource)
if (rule != null) hosts.second.add(rule.apply {
stagingType = 2 stagingType = 2
}) })
if (lineCount > ruleCommitSize) { if (lineCount > ruleCommitSize) {
ruleCount += commitLines(parsers) ruleCount += commitLines(parsers)
lineCount = 0 lineCount = 0
} }
} else { }
// If validParsings is at least 26 we know for sure that the current active parser had at least 5 successful hits } else {
// So we are going to keep it for the rest of the document, even if it fails val iterator = parsers.iterator()
if(validParsings <= 25) { for ((matcher, hosts) in iterator) {
if (matcher.reset(line).matches()) {
val rule = processLine(matcher, sourceId, source.whitelistSource)
if (rule != null) hosts.second.add(rule.apply {
stagingType = 2
})
if (lineCount > ruleCommitSize) {
ruleCount += commitLines(parsers)
lineCount = 0
}
} else {
if (hosts.first > 5) { if (hosts.first > 5) {
log("Matcher $matcher failed 5 times, last for '$line'. Removing.") log("Matcher $matcher failed 5 times, last for '$line'. Removing.")
iterator.remove() iterator.remove()
...@@ -315,6 +324,9 @@ class RuleImportService : IntentService("RuleImportService") { ...@@ -315,6 +324,9 @@ class RuleImportService : IntentService("RuleImportService") {
if (parsers.isEmpty()) { if (parsers.isEmpty()) {
log("No parsers left. Aborting.") log("No parsers left. Aborting.")
return@forEach return@forEach
} else if(parsers.size == 1) {
remainingMatcher = parsers.keys.first()
hostsOfRemainingMatcher = parsers.values.first().second
} }
} }
} }
......
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