1 /*
2 * This file is part of dependency-check-core.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Copyright (c) 2020 Jeremy Long. All Rights Reserved.
17 */
18 package org.owasp.dependencycheck.data.cpe;
19
20 import java.io.IOException;
21 import java.util.Set;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.index.CorruptIndexException;
24 import org.apache.lucene.queryparser.classic.ParseException;
25 import org.apache.lucene.search.Query;
26 import org.apache.lucene.search.TopDocs;
27 import org.owasp.dependencycheck.utils.Pair;
28 import org.owasp.dependencycheck.utils.Settings;
29
30 /**
31 *
32 * @author Jeremy Long
33 */
34 public interface MemoryIndex extends AutoCloseable {
35
36 /**
37 * Retrieves a document from the Index.
38 *
39 * @param documentId the id of the document to retrieve
40 * @return the Document
41 * @throws IOException thrown if there is an IOException
42 */
43 Document getDocument(int documentId) throws IOException;
44
45 /**
46 * Creates and loads data into an in memory index.
47 *
48 * @param data the CPE data
49 * @param settings a reference to the dependency-check settings
50 * @throws IndexException thrown if there is an error creating the index
51 */
52 void open(Set<Pair<String, String>> data, Settings settings) throws IndexException;
53
54 /**
55 * Parses the given string into a Lucene Query.
56 *
57 * @param searchString the search text
58 * @return the Query object
59 * @throws ParseException thrown if the search text cannot be parsed
60 * @throws IndexException thrown if there is an error resetting the
61 * analyzers
62 */
63 Query parseQuery(String searchString) throws ParseException, IndexException;
64
65 /**
66 * Searches the index using the given search string.
67 *
68 * @param searchString the query text
69 * @param maxQueryResults the maximum number of documents to return
70 * @return the TopDocs found by the search
71 * @throws ParseException thrown when the searchString is invalid
72 * @throws IndexException thrown when there is an internal error resetting
73 * the search analyzer
74 * @throws IOException is thrown if there is an issue with the underlying
75 * Index
76 */
77 TopDocs search(String searchString, int maxQueryResults) throws ParseException, IndexException, IOException;
78
79 /**
80 * Searches the index using the given query.
81 *
82 * @param query the query used to search the index
83 * @param maxQueryResults the max number of results to return
84 * @return the TopDocs found be the query
85 * @throws CorruptIndexException thrown if the Index is corrupt
86 * @throws IOException thrown if there is an IOException
87 */
88 TopDocs search(Query query, int maxQueryResults) throws CorruptIndexException, IOException;
89
90 /**
91 * Closes the MemoryIndex.
92 */
93 @Override
94 void close();
95 }