📄 segmentinfo.java
字号:
// code. So we must fallback to the original // directory list check: String[] result = dir.list(); if (result == null) throw new IOException("cannot read directory " + dir + ": list() returned null"); String pattern; pattern = name + ".s"; int patternLength = pattern.length(); for(int i = 0; i < result.length; i++){ if(result[i].startsWith(pattern) && Character.isDigit(result[i].charAt(patternLength))) return true; } return false; } } else { // This means this segment was saved with LOCKLESS // code so we first check whether any normGen's are >= 1 // (meaning they definitely have separate norms): for(int i=0;i<normGen.length;i++) { if (normGen[i] >= YES) { return true; } } // Next we look for any == 0. These cases were // pre-LOCKLESS and must be checked in directory: for(int i=0;i<normGen.length;i++) { if (normGen[i] == CHECK_DIR) { if (hasSeparateNorms(i)) { return true; } } } } return false; } /** * Increment the generation count for the norms file for * this field. * * @param fieldIndex field whose norm file will be rewritten */ void advanceNormGen(int fieldIndex) { if (normGen[fieldIndex] == NO) { normGen[fieldIndex] = YES; } else { normGen[fieldIndex]++; } clearFiles(); } /** * Get the file name for the norms file for this field. * * @param number field index */ String getNormFileName(int number) throws IOException { String prefix; long gen; if (normGen == null) { gen = CHECK_DIR; } else { gen = normGen[number]; } if (hasSeparateNorms(number)) { // case 1: separate norm prefix = ".s"; return IndexFileNames.fileNameFromGeneration(name, prefix + number, gen); } if (hasSingleNormFile) { // case 2: lockless (or nrm file exists) - single file for all norms prefix = "." + IndexFileNames.NORMS_EXTENSION; return IndexFileNames.fileNameFromGeneration(name, prefix, WITHOUT_GEN); } // case 3: norm file for each field prefix = ".f"; return IndexFileNames.fileNameFromGeneration(name, prefix + number, WITHOUT_GEN); } /** * Mark whether this segment is stored as a compound file. * * @param isCompoundFile true if this is a compound file; * else, false */ void setUseCompoundFile(boolean isCompoundFile) { if (isCompoundFile) { this.isCompoundFile = YES; } else { this.isCompoundFile = NO; } clearFiles(); } /** * Returns true if this segment is stored as a compound * file; else, false. */ boolean getUseCompoundFile() throws IOException { if (isCompoundFile == NO) { return false; } else if (isCompoundFile == YES) { return true; } else { return dir.fileExists(name + "." + IndexFileNames.COMPOUND_FILE_EXTENSION); } } int getDocStoreOffset() { return docStoreOffset; } boolean getDocStoreIsCompoundFile() { return docStoreIsCompoundFile; } void setDocStoreIsCompoundFile(boolean v) { docStoreIsCompoundFile = v; clearFiles(); } String getDocStoreSegment() { return docStoreSegment; } void setDocStoreOffset(int offset) { docStoreOffset = offset; clearFiles(); } /** * Save this segment's info. */ void write(IndexOutput output) throws IOException { output.writeString(name); output.writeInt(docCount); output.writeLong(delGen); output.writeInt(docStoreOffset); if (docStoreOffset != -1) { output.writeString(docStoreSegment); output.writeByte((byte) (docStoreIsCompoundFile ? 1:0)); } output.writeByte((byte) (hasSingleNormFile ? 1:0)); if (normGen == null) { output.writeInt(NO); } else { output.writeInt(normGen.length); for(int j = 0; j < normGen.length; j++) { output.writeLong(normGen[j]); } } output.writeByte(isCompoundFile); } private void addIfExists(List files, String fileName) throws IOException { if (dir.fileExists(fileName)) files.add(fileName); } /* * Return all files referenced by this SegmentInfo. The * returns List is a locally cached List so you should not * modify it. */ public List files() throws IOException { if (files != null) { // Already cached: return files; } files = new ArrayList(); boolean useCompoundFile = getUseCompoundFile(); if (useCompoundFile) { files.add(name + "." + IndexFileNames.COMPOUND_FILE_EXTENSION); } else { final String[] exts = IndexFileNames.NON_STORE_INDEX_EXTENSIONS; for(int i=0;i<exts.length;i++) addIfExists(files, name + "." + exts[i]); } if (docStoreOffset != -1) { // We are sharing doc stores (stored fields, term // vectors) with other segments assert docStoreSegment != null; if (docStoreIsCompoundFile) { files.add(docStoreSegment + "." + IndexFileNames.COMPOUND_FILE_STORE_EXTENSION); } else { final String[] exts = IndexFileNames.STORE_INDEX_EXTENSIONS; for(int i=0;i<exts.length;i++) addIfExists(files, docStoreSegment + "." + exts[i]); } } else if (!useCompoundFile) { // We are not sharing, and, these files were not // included in the compound file final String[] exts = IndexFileNames.STORE_INDEX_EXTENSIONS; for(int i=0;i<exts.length;i++) addIfExists(files, name + "." + exts[i]); } String delFileName = IndexFileNames.fileNameFromGeneration(name, "." + IndexFileNames.DELETES_EXTENSION, delGen); if (delFileName != null && (delGen >= YES || dir.fileExists(delFileName))) { files.add(delFileName); } // Careful logic for norms files if (normGen != null) { for(int i=0;i<normGen.length;i++) { long gen = normGen[i]; if (gen >= YES) { // Definitely a separate norm file, with generation: files.add(IndexFileNames.fileNameFromGeneration(name, "." + IndexFileNames.SEPARATE_NORMS_EXTENSION + i, gen)); } else if (NO == gen) { // No separate norms but maybe plain norms // in the non compound file case: if (!hasSingleNormFile && !useCompoundFile) { String fileName = name + "." + IndexFileNames.PLAIN_NORMS_EXTENSION + i; if (dir.fileExists(fileName)) { files.add(fileName); } } } else if (CHECK_DIR == gen) { // Pre-2.1: we have to check file existence String fileName = null; if (useCompoundFile) { fileName = name + "." + IndexFileNames.SEPARATE_NORMS_EXTENSION + i; } else if (!hasSingleNormFile) { fileName = name + "." + IndexFileNames.PLAIN_NORMS_EXTENSION + i; } if (fileName != null && dir.fileExists(fileName)) { files.add(fileName); } } } } else if (preLockless || (!hasSingleNormFile && !useCompoundFile)) { // Pre-2.1: we have to scan the dir to find all // matching _X.sN/_X.fN files for our segment: String prefix; if (useCompoundFile) prefix = name + "." + IndexFileNames.SEPARATE_NORMS_EXTENSION; else prefix = name + "." + IndexFileNames.PLAIN_NORMS_EXTENSION; int prefixLength = prefix.length(); String[] allFiles = dir.list(); if (allFiles == null) throw new IOException("cannot read directory " + dir + ": list() returned null"); for(int i=0;i<allFiles.length;i++) { String fileName = allFiles[i]; if (fileName.length() > prefixLength && Character.isDigit(fileName.charAt(prefixLength)) && fileName.startsWith(prefix)) { files.add(fileName); } } } return files; } /* Called whenever any change is made that affects which * files this segment has. */ private void clearFiles() { files = null; sizeInBytes = -1; } /** Used for debugging */ public String segString(Directory dir) { String cfs; try { if (getUseCompoundFile()) cfs = "c"; else cfs = "C"; } catch (IOException ioe) { cfs = "?"; } String docStore; if (docStoreOffset != -1) docStore = "->" + docStoreSegment; else docStore = ""; return name + ":" + cfs + (this.dir == dir ? "" : "x") + docCount + docStore; } /** We consider another SegmentInfo instance equal if it * has the same dir and same name. */ public boolean equals(Object obj) { SegmentInfo other; try { other = (SegmentInfo) obj; } catch (ClassCastException cce) { return false; } return other.dir == dir && other.name.equals(name); } public int hashCode() { return dir.hashCode() + name.hashCode(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -