svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / impl / DefaultFeatureIndexes.java @ 24017
History | View | Annotate | Download (6.15 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.data.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.data.exceptions.DataException; |
37 |
import org.gvsig.fmap.data.feature.FeatureIndex; |
38 |
import org.gvsig.fmap.data.feature.FeatureIndexes; |
39 |
import org.gvsig.fmap.data.feature.FeatureType; |
40 |
import org.gvsig.fmap.data.feature.spi.FeatureSetProvider; |
41 |
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProviderServices; |
42 |
import org.gvsig.tools.evaluator.Evaluator; |
43 |
import org.gvsig.tools.evaluator.EvaluatorFieldValue; |
44 |
|
45 |
/**
|
46 |
* This class provides access to a FeatureStore local indexes either by
|
47 |
* FeatureType and attrName or by index name.
|
48 |
* @author jyarza
|
49 |
*/
|
50 |
public class DefaultFeatureIndexes implements FeatureIndexes { |
51 |
|
52 |
// Access by FeatureType and attribute name
|
53 |
private Map featureTypes = new HashMap(); |
54 |
|
55 |
// Access by DataIndex name
|
56 |
private Map names = new HashMap(); |
57 |
|
58 |
private DefaultFeatureStore store;
|
59 |
|
60 |
/**
|
61 |
* Creates an empty DataIndexes for the given FeatureStore
|
62 |
*
|
63 |
* @param store
|
64 |
* FeatureStore to whom this belongs
|
65 |
* @throws DataException
|
66 |
*/
|
67 |
public DefaultFeatureIndexes(DefaultFeatureStore store)
|
68 |
throws DataException {
|
69 |
this.store = store;
|
70 |
Iterator it = store.getFeatureTypes().iterator();
|
71 |
while (it.hasNext()) {
|
72 |
FeatureType type = (FeatureType) it.next(); |
73 |
featureTypes.put(type, new HashMap()); |
74 |
} |
75 |
} |
76 |
|
77 |
/**
|
78 |
* @see org.gvsig.fmap.data.index.DataIndexes#getDataIndex(org.gvsig.fmap.data.feature.FeatureType,
|
79 |
* java.lang.String)
|
80 |
* @deprecated
|
81 |
*/
|
82 |
public FeatureIndex getFeatureIndex(FeatureType fType, String attrName) { |
83 |
Map indexes = (Map) featureTypes.get(fType); |
84 |
if (indexes != null) { |
85 |
return (FeatureIndex) (indexes.get(attrName));
|
86 |
} |
87 |
return null; |
88 |
} |
89 |
|
90 |
/* (non-Javadoc)
|
91 |
* @see org.gvsig.fmap.data.index.DataIndexes#getDataIndex(java.lang.String)
|
92 |
*/
|
93 |
public FeatureIndex getFeatureIndex(String name) { |
94 |
return (FeatureIndex) names.get(name);
|
95 |
} |
96 |
|
97 |
/* (non-Javadoc)
|
98 |
* @see org.gvsig.fmap.data.index.DataIndexes#addIndex(org.gvsig.fmap.data.feature.FeatureType, java.lang.String, org.gvsig.fmap.data.feature.DataIndex)
|
99 |
*/
|
100 |
public void addIndex(FeatureIndexProviderServices index) { |
101 |
Map indexes = (Map) featureTypes.get(index.getFeatureType()); |
102 |
if (indexes == null) { |
103 |
// This would mean that a new feature type has been added to the FeatureStore since this Indexes was created
|
104 |
indexes = new HashMap(); |
105 |
featureTypes.put(index.getFeatureType(), indexes); |
106 |
} |
107 |
// FIXME Taking into account only the first attribute
|
108 |
indexes.put(index.getAttributeNames()[0], index);
|
109 |
|
110 |
// By name
|
111 |
names.put(index.getName(), index); |
112 |
} |
113 |
|
114 |
/**
|
115 |
* @see org.gvsig.fmap.data.index.DataIndexes#contains(org.gvsig.fmap.data.feature.FeatureType,
|
116 |
* java.lang.String)
|
117 |
* @deprecated
|
118 |
*/
|
119 |
public boolean contains(FeatureType fType, String attrName) { |
120 |
Map map = (Map) featureTypes.get(fType); |
121 |
return map == null ? false : map.containsKey(attrName); |
122 |
} |
123 |
|
124 |
/**
|
125 |
* @deprecated
|
126 |
*/
|
127 |
public boolean contains(String name) { |
128 |
return names.containsKey(name);
|
129 |
} |
130 |
|
131 |
public Iterator iterator() { |
132 |
return names.values().iterator();
|
133 |
} |
134 |
|
135 |
/**
|
136 |
* Obtain a FeatureSetProvider over an index with the attributes used in the
|
137 |
* evaluator or null if can't apply any index.
|
138 |
*
|
139 |
* @param evaluator
|
140 |
* @return FeatureSetProvider or null if not has index availables.
|
141 |
*
|
142 |
*/
|
143 |
public FeatureSetProvider getFeatureSet(Evaluator evaluator) {
|
144 |
|
145 |
class ApplyIndex { |
146 |
DefaultFeatureIndex index; |
147 |
EvaluatorFieldValue[] data;
|
148 |
|
149 |
ApplyIndex(DefaultFeatureIndex index, EvaluatorFieldValue[] data) {
|
150 |
this.index = index;
|
151 |
this.data = data;
|
152 |
} |
153 |
} |
154 |
try {
|
155 |
List applyIndex = new ArrayList(); |
156 |
Iterator indexes = this.iterator(); |
157 |
while (indexes.hasNext()) {
|
158 |
DefaultFeatureIndex index = (DefaultFeatureIndex) indexes |
159 |
.next(); |
160 |
String[] attrs = index.getAttributeNames(); |
161 |
for (int i = 0; i < attrs.length; i++) { |
162 |
String attrname = attrs[i];
|
163 |
EvaluatorFieldValue[] values = evaluator
|
164 |
.getFieldValues(attrname); |
165 |
if (values != null) { |
166 |
applyIndex.add(new ApplyIndex(index, values));
|
167 |
break;
|
168 |
} |
169 |
} |
170 |
} |
171 |
switch (applyIndex.size()) {
|
172 |
case 0: |
173 |
return null; |
174 |
|
175 |
default:
|
176 |
// FIXME: hay mas de un indice aplicable, que hacer? de momento
|
177 |
// usa el primero.
|
178 |
this.store
|
179 |
.getLogger() |
180 |
.info( |
181 |
"Mas de un indice applica para la consulta. Se escoge arbitrariamente el primero.");
|
182 |
|
183 |
case 1: |
184 |
ApplyIndex x = (ApplyIndex) applyIndex.get(0);
|
185 |
switch (x.data[0].getType()) { |
186 |
case EvaluatorFieldValue.MATCH:
|
187 |
return x.index.getMatchFeatureSet(x.data[0].getValue()); |
188 |
case EvaluatorFieldValue.RANGE:
|
189 |
return x.index.getRangeFeatureSet(x.data[0].getValue1(), |
190 |
x.data[0].getValue2());
|
191 |
case EvaluatorFieldValue.NEAREST:
|
192 |
if (x.data[0].getTolerance() == -1) { |
193 |
return x.index.getNearestFeatureSet(x.data[0] |
194 |
.getCount(), x.data[0].getValue());
|
195 |
} else {
|
196 |
return x.index.getNearestFeatureSet(x.data[0] |
197 |
.getCount(), x.data[0].getTolerance(),
|
198 |
x.data[0].getValue());
|
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
} catch (Exception e) { |
204 |
this.store.getLogger().error("Error buscando indice que aplicar.", |
205 |
e); |
206 |
return null; |
207 |
} |
208 |
|
209 |
return null; |
210 |
} |
211 |
} |