You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by km...@apache.org on 2005/04/01 05:25:46 UTC
svn commit: r159659 -
incubator/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/log/ChecksumOperation.java
Author: kmarsden
Date: Thu Mar 31 19:25:45 2005
New Revision: 159659
URL: http://svn.apache.org/viewcvs?view=rev&rev=159659
Log:
Missing file from suresh's checksum checkin
Added:
incubator/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/log/ChecksumOperation.java (with props)
Added: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/log/ChecksumOperation.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/log/ChecksumOperation.java?view=auto&rev=159659
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/log/ChecksumOperation.java (added)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/log/ChecksumOperation.java Thu Mar 31 19:25:45 2005
@@ -0,0 +1,266 @@
+/*
+
+ Derby - Class org.apache.derby.impl.store.raw.log.ChecksumOperation
+
+ Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ */
+
+package org.apache.derby.impl.store.raw.log;
+
+import org.apache.derby.iapi.services.sanity.SanityManager;
+import org.apache.derby.iapi.services.io.Formatable;
+import org.apache.derby.iapi.services.io.FormatIdUtil;
+import org.apache.derby.iapi.services.io.StoredFormatIds;
+import org.apache.derby.catalog.UUID;
+
+import org.apache.derby.iapi.store.raw.Transaction;
+import org.apache.derby.iapi.store.raw.Loggable;
+import org.apache.derby.iapi.store.raw.log.LogInstant;
+import org.apache.derby.iapi.store.raw.log.LogFactory;
+import org.apache.derby.iapi.store.raw.xact.RawTransaction;
+
+import org.apache.derby.iapi.error.StandardException;
+
+import org.apache.derby.iapi.services.io.CompressedNumber;
+import org.apache.derby.iapi.util.ByteArray;
+
+import java.io.Externalizable;
+import java.io.OutputStream;
+import java.io.InputStream;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.IOException;
+import org.apache.derby.iapi.services.io.LimitObjectInput;
+
+import java.util.zip.Checksum;
+import java.util.zip.CRC32;
+
+
+/**
+ A Log Operation that represents a checksum for a group of log records
+ that are written to the tranaction log file.
+
+ <PRE>
+ @format_id LOGOP_CHECKSUM
+ the formatId is written by FormatIdOutputStream when this object is
+ written out by writeObject
+ @purpose checksum one or more log records while writing to disk
+ @upgrade
+ @disk_layout
+ checksumAlgo(byte) the checksum algorithm
+ checksumValue(long) the checksum value
+ dataLength(int) number of bytes that the checksum is calculated
+ @end_format
+ </PRE>
+
+ @author Suresh Thalamati
+ @see Loggable
+*/
+
+public class ChecksumOperation implements Loggable
+{
+
+ private byte checksumAlgo;
+ private long checksumValue;
+ private int dataLength;
+ private Checksum checksum;
+
+ /*
+ * constant values for algorithm that are used to perform the checksum.
+ */
+ public static final byte CRC32_ALGORITHM = (byte) 0x1; //java.util.zip.CRC32
+
+ private static final int formatLength = FormatIdUtil.getFormatIdByteLength(StoredFormatIds.LOGOP_CHECKSUM);
+
+ public void init()
+ {
+ this.checksumAlgo = CRC32_ALGORITHM;
+ initializeChecksumAlgo();
+ dataLength = 0;
+ }
+
+
+ // update the checksum
+ protected void update(byte[] buf, int off, int len)
+ {
+ checksum.update(buf, off , len);
+ dataLength += len;
+ }
+
+
+ // reset the checksum
+ protected void reset()
+ {
+ checksum.reset();
+ dataLength = 0;
+ }
+
+
+ private void initializeChecksumAlgo()
+ {
+ if(checksumAlgo == CRC32_ALGORITHM)
+ this.checksum = new CRC32();
+ }
+
+
+ /*
+ * Formatable methods
+ */
+
+ // no-arg constructor, required by Formatable
+ public ChecksumOperation() { super();}
+
+ public void writeExternal(ObjectOutput out) throws IOException
+ {
+ checksumValue = checksum.getValue();
+ out.writeByte(checksumAlgo);
+ out.writeInt(dataLength);
+ out.writeLong(checksumValue);
+ }
+
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
+ {
+ checksumAlgo = (byte) in.readUnsignedByte();
+ dataLength = in.readInt();
+ checksumValue = in.readLong();
+ initializeChecksumAlgo();
+ }
+
+
+ public int getStoredSize()
+ {
+ return formatLength + 1 + 4 + 8;
+ }
+
+
+
+ /**
+ Return my format identifier.
+ */
+ public int getTypeFormatId() {
+ return StoredFormatIds.LOGOP_CHECKSUM;
+ }
+
+
+
+
+
+ /**
+ Loggable methods
+ */
+
+ /**
+ * Nothing to do for the checksum log record because it does need to be
+ * applied during redo.
+ */
+ public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in) throws StandardException
+ {
+ }
+
+ /**
+ the default for prepared log is always null for all the operations
+ that don't have optionalData. If an operation has optional data,
+ the operation need to prepare the optional data for this method.
+
+ Checksum has no optional data to write out
+
+ @param out Where and how to write to optional data.
+
+ */
+ public ByteArray getPreparedLog()
+ {
+ return (ByteArray) null;
+ }
+
+ /**
+ Checksum does not need to be redone, it is used to just verify that
+ log records are written completely.
+ */
+ public boolean needsRedo(Transaction xact)
+ {
+ return false;
+ }
+
+
+ /**
+ Checksum has no resources to release
+ */
+ public void releaseResource(Transaction xact)
+ {}
+
+ /**
+ Checksum is a raw store operation
+ */
+ public int group()
+ {
+ return Loggable.RAWSTORE | Loggable.CHECKSUM;
+ }
+
+
+
+
+ /**
+ * Access attributes of the checksum log record
+ */
+
+ protected int getDataLength()
+ {
+ return dataLength;
+ }
+
+
+ protected boolean isChecksumValid(byte[] data, int off , int length)
+ {
+ checksum.reset();
+ checksum.update(data , off , length);
+ return checksum.getValue()== checksumValue;
+
+ }
+
+
+ /**
+ DEBUG: Print self.
+ */
+ public String toString()
+ {
+ if (SanityManager.DEBUG)
+ {
+ StringBuffer str = new StringBuffer(200)
+ .append("Checksum Operation ")
+ .append(" algorithm = ")
+ .append(checksumAlgo)
+ .append(" value = ")
+ .append(checksumValue)
+ .append(" data length= ").append(dataLength);
+
+ return str.toString();
+ }
+ else
+ return null;
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
Propchange: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/log/ChecksumOperation.java
------------------------------------------------------------------------------
svn:eol-style = native