| 1 | |
|
| 2 | |
|
| 3 | |
package org.galagosearch.core.index; |
| 4 | |
|
| 5 | |
import org.galagosearch.core.retrieval.structured.*; |
| 6 | |
import org.galagosearch.core.retrieval.query.Node; |
| 7 | |
import org.galagosearch.core.retrieval.query.NodeType; |
| 8 | |
import org.galagosearch.tupleflow.Parameters; |
| 9 | |
import java.io.File; |
| 10 | |
import java.io.IOException; |
| 11 | |
import java.lang.reflect.Constructor; |
| 12 | |
import java.util.HashMap; |
| 13 | |
import java.util.HashSet; |
| 14 | |
import java.util.Map; |
| 15 | |
import java.util.Map.Entry; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
public class StructuredIndex { |
| 22 | |
DocumentLengthsReader documentLengths; |
| 23 | |
DocumentNameReader documentNames; |
| 24 | |
Map<String, StructuredIndexPartReader> parts; |
| 25 | |
Parameters manifest; |
| 26 | |
|
| 27 | 12 | HashMap<String, String> defaultIndexOperators = new HashMap<String, String>(); |
| 28 | 12 | HashSet<String> knownIndexOperators = new HashSet<String>(); |
| 29 | |
|
| 30 | 12 | public StructuredIndex(String filename) throws IOException { |
| 31 | 12 | manifest = new Parameters(); |
| 32 | 12 | manifest.parse(filename + File.separator + "manifest"); |
| 33 | 12 | documentLengths = new DocumentLengthsReader(filename + File.separator + "documentLengths"); |
| 34 | 12 | documentNames = new DocumentNameReader(filename + File.separator + "documentNames"); |
| 35 | |
|
| 36 | 12 | File partsDirectory = new File(filename + File.separator + "parts"); |
| 37 | 12 | parts = new HashMap<String, StructuredIndexPartReader>(); |
| 38 | 36 | for (File part : partsDirectory.listFiles()) { |
| 39 | 24 | StructuredIndexPartReader reader = openIndexPart(part.getAbsolutePath()); |
| 40 | 24 | if (reader == null) { |
| 41 | 0 | continue; |
| 42 | |
} |
| 43 | 24 | parts.put(part.getName(), reader); |
| 44 | |
} |
| 45 | |
|
| 46 | 12 | initializeIndexOperators(); |
| 47 | 12 | } |
| 48 | |
|
| 49 | |
public static StructuredIndexPartReader openIndexPart(String path) throws IOException { |
| 50 | 24 | if (!IndexReader.isIndexFile(path)) { |
| 51 | 0 | return null; |
| 52 | |
} |
| 53 | 24 | IndexReader reader = new IndexReader(path); |
| 54 | 24 | if (!reader.getManifest().containsKey("readerClass")) { |
| 55 | 0 | throw new IOException("Tried to open an index part at " + path + ", but the " + |
| 56 | |
"file has no readerClass specified in its manifest. " + |
| 57 | |
"(the readerClass is the class that knows how to decode the " + |
| 58 | |
"contents of the file)"); |
| 59 | |
} |
| 60 | |
|
| 61 | 24 | String className = reader.getManifest().get("readerClass", (String) null); |
| 62 | |
Class readerClass; |
| 63 | |
try { |
| 64 | 24 | readerClass = Class.forName(className); |
| 65 | 0 | } catch (ClassNotFoundException e) { |
| 66 | 0 | throw new IOException("Class " + className + ", which was specified as the readerClass " + |
| 67 | |
"in " + path + ", could not be found."); |
| 68 | 24 | } |
| 69 | |
|
| 70 | 24 | if (!StructuredIndexPartReader.class.isAssignableFrom(readerClass)) { |
| 71 | 0 | throw new IOException(className + " is not a StructuredIndexPartReader subclass."); |
| 72 | |
} |
| 73 | |
|
| 74 | |
Constructor c; |
| 75 | |
try { |
| 76 | 24 | c = readerClass.getConstructor(IndexReader.class); |
| 77 | 0 | } catch (NoSuchMethodException ex) { |
| 78 | 0 | throw new IOException(className + " has no constructor that takes a single " + |
| 79 | |
"IndexReader argument."); |
| 80 | 0 | } catch (SecurityException ex) { |
| 81 | 0 | throw new IOException(className + " doesn't have a suitable constructor that " + |
| 82 | |
"this code has access to (SecurityException)"); |
| 83 | 24 | } |
| 84 | |
|
| 85 | |
StructuredIndexPartReader partReader; |
| 86 | |
try { |
| 87 | 24 | partReader = (StructuredIndexPartReader) c.newInstance(reader); |
| 88 | 0 | } catch (Exception ex) { |
| 89 | 0 | IOException e = new IOException("Caught an exception while instantiating " + |
| 90 | |
"a StructuredIndexPartReader: "); |
| 91 | 0 | e.initCause(ex); |
| 92 | 0 | throw e; |
| 93 | 24 | } |
| 94 | 24 | return partReader; |
| 95 | |
} |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public boolean containsPart(String partName) { |
| 104 | 0 | return parts.containsKey(partName); |
| 105 | |
} |
| 106 | |
|
| 107 | |
void initializeIndexOperators() { |
| 108 | 12 | for (Entry<String, StructuredIndexPartReader> entry : parts.entrySet()) { |
| 109 | 24 | String partName = entry.getKey(); |
| 110 | 24 | StructuredIndexPartReader part = entry.getValue(); |
| 111 | |
|
| 112 | 24 | for (String name : part.getNodeTypes().keySet()) { |
| 113 | 36 | knownIndexOperators.add(name); |
| 114 | |
|
| 115 | 36 | if (!defaultIndexOperators.containsKey(name)) { |
| 116 | 24 | defaultIndexOperators.put(name, partName); |
| 117 | 12 | } else if (name.startsWith("default")) { |
| 118 | 0 | if (defaultIndexOperators.get(name).startsWith("default")) { |
| 119 | 0 | defaultIndexOperators.remove(name); |
| 120 | |
} else { |
| 121 | 0 | defaultIndexOperators.put(name, partName); |
| 122 | |
} |
| 123 | |
} else { |
| 124 | 12 | defaultIndexOperators.remove(name); |
| 125 | |
} |
| 126 | |
} |
| 127 | 24 | } |
| 128 | 12 | } |
| 129 | |
|
| 130 | |
private StructuredIndexPartReader getIndexPart(Node node) throws IOException { |
| 131 | 20 | String operator = node.getOperator(); |
| 132 | 20 | StructuredIndexPartReader part = null; |
| 133 | |
|
| 134 | 20 | if (node.getParameters().containsKey("part")) { |
| 135 | 0 | String partName = node.getParameters().get("part"); |
| 136 | 0 | if (!parts.containsKey(partName)) { |
| 137 | 0 | throw new IOException("The index has no part named '" + partName + "'"); |
| 138 | |
} |
| 139 | 0 | part = parts.get(partName); |
| 140 | 0 | } else if (knownIndexOperators.contains(operator)) { |
| 141 | 8 | if (!defaultIndexOperators.containsKey(operator)) { |
| 142 | 0 | throw new IOException("More than one index part supplies the operator '" + |
| 143 | |
operator + "', but no part name was specified."); |
| 144 | |
} else { |
| 145 | 8 | String partName = defaultIndexOperators.get(operator); |
| 146 | 8 | part = parts.get(partName); |
| 147 | |
} |
| 148 | |
} |
| 149 | 20 | return part; |
| 150 | |
} |
| 151 | |
|
| 152 | |
public StructuredIterator getIterator(Node node) throws IOException { |
| 153 | 20 | StructuredIterator result = null; |
| 154 | 20 | StructuredIndexPartReader part = getIndexPart(node); |
| 155 | 20 | if (part != null) { |
| 156 | 8 | result = part.getIterator(node); |
| 157 | 8 | if (result == null) { |
| 158 | 0 | result = new NullExtentIterator(); |
| 159 | |
} |
| 160 | |
} |
| 161 | 20 | return result; |
| 162 | |
} |
| 163 | |
|
| 164 | |
public NodeType getNodeType(Node node) throws IOException { |
| 165 | 0 | NodeType result = null; |
| 166 | 0 | StructuredIndexPartReader part = getIndexPart(node); |
| 167 | 0 | if (part != null) { |
| 168 | 0 | final String operator = node.getOperator(); |
| 169 | 0 | final Map<String, NodeType> nodeTypes = part.getNodeTypes(); |
| 170 | 0 | result = nodeTypes.get(operator); |
| 171 | |
} |
| 172 | 0 | return result; |
| 173 | |
} |
| 174 | |
|
| 175 | |
public long getCollectionLength() { |
| 176 | 12 | return manifest.get("collectionLength", (long) 0); |
| 177 | |
} |
| 178 | |
|
| 179 | |
public long getDocumentCount() { |
| 180 | 12 | return manifest.get("documentCount", (long) 0); |
| 181 | |
} |
| 182 | |
|
| 183 | |
public void close() throws IOException { |
| 184 | 0 | for (StructuredIndexPartReader part : parts.values()) { |
| 185 | 0 | part.close(); |
| 186 | |
} |
| 187 | 0 | parts.clear(); |
| 188 | 0 | documentLengths.close(); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
public int getLength(int document) { |
| 192 | 20 | return documentLengths.getLength(document); |
| 193 | |
} |
| 194 | |
|
| 195 | |
public String getDocumentName(int document) { |
| 196 | 20 | return documentNames.get(document); |
| 197 | |
} |
| 198 | |
} |