~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/function/LongFieldSource.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
package org.apache.solr.search.function;
19
 
 
20
 
import org.apache.lucene.index.IndexReader;
21
 
import org.apache.lucene.search.FieldCache;
22
 
 
23
 
 
24
 
import java.io.IOException;
25
 
import java.util.Map;
26
 
 
27
 
/**
28
 
 * Obtains float field values from the {@link org.apache.lucene.search.FieldCache}
29
 
 * using <code>getFloats()</code>
30
 
 * and makes those values available as other numeric types, casting as needed.
31
 
 *
32
 
 * @version $Id: FloatFieldSource.java 555343 2007-07-11 17:46:25Z hossman $
33
 
 */
34
 
 
35
 
public class LongFieldSource extends FieldCacheSource {
36
 
  protected FieldCache.LongParser parser;
37
 
 
38
 
  public LongFieldSource(String field) {
39
 
    this(field, null);
40
 
  }
41
 
 
42
 
  public LongFieldSource(String field, FieldCache.LongParser parser) {
43
 
    super(field);
44
 
    this.parser = parser;
45
 
  }
46
 
 
47
 
  @Override
48
 
  public String description() {
49
 
    return "long(" + field + ')';
50
 
  }
51
 
 
52
 
 
53
 
  public long externalToLong(String extVal) {
54
 
    return Long.parseLong(extVal);
55
 
  }
56
 
 
57
 
  @Override
58
 
  public DocValues getValues(Map context, IndexReader reader) throws IOException {
59
 
    final long[] arr = (parser == null) ?
60
 
            cache.getLongs(reader, field) :
61
 
            cache.getLongs(reader, field, parser);
62
 
    return new DocValues() {
63
 
      @Override
64
 
      public float floatVal(int doc) {
65
 
        return (float) arr[doc];
66
 
      }
67
 
 
68
 
      @Override
69
 
      public int intVal(int doc) {
70
 
        return (int) arr[doc];
71
 
      }
72
 
 
73
 
      @Override
74
 
      public long longVal(int doc) {
75
 
        return arr[doc];
76
 
      }
77
 
 
78
 
      @Override
79
 
      public double doubleVal(int doc) {
80
 
        return arr[doc];
81
 
      }
82
 
 
83
 
      @Override
84
 
      public String strVal(int doc) {
85
 
        return Long.toString(arr[doc]);
86
 
      }
87
 
 
88
 
      @Override
89
 
      public String toString(int doc) {
90
 
        return description() + '=' + longVal(doc);
91
 
      }
92
 
 
93
 
      @Override
94
 
      public ValueSourceScorer getRangeScorer(IndexReader reader, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) {
95
 
        long lower,upper;
96
 
 
97
 
        // instead of using separate comparison functions, adjust the endpoints.
98
 
 
99
 
        if (lowerVal==null) {
100
 
          lower = Long.MIN_VALUE;
101
 
        } else {
102
 
          lower = externalToLong(lowerVal);
103
 
          if (!includeLower && lower < Long.MAX_VALUE) lower++;
104
 
        }
105
 
 
106
 
         if (upperVal==null) {
107
 
          upper = Long.MAX_VALUE;
108
 
        } else {
109
 
          upper = externalToLong(upperVal);
110
 
          if (!includeUpper && upper > Long.MIN_VALUE) upper--;
111
 
        }
112
 
 
113
 
        final long ll = lower;
114
 
        final long uu = upper;
115
 
 
116
 
        return new ValueSourceScorer(reader, this) {
117
 
          @Override
118
 
          public boolean matchesValue(int doc) {
119
 
            long val = arr[doc];
120
 
            // only check for deleted if it's the default value
121
 
            // if (val==0 && reader.isDeleted(doc)) return false;
122
 
            return val >= ll && val <= uu;
123
 
          }
124
 
        };
125
 
      }
126
 
 
127
 
 
128
 
    };
129
 
  }
130
 
 
131
 
  @Override
132
 
  public boolean equals(Object o) {
133
 
    if (o.getClass() != this.getClass()) return false;
134
 
    LongFieldSource other = (LongFieldSource) o;
135
 
    return super.equals(other)
136
 
            && (this.parser == null ? other.parser == null :
137
 
            this.parser.getClass() == other.parser.getClass());
138
 
  }
139
 
 
140
 
  @Override
141
 
  public int hashCode() {
142
 
    int h = parser == null ? this.getClass().hashCode() : parser.getClass().hashCode();
143
 
    h += super.hashCode();
144
 
    return h;
145
 
  }
146
 
 
147
 
}