001: /*
002: * $Id: StringBTree.java,v 1.10 2005/12/20 18:32:42 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 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.util;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.io.ObjectInputStream;
046: import java.io.ObjectOutputStream;
047: import java.util.Comparator;
048:
049: import org.axiondb.io.AxionFileSystem;
050:
051: /**
052: * An {@link ObjectBTree}optimized for reading and writing Strings.
053: *
054: * @version $Revision: 1.10 $ $Date: 2005/12/20 18:32:42 $
055: * @author Rodney Waldhoff
056: * @author Ahimanikya Satapathy
057: */
058: public class StringBTree extends ObjectBTree {
059:
060: // constructors
061: //-------------------------------------------------------------------------
062:
063: /**
064: * Create or load a new root node.
065: */
066: public StringBTree(File idxDir, String idxName,
067: int minimizationFactor, Comparator comp)
068: throws IOException, ClassNotFoundException {
069: super (idxDir, idxName, minimizationFactor, comp);
070: }
071:
072: /**
073: * Create a new, non-root node.
074: */
075: private StringBTree(BTreeMetaData meta, Comparator comp)
076: throws IOException, ClassNotFoundException {
077: super (meta, comp);
078: }
079:
080: /**
081: * Create a non-root node by reading it from disk.
082: */
083: private StringBTree(BTreeMetaData meta, Comparator comp, int fileId)
084: throws IOException, ClassNotFoundException {
085: super (meta, comp, fileId);
086: }
087:
088: // protected
089: //-------------------------------------------------------------------------
090:
091: protected ObjectBTree createNode(BTreeMetaData meta, Comparator comp)
092: throws IOException, ClassNotFoundException {
093: return new StringBTree(meta, comp);
094: }
095:
096: protected ObjectBTree loadNode(BTreeMetaData meta, Comparator comp,
097: int fileId) throws IOException, ClassNotFoundException {
098: return new StringBTree(meta, comp, fileId);
099: }
100:
101: protected void read() throws IOException, ClassNotFoundException {
102: ObjectInputStream in = null;
103: AxionFileSystem fs = new AxionFileSystem();
104: try {
105: in = fs.openObjectInputSteam(getBTreeMetaData()
106: .getFileById(getFileId()));
107: int size = in.readInt();
108: for (int i = 0; i < size; i++) {
109: addKeyValuePair(in.readUTF(), in.readInt(), false);
110: }
111: size = in.readInt();
112: for (int i = 0; i < size; i++) {
113: getChildIds().add(in.readInt());
114: }
115: } finally {
116: fs.closeInputStream(in);
117: }
118: }
119:
120: protected void write() throws IOException {
121: ObjectOutputStream out = null;
122: AxionFileSystem fs = new AxionFileSystem();
123: try {
124: out = fs.createObjectOutputSteam(getBTreeMetaData()
125: .getFileById(getFileId()));
126: int size = size();
127: out.writeInt(size);
128: for (int i = 0; i < size; i++) {
129: out.writeUTF((String) getKey(i));
130: out.writeInt(getValue(i));
131: }
132:
133: size = getChildIds().size();
134: out.writeInt(size);
135: for (int i = 0; i < size; i++) {
136: out.writeInt(getChildIds().get(i));
137: }
138: } finally {
139: fs.closeOutputStream(out);
140: }
141: }
142:
143: protected Object getNullKey() {
144: return NullObject.INSTANCE.toString();
145: }
146:
147: }
|