001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Centre for Computational Geography
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.index.rtree;
018:
019: import org.geotools.index.DataDefinition;
020: import org.geotools.index.Lock;
021: import org.geotools.index.LockManager;
022: import org.geotools.index.LockTimeoutException;
023: import org.geotools.index.TreeException;
024:
025: /**
026: * DOCUMENT ME!
027: *
028: * @author Tommaso Nolli
029: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/index/rtree/PageStore.java $
030: */
031: public abstract class PageStore {
032: public static final short SPLIT_QUADRATIC = 1;
033: public static final short SPLIT_LINEAR = 2;
034: protected DataDefinition def;
035: protected int maxNodeEntries;
036: protected int minNodeEntries;
037: protected short splitAlg;
038: private LockManager lockManager;
039:
040: /**
041: *
042: */
043: public PageStore() {
044: this .lockManager = new LockManager();
045: }
046:
047: /**
048: * DOCUMENT ME!
049: *
050: * @param def
051: * @param maxNodeEntries
052: * @param minNodeEntries
053: * @param splitAlg
054: *
055: * @throws TreeException
056: * @throws UnsupportedOperationException DOCUMENT ME!
057: */
058: public PageStore(DataDefinition def, int maxNodeEntries,
059: int minNodeEntries, short splitAlg) throws TreeException {
060: this ();
061:
062: if (minNodeEntries > (maxNodeEntries / 2)) {
063: throw new TreeException("minNodeEntries shoud be <= "
064: + "maxNodeEntries / 2");
065: }
066:
067: if ((splitAlg != SPLIT_LINEAR) && (splitAlg != SPLIT_QUADRATIC)) {
068: throw new TreeException("Split algorithm shoud be "
069: + "SPLIT_LINEAR or SPLIT_QUADRATIC");
070: }
071:
072: if (!def.isValid()) {
073: throw new TreeException("Invalid DataDefinition");
074: }
075:
076: //TODO: Remove when SPLIT_LINEAR is implemented
077: if (splitAlg != SPLIT_QUADRATIC) {
078: throw new UnsupportedOperationException(
079: "Only SPLIT_QUARDATIC is allowed by now...");
080: }
081:
082: this .def = def;
083: this .maxNodeEntries = maxNodeEntries;
084: this .minNodeEntries = minNodeEntries;
085: this .splitAlg = splitAlg;
086: }
087:
088: /**
089: * DOCUMENT ME!
090: *
091: */
092: public abstract Node getRoot();
093:
094: /**
095: * DOCUMENT ME!
096: *
097: * @param node
098: *
099: * @throws TreeException DOCUMENT ME!
100: */
101: public abstract void setRoot(Node node) throws TreeException;
102:
103: /**
104: * DOCUMENT ME!
105: *
106: * @param isLeaf
107: *
108: */
109: public abstract Node getEmptyNode(boolean isLeaf);
110:
111: /**
112: * Returns the Node pointed by this entry and having this Node as parent
113: *
114: * @param parentEntry
115: * @param parent
116: *
117: *
118: * @throws TreeException DOCUMENT ME!
119: */
120: public abstract Node getNode(Entry parentEntry, Node parent)
121: throws TreeException;
122:
123: /**
124: * DOCUMENT ME!
125: *
126: * @param node
127: *
128: */
129: public abstract Entry createEntryPointingNode(Node node);
130:
131: /**
132: * DOCUMENT ME!
133: *
134: * @return The maximum number of <code>Entry</code>s per page
135: */
136: public int getMaxNodeEntries() {
137: return this .maxNodeEntries;
138: }
139:
140: /**
141: * DOCUMENT ME!
142: *
143: * @return The minimum number of <code>Entry</code>s per page
144: */
145: public int getMinNodeEntries() {
146: return this .minNodeEntries;
147: }
148:
149: /**
150: * DOCUMENT ME!
151: *
152: * @return The split algorithm to use
153: */
154: public short getSplitAlgorithm() {
155: return this .splitAlg;
156: }
157:
158: /**
159: * DOCUMENT ME!
160: *
161: */
162: public DataDefinition getDataDefinition() {
163: return this .def;
164: }
165:
166: /**
167: * Frees resources used by this <code>Node</code>
168: *
169: * @param node The <code>Node</code> to free
170: */
171: public abstract void free(Node node);
172:
173: /**
174: * Aquires a write lock to the store
175: *
176: * @return an Object rapresenting the lock
177: *
178: * @throws LockTimeoutException
179: */
180: public Lock getWriteLock() throws LockTimeoutException {
181: return this .lockManager.aquireExclusive();
182: }
183:
184: /**
185: * Aquires a read lock to the store
186: *
187: * @return an Object rapresenting the lock
188: *
189: * @throws LockTimeoutException
190: */
191: public Lock getReadLock() throws LockTimeoutException {
192: return this .lockManager.aquireShared();
193: }
194:
195: /**
196: * DOCUMENT ME!
197: *
198: * @param lock
199: */
200: public void releaseLock(Lock lock) {
201: this .lockManager.release(lock);
202: }
203:
204: /**
205: * DOCUMENT ME!
206: *
207: * @throws TreeException
208: */
209: public abstract void close() throws TreeException;
210: }
|