001: /*
002: * $Id: BaseArrayIndexLoader.java,v 1.6 2005/05/02 22:22:47 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-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.indexes;
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.apache.commons.collections.primitives.ArrayIntList;
049: import org.apache.commons.collections.primitives.IntList;
050: import org.axiondb.AxionException;
051: import org.axiondb.Column;
052: import org.axiondb.Index;
053: import org.axiondb.IndexLoader;
054: import org.axiondb.Table;
055: import org.axiondb.io.AxionFileSystem;
056:
057: /**
058: * @version $Revision: 1.6 $ $Date: 2005/05/02 22:22:47 $
059: * @author Rodney Waldhoff
060: * @author Ahimanikya Satapathy
061: */
062: public abstract class BaseArrayIndexLoader implements IndexLoader {
063: public BaseArrayIndexLoader() {
064: }
065:
066: public final Index loadIndex(Table table, File dataDirectory)
067: throws AxionException {
068: ObjectInputStream in = null;
069: AxionFileSystem fs = new AxionFileSystem();
070: try {
071: String name = dataDirectory.getName();
072: File file = new File(dataDirectory, name + ".DATA");
073: in = fs.openObjectInputSteam(file);
074:
075: int ver = in.readInt();
076: if (ver != 1) {
077: throw new AxionException("Can't parse data file "
078: + file + " for index " + name
079: + ", unrecognized data file version " + ver);
080: }
081:
082: String col = in.readUTF();
083: boolean unique = in.readBoolean();
084: Object keys = readKeys(in);
085: IntList values = readIntList(in);
086: return makeIndex(name, table.getColumn(col), unique, keys,
087: values);
088: } catch (ClassNotFoundException e) {
089: throw new AxionException(e);
090: } catch (IOException e) {
091: throw new AxionException(e);
092: } finally {
093: fs.closeInputStream(in);
094: }
095: }
096:
097: public final void saveIndex(Index ndx, File dataDirectory)
098: throws AxionException {
099: BaseArrayIndex index = (BaseArrayIndex) ndx;
100: ObjectOutputStream out = null;
101: AxionFileSystem fs = new AxionFileSystem();
102: try {
103:
104: String name = index.getName();
105: File file = new File(dataDirectory, name + ".DATA");
106: out = fs.createObjectOutputSteam(file);
107:
108: out.writeInt(1); // write version number
109: out.writeUTF(index.getIndexedColumn().getName()); // write column name
110: out.writeBoolean(index.isUnique()); // write unique flag
111: writeKeys(out, index); // write keys
112: IntList valuelist = index.getValueList(); // write values
113: out.writeInt(valuelist.size()); // write size
114:
115: // values
116: for (int i = 0, I = valuelist.size(); i < I; i++) {
117: out.writeInt(valuelist.get(i));
118: }
119: out.flush();
120: } catch (IOException e) {
121: throw new AxionException(e);
122: } finally {
123: fs.closeOutputStream(out);
124: }
125: }
126:
127: public void saveIndexAfterTruncate(Index ndx, File dataDirectory)
128: throws AxionException {
129: saveIndex(ndx, dataDirectory);
130: }
131:
132: protected abstract Index makeIndex(String name, Column col,
133: boolean unique, Object keys, IntList values);
134:
135: protected IntList readIntList(ObjectInputStream in)
136: throws IOException {
137: int size = in.readInt();
138: IntList list = new ArrayIntList(size);
139: for (int i = 0; i < size; i++) {
140: list.add(in.readInt());
141: }
142: return list;
143: }
144:
145: protected abstract Object readKeys(ObjectInputStream in)
146: throws IOException, ClassNotFoundException;
147:
148: protected abstract void writeKeys(ObjectOutputStream out,
149: BaseArrayIndex baseindex) throws IOException;
150:
151: }
|