001: /*
002: * $Id: GlomLobsHelper.java,v 1.8 2005/12/20 18:32:28 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.tables;
042:
043: import java.io.BufferedInputStream;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.FileNotFoundException;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.io.SyncFailedException;
050:
051: import org.axiondb.AxionException;
052: import org.axiondb.Column;
053: import org.axiondb.Row;
054: import org.axiondb.engine.rows.SimpleRow;
055: import org.axiondb.io.AxionFileSystem;
056: import org.axiondb.io.BufferedDataOutputStream;
057: import org.axiondb.types.FileLobLocator;
058: import org.axiondb.types.FileOffsetLobLocator;
059: import org.axiondb.types.FileOffsetLobLocatorFactory;
060: import org.axiondb.types.LOBType;
061: import org.axiondb.types.LobLocator;
062:
063: /**
064: * An Utility to glom lobs.
065: *
066: * @version $Revision: 1.8 $ $Date: 2005/12/20 18:32:28 $
067: * @author Chuck Burdick
068: * @author Rodney Waldhoff
069: * @author Ahimanikya Satapathy
070: */
071: public final class GlomLobsHelper {
072:
073: public GlomLobsHelper(DiskTable table) {
074: _table = table;
075: }
076:
077: public void glomLobs() throws Exception {
078: // the glommed .data file
079: BufferedDataOutputStream gdata = BaseDiskTable.FS
080: .createBufferedDOS(new File(getTable().getRootDir(),
081: getTable().getName() + ".DATA.GLOM"));
082: // the glommed .pidx file
083: File gpidxfile = getTable().getTableFile(".PIDX.GLOM");
084:
085: BufferedDataOutputStream[] glom = createGlommedLobFiles();
086: AxionFileSystem.PidxList gpidx = BaseDiskTable.FS.newPidxList(
087: getTable().getPidxList().size(), gpidxfile, false);
088:
089: // FOR EACH ROW
090: for (int i = 0, I = getTable().getPidxList().size(); i < I; i++) {
091: long oldoffset = getTable().getPidxList().get(i);
092: if (oldoffset == BaseDiskTable.INVALID_OFFSET) {
093: gpidx.add(BaseDiskTable.INVALID_OFFSET);
094: } else {
095: SimpleRow grow = glomRow(glom, i, oldoffset);
096: gpidx.add(gdata.getPos());
097: writeGlommedRow(gdata, glom, grow);
098: }
099: }
100:
101: closeFiles(glom);
102:
103: // CLOSE OUT THE GLOMMED .DATA and .PIDX FILE
104: gdata.close();
105: gpidx.close();
106: }
107:
108: private void closeFiles(BufferedDataOutputStream[] glom)
109: throws IOException {
110: for (int i = 0; i < glom.length; i++) {
111: if (glom[i] != null) {
112: glom[i].close();
113: }
114: }
115: }
116:
117: private BufferedDataOutputStream[] createGlommedLobFiles()
118: throws AxionException {
119: BufferedDataOutputStream[] glom = new BufferedDataOutputStream[getTable()
120: .getColumnCount()];
121: for (int i = 0; i < glom.length; i++) {
122: Column col = getTable().getColumn(i);
123: if (col.getDataType() instanceof LOBType) {
124: glom[i] = BaseDiskTable.FS
125: .createBufferedDOS(new File(getTable()
126: .getLobDir(), col.getName() + ".GLOM"));
127: } else {
128: glom[i] = null;
129: }
130: }
131: return glom;
132: }
133:
134: private void glomLobColumn(BufferedDataOutputStream[] glom,
135: Row row, SimpleRow grow, int k) throws AxionException,
136: IOException, FileNotFoundException, SyncFailedException {
137: if (null != glom[k]) {
138: // GET THE OLD FILE
139: Column col = getTable().getColumn(k);
140: FileLobLocator loc = (FileLobLocator) (col.getDataType()
141: .convert(row.get(k)));
142: if (loc != null) {
143: File oldfile = loc.getFile(((LOBType) (col
144: .getDataType())).getLobDir());
145: // WRITE IT TO THE NEW FILE
146: long offset = glom[k].getPos();
147: int length = 0;
148: InputStream in = new BufferedInputStream(
149: new FileInputStream(oldfile));
150: for (int b = in.read(); b != -1; b = in.read()) {
151: glom[k].write(b);
152: length++;
153: }
154: in.close();
155:
156: // SET THE LOB LOCATOR FOR THAT
157: FileOffsetLobLocator gloc = new FileOffsetLobLocator(
158: offset, length);
159: grow.set(k, gloc);
160: } else {
161: grow.set(k, null);
162: }
163: }
164: }
165:
166: private SimpleRow glomRow(BufferedDataOutputStream[] glom, int i,
167: long oldoffset) throws AxionException, IOException,
168: FileNotFoundException, SyncFailedException {
169: Row row = getTable().getRowByOffset(i, oldoffset);
170: SimpleRow grow = new SimpleRow(row);
171: // FOR EACH COLUMN
172: for (int k = 0; k < glom.length; k++) {
173: // FOR EACH LOB COLUMN
174: glomLobColumn(glom, row, grow, k);
175: }
176: return grow;
177: }
178:
179: private void writeGlommedRow(BufferedDataOutputStream gdata,
180: BufferedDataOutputStream[] glom, SimpleRow grow)
181: throws IOException {
182: for (int j = 0, J = getTable().getColumnCount(); j < J; j++) {
183: if (glom[j] != null) {
184: gdata.writeBoolean(true);
185: FILE_OFFSET_LOB_LOCATOR_FACTORY.write(
186: (LobLocator) (grow.get(j)), gdata);
187: } else {
188: getTable().getColumn(j).getDataType().write(
189: grow.get(j), gdata);
190: }
191: }
192: }
193:
194: private DiskTable getTable() {
195: return _table;
196: }
197:
198: private static final FileOffsetLobLocatorFactory FILE_OFFSET_LOB_LOCATOR_FACTORY = new FileOffsetLobLocatorFactory();
199: private DiskTable _table = null;
200: }
|