Revision 23860

View differences:

branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/index/AbstractDataIndexProvider.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.index;
30

  
31
import org.apache.log4j.Logger;
32
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
33
import org.gvsig.tools.extensionPoint.ExtensionPoint;
34
import org.gvsig.tools.extensionPoint.ExtensionPointsSingleton;
35

  
36
/**
37
 * Abstract factory for registering and retrieving available indexing engines. 
38
 * You can force the use of a specific implementation by passing its key, or you can use the default
39
 * implementation for a given data type by specifying the {@link AttributeDescriptor} of the column 
40
 * to be indexed.
41
 * 
42
 * @author jyarza
43
 *
44
 */
45
public abstract class AbstractDataIndexProvider {
46

  
47
	private static Logger logger = Logger.getLogger(AbstractDataIndexProvider.class);
48
	
49
	/**
50
	 * Convenience constants to identify sets of implemented factories classified by supported data types. 
51
	 * Using these from a client is not permitted as implementations are optional and not visible.
52
	 */
53
	public interface Factories {
54
		
55
		public interface GEOMETRY {
56
			public static final String GT2_QUADTREE = "QuadTreeGt2Factory";
57
			public static final String JSI_RTREE = "RTreeJsiFactory";
58
			public static final String JTS_QUADTREE = "QuadTreeJtsFactory";
59
			public static final String SPATIALINDEX_RTREE = "RTreeSptLibFactory";
60
			public static final String DEFAULT = GT2_QUADTREE;
61
		}
62
		
63
		public interface DATE {
64
			// added as example
65
		}
66
		
67
		public interface INTEGER {
68
			// added as example			
69
		}
70
		
71
	}
72
	
73
	// Register extension point, all available factory implementations will hang off here.
74
	public static final String EXTENSION_POINT_NAME =  "IndexFactory";
75
	private static ExtensionPoint ep = new ExtensionPoint(EXTENSION_POINT_NAME, "Index Factory");
76
	
77
	static {		
78
		ExtensionPointsSingleton.getInstance().put(ep);
79
	}
80

  
81
	/**
82
	 * Returns the default index factory registered for the given data type.
83
	 * @param col
84
	 * @return
85
	 * @throws InstantiationException
86
	 * @throws IllegalAccessException
87
	 */
88
	public static AbstractDataIndexProvider getDefaultFactory(FeatureAttributeDescriptor col) throws InstantiationException, IllegalAccessException {
89
		int type = col.getDataType();
90
		if (type == FeatureAttributeDescriptor.GEOMETRY) {
91
			return getFactory(Factories.GEOMETRY.DEFAULT);
92
		} else {
93
			throw new UnsupportedOperationException(type + " indexes are not supported.");
94
		}
95
		
96
	}
97
	
98
	/**
99
	 * Returns a specific index factory by key. 
100
	 * The argument implType must be one of the constants defined in {@link AbstractDataIndexProvider.Factories}
101
	 * @param implType
102
	 * @return
103
	 * @throws InstantiationException
104
	 * @throws IllegalAccessException
105
	 */
106
	public static AbstractDataIndexProvider getFactory(String key) throws InstantiationException, IllegalAccessException {
107
		if (!ep.containsKey(key)) {
108
			throw new IllegalArgumentException(key + " is not a valid factory.");
109
		}
110
		return (AbstractDataIndexProvider) ep.create(key);
111
	}
112
	
113
	/**
114
	 * Registers an index factory.
115
	 * @param key Name by which the factory will be identified
116
	 * @param description
117
	 * @param value
118
	 */
119
	protected static void registerFactory(String key, String description, Class value) {
120
		ep.put(key, description, value);
121
		logger.debug(key + " registered.");
122
	}
123

  
124
	/**
125
	 * Creates and returns a new index using default parameters.
126
	 * @return
127
	 * @throws DataIndexException
128
	 */
129
	public DataIndex createIndex() throws DataIndexException {
130
		return createIndex(null);
131
	}
132
	
133
	/**
134
	 * Creates and returns a new index using the given parameters.
135
	 * @param params
136
	 * @return
137
	 * @throws DataIndexException
138
	 */
139
	public abstract DataIndex createIndex(IndexParameters params) throws DataIndexException;
140
	
141
}
142

  
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/index/DataIndexException.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
package org.gvsig.fmap.data.index;
29

  
30
import java.util.HashMap;
31
import java.util.Map;
32

  
33
import org.gvsig.tools.exception.BaseException;
34

  
35
/**
36
 * @author jiyarza
37
 */
38
public class DataIndexException extends BaseException {
39
	
40
	private static final long serialVersionUID = 932690269454242645L;	
41
	
42
	private static final String MESSAGE_KEY = "index_exception";
43
	private static final String FORMAT_STRING = 
44
		"Index Exception.";	
45
	
46
	/**
47
	 * Main constructor that provides a cause Exception
48
	 * @param e
49
	 */
50
	public DataIndexException(Exception e) {
51
		super(FORMAT_STRING, e, MESSAGE_KEY, serialVersionUID);
52
	}
53
	
54
	protected Map values() {
55
		HashMap map = new HashMap();
56
		return map;
57
	}
58
}
59

  
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/index/DataIndex.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.index;
30

  
31
import java.util.List;
32

  
33
import org.gvsig.fmap.data.feature.Feature;
34
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.data.feature.FeatureCollection;
36
import org.gvsig.fmap.data.feature.FeatureID;
37
import org.gvsig.fmap.data.feature.FeatureStore;
38
import org.gvsig.fmap.data.feature.FeatureType;
39

  
40
/**
41
 * Interface for any Index. 
42
 * @author jyarza
43
 *
44
 */
45
public interface DataIndex {
46
	
47
	/** Column to which belongs this index */
48
	public FeatureAttributeDescriptor getFeatureAttributeDescriptor();
49
	
50
	/** FeatureType to which belongs this index */
51
	public FeatureType getFeatureType();
52
	
53
	/** FeatureStore to which belongs this index */
54
	public FeatureStore getFeatureStore();
55
	
56
	/** Index name */
57
	public String getName();
58

  
59
	/** Indicates if this index should overwrite itself on creation, if it exists already */
60
	public boolean isOverwrite();
61
	
62
	/** 
63
	 * This is the most basic insertion method.
64
	 * Inserts an object into the index, with the specified id. 
65
	 */
66
	public void insert(Object o, FeatureID fid);
67
	
68
	/**
69
	 * Inserts a Feature in the index.
70
	 * The Feature must contain a column that matches this index's column (name and data type)
71
	 * @param feat
72
	 */
73
	public void insert(Feature feat);
74

  
75
	/**
76
	 * Inserts a FeatureCollection into this index
77
	 * FeatureType is not checked so it will accept any FeatureType
78
	 * as long as exists a column with a valid name
79
	 */	
80
	public void insert(FeatureCollection data);
81
	
82
	/** 
83
	 * This is the most basic deletion method.
84
	 * Deletes an object from the index, given its id
85
	 **/
86
	public void delete(Object o, FeatureID fid);
87

  
88
	/**
89
	 * Deletes a Feature in the index.
90
	 * The Feature must contain a column that matches this index's column (name and data type)
91
	 * @param feat
92
	 */
93
	public void delete(Feature feat);
94

  
95
	/**
96
	 * Deletes a FeatureCollection from this index
97
	 * FeatureType is not checked so it will accept any FeatureType
98
	 * as long as exists a column with a valid name
99
	 */	
100
	public void delete(FeatureCollection data) throws DataIndexException;
101
	
102
	/** Performs a search in the index that meet the given parameters */
103
	public List query(QueryParameters params) throws DataIndexException;
104
	
105
	
106
}
107

  
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/index/AbstractDataIndex.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.index;
30

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

  
34
import org.gvsig.fmap.data.exceptions.DataException;
35
import org.gvsig.fmap.data.feature.Feature;
36
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
37
import org.gvsig.fmap.data.feature.FeatureCollection;
38
import org.gvsig.fmap.data.feature.FeatureStore;
39
import org.gvsig.fmap.data.feature.FeatureType;
40

  
41

  
42
public abstract class AbstractDataIndex implements DataIndex {
43

  
44
	private final IndexParameters params;
45

  
46
	public AbstractDataIndex(IndexParameters params) throws DataIndexException {
47
		if (params == null) {
48
			throw new IllegalArgumentException("params cannot be null.");
49
		}
50
		this.params = params;
51
	}
52

  
53
	public final FeatureAttributeDescriptor getFeatureAttributeDescriptor() {
54
		return params.getFeatureAttributeDescriptor();
55
	}
56

  
57
	public final FeatureStore getFeatureStore() {
58
		return params.getFeatureStore();
59
	}
60

  
61
	public final FeatureType getFeatureType() {
62
		return params.getFeatureType();
63
	}
64

  
65
	public final String getName() {
66
		return params.getName();
67
	}
68

  
69
	public final boolean isOverwrite() {
70
		return params.isOverwrite();
71
	}
72

  
73
	/**
74
	 * Fills this index with all the data in its FeatureStore
75
	 */
76
	public final void fill() throws DataIndexException {
77
		try {
78
			insert(getFeatureStore().getFeatureCollection());
79
		} catch (DataException e) {
80
			throw new DataIndexException(e);
81
		}
82
	}
83

  
84
	public final void insert(FeatureCollection data) {
85
		Iterator it = data.iterator();
86
		while (it.hasNext()) {
87
			Feature feat = (Feature) it.next();
88
			insert(feat);
89
		}
90
	}
91

  
92
	public void insert(Feature feat) {
93
		try {
94
			insert(feat.get(getFeatureAttributeDescriptor().getName()), feat.getID());
95
		} catch (NullPointerException e) {
96
			throw new IllegalArgumentException("Feature does not contain a column with name " + getFeatureAttributeDescriptor().getName());
97
		} catch (ClassCastException e) {
98
			throw new IllegalArgumentException("Column data type is not valid.");
99
		}
100
	}
101

  
102
	public abstract List query(QueryParameters params) throws DataIndexException;
103
}
104

  
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/index/DataIndexes.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
package org.gvsig.fmap.data.index;
29

  
30
import java.util.HashMap;
31
import java.util.Iterator;
32
import java.util.Map;
33

  
34
import org.gvsig.fmap.data.exceptions.DataException;
35
import org.gvsig.fmap.data.feature.FeatureStore;
36
import org.gvsig.fmap.data.feature.FeatureType;
37

  
38
/**
39
 * Renombrar a Indexes--
40
 * 
41
 * 
42
 * API - Index
43
 * 		 AbstractIndex
44
 * Impl   ^
45
 * SPI - IndexProvider (initialize)
46
 *       AbstractIndexProvider
47
 * 
48
 * Eliminar factorias (logica de create index pasa a initialize())
49
 * Eliminar abstractFactory (l?gica pasa a DataManager).
50
 * 
51
 * 
52
 * 	// DATAMANAGER
53
	
54
	public List getIndexProviders(int dataType); (of names)
55
	
56
	public registerIndexProvider(IndexProvider);
57
	
58
	public IndexProvider createIndexProvider(store, ftype, att, [names]);
59
 * 
60
 *  public setDefaultIndexProvider(int dataType, String name);
61
 *  
62
 *  public File getTemporaryDirectory();
63
 *  
64
 *  INDEXES
65
 *  -------
66
 *  
67
 *  Lista plana por nombre.
68
 *  Quitar IndexParameters
69
 * 
70
 * 
71
 * 
72
 * This class provides access to a FeatureStore local indexes.
73
 * This class holds the correspondence (FeatureType, columName) -> Index
74
 * TODO there is not persistence and works only in memory
75
 * @author jyarza
76
 */
77
public class DataIndexes {
78

  
79
	private Map featureTypes = new HashMap();
80

  
81
	/**
82
	 * Creates an empty IndexRegistry for the given FeatureStore
83
	 * 
84
	 * @param store
85
	 *            FeatureStore to whom this IndexRegistry belongs
86
	 * @throws DataException
87
	 */
88
	public DataIndexes(FeatureStore store) throws DataException {
89
		Iterator it = store.getFeatureTypes().iterator();
90
		while (it.hasNext()) {
91
			FeatureType type = (FeatureType) it.next();
92
			featureTypes.put(type, new HashMap());
93
		}
94
	}
95

  
96
	/**
97
	 * Returns an Index given the FeatureType and the name of the column.
98
	 * If the column has no index then this method returns <code>null</code>.
99
	 * @param fType FeatureType to which belongs the index
100
	 * @param colName name of the column
101
	 * @return if the index exists then this method returns the index, otherwise it returns null.
102
	 */
103
	public DataIndex getIndex(FeatureType fType, String colName) {
104
		Map indexes = (Map) featureTypes.get(fType);
105
		if (indexes != null) {
106
			return (DataIndex) (featureTypes.get(fType));
107
		}
108
		return null;
109
	}
110

  
111
	/**
112
	 * Adds an index to the registry, given its FeatureType and column name
113
	 * @param fType
114
	 * @param colName
115
	 * @param index
116
	 */
117
	public void addIndex(FeatureType fType, String colName, DataIndex index) {
118
		Map indexes = (Map) featureTypes.get(fType);
119
		if (indexes == null) {
120
			// A new feature type has been added to the FeatureStore since this IndexStore was created
121
			indexes = new HashMap();
122
			featureTypes.put(fType, indexes);
123
		}
124
		indexes.put(colName, index);
125
	}
126

  
127
	/**
128
	 * Returns true if this registry contains an index for the given FeatureType and column name, otherwise
129
	 * it returns false.
130
	 * @param fType
131
	 * @param colName
132
	 * @return <code>true</code> if the index exists, <code>false</code> if it does not.
133
	 */
134
	public boolean contains(FeatureType fType, String colName) {
135
		Map map = (Map) featureTypes.get(fType);
136
		return map == null ? false : map.containsKey(colName);
137
	}
138
}
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/index/QueryParameters.java
28 28

  
29 29
package org.gvsig.fmap.data.index;
30 30

  
31

  
31
/**
32
 * Parameters for index querying.
33
 * 
34
 * Typically will hold a range, for example:
35
 * 
36
 * - If the index is of type geometry (spatial) it will hold an envelope
37
 * - If the index is of type integer it will hold an interval [min, max]
38
 * @author jyarza
39
 *
40
 */
32 41
public class QueryParameters extends BaseParameters {
33 42

  
34 43
	public void setDefaultValues() {

Also available in: Unified diff