Quadcap Embeddable Database

com/quadcap/sql/file/MemoryBlockStore.java

Go to the documentation of this file.
00001 package com.quadcap.sql.file; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.File; 00042 import java.io.FileDescriptor; 00043 import java.io.IOException; 00044 import java.io.RandomAccessFile; 00045 00046 import java.util.BitSet; 00047 00048 import com.quadcap.util.collections.LongIterator; 00049 import com.quadcap.util.collections.LongMap; 00050 00051 import com.quadcap.util.Debug; 00052 import com.quadcap.util.Util; 00053 00054 import com.quadcap.sql.Version; 00055 00056 /** 00057 * Below the cache, this class handles the actual I/O to the underlying file. 00058 * Block write operations are logged by this class. 00059 * 00060 * @author Stan Bailes 00061 */ 00062 public class MemoryBlockStore extends BlockStore { 00063 LongMap blocks; 00064 00065 public MemoryBlockStore() {} 00066 00067 /** 00068 * Initialize a new BlockStore object using the specified file and 00069 * blocksize. 00070 * 00071 * @param file the underlying file. 00072 * @param mode "r" for readonly access, otherwise "rw" 00073 * @param blocksize the block size to use when creating the file. 00074 * @param the synchronization object 00075 */ 00076 public void init(File file, String mode, int blockSize, Object lock) 00077 throws IOException 00078 { 00079 this.blockSize = blockSize; 00080 this.lock = lock; 00081 this.blocks = new LongMap(8888); 00082 createHeader(blockSize); 00083 } 00084 00085 /** 00086 * Read a block into a buffer. If the specified block is beyond the 00087 * current end of file, then grow the file 00088 * 00089 * @param blockNum the number of the block to read. 00090 * @param buf the buffer into which the data is read. 00091 */ 00092 public void read(long blockNum, byte[] buf, int off) 00093 throws IOException 00094 { 00095 //bug.println("read(" + blockNum + ")"); 00096 synchronized (lock) { 00097 byte[] block = (byte[])blocks.get(blockNum); 00098 if (block == null) { 00099 block = new byte[blockSize]; 00100 blocks.put(blockNum, block); 00101 } 00102 System.arraycopy(block, 0, buf, off, blockSize); 00103 readCount++; 00104 } 00105 } 00106 00107 /** 00108 * Write a block from a buffer into the file. 00109 * 00110 * @param blockNum the number of the block to write. 00111 * @param buf the buffer from which the data is written. 00112 * @exception IOException if an I/O error occurs. 00113 */ 00114 public void write(long blockNum, byte[] buf) 00115 throws IOException 00116 { 00117 //bug.println("write(" + blockNum + "): \n" + Util.strBytes(buf, 0, 64)); 00118 synchronized (lock) { 00119 byte[] block = (byte[])blocks.get(blockNum); 00120 boolean exists = (block != null); 00121 if (!exists) { 00122 block = new byte[blockSize]; 00123 blocks.put(blockNum, block); 00124 } 00125 if (log != null && !modified.get((int)blockNum)) { 00126 modified.set((int)blockNum); 00127 if (exists) { 00128 log.saveBlock(blockNum); 00129 } 00130 } 00131 System.arraycopy(buf, 0, block, 0, blockSize); 00132 writeCount++; 00133 //#ifdef DEBUG 00134 if (Trace.bit(20)) { 00135 Debug.println(toString() + ".write(" + blockNum + ")"); 00136 } 00137 //#endif 00138 } 00139 } 00140 00141 /** 00142 * Restore a block image 00143 */ 00144 public void restore(long blockNum, byte[] buf, int off) 00145 throws IOException 00146 { 00147 synchronized (lock) { 00148 byte[] block = (byte[]) blocks.get(blockNum); 00149 if (block == null) { 00150 throw new IOException("No block to restore: " + blockNum); 00151 } 00152 System.arraycopy(buf, off, block, 0, blockSize); 00153 } 00154 00155 } 00156 00157 public void setLength(long length) throws IOException { 00158 length /= blockSize; 00159 LongIterator iter = blocks.keys(); 00160 while (iter.hasNext()) { 00161 long blockNum = iter.nextLong(); 00162 if (blockNum >= length) { 00163 iter.remove(); 00164 } 00165 } 00166 } 00167 00168 private void createHeader(int blockSize) throws IOException { 00169 this.blockSize = blockSize; 00170 byte[] buf = newHeader(blockSize, 2); 00171 blocks.put(0, buf); 00172 } 00173 00174 public boolean isEncrypted() { 00175 return false; 00176 } 00177 00178 public void close() throws IOException { 00179 } 00180 00181 public void flush() throws IOException { 00182 readCount = writeCount = 0; 00183 } 00184 00185 //#ifdef JDK14 00186 public void setKey(com.quadcap.crypto.SymmetricKey key) 00187 throws IOException 00188 { 00189 throw new IOException("Not encrypted"); 00190 } 00191 //#endif 00192 00193 //#ifdef DEBUG 00194 public String toString() { 00195 return "MemoryBlockStore()"; 00196 } 00197 00198 public static String rw() { 00199 return "(r=" + BlockStore.readCount + 00200 " w=" + BlockStore.writeCount + ")"; 00201 } 00202 00203 //#endif 00204 00205 }