Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IntFieldSource.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 using FieldCache = Lucene.Net.Search.FieldCache;
22 
23 namespace Lucene.Net.Search.Function
24 {
25 
26  /// <summary> Expert: obtains int field values from the
27  /// <see cref="Lucene.Net.Search.FieldCache">FieldCache</see>
28  /// using <c>getInts()</c> and makes those values
29  /// available as other numeric types, casting as needed.
30  ///
31  /// <p/><font color="#FF0000">
32  /// WARNING: The status of the <b>Search.Function</b> package is experimental.
33  /// The APIs introduced here might change in the future and will not be
34  /// supported anymore in such a case.</font>
35  ///
36  /// </summary>
37  /// <seealso cref="Lucene.Net.Search.Function.FieldCacheSource"> for requirements
38  /// on the field.
39  ///
40  /// <p/><b>NOTE</b>: with the switch in 2.9 to segment-based
41  /// searching, if <see cref="FieldCacheSource.GetValues" /> is invoked with a
42  /// composite (multi-segment) reader, this can easily cause
43  /// double RAM usage for the values in the FieldCache. It's
44  /// best to switch your application to pass only atomic
45  /// (single segment) readers to this API.<p/>
46  /// </seealso>
47  [Serializable]
49  {
50  private class AnonymousClassDocValues:DocValues
51  {
52  public AnonymousClassDocValues(int[] arr, IntFieldSource enclosingInstance)
53  {
54  InitBlock(arr, enclosingInstance);
55  }
56  private void InitBlock(int[] arr, IntFieldSource enclosingInstance)
57  {
58  this.arr = arr;
59  this.enclosingInstance = enclosingInstance;
60  }
61  private int[] arr;
62  private IntFieldSource enclosingInstance;
63  public IntFieldSource Enclosing_Instance
64  {
65  get
66  {
67  return enclosingInstance;
68  }
69 
70  }
71  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.DocValues.floatVal(int) */
72  public override float FloatVal(int doc)
73  {
74  return (float) arr[doc];
75  }
76  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.DocValues.intVal(int) */
77  public override int IntVal(int doc)
78  {
79  return arr[doc];
80  }
81  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.DocValues.toString(int) */
82  public override System.String ToString(int doc)
83  {
84  return Enclosing_Instance.Description() + '=' + IntVal(doc);
85  }
86  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.DocValues.getInnerArray() */
87 
88  protected internal override object InnerArray
89  {
90  get { return arr; }
91  }
92  }
93  private Lucene.Net.Search.IntParser parser;
94 
95  /// <summary> Create a cached int field source with default string-to-int parser. </summary>
96  public IntFieldSource(System.String field):this(field, null)
97  {
98  }
99 
100  /// <summary> Create a cached int field source with a specific string-to-int parser. </summary>
101  public IntFieldSource(System.String field, Lucene.Net.Search.IntParser parser):base(field)
102  {
103  this.parser = parser;
104  }
105 
106  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.ValueSource.description() */
107  public override System.String Description()
108  {
109  return "int(" + base.Description() + ')';
110  }
111 
112  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.FieldCacheSource.getCachedValues(Lucene.Net.Search.FieldCache, java.lang.String, Lucene.Net.Index.IndexReader) */
113  public override DocValues GetCachedFieldValues(FieldCache cache, System.String field, IndexReader reader)
114  {
115  int[] arr = cache.GetInts(reader, field, parser);
116  return new AnonymousClassDocValues(arr, this);
117  }
118 
119  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.FieldCacheSource.cachedFieldSourceEquals(Lucene.Net.Search.Function.FieldCacheSource) */
120  public override bool CachedFieldSourceEquals(FieldCacheSource o)
121  {
122  if (o.GetType() != typeof(IntFieldSource))
123  {
124  return false;
125  }
126  IntFieldSource other = (IntFieldSource) o;
127  return this.parser == null?other.parser == null:this.parser.GetType() == other.parser.GetType();
128  }
129 
130  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.FieldCacheSource.cachedFieldSourceHashCode() */
131  public override int CachedFieldSourceHashCode()
132  {
133  return parser == null?typeof(System.Int32).GetHashCode():parser.GetType().GetHashCode();
134  }
135  }
136 }