KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > file > MemoryBlockStore


1 package com.quadcap.sql.file;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.FileDescriptor JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.RandomAccessFile JavaDoc;
45
46 import java.util.BitSet JavaDoc;
47
48 import com.quadcap.util.collections.LongIterator;
49 import com.quadcap.util.collections.LongMap;
50
51 import com.quadcap.util.Debug;
52 import com.quadcap.util.Util;
53
54 import com.quadcap.sql.Version;
55
56 /**
57  * Below the cache, this class handles the actual I/O to the underlying file.
58  * Block write operations are logged by this class.
59  *
60  * @author Stan Bailes
61  */

62 public class MemoryBlockStore extends BlockStore {
63     LongMap blocks;
64
65     public MemoryBlockStore() {}
66
67     /**
68      * Initialize a new BlockStore object using the specified file and
69      * blocksize.
70      *
71      * @param file the underlying file.
72      * @param mode "r" for readonly access, otherwise "rw"
73      * @param blocksize the block size to use when creating the file.
74      * @param the synchronization object
75      */

76     public void init(File JavaDoc file, String JavaDoc mode, int blockSize, Object JavaDoc lock)
77         throws IOException JavaDoc
78     {
79         this.blockSize = blockSize;
80         this.lock = lock;
81         this.blocks = new LongMap(8888);
82         createHeader(blockSize);
83     }
84
85     /**
86      * Read a block into a buffer. If the specified block is beyond the
87      * current end of file, then grow the file
88      *
89      * @param blockNum the number of the block to read.
90      * @param buf the buffer into which the data is read.
91      */

92     public void read(long blockNum, byte[] buf, int off)
93     throws IOException JavaDoc
94     {
95         //bug.println("read(" + blockNum + ")");
96
synchronized (lock) {
97             byte[] block = (byte[])blocks.get(blockNum);
98             if (block == null) {
99                 block = new byte[blockSize];
100                 blocks.put(blockNum, block);
101             }
102             System.arraycopy(block, 0, buf, off, blockSize);
103             readCount++;
104         }
105     }
106     
107     /**
108      * Write a block from a buffer into the file.
109      *
110      * @param blockNum the number of the block to write.
111      * @param buf the buffer from which the data is written.
112      * @exception IOException if an I/O error occurs.
113      */

114     public void write(long blockNum, byte[] buf)
115     throws IOException JavaDoc
116     {
117         //bug.println("write(" + blockNum + "): \n" + Util.strBytes(buf, 0, 64));
118
synchronized (lock) {
119             byte[] block = (byte[])blocks.get(blockNum);
120             boolean exists = (block != null);
121             if (!exists) {
122                 block = new byte[blockSize];
123                 blocks.put(blockNum, block);
124             }
125             if (log != null && !modified.get((int)blockNum)) {
126                 modified.set((int)blockNum);
127                 if (exists) {
128                     log.saveBlock(blockNum);
129                 }
130             }
131             System.arraycopy(buf, 0, block, 0, blockSize);
132             writeCount++;
133             //#ifdef DEBUG
134
if (Trace.bit(20)) {
135                 Debug.println(toString() + ".write(" + blockNum + ")");
136             }
137             //#endif
138
}
139     }
140
141     /**
142      * Restore a block image
143      */

144     public void restore(long blockNum, byte[] buf, int off)
145         throws IOException JavaDoc
146     {
147         synchronized (lock) {
148             byte[] block = (byte[]) blocks.get(blockNum);
149             if (block == null) {
150                 throw new IOException JavaDoc("No block to restore: " + blockNum);
151             }
152             System.arraycopy(buf, off, block, 0, blockSize);
153         }
154         
155     }
156
157     public void setLength(long length) throws IOException JavaDoc {
158         length /= blockSize;
159         LongIterator iter = blocks.keys();
160         while (iter.hasNext()) {
161             long blockNum = iter.nextLong();
162             if (blockNum >= length) {
163                 iter.remove();
164             }
165         }
166     }
167
168     private void createHeader(int blockSize) throws IOException JavaDoc {
169         this.blockSize = blockSize;
170         byte[] buf = newHeader(blockSize, 2);
171         blocks.put(0, buf);
172     }
173
174     public boolean isEncrypted() {
175         return false;
176     }
177     
178     public void close() throws IOException JavaDoc {
179     }
180
181     public void flush() throws IOException JavaDoc {
182         readCount = writeCount = 0;
183     }
184
185     //#ifdef JDK14
186
public void setKey(com.quadcap.crypto.SymmetricKey key)
187         throws IOException JavaDoc
188     {
189         throw new IOException JavaDoc("Not encrypted");
190     }
191     //#endif
192

193     //#ifdef DEBUG
194
public String JavaDoc toString() {
195         return "MemoryBlockStore()";
196     }
197
198     public static String JavaDoc rw() {
199         return "(r=" + BlockStore.readCount +
200             " w=" + BlockStore.writeCount + ")";
201     }
202                    
203     //#endif
204

205 }
206
Popular Tags