⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segmentreader.java

📁 lucene完整源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.apache.lucene.index;/** * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import java.io.IOException;import java.util.*;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.Directory;import org.apache.lucene.util.BitVector;import org.apache.lucene.search.DefaultSimilarity;/** * @version $Id: SegmentReader.java 405870 2006-05-12 21:04:00Z dnaber $ */class SegmentReader extends IndexReader {  private String segment;  FieldInfos fieldInfos;  private FieldsReader fieldsReader;  TermInfosReader tis;  TermVectorsReader termVectorsReaderOrig = null;  ThreadLocal termVectorsLocal = new ThreadLocal();  BitVector deletedDocs = null;  private boolean deletedDocsDirty = false;  private boolean normsDirty = false;  private boolean undeleteAll = false;  IndexInput freqStream;  IndexInput proxStream;  // Compound File Reader when based on a compound file segment  CompoundFileReader cfsReader = null;  private class Norm {    public Norm(IndexInput in, int number)    {      this.in = in;      this.number = number;    }    private IndexInput in;    private byte[] bytes;    private boolean dirty;    private int number;    private void reWrite() throws IOException {      // NOTE: norms are re-written in regular directory, not cfs      IndexOutput out = directory().createOutput(segment + ".tmp");      try {        out.writeBytes(bytes, maxDoc());      } finally {        out.close();      }      String fileName;      if(cfsReader == null)          fileName = segment + ".f" + number;      else{          // use a different file name if we have compound format          fileName = segment + ".s" + number;      }      directory().renameFile(segment + ".tmp", fileName);      this.dirty = false;    }  }  private Hashtable norms = new Hashtable();  /** The class which implements SegmentReader. */  private static Class IMPL;  static {    try {      String name =        System.getProperty("org.apache.lucene.SegmentReader.class",                           SegmentReader.class.getName());      IMPL = Class.forName(name);    } catch (ClassNotFoundException e) {      throw new RuntimeException("cannot load SegmentReader class: " + e, e);    } catch (SecurityException se) {      try {        IMPL = Class.forName(SegmentReader.class.getName());      } catch (ClassNotFoundException e) {        throw new RuntimeException("cannot load default SegmentReader class: " + e, e);      }    }  }  protected SegmentReader() { super(null); }  public static SegmentReader get(SegmentInfo si) throws IOException {    return get(si.dir, si, null, false, false);  }  public static SegmentReader get(SegmentInfos sis, SegmentInfo si,                                  boolean closeDir) throws IOException {    return get(si.dir, si, sis, closeDir, true);  }  public static SegmentReader get(Directory dir, SegmentInfo si,                                  SegmentInfos sis,                                  boolean closeDir, boolean ownDir)    throws IOException {    SegmentReader instance;    try {      instance = (SegmentReader)IMPL.newInstance();    } catch (Exception e) {      throw new RuntimeException("cannot load SegmentReader class: " + e, e);    }    instance.init(dir, sis, closeDir, ownDir);    instance.initialize(si);    return instance;  }   private void initialize(SegmentInfo si) throws IOException {    segment = si.name;    // Use compound file directory for some files, if it exists    Directory cfsDir = directory();    if (directory().fileExists(segment + ".cfs")) {      cfsReader = new CompoundFileReader(directory(), segment + ".cfs");      cfsDir = cfsReader;    }    // No compound file exists - use the multi-file format    fieldInfos = new FieldInfos(cfsDir, segment + ".fnm");    fieldsReader = new FieldsReader(cfsDir, segment, fieldInfos);    tis = new TermInfosReader(cfsDir, segment, fieldInfos);    // NOTE: the bitvector is stored using the regular directory, not cfs    if (hasDeletions(si))      deletedDocs = new BitVector(directory(), segment + ".del");    // make sure that all index files have been read or are kept open    // so that if an index update removes them we'll still have them    freqStream = cfsDir.openInput(segment + ".frq");    proxStream = cfsDir.openInput(segment + ".prx");    openNorms(cfsDir);    if (fieldInfos.hasVectors()) { // open term vector files only as needed      termVectorsReaderOrig = new TermVectorsReader(cfsDir, segment, fieldInfos);    }  }   protected void finalize() {     // patch for pre-1.4.2 JVMs, whose ThreadLocals leak     termVectorsLocal.set(null);     super.finalize();   }  protected void doCommit() throws IOException {    if (deletedDocsDirty) {               // re-write deleted      deletedDocs.write(directory(), segment + ".tmp");      directory().renameFile(segment + ".tmp", segment + ".del");    }    if(undeleteAll && directory().fileExists(segment + ".del")){      directory().deleteFile(segment + ".del");    }    if (normsDirty) {               // re-write norms      Enumeration values = norms.elements();      while (values.hasMoreElements()) {        Norm norm = (Norm) values.nextElement();        if (norm.dirty) {          norm.reWrite();        }      }    }    deletedDocsDirty = false;    normsDirty = false;    undeleteAll = false;  }  protected void doClose() throws IOException {    fieldsReader.close();    tis.close();    if (freqStream != null)      freqStream.close();    if (proxStream != null)      proxStream.close();    closeNorms();    if (termVectorsReaderOrig != null)      termVectorsReaderOrig.close();    if (cfsReader != null)      cfsReader.close();  }  static boolean hasDeletions(SegmentInfo si) throws IOException {    return si.dir.fileExists(si.name + ".del");  }  public boolean hasDeletions() {    return deletedDocs != null;  }  static boolean usesCompoundFile(SegmentInfo si) throws IOException {    return si.dir.fileExists(si.name + ".cfs");  }  static boolean hasSeparateNorms(SegmentInfo si) throws IOException {    String[] result = si.dir.list();    String pattern = si.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;  }  protected void doDelete(int docNum) {    if (deletedDocs == null)      deletedDocs = new BitVector(maxDoc());    deletedDocsDirty = true;    undeleteAll = false;    deletedDocs.set(docNum);  }  protected void doUndeleteAll() {      deletedDocs = null;      deletedDocsDirty = false;      undeleteAll = true;  }  Vector files() throws IOException {    Vector files = new Vector(16);    for (int i = 0; i < IndexFileNames.INDEX_EXTENSIONS.length; i++) {      String name = segment + "." + IndexFileNames.INDEX_EXTENSIONS[i];      if (directory().fileExists(name))        files.addElement(name);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -