001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: // TODO Store somewhere root pageId: add in rtrees_cat probably...
017: package org.geotools.index.rtree.database;
018:
019: import org.geotools.index.DataDefinition;
020: import org.geotools.index.Lock;
021: import org.geotools.index.LockTimeoutException;
022: import org.geotools.index.TreeException;
023: import org.geotools.index.rtree.Entry;
024: import org.geotools.index.rtree.Node;
025: import org.geotools.index.rtree.PageStore;
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030: import javax.sql.DataSource;
031:
032: /**
033: * DOCUMENT ME!
034: *
035: * @author Tommaso Nolli
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/index/rtree/database/DatabasePageStore.java $
037: */
038: public class DatabasePageStore extends PageStore {
039: protected static final int DEF_MAX = 50;
040: protected static final int DEF_MIN = 25;
041: protected static final short DEF_SPLIT = SPLIT_QUADRATIC;
042: private DataSource dataSource;
043: private Dialect dialect;
044: private String rtreeName;
045: private DatabaseNode root;
046:
047: /**
048: * Constructor
049: *
050: * @param ds
051: * @param dialect DOCUMENT ME!
052: * @param rtreeName
053: *
054: * @throws TreeException DOCUMENT ME!
055: */
056: public DatabasePageStore(DataSource ds, Dialect dialect,
057: String rtreeName) throws TreeException {
058: super ();
059:
060: this .dataSource = ds;
061: this .dialect = dialect;
062: this .rtreeName = rtreeName;
063:
064: this .createNew();
065: }
066:
067: /**
068: * Constructor
069: *
070: * @param ds
071: * @param dialect DOCUMENT ME!
072: * @param rtreeName
073: * @param def
074: *
075: * @throws TreeException DOCUMENT ME!
076: */
077: public DatabasePageStore(DataSource ds, Dialect dialect,
078: String rtreeName, DataDefinition def) throws TreeException {
079: this (ds, dialect, rtreeName, def, DEF_MAX, DEF_MIN, DEF_SPLIT);
080: }
081:
082: /**
083: * Constructor
084: *
085: * @param ds
086: * @param dialect DOCUMENT ME!
087: * @param rtreeName
088: * @param def
089: * @param maxNodeEntries
090: * @param minNodeEntries
091: * @param splitAlg
092: *
093: * @throws TreeException DOCUMENT ME!
094: */
095: public DatabasePageStore(DataSource ds, Dialect dialect,
096: String rtreeName, DataDefinition def, int maxNodeEntries,
097: int minNodeEntries, short splitAlg) throws TreeException {
098: super (def, maxNodeEntries, minNodeEntries, splitAlg);
099:
100: this .dataSource = ds;
101: this .dialect = dialect;
102: this .rtreeName = rtreeName;
103:
104: init();
105: }
106:
107: /**
108: * DOCUMENT ME!
109: *
110: * @throws TreeException
111: */
112: private void createNew() throws TreeException {
113: Connection cnn = null;
114: PreparedStatement pst = null;
115: ResultSet rs = null;
116:
117: try {
118: cnn = this .dataSource.getConnection();
119: pst = cnn.prepareStatement(dialect.getCatalogQuery());
120: pst.setString(1, this .rtreeName);
121: rs = pst.executeQuery();
122:
123: if (rs.next()) {
124: throw new TreeException(this .rtreeName
125: + " already exists!");
126: }
127:
128: rs.close();
129: pst.close();
130:
131: int i = 1;
132: pst = cnn.prepareStatement(dialect.getCatalogInsert());
133: pst.setString(i++, this .rtreeName);
134: pst.setInt(i++, this .minNodeEntries);
135: pst.setInt(i++, this .maxNodeEntries);
136: pst.setShort(i++, this .splitAlg);
137:
138: pst.executeUpdate();
139: cnn.commit();
140: pst.close();
141:
142: pst = cnn.prepareStatement(dialect
143: .getCreateTable(this .rtreeName));
144: pst.execute();
145:
146: this .root = new DatabaseNode(this .maxNodeEntries,
147: this .dataSource, this .dialect, this .rtreeName);
148:
149: this .root.setLeaf(true);
150: this .root.save();
151: } catch (SQLException e) {
152: try {
153: cnn.rollback();
154: } catch (Exception ee) {
155: }
156:
157: throw new TreeException(e);
158: } finally {
159: try {
160: rs.close();
161: } catch (Exception ee) {
162: }
163:
164: try {
165: pst.close();
166: } catch (Exception ee) {
167: }
168:
169: try {
170: cnn.close();
171: } catch (Exception ee) {
172: }
173: }
174: }
175:
176: /**
177: * Initializes the PageStore
178: */
179: private void init() {
180: }
181:
182: /**
183: * @see org.geotools.index.rtree.PageStore#getRoot()
184: */
185: public Node getRoot() {
186: return this .root;
187: }
188:
189: /**
190: * @see org.geotools.index.rtree.PageStore#setRoot(org.geotools.index.rtree.Node)
191: */
192: public void setRoot(Node node) throws TreeException {
193: DatabaseNode n = (DatabaseNode) node;
194: n.setParent(null);
195:
196: this .root = n;
197: }
198:
199: /**
200: * @see org.geotools.index.rtree.PageStore#getEmptyNode(boolean)
201: */
202: public Node getEmptyNode(boolean isLeaf) {
203: // TODO Auto-generated method stub
204: return null;
205: }
206:
207: /**
208: * @see org.geotools.index.rtree.PageStore#getNode(org.geotools.index.rtree.Entry,
209: * org.geotools.index.rtree.Node)
210: */
211: public Node getNode(Entry parentEntry, Node parent)
212: throws TreeException {
213: // TODO Auto-generated method stub
214: return null;
215: }
216:
217: /**
218: * @see org.geotools.index.rtree.PageStore#createEntryPointingNode(org.geotools.index.rtree.Node)
219: */
220: public Entry createEntryPointingNode(Node node) {
221: // TODO Auto-generated method stub
222: return null;
223: }
224:
225: /**
226: * @see org.geotools.index.rtree.PageStore#getMaxNodeEntries()
227: */
228: public int getMaxNodeEntries() {
229: // TODO Auto-generated method stub
230: return 0;
231: }
232:
233: /* (non-Javadoc)
234: * @see org.geotools.index.rtree.PageStore#getMinNodeEntries()
235: */
236: public int getMinNodeEntries() {
237: // TODO Auto-generated method stub
238: return 0;
239: }
240:
241: /* (non-Javadoc)
242: * @see org.geotools.index.rtree.PageStore#getSplitAlgorithm()
243: */
244: public short getSplitAlgorithm() {
245: // TODO Auto-generated method stub
246: return 0;
247: }
248:
249: /* (non-Javadoc)
250: * @see org.geotools.index.rtree.PageStore#getDataDefinition()
251: */
252: public DataDefinition getDataDefinition() {
253: // TODO Auto-generated method stub
254: return null;
255: }
256:
257: /* (non-Javadoc)
258: * @see org.geotools.index.rtree.PageStore#free(org.geotools.index.rtree.Node)
259: */
260: public void free(Node node) {
261: // TODO Auto-generated method stub
262: }
263:
264: /* (non-Javadoc)
265: * @see org.geotools.index.rtree.PageStore#getWriteLock()
266: */
267: public Lock getWriteLock() throws LockTimeoutException {
268: // TODO Auto-generated method stub
269: return null;
270: }
271:
272: /* (non-Javadoc)
273: * @see org.geotools.index.rtree.PageStore#getReadLock()
274: */
275: public Lock getReadLock() throws LockTimeoutException {
276: // TODO Auto-generated method stub
277: return null;
278: }
279:
280: /* (non-Javadoc)
281: * @see org.geotools.index.rtree.PageStore#releaseLock(org.geotools.index.rtree.Lock)
282: */
283: public void releaseLock(Lock lock) {
284: // TODO Auto-generated method stub
285: }
286:
287: /* (non-Javadoc)
288: * @see org.geotools.index.rtree.PageStore#close()
289: */
290: public void close() throws TreeException {
291: // TODO Auto-generated method stub
292: }
293: }
|