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
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {{Company}}   {{Task}}
26
 */
27

    
28
package org.gvsig.fmap.dal.feature.impl;
29

    
30
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Map;
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
        // Access by index name
57
        private Map names;
58
        // Store to which this belongs
59
        private DefaultFeatureStore store;
60

    
61
        /**
62
         * Creates an empty DataIndexes for the given FeatureStore
63
         *
64
         * @param store
65
         *            FeatureStore to whom this belongs
66
         * @throws DataException
67
         */
68
        public DefaultFeatureIndexes(DefaultFeatureStore store)
69
                        throws DataException {
70
                names = new HashMap();
71
                this.store = store;
72
        }
73

    
74
        /*
75
         * (non-Javadoc)
76
         *
77
         * @see org.gvsig.fmap.dal.index.DataIndexes#getDataIndex(java.lang.String)
78
         */
79
        public FeatureIndex getFeatureIndex(String name) {
80
                return (FeatureIndex) names.get(name);
81
        }
82

    
83
        /*
84
         * (non-Javadoc)
85
         *
86
         * @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
         */
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

    
98
        /**
99
         * Using the given evaluator attributes, choose and use an appropriate index
100
         * to obtain a FeatureSet. If no index can be applied, then this method
101
         * returns null
102
         * 
103
         * @param evaluator
104
         * @return FeatureSet or null if could not find any appropriate index.
105
         * @throws FeatureIndexException
106
         * 
107
         */
108
        public FeatureSet getFeatureSet(Evaluator evaluator)
109
                        throws FeatureIndexException {
110

    
111
                class ApplyIndex {
112
                        DefaultFeatureIndex index;
113
                        EvaluatorFieldValue[] data;
114

    
115
                        ApplyIndex(DefaultFeatureIndex index, EvaluatorFieldValue[] data) {
116
                                this.index = index;
117
                                this.data = data;
118
                        }
119

    
120
                        /**
121
                         * Checks whether the index supports the evaluator request
122
                         *
123
                         * @return
124
                         */
125
                        boolean isSupported() {
126
                                switch (data[0].getType()) {
127
                                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

    
138
                        /**
139
                         * Applies the index using the evaluator fields
140
                         *
141
                         * @return FeatureSet with the result
142
                         * @throws FeatureIndexException
143
                         */
144
                        IndexFeatureSet apply() throws FeatureIndexException {
145

    
146
                                EvaluatorFieldValueRange rangeField;
147
                                EvaluatorFieldValueNearest nearestField;
148
                                // Trick: we know DefaultIndexProvider returns an IndexFeatureSet,
149
                                // which implements both FeatureSetProvider and FeatureSet.
150
                                switch (data[0].getType()) {
151
                                case EvaluatorFieldValue.MATCH:
152
                                        return (IndexFeatureSet) index.getMatchFeatureSet(
153
                                                        ((EvaluatorFieldValueMatch) data[0])
154
                                                                        .getValue());
155
                                case EvaluatorFieldValue.RANGE:
156
                                        rangeField = (EvaluatorFieldValueRange) data[0];
157
                                        return (IndexFeatureSet) index.getRangeFeatureSet(
158
                                                        rangeField.getValue1(), rangeField.getValue2());
159
                                case EvaluatorFieldValue.NEAREST:
160
                                        nearestField = (EvaluatorFieldValueNearest) data[0];
161
                                        if ((nearestField.getTolerance() == null)
162
                                                        || (!isSupported())) {
163
                                                return (IndexFeatureSet) index.getNearestFeatureSet(
164
                                                                nearestField.getCount(), nearestField
165
                                                                                .getValue());
166
                                        } else {
167
                                                return (IndexFeatureSet) index.getNearestFeatureSet(
168
                                                                nearestField.getCount(), nearestField
169
                                                                                .getValue(), nearestField
170
                                                                                .getTolerance());
171
                                        }
172
                                }
173
                                return null;
174
                        }
175
                }
176

    
177
                // Select applicable indexes
178
                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
                                }
190
                                if (values != null) {
191
                                        applyIndexes.add(new ApplyIndex(index, values));
192
                                        break;
193
                                }
194
                        }
195
                }
196

    
197
                // If there's not any applicable index, return null
198
                if (applyIndexes.size() == 0) {
199
                        return null;
200
                }
201

    
202
                // 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

    
209
                // 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

    
219
                // Apply index
220
                return index.apply();
221

    
222
        }
223
}