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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/schema/SortableLongField.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.schema;
19
 
 
20
 
import org.apache.lucene.search.SortField;
21
 
import org.apache.solr.search.QParser;
22
 
import org.apache.solr.search.function.ValueSource;
23
 
import org.apache.solr.search.function.FieldCacheSource;
24
 
import org.apache.solr.search.function.DocValues;
25
 
import org.apache.solr.search.function.StringIndexDocValues;
26
 
import org.apache.lucene.document.Fieldable;
27
 
import org.apache.lucene.index.IndexReader;
28
 
import org.apache.solr.util.NumberUtils;
29
 
import org.apache.solr.response.TextResponseWriter;
30
 
import org.apache.solr.response.XMLWriter;
31
 
 
32
 
import java.util.Map;
33
 
import java.io.IOException;
34
 
/**
35
 
 * @version $Id: SortableLongField.java 1071480 2011-02-17 02:23:10Z hossman $
36
 
 */
37
 
public class SortableLongField extends FieldType {
38
 
  @Override
39
 
  protected void init(IndexSchema schema, Map<String,String> args) {
40
 
  }
41
 
 
42
 
  @Override
43
 
  public SortField getSortField(SchemaField field,boolean reverse) {
44
 
    return getStringSort(field,reverse);
45
 
  }
46
 
 
47
 
  @Override
48
 
    public ValueSource getValueSource(SchemaField field, QParser qparser) {
49
 
    field.checkFieldCacheSource(qparser);
50
 
    return new SortableLongFieldSource(field.name);
51
 
  }
52
 
 
53
 
  @Override
54
 
  public String toInternal(String val) {
55
 
    return NumberUtils.long2sortableStr(val);
56
 
  }
57
 
 
58
 
  @Override
59
 
  public String indexedToReadable(String indexedForm) {
60
 
    return NumberUtils.SortableStr2long(indexedForm);
61
 
  }
62
 
 
63
 
  @Override
64
 
  public String toExternal(Fieldable f) {
65
 
    return indexedToReadable(f.stringValue());
66
 
  }
67
 
 
68
 
  @Override
69
 
  public Long toObject(Fieldable f) {
70
 
    return NumberUtils.SortableStr2long(f.stringValue(),0,5);
71
 
  }
72
 
  
73
 
  @Override
74
 
  public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
75
 
    String sval = f.stringValue();
76
 
    xmlWriter.writeLong(name, NumberUtils.SortableStr2long(sval,0,sval.length()));
77
 
  }
78
 
 
79
 
  @Override
80
 
  public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
81
 
    String sval = f.stringValue();
82
 
    writer.writeLong(name, NumberUtils.SortableStr2long(sval,0,sval.length()));
83
 
  }
84
 
}
85
 
 
86
 
 
87
 
 
88
 
 
89
 
 
90
 
class SortableLongFieldSource extends FieldCacheSource {
91
 
  protected long defVal;
92
 
 
93
 
  public SortableLongFieldSource(String field) {
94
 
    this(field, 0);
95
 
  }
96
 
 
97
 
  public SortableLongFieldSource(String field, long defVal) {
98
 
    super(field);
99
 
    this.defVal = defVal;
100
 
  }
101
 
 
102
 
  @Override
103
 
  public String description() {
104
 
    return "slong(" + field + ')';
105
 
  }
106
 
 
107
 
  @Override
108
 
  public DocValues getValues(Map context, IndexReader reader) throws IOException {
109
 
    final long def = defVal;
110
 
 
111
 
    return new StringIndexDocValues(this, reader, field) {
112
 
      @Override
113
 
      protected String toTerm(String readableValue) {
114
 
        return NumberUtils.long2sortableStr(readableValue);
115
 
      }
116
 
 
117
 
      @Override
118
 
      public float floatVal(int doc) {
119
 
        return (float)longVal(doc);
120
 
      }
121
 
 
122
 
      @Override
123
 
      public int intVal(int doc) {
124
 
        return (int)longVal(doc);
125
 
      }
126
 
 
127
 
      @Override
128
 
      public long longVal(int doc) {
129
 
        int ord=order[doc];
130
 
        return ord==0 ? def  : NumberUtils.SortableStr2long(lookup[ord],0,5);
131
 
      }
132
 
 
133
 
      @Override
134
 
      public double doubleVal(int doc) {
135
 
        return (double)longVal(doc);
136
 
      }
137
 
 
138
 
      @Override
139
 
      public String strVal(int doc) {
140
 
        return Long.toString(longVal(doc));
141
 
      }
142
 
 
143
 
      @Override
144
 
      public String toString(int doc) {
145
 
        return description() + '=' + longVal(doc);
146
 
      }
147
 
    };
148
 
  }
149
 
 
150
 
  @Override
151
 
  public boolean equals(Object o) {
152
 
    return o instanceof SortableLongFieldSource
153
 
            && super.equals(o)
154
 
            && defVal == ((SortableLongFieldSource)o).defVal;
155
 
  }
156
 
 
157
 
  private static int hcode = SortableLongFieldSource.class.hashCode();
158
 
  @Override
159
 
  public int hashCode() {
160
 
    return hcode + super.hashCode() + (int)defVal;
161
 
  };
162
 
}