X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.4.0/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java diff --git a/lucene-java-3.4.0/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java b/lucene-java-3.4.0/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java deleted file mode 100644 index 8906d9b..0000000 --- a/lucene-java-3.4.0/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java +++ /dev/null @@ -1,745 +0,0 @@ -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 java.io.IOException; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.WeakHashMap; - -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 org.apache.lucene.util.BitVector; -import org.apache.lucene.util.DocIdBitSet; -import org.apache.lucene.util.OpenBitSet; -import org.apache.lucene.util.StringHelper; -import org.apache.lucene.util.FieldCacheSanityChecker; - -/** - * Expert: The default cache implementation, storing all values in memory. - * A WeakHashMap is used for storage. - * - *

Created: May 19, 2004 4:40:36 PM - * - * @since lucene 1.4 - */ -class FieldCacheImpl implements FieldCache { - - private Map,Cache> caches; - FieldCacheImpl() { - init(); - } - private synchronized void init() { - caches = new HashMap,Cache>(9); - caches.put(Byte.TYPE, new ByteCache(this)); - caches.put(Short.TYPE, new ShortCache(this)); - caches.put(Integer.TYPE, new IntCache(this)); - caches.put(Float.TYPE, new FloatCache(this)); - caches.put(Long.TYPE, new LongCache(this)); - caches.put(Double.TYPE, new DoubleCache(this)); - caches.put(String.class, new StringCache(this)); - caches.put(StringIndex.class, new StringIndexCache(this)); - caches.put(UnValuedDocsCache.class, new UnValuedDocsCache(this)); - } - - public synchronized void purgeAllCaches() { - init(); - } - - public synchronized void purge(IndexReader r) { - for(Cache c : caches.values()) { - c.purge(r); - } - } - - public synchronized CacheEntry[] getCacheEntries() { - List result = new ArrayList(17); - for(final Map.Entry,Cache> cacheEntry: caches.entrySet()) { - final Cache cache = cacheEntry.getValue(); - final Class cacheType = cacheEntry.getKey(); - synchronized(cache.readerCache) { - for (final Map.Entry> readerCacheEntry : cache.readerCache.entrySet()) { - final Object readerKey = readerCacheEntry.getKey(); - if (readerKey == null) continue; - final Map innerCache = readerCacheEntry.getValue(); - for (final Map.Entry mapEntry : innerCache.entrySet()) { - Entry entry = mapEntry.getKey(); - result.add(new CacheEntryImpl(readerKey, entry.field, - cacheType, entry.custom, - mapEntry.getValue())); - } - } - } - } - return result.toArray(new CacheEntry[result.size()]); - } - - private static final class CacheEntryImpl extends CacheEntry { - private final Object readerKey; - private final String fieldName; - private final Class cacheType; - private final Object custom; - private final Object value; - CacheEntryImpl(Object readerKey, String fieldName, - Class cacheType, - Object custom, - Object value) { - this.readerKey = readerKey; - this.fieldName = fieldName; - this.cacheType = cacheType; - this.custom = custom; - this.value = value; - - // :HACK: for testing. -// if (null != locale || SortField.CUSTOM != sortFieldType) { -// throw new RuntimeException("Locale/sortFieldType: " + this); -// } - - } - @Override - public Object getReaderKey() { return readerKey; } - @Override - public String getFieldName() { return fieldName; } - @Override - public Class getCacheType() { return cacheType; } - @Override - public Object getCustom() { return custom; } - @Override - public Object getValue() { return value; } - } - - /** - * Hack: When thrown from a Parser (NUMERIC_UTILS_* ones), this stops - * processing terms and returns the current FieldCache - * array. - */ - static final class StopFillCacheException extends RuntimeException { - } - - final static IndexReader.ReaderFinishedListener purgeReader = new IndexReader.ReaderFinishedListener() { - // @Override -- not until Java 1.6 - public void finished(IndexReader reader) { - FieldCache.DEFAULT.purge(reader); - } - }; - - /** Expert: Internal cache. */ - abstract static class Cache { - Cache() { - this.wrapper = null; - } - - Cache(FieldCache wrapper) { - this.wrapper = wrapper; - } - - final FieldCache wrapper; - - final Map> readerCache = new WeakHashMap>(); - - protected abstract Object createValue(IndexReader reader, Entry key) - throws IOException; - - /** Remove this reader from the cache, if present. */ - public void purge(IndexReader r) { - Object readerKey = r.getCoreCacheKey(); - synchronized(readerCache) { - readerCache.remove(readerKey); - } - } - - public Object get(IndexReader reader, Entry key) throws IOException { - Map innerCache; - Object value; - final Object readerKey = reader.getCoreCacheKey(); - synchronized (readerCache) { - innerCache = readerCache.get(readerKey); - if (innerCache == null) { - // First time this reader is using FieldCache - innerCache = new HashMap(); - readerCache.put(readerKey, innerCache); - reader.addReaderFinishedListener(purgeReader); - 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); - } - - // Only check if key.custom (the parser) is - // non-null; else, we check twice for a single - // call to FieldCache.getXXX - if (key.custom != null && wrapper != null) { - final PrintStream infoStream = wrapper.getInfoStream(); - if (infoStream != null) { - printNewInsanity(infoStream, progress.value); - } - } - } - return progress.value; - } - } - return value; - } - - private void printNewInsanity(PrintStream infoStream, Object value) { - final FieldCacheSanityChecker.Insanity[] insanities = FieldCacheSanityChecker.checkSanity(wrapper); - for(int i=0;i= mterms.length) break; - - // store term text - mterms[t] = term.text(); - - termDocs.seek (termEnum); - while (termDocs.next()) { - retArray[termDocs.doc()] = t; - } - - t++; - } while (termEnum.next()); - } finally { - termDocs.close(); - termEnum.close(); - } - - if (t == 0) { - // if there are no terms, make the term array - // have a single null entry - mterms = new String[1]; - } else if (t < mterms.length) { - // if there are less terms than documents, - // trim off the dead array space - String[] terms = new String[t]; - System.arraycopy (mterms, 0, terms, 0, t); - mterms = terms; - } - - StringIndex value = new StringIndex (retArray, mterms); - return value; - } - } - - private volatile PrintStream infoStream; - - public void setInfoStream(PrintStream stream) { - infoStream = stream; - } - - public PrintStream getInfoStream() { - return infoStream; - } - - public DocIdSet getUnValuedDocs(IndexReader reader, String field) - throws IOException { - return (DocIdSet) caches.get(UnValuedDocsCache.class).get(reader, new Entry(field, null)); - } -} -