1 package org.apache.lucene.index; 2 3 18 19 import java.util.Vector ; 20 import java.io.IOException ; 21 import org.apache.lucene.store.Directory; 22 import org.apache.lucene.store.IndexInput; 23 import org.apache.lucene.store.IndexOutput; 24 import org.apache.lucene.util.Constants; 25 26 final class SegmentInfos extends Vector { 27 28 29 30 public static final int FORMAT = -1; 31 32 public int counter = 0; 37 private long version = System.currentTimeMillis(); 38 39 public final SegmentInfo info(int i) { 40 return (SegmentInfo) elementAt(i); 41 } 42 43 public final void read(Directory directory) throws IOException { 44 45 IndexInput input = directory.openInput(IndexFileNames.SEGMENTS); 46 try { 47 int format = input.readInt(); 48 if(format < 0){ if (format < FORMAT) 51 throw new IOException ("Unknown format version: " + format); 52 version = input.readLong(); counter = input.readInt(); } 55 else{ counter = format; 57 } 58 59 for (int i = input.readInt(); i > 0; i--) { SegmentInfo si = 61 new SegmentInfo(input.readString(), input.readInt(), directory); 62 addElement(si); 63 } 64 65 if(format >= 0){ if (input.getFilePointer() >= input.length()) 67 version = System.currentTimeMillis(); else 69 version = input.readLong(); } 71 } 72 finally { 73 input.close(); 74 } 75 } 76 77 public final void write(Directory directory) throws IOException { 78 IndexOutput output = directory.createOutput("segments.new"); 79 try { 80 output.writeInt(FORMAT); output.writeLong(++version); output.writeInt(counter); output.writeInt(size()); for (int i = 0; i < size(); i++) { 85 SegmentInfo si = info(i); 86 output.writeString(si.name); 87 output.writeInt(si.docCount); 88 } 89 } 90 finally { 91 output.close(); 92 } 93 94 directory.renameFile("segments.new", IndexFileNames.SEGMENTS); 96 } 97 98 101 public long getVersion() { 102 return version; 103 } 104 105 108 public static long readCurrentVersion(Directory directory) 109 throws IOException { 110 111 IndexInput input = directory.openInput(IndexFileNames.SEGMENTS); 112 int format = 0; 113 long version = 0; 114 try { 115 format = input.readInt(); 116 if(format < 0){ 117 if (format < FORMAT) 118 throw new IOException ("Unknown format version: " + format); 119 version = input.readLong(); } 121 } 122 finally { 123 input.close(); 124 } 125 126 if(format < 0) 127 return version; 128 129 132 SegmentInfos sis = new SegmentInfos(); 133 sis.read(directory); 134 return sis.getVersion(); 135 } 136 } 137 | Popular Tags |