KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > log > ChecksumOperation


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.log.ChecksumOperation
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.raw.log;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.services.io.Formatable;
26 import org.apache.derby.iapi.services.io.FormatIdUtil;
27 import org.apache.derby.iapi.services.io.StoredFormatIds;
28 import org.apache.derby.catalog.UUID;
29
30 import org.apache.derby.iapi.store.raw.Transaction;
31 import org.apache.derby.iapi.store.raw.Loggable;
32 import org.apache.derby.iapi.store.raw.log.LogInstant;
33 import org.apache.derby.iapi.store.raw.log.LogFactory;
34 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
35
36 import org.apache.derby.iapi.error.StandardException;
37
38 import org.apache.derby.iapi.services.io.CompressedNumber;
39 import org.apache.derby.iapi.util.ByteArray;
40
41 import java.io.Externalizable JavaDoc;
42 import java.io.OutputStream JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.io.ObjectInput JavaDoc;
45 import java.io.ObjectOutput JavaDoc;
46 import java.io.IOException JavaDoc;
47 import org.apache.derby.iapi.services.io.LimitObjectInput;
48
49 import java.util.zip.Checksum JavaDoc;
50 import java.util.zip.CRC32 JavaDoc;
51
52
53 /**
54     A Log Operation that represents a checksum for a group of log records
55     that are written to the tranaction log file.
56
57     <PRE>
58     @format_id LOGOP_CHECKSUM
59         the formatId is written by FormatIdOutputStream when this object is
60         written out by writeObject
61     @purpose checksum one or more log records while writing to disk
62     @upgrade
63     @disk_layout
64         checksumAlgo(byte) the checksum algorithm
65         checksumValue(long) the checksum value
66         dataLength(int) number of bytes that the checksum is calculated
67     @end_format
68     </PRE>
69
70     @author Suresh Thalamati
71     @see Loggable
72 */

73
74 public class ChecksumOperation implements Loggable
75 {
76
77     private byte checksumAlgo;
78     private long checksumValue;
79     private int dataLength;
80     private Checksum JavaDoc checksum;
81
82     /*
83      * constant values for algorithm that are used to perform the checksum.
84      */

85     public static final byte CRC32_ALGORITHM = (byte) 0x1; //java.util.zip.CRC32
86

87     private static final int formatLength = FormatIdUtil.getFormatIdByteLength(StoredFormatIds.LOGOP_CHECKSUM);
88     
89     public void init()
90     {
91         this.checksumAlgo = CRC32_ALGORITHM;
92         initializeChecksumAlgo();
93         dataLength = 0;
94     }
95
96     
97     // update the checksum
98
protected void update(byte[] buf, int off, int len)
99     {
100         checksum.update(buf, off , len);
101         dataLength += len;
102     }
103
104     
105     // reset the checksum
106
protected void reset()
107     {
108         checksum.reset();
109         dataLength = 0;
110     }
111
112
113     private void initializeChecksumAlgo()
114     {
115         if(checksumAlgo == CRC32_ALGORITHM)
116             this.checksum = new CRC32 JavaDoc();
117     }
118
119
120     /*
121      * Formatable methods
122      */

123
124     // no-arg constructor, required by Formatable
125
public ChecksumOperation() { super();}
126
127     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
128     {
129         checksumValue = checksum.getValue();
130         out.writeByte(checksumAlgo);
131         out.writeInt(dataLength);
132         out.writeLong(checksumValue);
133     }
134
135     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
136     {
137         checksumAlgo = (byte) in.readUnsignedByte();
138         dataLength = in.readInt();
139         checksumValue = in.readLong();
140         initializeChecksumAlgo();
141     }
142
143
144     public int getStoredSize()
145     {
146         return formatLength + 1 + 4 + 8;
147     }
148
149
150
151     /**
152         Return my format identifier.
153     */

154     public int getTypeFormatId() {
155         return StoredFormatIds.LOGOP_CHECKSUM;
156     }
157
158
159
160
161
162     /**
163         Loggable methods
164     */

165
166     /**
167      * Nothing to do for the checksum log record because it does need to be
168      * applied during redo.
169      */

170     public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in) throws StandardException
171     {
172     }
173
174     /**
175         the default for prepared log is always null for all the operations
176         that don't have optionalData. If an operation has optional data,
177         the operation need to prepare the optional data for this method.
178
179         Checksum has no optional data to write out
180
181         
182     */

183     public ByteArray getPreparedLog()
184     {
185         return (ByteArray) null;
186     }
187
188     /**
189         Checksum does not need to be redone, it is used to just verify that
190         log records are written completely.
191     */

192     public boolean needsRedo(Transaction xact)
193     {
194         return false;
195     }
196
197
198     /**
199       Checksum has no resources to release
200     */

201     public void releaseResource(Transaction xact)
202     {}
203
204     /**
205         Checksum is a raw store operation
206     */

207     public int group()
208     {
209         return Loggable.RAWSTORE | Loggable.CHECKSUM;
210     }
211
212     
213
214
215     /**
216      * Access attributes of the checksum log record
217      */

218
219     protected int getDataLength()
220     {
221         return dataLength;
222     }
223
224
225     protected boolean isChecksumValid(byte[] data, int off , int length)
226     {
227         checksum.reset();
228         checksum.update(data , off , length);
229         return checksum.getValue()== checksumValue;
230
231     }
232
233
234     /**
235       DEBUG: Print self.
236     */

237     public String JavaDoc toString()
238     {
239         if (SanityManager.DEBUG)
240         {
241             StringBuffer JavaDoc str = new StringBuffer JavaDoc(200)
242                 .append("Checksum Operation ")
243                 .append(" algorithm = ")
244                 .append(checksumAlgo)
245                 .append(" value = ")
246                 .append(checksumValue)
247                 .append(" data length= ").append(dataLength);
248
249                 return str.toString();
250         }
251         else
252             return null;
253     }
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
Popular Tags