1 /** Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.apache.lucene.spatial.tier;
20 import java.util.WeakHashMap;
21 import java.util.HashMap;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
25 import org.apache.lucene.search.Filter;
26 import org.apache.lucene.spatial.tier.DistanceHandler.Precision;
29 * <p><font color="red"><b>NOTE:</b> This API is still in
30 * flux and might change in incompatible ways in the next
33 public abstract class DistanceFilter extends Filter {
35 final protected Filter startingFilter;
36 protected Precision precise;
37 protected Map<Integer,Double> distances;
38 protected double distance;
40 protected int nextDocBase;
41 protected transient WeakHashMap<String,Double> distanceLookupCache;
43 /** Filters the startingFilter by precise distance
45 public DistanceFilter(Filter startingFilter, double distance) {
46 if (startingFilter == null) {
47 throw new IllegalArgumentException("please provide a non-null startingFilter; you can use QueryWrapperFilter(MatchAllDocsQuery) as a no-op filter");
49 this.startingFilter = startingFilter;
50 this.distance = distance;
52 // NOTE: neither of the distance filters use precision
53 // now - if we turn that on, we'll need to pass top
55 // setPrecision(reader.maxDoc());
57 /* store calculated distances for reuse by other components */
58 distances = new HashMap<Integer,Double>();
60 // create an intermediate cache to avoid recomputing
61 // distances for the same point
62 // TODO: Why is this a WeakHashMap?
63 distanceLookupCache = new WeakHashMap<String,Double>();
66 /** needed for deserialization, because the cache is transient */
67 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
68 stream.defaultReadObject();
69 distanceLookupCache = new WeakHashMap<String,Double>();
72 public Map<Integer,Double> getDistances(){
76 public Double getDistance(int docid){
77 return distances.get(docid);
80 public void setDistances(Map<Integer, Double> distances) {
81 this.distances = distances;
84 /** You must call this before re-using this DistanceFilter
90 /** Returns true if <code>o</code> is equal to this. */
92 public abstract boolean equals(Object o);
94 /** Returns a hash code value for this object.*/
96 public abstract int hashCode();
99 private void setPrecision(int maxDocs) {
100 precise = Precision.EXACT;
102 if (maxDocs > 1000 && distance > 10) {
103 precise = Precision.TWENTYFEET;
106 if (maxDocs > 10000 && distance > 10){
107 precise = Precision.TWOHUNDREDFEET;