Revision 24034

View differences:

branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/impl/DefaultFeatureIndex.java
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.data.feature.impl;
30

  
31
import java.util.Iterator;
32
import java.util.List;
33

  
34
import org.gvsig.fmap.data.DataTypes;
35
import org.gvsig.fmap.data.exceptions.DataException;
36
import org.gvsig.fmap.data.feature.Feature;
37
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.data.feature.FeatureReference;
39
import org.gvsig.fmap.data.feature.FeatureSet;
40
import org.gvsig.fmap.data.feature.FeatureType;
41
import org.gvsig.fmap.data.feature.exceptions.DataIndexException;
42
import org.gvsig.fmap.data.feature.spi.DefaultLongList;
43
import org.gvsig.fmap.data.feature.spi.FeatureSetProvider;
44
import org.gvsig.fmap.data.feature.spi.FeatureStoreProviderServices;
45
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProvider;
46
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProviderServices;
47
import org.gvsig.tools.exception.NotYetImplemented;
48

  
49
/**
50
 * Abstract index provider.
51
 *
52
 * @author jyarza
53
 *
54
 */
55
public class DefaultFeatureIndex implements FeatureIndexProviderServices {
56

  
57
	private final FeatureStoreProviderServices featureStore;
58
	private final FeatureType featureType;
59
	private final String attributeName;
60
	private final String name;
61
	private final int dataType;
62
	private final FeatureIndexProvider indexProvider;
63

  
64
	public DefaultFeatureIndex(FeatureStoreProviderServices featureStore, FeatureType featureType, FeatureIndexProvider indexProvider, String attributeName, String indexName) {
65
		if (featureStore == null) {
66
			throw new IllegalArgumentException("featureStore cannot be null.");
67
		}
68
		if (featureType == null) {
69
			throw new IllegalArgumentException("featureType cannot be null.");
70
		}
71
		if (attributeName == null) {
72
			throw new IllegalArgumentException("attributeName cannot be null.");
73
		}
74
		if (indexName == null) {
75
			throw new IllegalArgumentException("indexName cannot be null.");
76
		}
77

  
78
		if (featureStore.getProvider().getFeatureReferenceIdType() != DataTypes.INT) {
79
			throw new IllegalArgumentException();
80
		}
81

  
82
		FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(attributeName);
83
		if (attr == null) {
84
			throw new IllegalArgumentException("Attribute " + attributeName +" not found in FeatureType " + featureType.toString());
85
		}
86

  
87
		this.featureStore = featureStore;
88
		this.featureType = featureType;
89
		this.attributeName = attributeName;
90
		this.name = indexName;
91
		this.dataType = attr.getDataType();
92
		this.indexProvider = indexProvider;
93
	}
94

  
95
	public final FeatureAttributeDescriptor getFeatureAttributeDescriptor() {
96
		return featureType.getAttributeDescriptor(attributeName);
97
	}
98

  
99
	public final FeatureStoreProviderServices getFeatureStore() {
100
		return featureStore;
101
	}
102

  
103
	public final FeatureType getFeatureType() {
104
		return featureType;
105
	}
106

  
107
	public final String getName() {
108
		return name;
109
	}
110

  
111
	public final String getAttributeName() {
112
		return attributeName;
113
	}
114

  
115
	public final int getDataType() {
116
		return dataType;
117
	}
118

  
119
	/**
120
	 * Fills this index with all the data in its FeatureStore
121
	 */
122
	public final void fill() throws DataIndexException {
123
		try {
124
			insert(getFeatureStore().getFeatureSet());
125
		} catch (DataException e) {
126
			throw new DataIndexException(e);
127
		}
128
	}
129

  
130
	public final void insert(FeatureSet data) throws DataException {
131
		Iterator it = data.iterator();
132
		while (it.hasNext()) {
133
			Feature feat = (Feature) it.next();
134
			insert(feat);
135
		}
136
	}
137

  
138
	public void insert(Feature feat) {
139
		try {
140
			indexProvider.insert(feat.get(attributeName), feat.getReference());
141
		} catch (NullPointerException e) {
142
			throw new IllegalArgumentException("Feature does not contain a column with name " + attributeName);
143
		} catch (ClassCastException e) {
144
			throw new IllegalArgumentException("Attribute data type is not valid.");
145
		}
146
	}
147

  
148
	public FeatureSetProvider getMatchFeatureSet(Object value)
149
			throws DataIndexException {
150
		return new IndexFeatureSet(this, new DefaultLongList(indexProvider
151
				.match(value)));
152
	}
153

  
154
	public FeatureSetProvider getRangeFeatureSet(Object value1, Object value2)
155
			throws DataIndexException {
156
		return new IndexFeatureSet(this, new DefaultLongList(indexProvider
157
				.match(value1, value2)));
158
	}
159

  
160
	public FeatureSetProvider getNearestFeatureSet(int count, Object value)
161
			throws DataIndexException {
162
		return new IndexFeatureSet(this, new DefaultLongList(indexProvider
163
				.nearest(count, value)));
164
	}
165

  
166
	public FeatureSetProvider getNearestFeatureSet(int count, int tolerance,
167
			Object value) throws DataIndexException {
168
		throw new NotYetImplemented();
169
	}
170

  
171
	/**
172
	 * Performs a search in the index. The type of search depends on the passed
173
	 * data
174
	 *
175
	 * @deprecated
176
	 * */
177
	public FeatureSetProvider getFeatureSet(String attrName, Object[] data) throws DataIndexException {
178
		return new IndexFeatureSet(this, new DefaultLongList(this.match(data)));
179
	}
180

  
181
	/**
182
	 * Performs a search in the index and returns a list with the elements that
183
	 * match the parameters
184
	 *
185
	 * @deprecated
186
	 */
187
	private List match(Object[] values) throws DataIndexException {
188

  
189
		if (values == null) {
190
			throw new IllegalArgumentException("values can't be null.");
191
		}
192
		if (values.length == 0) {
193
			throw new IllegalArgumentException("values must contain at least 1 value.");
194
		}
195

  
196
		List result = null;
197
		if (values.length == 1) {
198
			result = indexProvider.match(values[0]);
199
		}
200
		if (values.length == 2) {
201
			if (values[0] instanceof Integer && !(values[1] instanceof Integer)) {
202
				result = indexProvider.nearest(((Integer)values[0]).intValue(), values[1]);
203
			} else {
204
				result = indexProvider.match(values[0], values[1]);
205
			}
206
		}
207
		return result;
208
	}
209

  
210

  
211
	public void initialize() {
212
		indexProvider.initialize();
213
	}
214

  
215
	public void delete(Object o, FeatureReference fref) {
216
		indexProvider.delete(o, fref);
217
	}
218

  
219
	public void delete(Feature feat) {
220
		// TODO Auto-generated method stub
221
	}
222

  
223
	public void delete(FeatureSet data) throws DataIndexException {
224
		// TODO Auto-generated method stub
225

  
226
	}
227

  
228
	public String[] getAttributeNames() {
229
		// TODO Auto-generated method stub
230
		return null;
231
	}
232

  
233
	/**
234
	 * FIXME ver que hacemos con estas constantes
235
	 * Convenience constants to identify sets of providers classified by supported data types.
236
	 */
237
	public interface PROVIDERS {
238

  
239
		public interface GEOMETRY {
240
			public static final String GT2_QUADTREE = "QuadTreeGt2Factory";
241
			public static final String JSI_RTREE = "RTreeJsiFactory";
242
			public static final String JTS_QUADTREE = "QuadTreeJtsFactory";
243
			public static final String SPATIALINDEX_RTREE = "RTreeSptLibFactory";
244
			public static final String DEFAULT = GT2_QUADTREE;
245
		}
246

  
247
		public interface DATE {
248
			// added as example
249
		}
250

  
251
		public interface INTEGER {
252
			// added as example
253
		}
254
	}
255

  
256

  
257
}
258

  
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/impl/DefaultFeatureIndexProviderServices.java
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.data.feature.impl;
30

  
31
import java.io.File;
32
import java.util.Iterator;
33
import java.util.List;
34

  
35
import org.gvsig.fmap.data.DataTypes;
36
import org.gvsig.fmap.data.exceptions.DataException;
37
import org.gvsig.fmap.data.exceptions.InitializeException;
38
import org.gvsig.fmap.data.feature.Feature;
39
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
40
import org.gvsig.fmap.data.feature.FeatureReference;
41
import org.gvsig.fmap.data.feature.FeatureSet;
42
import org.gvsig.fmap.data.feature.FeatureType;
43
import org.gvsig.fmap.data.feature.exceptions.DataIndexException;
44
import org.gvsig.fmap.data.feature.spi.DefaultLongList;
45
import org.gvsig.fmap.data.feature.spi.FeatureSetProvider;
46
import org.gvsig.fmap.data.feature.spi.FeatureStoreProviderServices;
47
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProvider;
48
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProviderServices;
49

  
50
/**
51
 * Default feature index provider services.
52
 *
53
 * @author jyarza
54
 *
55
 */
56
public class DefaultFeatureIndexProviderServices implements FeatureIndexProviderServices {
57

  
58
	private final FeatureStoreProviderServices featureStore;
59
	private final FeatureType featureType;
60
	private final String attributeName;
61
	private final String indexName;
62
	private final int dataType;
63
	private final FeatureIndexProvider indexProvider;
64

  
65
	public DefaultFeatureIndexProviderServices(FeatureStoreProviderServices featureStore, FeatureType featureType, FeatureIndexProvider indexProvider, String attributeName, String indexName) {
66
		if (featureStore == null) {
67
			throw new IllegalArgumentException("featureStore cannot be null.");
68
		}
69
		if (featureType == null) {
70
			throw new IllegalArgumentException("featureType cannot be null.");
71
		}
72
		if (attributeName == null) {
73
			throw new IllegalArgumentException("attributeName cannot be null.");
74
		}
75
		if (indexName == null) {
76
			throw new IllegalArgumentException("indexName cannot be null.");
77
		}
78

  
79
		if (featureStore.getProvider().getFeatureReferenceIdType() != DataTypes.INT) {
80
			throw new IllegalArgumentException();
81
		}
82

  
83
		FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(attributeName);
84
		if (attr == null) {
85
			throw new IllegalArgumentException("Attribute " + attributeName +" not found in FeatureType " + featureType.toString());
86
		}
87

  
88
		this.featureStore = featureStore;
89
		this.featureType = featureType;
90
		this.attributeName = attributeName;
91
		this.indexName = indexName;
92
		this.dataType = attr.getDataType();
93
		this.indexProvider = indexProvider;
94
	}
95

  
96
	public final FeatureAttributeDescriptor getFeatureAttributeDescriptor() {
97
		return featureType.getAttributeDescriptor(attributeName);
98
	}
99

  
100
	public final FeatureStoreProviderServices getFeatureStore() {
101
		return featureStore;
102
	}
103

  
104
	public final FeatureType getFeatureType() {
105
		return featureType;
106
	}
107

  
108
	public final String getName() {
109
		return indexName;
110
	}
111

  
112
	public final String getAttributeName() {
113
		return attributeName;
114
	}
115

  
116
	public final int getDataType() {
117
		return dataType;
118
	}
119

  
120
	/**
121
	 * Fills this index with all the data in its FeatureStore
122
	 */
123
	public final void fill() throws DataIndexException {
124
		try {
125
			insert(getFeatureStore().getFeatureSet());
126
		} catch (DataException e) {
127
			throw new DataIndexException(e);
128
		}
129
	}
130

  
131
	public final void insert(FeatureSet data) throws DataException {
132
		Iterator it = data.iterator();
133
		while (it.hasNext()) {
134
			Feature feat = (Feature) it.next();
135
			insert(feat);
136
		}
137
	}
138

  
139
	public void insert(Feature feat) {
140
		try {
141
			indexProvider.insert(feat.get(attributeName), feat.getReference());
142
		} catch (NullPointerException e) {
143
			throw new IllegalArgumentException("Feature does not contain a column with name " + attributeName);
144
		} catch (ClassCastException e) {
145
			throw new IllegalArgumentException("Attribute data type is not valid.");
146
		}
147
	}
148

  
149
	public FeatureSetProvider getMatchFeatureSet(Object value)
150
			throws DataIndexException {
151
		return new IndexFeatureSet(this, new DefaultLongList(indexProvider
152
				.match(value)));
153
	}
154

  
155
	public FeatureSetProvider getRangeFeatureSet(Object value1, Object value2)
156
			throws DataIndexException {
157
		return new IndexFeatureSet(this, new DefaultLongList(indexProvider
158
				.match(value1, value2)));
159
	}
160

  
161
	public FeatureSetProvider getNearestFeatureSet(int count, Object value)
162
			throws DataIndexException {
163
		return new IndexFeatureSet(this, new DefaultLongList(indexProvider
164
				.nearest(count, value)));
165
	}
166

  
167
	public FeatureSetProvider getNearestFeatureSet(int count, int tolerance,
168
			Object value) throws DataIndexException {
169
		throw new UnsupportedOperationException();
170
	}
171

  
172
	/**
173
	 * Performs a search in the index. The type of search depends on the passed
174
	 * data
175
	 *
176
	 * @deprecated
177
	 * */
178
	public FeatureSetProvider getFeatureSet(String attrName, Object[] data) throws DataIndexException {
179
		return new IndexFeatureSet(this, new DefaultLongList(this.match(data)));
180
	}
181

  
182
	/**
183
	 * Performs a search in the index and returns a list with the elements that
184
	 * match the parameters
185
	 *
186
	 * @deprecated
187
	 */
188
	private List match(Object[] values) throws DataIndexException {
189

  
190
		if (values == null) {
191
			throw new IllegalArgumentException("values can't be null.");
192
		}
193
		if (values.length == 0) {
194
			throw new IllegalArgumentException("values must contain at least 1 value.");
195
		}
196

  
197
		List result = null;
198
		if (values.length == 1) {
199
			result = indexProvider.match(values[0]);
200
		}
201
		if (values.length == 2) {
202
			if (values[0] instanceof Integer && !(values[1] instanceof Integer)) {
203
				result = indexProvider.nearest(((Integer)values[0]).intValue(), values[1]);
204
			} else {
205
				result = indexProvider.match(values[0], values[1]);
206
			}
207
		}
208
		return result;
209
	}
210

  
211

  
212
	public void initialize() throws InitializeException {
213
		indexProvider.setFeatureIndexProviderServices(this);
214
		indexProvider.initialize();
215
	}
216

  
217
	public void delete(Object value, FeatureReference fref) {
218
		indexProvider.delete(value, fref);
219
	}
220

  
221
	public void delete(Feature feat) {
222
		indexProvider.delete(feat.get(this.attributeName), feat.getReference());
223
	}
224

  
225
	public void delete(FeatureSet data) throws DataIndexException {
226
		try {
227
			Iterator it = data.iterator();
228
			while (it.hasNext()) {
229
				Feature feat = (Feature) it.next();
230
				delete(feat);
231
			}
232
		} catch (DataException e) {
233
			throw new DataIndexException(e);
234
		}
235
	}
236

  
237
	public String[] getAttributeNames() {
238
		// TODO Auto-generated method stub
239
		return null;
240
	}
241

  
242
	/**
243
	 * FIXME ver que hacemos con estas constantes
244
	 * Convenience constants to identify sets of providers classified by supported data types.
245
	 */
246
	public interface PROVIDERS {
247

  
248
		public interface GEOMETRY {
249
			public static final String GT2_QUADTREE = "QuadTreeGt2Provider";
250
			public static final String JSI_RTREE = "RTreeJsiProvider";
251
			public static final String JTS_QUADTREE = "QuadTreeJtsProvider";
252
			public static final String SPATIALINDEX_RTREE = "RTreeSptLibProvider";
253
			public static final String DEFAULT = GT2_QUADTREE;
254
		}
255

  
256
		public interface DATE {
257
			// added as example
258
		}
259

  
260
		public interface INTEGER {
261
			// added as example
262
		}
263
	}
264

  
265
	public String getNewFileName(String prefix, String sufix) {
266
		int n=0;
267
		File file = new File(prefix + getName(), sufix);
268
		while (file.exists()) {
269
			n++;
270
			file = new File(prefix + getName() + n, sufix);
271
		}
272
		return file.getAbsolutePath();
273
	}
274
	
275
	public String getFileName() {
276
		// TODO Auto-generated method stub
277
		return null;
278
	}
279

  
280
	public String getTemporaryFileName() {
281
		// TODO Auto-generated method stub
282
		return null;
283
	}
284

  
285

  
286
}
287

  
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/impl/DefaultFeatureIndexes.java
52 52
	// Access by FeatureType and attribute name
53 53
	private Map featureTypes = new HashMap();
54 54

  
55
	// Access by DataIndex name
55
	// Access by index name
56 56
	private Map names = new HashMap();
57 57

  
58 58
	private DefaultFeatureStore store;
......
143 143
	public FeatureSetProvider getFeatureSet(Evaluator evaluator) {
144 144

  
145 145
		class ApplyIndex {
146
			DefaultFeatureIndex index;
146
			DefaultFeatureIndexProviderServices index;
147 147
			EvaluatorFieldValue[] data;
148 148

  
149
			ApplyIndex(DefaultFeatureIndex index, EvaluatorFieldValue[] data) {
149
			ApplyIndex(DefaultFeatureIndexProviderServices index, EvaluatorFieldValue[] data) {
150 150
				this.index = index;
151 151
				this.data = data;
152 152
			}
......
155 155
			List applyIndex = new ArrayList();
156 156
			Iterator indexes = this.iterator();
157 157
			while (indexes.hasNext()) {
158
				DefaultFeatureIndex index = (DefaultFeatureIndex) indexes
158
				DefaultFeatureIndexProviderServices index = (DefaultFeatureIndexProviderServices) indexes
159 159
						.next();
160 160
				String[] attrs = index.getAttributeNames();
161 161
				for (int i = 0; i < attrs.length; i++) {

Also available in: Unified diff