/*
* 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.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Lucene.Net.Support;
using NumericField = Lucene.Net.Documents.NumericField;
using IndexReader = Lucene.Net.Index.IndexReader;
using Term = Lucene.Net.Index.Term;
using TermDocs = Lucene.Net.Index.TermDocs;
using TermEnum = Lucene.Net.Index.TermEnum;
using FieldCacheSanityChecker = Lucene.Net.Util.FieldCacheSanityChecker;
using Single = Lucene.Net.Support.Single;
using StringHelper = Lucene.Net.Util.StringHelper;
namespace Lucene.Net.Search
{
/// Expert: The default cache implementation, storing all values in memory.
/// A WeakDictionary is used for storage.
///
/// Created: May 19, 2004 4:40:36 PM
///
///
/// lucene 1.4
///
class FieldCacheImpl : FieldCache
{
private IDictionary caches;
internal FieldCacheImpl()
{
Init();
}
private void Init()
{
lock (this)
{
caches = new HashMap(7);
caches[typeof(sbyte)] = new ByteCache(this);
caches[typeof(short)] = new ShortCache(this);
caches[typeof(int)] = new IntCache(this);
caches[typeof(float)] = new FloatCache(this);
caches[typeof(long)] = new LongCache(this);
caches[typeof(double)] = new DoubleCache(this);
caches[typeof(string)] = new StringCache(this);
caches[typeof(StringIndex)] = new StringIndexCache(this);
}
}
// lucene.net: java version 3.0.3 with patch in rev. 912330 applied:
// uschindler 21/02/2010 12:16:42 LUCENE-2273: Fixed bug in FieldCacheImpl.getCacheEntries() that used
// WeakHashMap incorrectly and lead to ConcurrentModificationException
public virtual void PurgeAllCaches()
{
lock (this)
{
Init();
}
}
// lucene.net: java version 3.0.3 with patch in rev. 912330 applied:
// uschindler 21/02/2010 12:16:42 LUCENE-2273: Fixed bug in FieldCacheImpl.getCacheEntries() that used
// WeakHashMap incorrectly and lead to ConcurrentModificationException
public void Purge(IndexReader r)
{
lock (this)
{
foreach (Cache c in caches.Values)
{
c.Purge(r);
}
}
}
// lucene.net: java version 3.0.3 with patch in rev. 912330 applied:
// uschindler 21/02/2010 12:16:42 LUCENE-2273: Fixed bug in FieldCacheImpl.getCacheEntries() that used
// WeakHashMap incorrectly and lead to ConcurrentModificationException
public virtual CacheEntry[] GetCacheEntries()
{
lock (this)
{
IList result = new List(17);
foreach (var cacheEntry in caches)
{
var cache = cacheEntry.Value;
var cacheType = cacheEntry.Key;
lock (cache.readerCache)
{
foreach (var readerCacheEntry in cache.readerCache)
{
var readerKey = readerCacheEntry.Key;
var innerCache = readerCacheEntry.Value;
foreach (var mapEntry in innerCache)
{
Entry entry = mapEntry.Key;
result.Add(new CacheEntryImpl(readerKey, entry.field, cacheType, entry.custom, mapEntry.Value));
}
}
}
}
return result.ToArray();
}
}
private sealed class CacheEntryImpl : CacheEntry
{
private System.Object readerKey;
private System.String fieldName;
private System.Type cacheType;
private System.Object custom;
private System.Object value;
internal CacheEntryImpl(System.Object readerKey, System.String fieldName, System.Type cacheType, System.Object custom, System.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);
// }
}
public override object ReaderKey
{
get { return readerKey; }
}
public override string FieldName
{
get { return fieldName; }
}
public override Type CacheType
{
get { return cacheType; }
}
public override object Custom
{
get { return custom; }
}
public override object Value
{
get { return value; }
}
}
/// Hack: When thrown from a Parser (NUMERIC_UTILS_* ones), this stops
/// processing terms and returns the current FieldCache
/// array.
///
[Serializable]
internal sealed class StopFillCacheException:System.SystemException
{
}
/// Expert: Internal cache.
internal abstract class Cache
{
internal Cache()
{
this.wrapper = null;
}
internal Cache(FieldCache wrapper)
{
this.wrapper = wrapper;
}
internal FieldCache wrapper;
internal IDictionary