1 package org.apache.lucene.index; 2 3 18 19 import java.util.*; 20 import java.io.IOException ; 21 22 import org.apache.lucene.document.Document; 23 import org.apache.lucene.document.Field; 24 25 import org.apache.lucene.store.Directory; 26 import org.apache.lucene.store.IndexOutput; 27 import org.apache.lucene.store.IndexInput; 28 29 35 final class FieldInfos { 36 37 static final byte IS_INDEXED = 0x1; 38 static final byte STORE_TERMVECTOR = 0x2; 39 static final byte STORE_POSITIONS_WITH_TERMVECTOR = 0x4; 40 static final byte STORE_OFFSET_WITH_TERMVECTOR = 0x8; 41 static final byte OMIT_NORMS = 0x10; 42 43 private ArrayList byNumber = new ArrayList(); 44 private HashMap byName = new HashMap(); 45 46 FieldInfos() { } 47 48 55 FieldInfos(Directory d, String name) throws IOException { 56 IndexInput input = d.openInput(name); 57 try { 58 read(input); 59 } finally { 60 input.close(); 61 } 62 } 63 64 65 public void add(Document doc) { 66 Enumeration fields = doc.fields(); 67 while (fields.hasMoreElements()) { 68 Field field = (Field) fields.nextElement(); 69 add(field.name(), field.isIndexed(), field.isTermVectorStored(), field.isStorePositionWithTermVector(), 70 field.isStoreOffsetWithTermVector(), field.getOmitNorms()); 71 } 72 } 73 74 82 public void addIndexed(Collection names, boolean storeTermVectors, boolean storePositionWithTermVector, 83 boolean storeOffsetWithTermVector) { 84 Iterator i = names.iterator(); 85 while (i.hasNext()) { 86 add((String )i.next(), true, storeTermVectors, storePositionWithTermVector, storeOffsetWithTermVector); 87 } 88 } 89 90 98 public void add(Collection names, boolean isIndexed) { 99 Iterator i = names.iterator(); 100 while (i.hasNext()) { 101 add((String )i.next(), isIndexed); 102 } 103 } 104 105 112 public void add(String name, boolean isIndexed) { 113 add(name, isIndexed, false, false, false, false); 114 } 115 116 123 public void add(String name, boolean isIndexed, boolean storeTermVector){ 124 add(name, isIndexed, storeTermVector, false, false, false); 125 } 126 127 138 public void add(String name, boolean isIndexed, boolean storeTermVector, 139 boolean storePositionWithTermVector, boolean storeOffsetWithTermVector) { 140 141 add(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, false); 142 } 143 144 156 public void add(String name, boolean isIndexed, boolean storeTermVector, 157 boolean storePositionWithTermVector, boolean storeOffsetWithTermVector, boolean omitNorms) { 158 FieldInfo fi = fieldInfo(name); 159 if (fi == null) { 160 addInternal(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, omitNorms); 161 } else { 162 if (fi.isIndexed != isIndexed) { 163 fi.isIndexed = true; } 165 if (fi.storeTermVector != storeTermVector) { 166 fi.storeTermVector = true; } 168 if (fi.storePositionWithTermVector != storePositionWithTermVector) { 169 fi.storePositionWithTermVector = true; } 171 if (fi.storeOffsetWithTermVector != storeOffsetWithTermVector) { 172 fi.storeOffsetWithTermVector = true; } 174 if (fi.omitNorms != omitNorms) { 175 fi.omitNorms = false; } 177 178 } 179 } 180 181 182 private void addInternal(String name, boolean isIndexed, 183 boolean storeTermVector, boolean storePositionWithTermVector, 184 boolean storeOffsetWithTermVector, boolean omitNorms) { 185 FieldInfo fi = 186 new FieldInfo(name, isIndexed, byNumber.size(), storeTermVector, storePositionWithTermVector, 187 storeOffsetWithTermVector, omitNorms); 188 byNumber.add(fi); 189 byName.put(name, fi); 190 } 191 192 public int fieldNumber(String fieldName) { 193 try { 194 FieldInfo fi = fieldInfo(fieldName); 195 if (fi != null) 196 return fi.number; 197 } 198 catch (IndexOutOfBoundsException ioobe) { 199 return -1; 200 } 201 return -1; 202 } 203 204 public FieldInfo fieldInfo(String fieldName) { 205 return (FieldInfo) byName.get(fieldName); 206 } 207 208 215 public String fieldName(int fieldNumber) { 216 try { 217 return fieldInfo(fieldNumber).name; 218 } 219 catch (NullPointerException npe) { 220 return ""; 221 } 222 } 223 224 230 public FieldInfo fieldInfo(int fieldNumber) { 231 try { 232 return (FieldInfo) byNumber.get(fieldNumber); 233 } 234 catch (IndexOutOfBoundsException ioobe) { 235 return null; 236 } 237 } 238 239 public int size() { 240 return byNumber.size(); 241 } 242 243 public boolean hasVectors() { 244 boolean hasVectors = false; 245 for (int i = 0; i < size(); i++) { 246 if (fieldInfo(i).storeTermVector) { 247 hasVectors = true; 248 break; 249 } 250 } 251 return hasVectors; 252 } 253 254 public void write(Directory d, String name) throws IOException { 255 IndexOutput output = d.createOutput(name); 256 try { 257 write(output); 258 } finally { 259 output.close(); 260 } 261 } 262 263 public void write(IndexOutput output) throws IOException { 264 output.writeVInt(size()); 265 for (int i = 0; i < size(); i++) { 266 FieldInfo fi = fieldInfo(i); 267 byte bits = 0x0; 268 if (fi.isIndexed) bits |= IS_INDEXED; 269 if (fi.storeTermVector) bits |= STORE_TERMVECTOR; 270 if (fi.storePositionWithTermVector) bits |= STORE_POSITIONS_WITH_TERMVECTOR; 271 if (fi.storeOffsetWithTermVector) bits |= STORE_OFFSET_WITH_TERMVECTOR; 272 if (fi.omitNorms) bits |= OMIT_NORMS; 273 output.writeString(fi.name); 274 output.writeByte(bits); 275 } 276 } 277 278 private void read(IndexInput input) throws IOException { 279 int size = input.readVInt(); for (int i = 0; i < size; i++) { 281 String name = input.readString().intern(); 282 byte bits = input.readByte(); 283 boolean isIndexed = (bits & IS_INDEXED) != 0; 284 boolean storeTermVector = (bits & STORE_TERMVECTOR) != 0; 285 boolean storePositionsWithTermVector = (bits & STORE_POSITIONS_WITH_TERMVECTOR) != 0; 286 boolean storeOffsetWithTermVector = (bits & STORE_OFFSET_WITH_TERMVECTOR) != 0; 287 boolean omitNorms = (bits & OMIT_NORMS) != 0; 288 289 addInternal(name, isIndexed, storeTermVector, storePositionsWithTermVector, storeOffsetWithTermVector, omitNorms); 290 } 291 } 292 293 } 294 | Popular Tags |