001: // kelondroapTable.java
002: // -----------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2005
006: // last major change: 13.03.2005
007: //
008: // This program is free software; you can redistribute it and/or modify
009: // it under the terms of the GNU General Public License as published by
010: // the Free Software Foundation; either version 2 of the License, or
011: // (at your option) any later version.
012: //
013: // This program is distributed in the hope that it will be useful,
014: // but WITHOUT ANY WARRANTY; without even the implied warranty of
015: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: // GNU General Public License for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // along with this program; if not, write to the Free Software
020: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: //
022: // Using this software in any meaning (reading, learning, copying, compiling,
023: // running) means that you agree that the Author(s) is (are) not responsible
024: // for cost, loss of data or any harm that may be caused directly or indirectly
025: // by usage of this softare or this documentation. The usage of this software
026: // is on your own risk. The installation and usage (starting/running) of this
027: // software may allow other people or application to access your computer and
028: // any attached devices and is highly dependent on the configuration of the
029: // software which must be done by the user of the software; the author(s) is
030: // (are) also not responsible for proper configuration and usage of the
031: // software, even if provoked by documentation provided together with
032: // the software.
033: //
034: // Any changes to this file according to the GPL as documented in the file
035: // gpl.txt aside this file in the shipment you received can be done to the
036: // lines that follows this copyright notice here, but changes must not be
037: // done inside the copyright notive above. A re-distribution must contain
038: // the intact and unchanged copyright notice.
039: // Contributions and changes to the program code must be marked as such.
040:
041: // this is mainly a convenience class to bundle many kelondroMap Objects
042:
043: package de.anomic.kelondro;
044:
045: import java.io.File;
046: import java.io.IOException;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.Map;
050:
051: public class kelondroMapTable {
052:
053: private HashMap<String, kelondroMapObjects> mTables;
054: private HashMap<String, kelondroIndex> tTables;
055: //private HashMap sTables;
056: private File tablesPath;
057:
058: public kelondroMapTable(File tablesPath) {
059: this .mTables = new HashMap<String, kelondroMapObjects>();
060: this .tTables = new HashMap<String, kelondroIndex>();
061: //this.sTables = new HashMap();
062: this .tablesPath = tablesPath;
063: if (!(tablesPath.exists()))
064: tablesPath.mkdirs();
065: }
066:
067: public void declareMaps(String tablename, int keysize,
068: kelondroByteOrder objectOrder, int nodesize,
069: int cacheslots, char fillChar, boolean resetOnFail) {
070: declareMaps(tablename, keysize, objectOrder, nodesize,
071: cacheslots, null, null, null, fillChar, resetOnFail);
072: }
073:
074: public void declareMaps(String tablename, int keysize,
075: kelondroByteOrder objectOrder, int nodesize,
076: int cacheslots, String[] sortfields,
077: String[] longaccfields, String[] doubleaccfields,
078: char fillChar, boolean resetOnFail) {
079: declareMaps(tablename, keysize, objectOrder, nodesize,
080: cacheslots, sortfields, longaccfields, doubleaccfields,
081: fillChar, 0, resetOnFail);
082: }
083:
084: public void declareMaps(String tablename, int keysize,
085: kelondroByteOrder objectOrder, int nodesize,
086: int cacheslots, String[] sortfields,
087: String[] longaccfields, String[] doubleaccfields,
088: char fillChar, long preloadTime, boolean resetOnFail) {
089: if (mTables.containsKey(tablename))
090: throw new RuntimeException(
091: "kelondroTables.declareMap: table '" + tablename
092: + "' declared twice.");
093: if (tTables.containsKey(tablename))
094: throw new RuntimeException(
095: "kelondroTables.declareMap: table '" + tablename
096: + "' declared already in other context.");
097: File tablefile = new File(tablesPath, "table." + tablename
098: + ".mdb");
099: kelondroDyn dyn;
100: if (!(tablefile.exists()))
101: tablefile.getParentFile().mkdirs();
102: dyn = new kelondroDyn(tablefile, true, true, preloadTime,
103: keysize, nodesize, fillChar, objectOrder, true, false,
104: resetOnFail);
105: kelondroMapObjects map = new kelondroMapObjects(dyn,
106: cacheslots, sortfields, longaccfields, doubleaccfields,
107: null, null);
108: mTables.put(tablename, map);
109: }
110:
111: public void declareTree(String tablename, kelondroRow rowdef,
112: long buffersize /*bytes*/, long preloadTime) {
113: if (mTables.containsKey(tablename))
114: throw new RuntimeException(
115: "kelondroTables.declareTree: table '" + tablename
116: + "' declared already in other context.");
117: if (tTables.containsKey(tablename))
118: throw new RuntimeException(
119: "kelondroTables.declareTree: table '" + tablename
120: + "' declared twice.");
121: File tablefile = new File(tablesPath, "table." + tablename
122: + ".tdb");
123: kelondroIndex Tree = new kelondroCache(kelondroTree.open(
124: tablefile, true, preloadTime, rowdef));
125: tTables.put(tablename, Tree);
126: }
127:
128: public synchronized void update(String tablename, String key,
129: HashMap<String, String> map) throws IOException {
130: kelondroMapObjects table = (kelondroMapObjects) mTables
131: .get(tablename);
132: if (table == null)
133: throw new RuntimeException(
134: "kelondroTables.update: map table '" + tablename
135: + "' does not exist.");
136: if (key.length() > table.keySize())
137: key = key.substring(0, table.keySize());
138: table.set(key, map);
139: mTables.put(tablename, table);
140: }
141:
142: public synchronized void update(String tablename,
143: kelondroRow.Entry row /* first element is the unique key = index */)
144: throws IOException {
145: kelondroIndex tree = (kelondroIndex) tTables.get(tablename);
146: if (tree == null)
147: throw new RuntimeException(
148: "kelondroTables.update: tree table '" + tablename
149: + "' does not exist.");
150: tree.put(row);
151: tTables.put(tablename, tree);
152: }
153:
154: public synchronized Map<String, String> selectMap(String tablename,
155: String key) {
156: kelondroMapObjects table = (kelondroMapObjects) mTables
157: .get(tablename);
158: if (table == null)
159: throw new RuntimeException(
160: "kelondroTables.selectMap: map table '" + tablename
161: + "' does not exist.");
162: if (key.length() > table.keySize())
163: key = key.substring(0, table.keySize());
164: return table.getMap(key);
165: }
166:
167: public synchronized kelondroRow.Entry selectByte(String tablename,
168: String key) throws IOException {
169: kelondroIndex tree = (kelondroIndex) tTables.get(tablename);
170: if (tree == null)
171: throw new RuntimeException(
172: "kelondroTables.selectByte: tree table '"
173: + tablename + "' does not exist.");
174: return tree.get(key.getBytes());
175: }
176:
177: public synchronized kelondroMapObjects.mapIterator /* of Map-Elements */maps(
178: String tablename, boolean up, boolean rotating)
179: throws IOException {
180: kelondroMapObjects table = (kelondroMapObjects) mTables
181: .get(tablename);
182: if (table == null)
183: throw new RuntimeException(
184: "kelondroTables.maps: map table '" + tablename
185: + "' does not exist.");
186: return table.maps(up, rotating);
187: }
188:
189: public synchronized kelondroMapObjects.mapIterator /* of Map-Elements */maps(
190: String tablename, boolean up, boolean rotating,
191: byte[] firstKey, byte[] secondKey) throws IOException {
192: kelondroMapObjects table = (kelondroMapObjects) mTables
193: .get(tablename);
194: if (table == null)
195: throw new RuntimeException(
196: "kelondroTables.maps: map table '" + tablename
197: + "' does not exist.");
198: return table.maps(up, rotating, firstKey, secondKey);
199: }
200:
201: public synchronized kelondroMapObjects.mapIterator /* of Map-Elements */maps(
202: String tablename, boolean up, String field) {
203: kelondroMapObjects table = (kelondroMapObjects) mTables
204: .get(tablename);
205: if (table == null)
206: throw new RuntimeException(
207: "kelondroTables.maps: map table '" + tablename
208: + "' does not exist.");
209: return table.maps(up, field);
210: }
211:
212: public synchronized kelondroCloneableIterator<kelondroRow.Entry> /* of kelondroRow.Entry-Elements */rows(
213: String tablename, boolean up, boolean rotating,
214: byte[] firstKey, byte[] secondKey) throws IOException {
215: kelondroIndex tree = (kelondroIndex) tTables.get(tablename);
216: if (tree == null)
217: throw new RuntimeException(
218: "kelondroTables.bytes: tree table '" + tablename
219: + "' does not exist.");
220: kelondroCloneableIterator<kelondroRow.Entry> i = tree.rows(up,
221: firstKey);
222: if (rotating)
223: return new kelondroRotateIterator<kelondroRow.Entry>(i,
224: secondKey, tree.size());
225: else
226: return i;
227: }
228:
229: // if you need the long-values from a row-iteration, please use kelondroRecords.bytes2long to convert from byte[] to long
230:
231: public synchronized void delete(String tablename, String key)
232: throws IOException {
233: kelondroMapObjects table = (kelondroMapObjects) mTables
234: .get(tablename);
235: if (key.length() > table.keySize())
236: key = key.substring(0, table.keySize());
237: if (table != null) {
238: table.remove(key);
239: mTables.put(tablename, table);
240: return;
241: }
242:
243: kelondroIndex Tree = (kelondroIndex) tTables.get(tablename);
244: if (Tree != null) {
245: Tree.remove(key.getBytes(), false);
246: tTables.put(tablename, Tree);
247: return;
248: }
249:
250: throw new RuntimeException("kelondroTables.delete: table '"
251: + tablename + "' does not exist.");
252: }
253:
254: public synchronized long longAccumulator(String tablename,
255: String field) {
256: kelondroMapObjects table = (kelondroMapObjects) mTables
257: .get(tablename);
258: if (table == null)
259: throw new RuntimeException(
260: "kelondroTables.accumulator: map table '"
261: + tablename + "' does not exist.");
262: return table.getLongAcc(field);
263: }
264:
265: public synchronized double doubleAccumulator(String tablename,
266: String field) {
267: kelondroMapObjects table = (kelondroMapObjects) mTables
268: .get(tablename);
269: if (table == null)
270: throw new RuntimeException(
271: "kelondroTables.accumulator: map table '"
272: + tablename + "' does not exist.");
273: return table.getDoubleAcc(field);
274: }
275:
276: public synchronized int size(String tablename) {
277: kelondroMapObjects table = (kelondroMapObjects) mTables
278: .get(tablename);
279: if (table != null)
280: return table.size();
281:
282: kelondroIndex Tree = (kelondroIndex) tTables.get(tablename);
283: if (Tree != null)
284: return Tree.size();
285:
286: throw new RuntimeException(
287: "kelondroTables.accumulator: table '" + tablename
288: + "' does not exist.");
289: }
290:
291: public void close() {
292: Iterator<kelondroMapObjects> tablesIt = mTables.values()
293: .iterator();
294: while (tablesIt.hasNext())
295: ((kelondroMapObjects) tablesIt.next()).close();
296: mTables = null;
297:
298: Iterator<kelondroIndex> TreeIt = tTables.values().iterator();
299: while (TreeIt.hasNext())
300: ((kelondroIndex) TreeIt.next()).close();
301: tTables = null;
302: }
303:
304: }
|