001: /*
002: * $Id: StringBTreeIndexLoader.java,v 1.5 2005/04/24 21:29:11 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2003-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.engine;
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.Index;
050: import org.axiondb.Table;
051: import org.axiondb.engine.indexes.ObjectBTreeIndex;
052: import org.axiondb.engine.indexes.StringBTreeIndex;
053: import org.axiondb.io.AxionFileSystem;
054:
055: /**
056: * @version $Revision: 1.5 $ $Date: 2005/04/24 21:29:11 $
057: * @author Rodney Waldhoff
058: * @author Ahimanikya Satapathy
059: */
060: public class StringBTreeIndexLoader extends ObjectBTreeIndexLoader {
061: public StringBTreeIndexLoader() {
062: }
063:
064: public Index loadIndex(Table table, File dataDirectory)
065: throws AxionException {
066: ObjectInputStream in = null;
067: AxionFileSystem fs = new AxionFileSystem();
068: try {
069: String name = dataDirectory.getName();
070: File file = new File(dataDirectory, name + ".data");
071: in = fs.openObjectInputSteam(file);
072:
073: int ver = in.readInt(); // read version number
074: if (ver != VERSION_NUMBER) {
075: throw new AxionException("Can't parse data file "
076: + file + " for index " + name
077: + ", unrecognized data file version " + ver);
078: }
079:
080: String col = in.readUTF(); // read column name
081: boolean unique = in.readBoolean(); // read unique flag
082: int minimizationFactor = in.readInt(); // read minimization factor
083:
084: return new StringBTreeIndex(name, table.getColumn(col),
085: unique, minimizationFactor, dataDirectory);
086: } catch (IOException e) {
087: throw new AxionException(e);
088: } finally {
089: fs.closeInputStream(in);
090: }
091: }
092:
093: public void saveIndex(Index ndx, File dataDirectory)
094: throws AxionException {
095: ObjectBTreeIndex index = (ObjectBTreeIndex) ndx;
096: ObjectOutputStream out = null;
097: try {
098: String name = index.getName();
099: File file = new File(dataDirectory, name + ".data");
100: out = FS.createObjectOutputSteam(file);
101: out.writeInt(VERSION_NUMBER); // write version number
102: out.writeUTF(index.getIndexedColumn().getName()); // write column name
103: out.writeBoolean(index.isUnique()); // write unique flag
104: out.writeInt(((StringBTreeIndex) index)
105: .getMinimizationFactor()); // write minimization factor
106: } catch (IOException e) {
107: throw new AxionException(e);
108: } finally {
109: FS.closeOutputStream(out);
110: }
111: save(ndx, dataDirectory);
112: }
113:
114: private static final int VERSION_NUMBER = 2;
115: }
|