001: /*
002: * $Id: TTreeIndexLoader.java,v 1.1 2005/06/30 01:14:44 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.ext.indexes.ttree;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.io.ObjectInputStream;
046: import java.io.ObjectOutputStream;
047:
048: import org.axiondb.AxionException;
049: import org.axiondb.Column;
050: import org.axiondb.Index;
051: import org.axiondb.IndexLoader;
052: import org.axiondb.Table;
053: import org.axiondb.io.AxionFileSystem;
054: import org.axiondb.util.NullObject;
055:
056: /**
057: * Abstract IndexLoader for T-Tree.
058: *
059: * @version $Revision: 1.1 $ $Date: 2005/06/30 01:14:44 $
060: * @author Pavels Andrejevs
061: */
062: public abstract class TTreeIndexLoader implements IndexLoader {
063: protected static AxionFileSystem FS = new AxionFileSystem();
064:
065: public TTreeIndexLoader() {
066: }
067:
068: // ---------------------------------------------------------------------
069:
070: public Object getNullKey() {
071: return NullObject.INSTANCE;
072: }
073:
074: public Index loadIndex(Table table, File dataDirectory)
075: throws AxionException {
076: ObjectInputStream in = null;
077: try {
078: String name = dataDirectory.getName();
079: File file = new File(dataDirectory, name + ".DATA");
080: in = FS.openObjectInputSteam(file);
081:
082: // read version number
083: int ver = in.readInt();
084: if (ver != 1) {
085: throw new AxionException("Can't parse data file "
086: + file + " for index " + name
087: + ", unrecognized data file version " + ver);
088: }
089:
090: String col = in.readUTF(); // read column name
091: boolean unique = in.readBoolean(); // read unique flag
092: int maxNodeSize = in.readInt(); // read minimization factor
093:
094: return makeIndex(name, table.getColumn(col), unique,
095: dataDirectory, maxNodeSize, true);
096: } catch (IOException e) {
097: throw new AxionException(e);
098: } finally {
099: FS.closeInputStream(in);
100: }
101: }
102:
103: public void saveIndex(Index ndx, File dataDirectory)
104: throws AxionException {
105: DiskTTreeIndex index = (DiskTTreeIndex) ndx;
106: ObjectOutputStream out = null;
107: try {
108: String name = index.getName();
109: File file = new File(dataDirectory, name + ".DATA");
110: out = FS.createObjectOutputSteam(file);
111:
112: out.writeInt(1); // write version number
113: out.writeUTF(index.getIndexedColumn().getName()); // write column name
114: out.writeBoolean(index.isUnique()); // write unique flag
115: out.writeInt(index.getTTree().getMaxNodeSize()); // write max node size
116:
117: index.getTTree().save(dataDirectory);
118:
119: } catch (IOException e) {
120: throw new AxionException(e);
121: } finally {
122: FS.closeOutputStream(out);
123: }
124: }
125:
126: public void saveIndexAfterTruncate(Index ndx, File dataDirectory)
127: throws AxionException {
128: DiskTTreeIndex index = (DiskTTreeIndex) ndx;
129: index.getTTree().saveAfterTruncate(dataDirectory);
130: }
131:
132: protected abstract Index makeIndex(String name, Column col,
133: boolean unique, File dataDirectory, int maxNodeSize,
134: boolean memorydb) throws AxionException;
135:
136: }
|