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

📄 fieldcacheimpl.java

📁 Lucene a java open-source SearchEngine Framework
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.apache.lucene.search;/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 org.apache.lucene.index.IndexReader;import org.apache.lucene.index.Term;import org.apache.lucene.index.TermDocs;import org.apache.lucene.index.TermEnum;import java.io.IOException;import java.util.HashMap;import java.util.Locale;import java.util.Map;import java.util.WeakHashMap;/** * Expert: The default cache implementation, storing all values in memory. * A WeakHashMap is used for storage. * * <p>Created: May 19, 2004 4:40:36 PM * * @author  Tim Jones (Nacimiento Software) * @since   lucene 1.4 * @version $Id: FieldCacheImpl.java 605225 2007-12-18 15:13:05Z gsingers $ */class FieldCacheImplimplements FieldCache {	  /** Expert: Internal cache. */  abstract static class Cache {    private final Map readerCache = new WeakHashMap();        protected abstract Object createValue(IndexReader reader, Object key)        throws IOException;    public Object get(IndexReader reader, Object key) throws IOException {      Map innerCache;      Object value;      synchronized (readerCache) {        innerCache = (Map) readerCache.get(reader);        if (innerCache == null) {          innerCache = new HashMap();          readerCache.put(reader, innerCache);          value = null;        } else {          value = innerCache.get(key);        }        if (value == null) {          value = new CreationPlaceholder();          innerCache.put(key, value);        }      }      if (value instanceof CreationPlaceholder) {        synchronized (value) {          CreationPlaceholder progress = (CreationPlaceholder) value;          if (progress.value == null) {            progress.value = createValue(reader, key);            synchronized (readerCache) {              innerCache.put(key, progress.value);            }          }          return progress.value;        }      }      return value;    }  }  static final class CreationPlaceholder {    Object value;  }  /** Expert: Every composite-key in the internal cache is of this type. */  static class Entry {    final String field;        // which Fieldable    final int type;            // which SortField type    final Object custom;       // which custom comparator    final Locale locale;       // the locale we're sorting (if string)    /** Creates one of these objects. */    Entry (String field, int type, Locale locale) {      this.field = field.intern();      this.type = type;      this.custom = null;      this.locale = locale;    }    /** Creates one of these objects for a custom comparator. */    Entry (String field, Object custom) {      this.field = field.intern();      this.type = SortField.CUSTOM;      this.custom = custom;      this.locale = null;    }    /** Two of these are equal iff they reference the same field and type. */    public boolean equals (Object o) {      if (o instanceof Entry) {        Entry other = (Entry) o;        if (other.field == field && other.type == type) {          if (other.locale == null ? locale == null : other.locale.equals(locale)) {            if (other.custom == null) {              if (custom == null) return true;            } else if (other.custom.equals (custom)) {              return true;            }          }        }      }      return false;    }    /** Composes a hashcode based on the field and type. */    public int hashCode() {      return field.hashCode() ^ type ^ (custom==null ? 0 : custom.hashCode()) ^ (locale==null ? 0 : locale.hashCode());    }  }  private static final ByteParser BYTE_PARSER = new ByteParser() {    public byte parseByte(String value) {      return Byte.parseByte(value);    }  };  private static final ShortParser SHORT_PARSER = new ShortParser() {    public short parseShort(String value) {      return Short.parseShort(value);    }  };  private static final IntParser INT_PARSER = new IntParser() {      public int parseInt(String value) {        return Integer.parseInt(value);      }  };  private static final FloatParser FLOAT_PARSER = new FloatParser() {      public float parseFloat(String value) {        return Float.parseFloat(value);      }  };  // inherit javadocs  public byte[] getBytes (IndexReader reader, String field) throws IOException {    return getBytes(reader, field, BYTE_PARSER);  }  // inherit javadocs  public byte[] getBytes(IndexReader reader, String field, ByteParser parser)      throws IOException {    return (byte[]) bytesCache.get(reader, new Entry(field, parser));  }  Cache bytesCache = new Cache() {    protected Object createValue(IndexReader reader, Object entryKey)        throws IOException {      Entry entry = (Entry) entryKey;      String field = entry.field;      ByteParser parser = (ByteParser) entry.custom;      final byte[] retArray = new byte[reader.maxDoc()];      TermDocs termDocs = reader.termDocs();      TermEnum termEnum = reader.terms (new Term (field, ""));      try {        do {          Term term = termEnum.term();          if (term==null || term.field() != field) break;          byte termval = parser.parseByte(term.text());          termDocs.seek (termEnum);          while (termDocs.next()) {            retArray[termDocs.doc()] = termval;          }        } while (termEnum.next());      } finally {        termDocs.close();        termEnum.close();      }      return retArray;    }  };    // inherit javadocs  public short[] getShorts (IndexReader reader, String field) throws IOException {    return getShorts(reader, field, SHORT_PARSER);  }  // inherit javadocs  public short[] getShorts(IndexReader reader, String field, ShortParser parser)      throws IOException {    return (short[]) shortsCache.get(reader, new Entry(field, parser));  }  Cache shortsCache = new Cache() {    protected Object createValue(IndexReader reader, Object entryKey)        throws IOException {      Entry entry = (Entry) entryKey;      String field = entry.field;      ShortParser parser = (ShortParser) entry.custom;      final short[] retArray = new short[reader.maxDoc()];      TermDocs termDocs = reader.termDocs();      TermEnum termEnum = reader.terms (new Term (field, ""));      try {        do {          Term term = termEnum.term();          if (term==null || term.field() != field) break;          short termval = parser.parseShort(term.text());          termDocs.seek (termEnum);          while (termDocs.next()) {            retArray[termDocs.doc()] = termval;          }        } while (termEnum.next());      } finally {        termDocs.close();        termEnum.close();      }      return retArray;    }  };    // inherit javadocs  public int[] getInts (IndexReader reader, String field) throws IOException {    return getInts(reader, field, INT_PARSER);  }  // inherit javadocs  public int[] getInts(IndexReader reader, String field, IntParser parser)      throws IOException {    return (int[]) intsCache.get(reader, new Entry(field, parser));  }  Cache intsCache = new Cache() {    protected Object createValue(IndexReader reader, Object entryKey)        throws IOException {      Entry entry = (Entry) entryKey;      String field = entry.field;      IntParser parser = (IntParser) entry.custom;      final int[] retArray = new int[reader.maxDoc()];      TermDocs termDocs = reader.termDocs();

⌨️ 快捷键说明

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