Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional Regular Expression cache to MatchesOperator #5837

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand All @@ -20,13 +20,67 @@

import org.drools.model.functions.Operator;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public enum MatchesOperator implements Operator.SingleValue<String, String> {

INSTANCE;

// not final due to unit tests
private int MAX_SIZE_CACHE = getMaxSizeCache();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this field and also the Map be static?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we have a singleton only usage since it is an enum. Does that make all class variables in a sense static? Want me to change it? I did not benchmark it either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed them to static and pushed


// store Pattern for regular expressions using the regular expression as the key up to MAX_SIZE_CACHE entries.
private final Map<String, Pattern> patternMap = Collections.synchronizedMap(new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Pattern> eldest) {
return size() > (MAX_SIZE_CACHE);
}
});

// 0 default disables the Pattern map
private static int getMaxSizeCache() {
final String CACHE_MATCHES_COMPILED_MAX_PROPERTY = "drools.matches.compiled.cache.count";
return Integer.parseInt(System.getProperty(CACHE_MATCHES_COMPILED_MAX_PROPERTY, "0"));
}

// package-private for unit testing
void forceCacheSize(int size) {
MAX_SIZE_CACHE = size;
patternMap.clear();
}

// package-private for unit testing
void reInitialize() {
forceCacheSize(getMaxSizeCache());
}

// package-private for unit testing
int mapSize() {
return patternMap.size();
}

@Override
public boolean eval( String s1, String s2 ) {
return s1 != null && s1.matches( s2 );
public boolean eval(String input, String regex) {
if (input == null) {
return false;
} else if (MAX_SIZE_CACHE == 0) {
return input.matches(regex);
} else {
Pattern pattern = patternMap.get(regex);
if (pattern == null) {
// Cache miss on regex, compile it, store it.
// Storing in patternMap may remove the oldest entry per MAX_SIZE_CACHE.
pattern = Pattern.compile(regex);
patternMap.put(regex, pattern);
}
Matcher matcher = pattern.matcher(input);
return matcher.matches();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.drools.model.operators;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Apache License header just like other java sources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - will change


import org.junit.After;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;


public class MatchesOperatorTest {


@Test
public void testMatchesOperatorCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.forceCacheSize(100);

// input maybe null
assertThat(instance.eval(null,"anything")).isFalse();
assertThat(instance.mapSize()).isEqualTo(0); // not added to cache with null input
// cache enabled
assertThat(instance.eval("a","a")).isTrue();
assertThat(instance.mapSize()).isEqualTo(1);
assertThat(instance.eval("a","b")).isFalse();
assertThat(instance.mapSize()).isEqualTo(2);
assertThat(instance.eval("a","a")).isTrue(); // regular expression "a" in map.
assertThat(instance.eval("b","b")).isTrue(); // regular expression "b" in map.
assertThat(instance.eval("c","a")).isFalse(); // regular expression "a" in map.
assertThat(instance.eval("c","b")).isFalse(); // regular expression "b" in map.
assertThat(instance.mapSize()).isEqualTo(2);
}

@Test
public void testMatchesOperatorNoCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.forceCacheSize(0);
// input maybe null
assertThat(instance.eval(null,"anything")).isFalse();
assertThat(instance.eval("a","a")).isTrue();
assertThat(instance.eval("a","b")).isFalse();
assertThat(instance.eval("b","a")).isFalse();
assertThat(instance.eval("b","b")).isTrue();
assertThat(instance.mapSize()).isEqualTo(0);
}

@After
public void resetCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.reInitialize();
}

}