svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DefaultFeatureIndexes.java @ 46030
History | View | Annotate | Download (8.78 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | |||
25 | package org.gvsig.fmap.dal.feature.impl; |
||
26 | |||
27 | import java.util.ArrayList; |
||
28 | import java.util.HashMap; |
||
29 | import java.util.Iterator; |
||
30 | import java.util.List; |
||
31 | import java.util.Map; |
||
32 | |||
33 | import org.slf4j.Logger; |
||
34 | import org.slf4j.LoggerFactory; |
||
35 | |||
36 | import org.gvsig.fmap.dal.exception.DataException; |
||
37 | import org.gvsig.fmap.dal.feature.FeatureIndex; |
||
38 | import org.gvsig.fmap.dal.feature.FeatureIndexes; |
||
39 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
40 | import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
||
41 | import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices; |
||
42 | import org.gvsig.tools.evaluator.Evaluator; |
||
43 | import org.gvsig.tools.evaluator.EvaluatorFieldValue; |
||
44 | import org.gvsig.tools.evaluator.EvaluatorFieldValueMatch; |
||
45 | import org.gvsig.tools.evaluator.EvaluatorFieldValueNearest; |
||
46 | import org.gvsig.tools.evaluator.EvaluatorFieldValueRange; |
||
47 | |||
48 | /**
|
||
49 | * This class provides access to a FeatureStore local indexes and also decides
|
||
50 | * which index to use given an evaluator containing the filter expression.
|
||
51 | *
|
||
52 | * @author jyarza
|
||
53 | */
|
||
54 | public class DefaultFeatureIndexes implements FeatureIndexes { |
||
55 | |||
56 | private static final Logger LOG = LoggerFactory |
||
57 | .getLogger(DefaultFeatureIndexes.class); |
||
58 | // Access by index name
|
||
59 | private Map names; |
||
60 | // Store to which this belongs
|
||
61 | private DefaultFeatureStore store;
|
||
62 | |||
63 | /**
|
||
64 | * Creates an empty DataIndexes for the given FeatureStore
|
||
65 | *
|
||
66 | * @param store
|
||
67 | * FeatureStore to whom this belongs
|
||
68 | * @throws DataException
|
||
69 | */
|
||
70 | public DefaultFeatureIndexes(DefaultFeatureStore store)
|
||
71 | throws DataException {
|
||
72 | names = new HashMap(); |
||
73 | this.store = store;
|
||
74 | } |
||
75 | |||
76 | /*
|
||
77 | * (non-Javadoc)
|
||
78 | *
|
||
79 | * @see org.gvsig.fmap.dal.index.DataIndexes#getDataIndex(java.lang.String)
|
||
80 | */
|
||
81 | public FeatureIndex getFeatureIndex(String name) { |
||
82 | return (FeatureIndex) names.get(name);
|
||
83 | } |
||
84 | |||
85 | /*
|
||
86 | * (non-Javadoc)
|
||
87 | *
|
||
88 | * @see
|
||
89 | * org.gvsig.fmap.dal.index.DataIndexes#addIndex(org.gvsig.fmap.dal.feature
|
||
90 | * .FeatureType,
|
||
91 | * java.lang.String, org.gvsig.fmap.dal.feature.DataIndex)
|
||
92 | */
|
||
93 | public void addIndex(FeatureIndexProviderServices index) { |
||
94 | // By name
|
||
95 | names.put(index.getName(), index); |
||
96 | } |
||
97 | |||
98 | public Iterator iterator() { |
||
99 | return names.values().iterator();
|
||
100 | } |
||
101 | |||
102 | /**
|
||
103 | * Using the given evaluator attributes, choose and use an appropriate index
|
||
104 | * to obtain a FeatureSet. If no index can be applied, then this method
|
||
105 | * returns null
|
||
106 | *
|
||
107 | * @param evaluator
|
||
108 | * @return FeatureSet or null if could not find any appropriate index.
|
||
109 | * @throws FeatureIndexException
|
||
110 | *
|
||
111 | */
|
||
112 | public FeatureSet getFeatureSet(Evaluator evaluator)
|
||
113 | throws FeatureIndexException {
|
||
114 | |||
115 | class ApplyIndex { |
||
116 | |||
117 | DefaultFeatureIndex index; |
||
118 | EvaluatorFieldValue[] data;
|
||
119 | |||
120 | ApplyIndex(DefaultFeatureIndex index, EvaluatorFieldValue[] data) {
|
||
121 | this.index = index;
|
||
122 | this.data = data;
|
||
123 | } |
||
124 | |||
125 | /**
|
||
126 | * Checks whether the index supports the evaluator request
|
||
127 | *
|
||
128 | * @return
|
||
129 | */
|
||
130 | boolean isSupported() {
|
||
131 | switch (data[0].getType()) { |
||
132 | case EvaluatorFieldValue.MATCH:
|
||
133 | return index.getFeatureIndexProvider().isMatchSupported();
|
||
134 | case EvaluatorFieldValue.NEAREST:
|
||
135 | return index.getFeatureIndexProvider().isNearestSupported();
|
||
136 | case EvaluatorFieldValue.RANGE:
|
||
137 | return index.getFeatureIndexProvider().isRangeSupported();
|
||
138 | default:
|
||
139 | return false; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /**
|
||
144 | * Applies the index using the evaluator fields
|
||
145 | *
|
||
146 | * @return FeatureSet with the result
|
||
147 | * @throws FeatureIndexException
|
||
148 | */
|
||
149 | IndexFeatureSet apply() throws FeatureIndexException {
|
||
150 | |||
151 | EvaluatorFieldValueRange rangeField; |
||
152 | EvaluatorFieldValueNearest nearestField; |
||
153 | // Trick: we know DefaultIndexProvider returns an
|
||
154 | // IndexFeatureSet,
|
||
155 | // which implements both FeatureSetProvider and FeatureSet.
|
||
156 | switch (data[0].getType()) { |
||
157 | case EvaluatorFieldValue.MATCH:
|
||
158 | return (IndexFeatureSet) index
|
||
159 | .getMatchFeatureSet(((EvaluatorFieldValueMatch) data[0])
|
||
160 | .getValue()); |
||
161 | case EvaluatorFieldValue.RANGE:
|
||
162 | rangeField = (EvaluatorFieldValueRange) data[0];
|
||
163 | return (IndexFeatureSet) index.getRangeFeatureSet(
|
||
164 | rangeField.getValue1(), rangeField.getValue2()); |
||
165 | case EvaluatorFieldValue.NEAREST:
|
||
166 | nearestField = (EvaluatorFieldValueNearest) data[0];
|
||
167 | if ((nearestField.getTolerance() == null) |
||
168 | || (!isSupported())) { |
||
169 | return (IndexFeatureSet) index.getNearestFeatureSet(
|
||
170 | nearestField.getCount(), nearestField.getValue()); |
||
171 | } else {
|
||
172 | return (IndexFeatureSet) index.getNearestFeatureSet(
|
||
173 | nearestField.getCount(), nearestField.getValue(), |
||
174 | nearestField.getTolerance()); |
||
175 | } |
||
176 | } |
||
177 | return null; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | // Select applicable indexes
|
||
182 | List applyIndexes = new ArrayList(); |
||
183 | Iterator indexes = this.iterator(); |
||
184 | while (indexes.hasNext()) {
|
||
185 | DefaultFeatureIndex index = (DefaultFeatureIndex) indexes.next(); |
||
186 | if (index.isValid()) {
|
||
187 | String[] attrs = |
||
188 | (String[]) index.getAttributeNames().toArray(new String[0]); |
||
189 | for (int i = 0; i < attrs.length; i++) { |
||
190 | String attrname = attrs[i];
|
||
191 | EvaluatorFieldValue[] values = null; |
||
192 | if (evaluator.getFieldsInfo() != null) { |
||
193 | values = |
||
194 | evaluator.getFieldsInfo().getFieldValues(attrname); |
||
195 | } |
||
196 | if (values != null) { |
||
197 | applyIndexes.add(new ApplyIndex(index, values));
|
||
198 | break;
|
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | // If there's not any applicable index, return null
|
||
205 | if (applyIndexes.size() == 0) { |
||
206 | return null; |
||
207 | } |
||
208 | |||
209 | // Lookup an index with support for the requested function
|
||
210 | Iterator it = applyIndexes.iterator();
|
||
211 | ApplyIndex index = (ApplyIndex) it.next(); |
||
212 | while (it.hasNext() && (!index.isSupported())) {
|
||
213 | index = (ApplyIndex) it.next(); |
||
214 | } |
||
215 | |||
216 | // If there is not any any index supporting the function, use the
|
||
217 | // first one
|
||
218 | if (!index.isSupported()) {
|
||
219 | LOG.info("No index support for the evaluator values. Using default index.");
|
||
220 | index = (ApplyIndex) applyIndexes.get(0);
|
||
221 | } |
||
222 | |||
223 | // Apply index
|
||
224 | return index.apply();
|
||
225 | |||
226 | } |
||
227 | |||
228 | public boolean areValid() { |
||
229 | Iterator indexes = this.iterator(); |
||
230 | while (indexes.hasNext()) {
|
||
231 | FeatureIndex index = (FeatureIndex) indexes.next(); |
||
232 | if (!index.isValid()) {
|
||
233 | return false; |
||
234 | } |
||
235 | } |
||
236 | return true; |
||
237 | } |
||
238 | |||
239 | public String toString() { |
||
240 | StringBuffer text = new StringBuffer(super.toString()); |
||
241 | text.append(": [");
|
||
242 | Iterator indexes = this.iterator(); |
||
243 | while (indexes.hasNext()) {
|
||
244 | FeatureIndex index = (FeatureIndex) indexes.next(); |
||
245 | text.append(index); |
||
246 | text.append("]");
|
||
247 | if (indexes.hasNext()) {
|
||
248 | text.append(", ");
|
||
249 | } |
||
250 | } |
||
251 | return text.toString();
|
||
252 | } |
||
253 | } |