root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureIndex.java @ 29326
History | View | Annotate | Download (7.01 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 |
|
29 |
package org.gvsig.fmap.dal.feature.impl; |
30 |
|
31 |
import java.io.File; |
32 |
import java.util.ArrayList; |
33 |
import java.util.Iterator; |
34 |
import java.util.List; |
35 |
|
36 |
import org.gvsig.fmap.dal.DataTypes; |
37 |
import org.gvsig.fmap.dal.exception.DataException; |
38 |
import org.gvsig.fmap.dal.exception.InitializeException; |
39 |
import org.gvsig.fmap.dal.feature.Feature; |
40 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
43 |
import org.gvsig.fmap.dal.feature.FeatureType; |
44 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
45 |
import org.gvsig.fmap.dal.feature.spi.DefaultLongList; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
47 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
48 |
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider; |
49 |
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices; |
50 |
|
51 |
/**
|
52 |
* Default feature index provider services.
|
53 |
*
|
54 |
* @author jyarza
|
55 |
*
|
56 |
*/
|
57 |
public class DefaultFeatureIndex implements FeatureIndexProviderServices { |
58 |
|
59 |
private final FeatureStoreProviderServices featureStore; |
60 |
private final FeatureType featureType; |
61 |
private final String attributeName; |
62 |
private final String indexName; |
63 |
private final int dataType; |
64 |
private final FeatureIndexProvider indexProvider; |
65 |
private List attributeNames; |
66 |
|
67 |
public DefaultFeatureIndex(FeatureStoreProviderServices featureStore, FeatureType featureType, FeatureIndexProvider indexProvider, String attributeName, String indexName) { |
68 |
if (featureStore == null) { |
69 |
throw new IllegalArgumentException("featureStore cannot be null."); |
70 |
} |
71 |
if (featureType == null) { |
72 |
throw new IllegalArgumentException("featureType cannot be null."); |
73 |
} |
74 |
if (attributeName == null) { |
75 |
throw new IllegalArgumentException("attributeName cannot be null."); |
76 |
} |
77 |
if (indexName == null) { |
78 |
throw new IllegalArgumentException("indexName cannot be null."); |
79 |
} |
80 |
|
81 |
// FIXME Esto debe ir al provider
|
82 |
if (featureStore.getProvider().getOIDType() != DataTypes.INT &&
|
83 |
featureStore.getProvider().getOIDType() != DataTypes.LONG) { |
84 |
throw new IllegalArgumentException(); |
85 |
} |
86 |
|
87 |
FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(attributeName); |
88 |
if (attr == null) { |
89 |
throw new IllegalArgumentException("Attribute " + attributeName +" not found in FeatureType " + featureType.toString()); |
90 |
} |
91 |
|
92 |
this.featureStore = featureStore;
|
93 |
this.featureType = featureType;
|
94 |
this.attributeName = attributeName;
|
95 |
this.indexName = indexName;
|
96 |
this.dataType = attr.getDataType();
|
97 |
this.indexProvider = indexProvider;
|
98 |
|
99 |
attributeNames = new ArrayList(); |
100 |
attributeNames.add(attributeName); |
101 |
} |
102 |
|
103 |
public final FeatureAttributeDescriptor getFeatureAttributeDescriptor() { |
104 |
return featureType.getAttributeDescriptor(attributeName);
|
105 |
} |
106 |
|
107 |
public final FeatureStoreProviderServices getFeatureStoreProviderServices() { |
108 |
return featureStore;
|
109 |
} |
110 |
|
111 |
public final FeatureType getFeatureType() { |
112 |
return featureType;
|
113 |
} |
114 |
|
115 |
public final String getName() { |
116 |
return indexName;
|
117 |
} |
118 |
|
119 |
public final String getAttributeName() { |
120 |
return attributeName;
|
121 |
} |
122 |
|
123 |
public final int getDataType() { |
124 |
return dataType;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Fills this index with all the data in its FeatureStore
|
129 |
*/
|
130 |
public final void fill() throws FeatureIndexException { |
131 |
try {
|
132 |
insert(getFeatureStoreProviderServices().getFeatureStore() |
133 |
.getFeatureSet()); |
134 |
} catch (DataException e) {
|
135 |
throw new FeatureIndexException(e); |
136 |
} |
137 |
} |
138 |
|
139 |
public final void insert(FeatureSet data) throws DataException { |
140 |
Iterator it = data.iterator();
|
141 |
while (it.hasNext()) {
|
142 |
Feature feat = (Feature) it.next(); |
143 |
insert(feat); |
144 |
} |
145 |
} |
146 |
|
147 |
public void insert(Feature feat) { |
148 |
try {
|
149 |
indexProvider.insert(feat.get(attributeName), |
150 |
(FeatureReferenceProviderServices) feat.getReference()); |
151 |
} catch (NullPointerException e) { |
152 |
throw new IllegalArgumentException("Feature does not contain a column with name " + attributeName); |
153 |
} catch (ClassCastException e) { |
154 |
throw new IllegalArgumentException("Attribute data type is not valid."); |
155 |
} |
156 |
} |
157 |
|
158 |
public FeatureSet getMatchFeatureSet(Object value) |
159 |
throws FeatureIndexException {
|
160 |
return new IndexFeatureSet(this, new DefaultLongList(indexProvider |
161 |
.match(value))); |
162 |
} |
163 |
|
164 |
public FeatureSet getRangeFeatureSet(Object value1, Object value2) |
165 |
throws FeatureIndexException {
|
166 |
return new IndexFeatureSet(this, new DefaultLongList(indexProvider |
167 |
.range(value1, value2))); |
168 |
} |
169 |
|
170 |
public FeatureSet getNearestFeatureSet(int count, Object value) |
171 |
throws FeatureIndexException {
|
172 |
return new IndexFeatureSet(this, new DefaultLongList(indexProvider |
173 |
.nearest(count, value))); |
174 |
} |
175 |
|
176 |
public FeatureSet getNearestFeatureSet(int count, Object value, |
177 |
Object tolerance) throws FeatureIndexException { |
178 |
return new IndexFeatureSet(this, new DefaultLongList(indexProvider |
179 |
.nearest(count, value, tolerance))); |
180 |
} |
181 |
|
182 |
public void initialize() throws InitializeException { |
183 |
indexProvider.setFeatureIndexProviderServices(this);
|
184 |
indexProvider.initialize(); |
185 |
} |
186 |
|
187 |
public void delete(Object value, FeatureReference fref) { |
188 |
indexProvider.delete(value, (FeatureReferenceProviderServices) fref); |
189 |
} |
190 |
|
191 |
public void delete(Feature feat) { |
192 |
indexProvider.delete(feat.get(this.attributeName),
|
193 |
(FeatureReferenceProviderServices) feat.getReference()); |
194 |
} |
195 |
|
196 |
public void delete(FeatureSet data) throws FeatureIndexException { |
197 |
try {
|
198 |
Iterator it = data.iterator();
|
199 |
while (it.hasNext()) {
|
200 |
Feature feat = (Feature) it.next(); |
201 |
delete(feat); |
202 |
} |
203 |
} catch (DataException e) {
|
204 |
throw new FeatureIndexException(e); |
205 |
} |
206 |
} |
207 |
|
208 |
public List getAttributeNames() { |
209 |
return attributeNames;
|
210 |
} |
211 |
|
212 |
public String getNewFileName(String prefix, String sufix) { |
213 |
int n=0; |
214 |
File file = new File(prefix + getName(), sufix); |
215 |
while (file.exists()) {
|
216 |
n++; |
217 |
file = new File(prefix + getName() + n, sufix); |
218 |
} |
219 |
return file.getAbsolutePath();
|
220 |
} |
221 |
|
222 |
public String getFileName() { |
223 |
// TODO Auto-generated method stub
|
224 |
return null; |
225 |
} |
226 |
|
227 |
public String getTemporaryFileName() { |
228 |
// TODO Auto-generated method stub
|
229 |
return null; |
230 |
} |
231 |
|
232 |
public FeatureIndexProvider getFeatureIndexProvider() {
|
233 |
return this.indexProvider; |
234 |
} |
235 |
|
236 |
} |
237 |
|