I have a table that contains information about users, which I deny loading a specific web page.
Each record contains 4 columns: referrer, ip, userAgent & email.
Each column can contain a wild card (i.e. an asterisk *
), which make me block a group.
How could performance be improved with this code?
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.RAMDirectory;
import java.io.IOException;
class BlacklistLucene {
private static final String MATCH_ALL = "*";
private static IndexSearcher cache;
private enum Fields {
referrer, ip, userAgent, email
}
private static Document getDocument(String referrer, String ip, String userAgent, String email) {
Document doc = new Document();
doc.add(getStringField(referrer, Fields.referrer.name()));
doc.add(getStringField(ip, Fields.ip.name()));
doc.add(getStringField(userAgent, Fields.userAgent.name()));
doc.add(getStringField(email, Fields.email.name()));
return doc;
}
private static StringField getStringField(String val, String field) {
return new StringField(field, val, Field.Store.NO);
}
private static BooleanQuery createQuery(String referrer, String ip, String userAgent, String email) {
return new BooleanQuery.Builder()
.add(createBooleanQuery(Fields.referrer.name(), referrer), BooleanClause.Occur.FILTER)
.add(createBooleanQuery(Fields.ip.name(), ip), BooleanClause.Occur.FILTER)
.add(createBooleanQuery(Fields.userAgent.name(), userAgent), BooleanClause.Occur.FILTER)
.add(createBooleanQuery(Fields.email.name(), email), BooleanClause.Occur.FILTER)
.build();
}
private static BooleanQuery createBooleanQuery(String key, String value) {
return new BooleanQuery.Builder()
.add(new TermQuery(new Term(key, value)), BooleanClause.Occur.SHOULD)
.add(new TermQuery(new Term(key, MATCH_ALL)), BooleanClause.Occur.SHOULD)
.build();
}
private static boolean isBlacklisted(String ref, String ip, String ue, String email) throws IOException {
BooleanQuery query = createQuery(ref, ip, ue, email);
return cache.search(query, 1).totalHits > 0;
}
public static void main(String[] args) throws Exception {
RAMDirectory directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()));
writer.addDocument(getDocument("ref1", "127.0.0.ip1", "Mozilla UserAgent1", "email.com"));
writer.addDocument(getDocument("ref2", "127.0.0.ip2", "Mozilla UserAgent2", "*"));
writer.close();
DirectoryReader reader = DirectoryReader.open(directory);
cache = new IndexSearcher(reader);
System.out.println(isBlacklisted("ref1", "127.0.0.ip1", "Mozilla UserAgent1", "email.com")); // true
System.out.println(isBlacklisted("r2.com", "127.0.0.4", "Mozilla", "yahoo.com")); // false
System.out.println(isBlacklisted("ref2", "127.0.0.ip2", "Mozilla UserAgent2", "this is ignored")); // true
System.out.println(isBlacklisted("*", "127.0.0.ip2", "Mozilla UserAgent2", "*")); // true
}
}