Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureIndexes.java @ 28076

History | View | Annotate | Download (6.62 KB)

1 23962 jiyarza
/* gvSIG. Geographic Information System of the Valencian Government
2 25716 jiyarza
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
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
 * as published by the Free Software Foundation; either version 2
9
 * 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
 */
22 23962 jiyarza
23
/*
24 25716 jiyarza
 * AUTHORS (In addition to CIT):
25
 * 2008 {{Company}}   {{Task}}
26
 */
27 23962 jiyarza
28 24496 jmvivo
package org.gvsig.fmap.dal.feature.impl;
29 23962 jiyarza
30 24017 jjdelcerro
import java.util.ArrayList;
31 23962 jiyarza
import java.util.HashMap;
32
import java.util.Iterator;
33 24017 jjdelcerro
import java.util.List;
34 23962 jiyarza
import java.util.Map;
35
36 24505 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
37 24496 jmvivo
import org.gvsig.fmap.dal.feature.FeatureIndex;
38
import org.gvsig.fmap.dal.feature.FeatureIndexes;
39 25747 jiyarza
import org.gvsig.fmap.dal.feature.FeatureSet;
40 24505 jmvivo
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
41 24496 jmvivo
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
42 24017 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
43
import org.gvsig.tools.evaluator.EvaluatorFieldValue;
44 26325 jmvivo
import org.gvsig.tools.evaluator.EvaluatorFieldValueMatch;
45
import org.gvsig.tools.evaluator.EvaluatorFieldValueNearest;
46
import org.gvsig.tools.evaluator.EvaluatorFieldValueRange;
47 23962 jiyarza
48
/**
49 25716 jiyarza
 * 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 26325 jmvivo
 *
52 23962 jiyarza
 * @author jyarza
53 25588 vcaballero
 */
54 23962 jiyarza
public class DefaultFeatureIndexes implements FeatureIndexes {
55
56 24034 jiyarza
        // Access by index name
57 24366 jiyarza
        private Map names;
58
        // Store to which this belongs
59 24017 jjdelcerro
        private DefaultFeatureStore store;
60
61 23962 jiyarza
        /**
62
         * Creates an empty DataIndexes for the given FeatureStore
63 26325 jmvivo
         *
64 23962 jiyarza
         * @param store
65
         *            FeatureStore to whom this belongs
66
         * @throws DataException
67
         */
68 24017 jjdelcerro
        public DefaultFeatureIndexes(DefaultFeatureStore store)
69
                        throws DataException {
70 24366 jiyarza
                names = new HashMap();
71 24017 jjdelcerro
                this.store = store;
72 23962 jiyarza
        }
73
74 25716 jiyarza
        /*
75
         * (non-Javadoc)
76 26325 jmvivo
         *
77 24496 jmvivo
         * @see org.gvsig.fmap.dal.index.DataIndexes#getDataIndex(java.lang.String)
78 23962 jiyarza
         */
79
        public FeatureIndex getFeatureIndex(String name) {
80
                return (FeatureIndex) names.get(name);
81
        }
82
83 25716 jiyarza
        /*
84
         * (non-Javadoc)
85 26325 jmvivo
         *
86 25716 jiyarza
         * @see org.gvsig.fmap.dal.index.DataIndexes#addIndex(org.gvsig.fmap.dal.feature.FeatureType,
87
         *      java.lang.String, org.gvsig.fmap.dal.feature.DataIndex)
88 23962 jiyarza
         */
89
        public void addIndex(FeatureIndexProviderServices index) {
90
                // By name
91
                names.put(index.getName(), index);
92
        }
93
94
        public Iterator iterator() {
95
                return names.values().iterator();
96
        }
97 24017 jjdelcerro
98
        /**
99 25588 vcaballero
         * Using the given evaluator attributes, choose and use an appropriate index
100 25716 jiyarza
         * to obtain a FeatureSet. If no index can be applied, then this method
101
         * returns null
102 28008 jmvivo
         *
103 24017 jjdelcerro
         * @param evaluator
104 28008 jmvivo
         * @return FeatureSet or null if could not find any appropriate index.
105
         * @throws FeatureIndexException
106
         *
107 24017 jjdelcerro
         */
108 28008 jmvivo
        public FeatureSet getFeatureSet(Evaluator evaluator)
109
                        throws FeatureIndexException {
110 24017 jjdelcerro
111
                class ApplyIndex {
112 24111 jiyarza
                        DefaultFeatureIndex index;
113 24017 jjdelcerro
                        EvaluatorFieldValue[] data;
114
115 24111 jiyarza
                        ApplyIndex(DefaultFeatureIndex index, EvaluatorFieldValue[] data) {
116 24017 jjdelcerro
                                this.index = index;
117
                                this.data = data;
118
                        }
119 25588 vcaballero
120 24202 jiyarza
                        /**
121
                         * Checks whether the index supports the evaluator request
122 26325 jmvivo
                         *
123 24202 jiyarza
                         * @return
124
                         */
125
                        boolean isSupported() {
126 25716 jiyarza
                                switch (data[0].getType()) {
127 24202 jiyarza
                                case EvaluatorFieldValue.MATCH:
128
                                        return index.getFeatureIndexProvider().isMatchSupported();
129
                                case EvaluatorFieldValue.NEAREST:
130
                                        return index.getFeatureIndexProvider().isNearestSupported();
131
                                case EvaluatorFieldValue.RANGE:
132
                                        return index.getFeatureIndexProvider().isRangeSupported();
133
                                default:
134
                                        return false;
135
                                }
136
                        }
137 25588 vcaballero
138 24202 jiyarza
                        /**
139
                         * Applies the index using the evaluator fields
140 26325 jmvivo
                         *
141 25716 jiyarza
                         * @return FeatureSet with the result
142 24202 jiyarza
                         * @throws FeatureIndexException
143
                         */
144 25747 jiyarza
                        IndexFeatureSet apply() throws FeatureIndexException {
145 26325 jmvivo
146
                                EvaluatorFieldValueRange rangeField;
147
                                EvaluatorFieldValueNearest nearestField;
148
                                // Trick: we know DefaultIndexProvider returns an IndexFeatureSet,
149 25716 jiyarza
                                // which implements both FeatureSetProvider and FeatureSet.
150 24202 jiyarza
                                switch (data[0].getType()) {
151
                                case EvaluatorFieldValue.MATCH:
152 25747 jiyarza
                                        return (IndexFeatureSet) index.getMatchFeatureSet(
153 26325 jmvivo
                                                        ((EvaluatorFieldValueMatch) data[0])
154
                                                                        .getValue());
155 24202 jiyarza
                                case EvaluatorFieldValue.RANGE:
156 26325 jmvivo
                                        rangeField = (EvaluatorFieldValueRange) data[0];
157 25747 jiyarza
                                        return (IndexFeatureSet) index.getRangeFeatureSet(
158 26325 jmvivo
                                                        rangeField.getValue1(), rangeField.getValue2());
159 24202 jiyarza
                                case EvaluatorFieldValue.NEAREST:
160 26325 jmvivo
                                        nearestField = (EvaluatorFieldValueNearest) data[0];
161
                                        if ((nearestField.getTolerance() == null)
162
                                                        || (!isSupported())) {
163
                                                return (IndexFeatureSet) index.getNearestFeatureSet(
164
                                                                nearestField.getCount(), nearestField
165
                                                                                .getValue());
166 24202 jiyarza
                                        } else {
167 26325 jmvivo
                                                return (IndexFeatureSet) index.getNearestFeatureSet(
168
                                                                nearestField.getCount(), nearestField
169
                                                                                .getValue(), nearestField
170
                                                                                .getTolerance());
171 24202 jiyarza
                                        }
172
                                }
173
                                return null;
174
                        }
175 24017 jjdelcerro
                }
176 25588 vcaballero
177 24202 jiyarza
                // Select applicable indexes
178 28008 jmvivo
                List applyIndexes = new ArrayList();
179
                Iterator indexes = this.iterator();
180
                while (indexes.hasNext()) {
181
                        DefaultFeatureIndex index = (DefaultFeatureIndex) indexes.next();
182
                        String[] attrs = (String[]) index.getAttributeNames().toArray(
183
                                        new String[0]);
184
                        for (int i = 0; i < attrs.length; i++) {
185
                                String attrname = attrs[i];
186
                                EvaluatorFieldValue[] values = null;
187
                                if (evaluator.getFieldsInfo() != null) {
188
                                        values = evaluator.getFieldsInfo().getFieldValues(attrname);
189 24017 jjdelcerro
                                }
190 28008 jmvivo
                                if (values != null) {
191
                                        applyIndexes.add(new ApplyIndex(index, values));
192
                                        break;
193
                                }
194 24017 jjdelcerro
                        }
195 28008 jmvivo
                }
196 25588 vcaballero
197 28008 jmvivo
                // If there's not any applicable index, return null
198
                if (applyIndexes.size() == 0) {
199
                        return null;
200
                }
201 25588 vcaballero
202 28008 jmvivo
                // Lookup an index with support for the requested function
203
                Iterator it = applyIndexes.iterator();
204
                ApplyIndex index = (ApplyIndex) it.next();
205
                while (it.hasNext() && (!index.isSupported())) {
206
                        index = (ApplyIndex) it.next();
207
                }
208 25588 vcaballero
209 28008 jmvivo
                // If there is not any any index supporting the function, use the
210
                // first one
211
                if (!index.isSupported()) {
212
                        this.store
213
                                        .getLogger()
214
                                        .info(
215
                                                        "No index support for the evaluator values. Using default index.");
216
                        index = (ApplyIndex) applyIndexes.get(0);
217
                }
218 25588 vcaballero
219 28008 jmvivo
                // Apply index
220
                return index.apply();
221 25588 vcaballero
222 24017 jjdelcerro
        }
223 23962 jiyarza
}