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