This commit is contained in:
Jeremy Long
2017-08-20 11:01:10 -04:00
parent a015cf4210
commit 631c10f8b6

View File

@@ -47,7 +47,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* An in memory lucene index that contains the vendor/product combinations from * An in memory Lucene index that contains the vendor/product combinations from
* the CPE (application) identifiers within the NVD CVE data. * the CPE (application) identifiers within the NVD CVE data.
* *
* @author Jeremy Long * @author Jeremy Long
@@ -91,9 +91,10 @@ public final class CpeMemoryIndex implements AutoCloseable {
*/ */
private SearchFieldAnalyzer vendorFieldAnalyzer; private SearchFieldAnalyzer vendorFieldAnalyzer;
/** /**
* A flag indicating whether or not the index is open. * Track the number of current users of the Lucene index; used to track it
* it is okay to actually close the index.
*/ */
private boolean openState = false; private int usageCount = 0;
/** /**
* private constructor for singleton. * private constructor for singleton.
@@ -117,7 +118,8 @@ public final class CpeMemoryIndex implements AutoCloseable {
* @throws IndexException thrown if there is an error creating the index * @throws IndexException thrown if there is an error creating the index
*/ */
public synchronized void open(CveDB cve) throws IndexException { public synchronized void open(CveDB cve) throws IndexException {
if (!openState) { if (INSTANCE.usageCount <= 0) {
INSTANCE.usageCount = 0;
index = new RAMDirectory(); index = new RAMDirectory();
buildIndex(cve); buildIndex(cve);
try { try {
@@ -128,8 +130,8 @@ public final class CpeMemoryIndex implements AutoCloseable {
indexSearcher = new IndexSearcher(indexReader); indexSearcher = new IndexSearcher(indexReader);
searchingAnalyzer = createSearchingAnalyzer(); searchingAnalyzer = createSearchingAnalyzer();
queryParser = new QueryParser(LuceneUtils.CURRENT_VERSION, Fields.DOCUMENT_KEY, searchingAnalyzer); queryParser = new QueryParser(LuceneUtils.CURRENT_VERSION, Fields.DOCUMENT_KEY, searchingAnalyzer);
openState = true;
} }
INSTANCE.usageCount += 1;
} }
/** /**
@@ -138,7 +140,7 @@ public final class CpeMemoryIndex implements AutoCloseable {
* @return whether or not the index is open * @return whether or not the index is open
*/ */
public synchronized boolean isOpen() { public synchronized boolean isOpen() {
return openState; return INSTANCE.usageCount > 0;
} }
/** /**
@@ -162,6 +164,8 @@ public final class CpeMemoryIndex implements AutoCloseable {
*/ */
@Override @Override
public synchronized void close() { public synchronized void close() {
INSTANCE.usageCount -= 1;
if (INSTANCE.usageCount <= 0) {
if (searchingAnalyzer != null) { if (searchingAnalyzer != null) {
searchingAnalyzer.close(); searchingAnalyzer.close();
searchingAnalyzer = null; searchingAnalyzer = null;
@@ -180,7 +184,7 @@ public final class CpeMemoryIndex implements AutoCloseable {
index.close(); index.close();
index = null; index = null;
} }
openState = false; }
} }
/** /**