db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r902742 - in /db/derby/code/trunk/java/engine/org/apache/derby: iapi/services/io/ impl/store/raw/data/
Date Mon, 25 Jan 2010 09:21:54 GMT
Author: kristwaa
Date: Mon Jan 25 09:21:53 2010
New Revision: 902742

URL: http://svn.apache.org/viewvc?rev=902742&view=rev
Log:
DERBY-4520 (partial): Refactor and extend data type cloning facilities
Added functionality to clone store streams (without materialization).
Delayed filling the byte buffer in OverflowInputStream constructor and in
OverflowInputStream.resetStream.

Orginal patch contributed by Mike Matrigali (mikem_app at sbcglobal dot net) as
part of DERBY-3650, modified by Kristian Waagan (Kristian dot Waagan at Sun dot com).

Patch file: derby-4520-3b-CloneableStream_and_delayed_fill.diff

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CloneableStream.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatIdInputStream.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/ByteHolder.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/MemByteHolder.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/OverflowInputStream.java

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CloneableStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CloneableStream.java?rev=902742&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CloneableStream.java
(added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CloneableStream.java
Mon Jan 25 09:21:53 2010
@@ -0,0 +1,47 @@
+/*
+
+   Derby - Class org.apache.derby.iapi.services.io.CloneableStream
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you 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.iapi.services.io;
+
+import java.io.InputStream;
+
+/**
+ * This is a simple interface that is used by streams that can clone themselves.
+ * <p>
+ * The purpose is for the implementation of BLOB/CLOB (and potentially other
+ * types whose value is represented by a stream), for which their size makes it
+ * impossible or very expensive to materialize the value.
+ */
+public interface CloneableStream {
+
+    /**
+     * Clone the stream.
+     * <p>
+     * To be used when a "deep" clone of a stream is required rather than
+     * multiple references to the same stream.
+     * <p>
+     * The resulting clone should support reads, resets, closes which 
+     * do not affect the original stream source of the clone.
+     *
+     * @return The cloned stream.
+     */
+    public InputStream cloneStream() ;
+}

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/CloneableStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatIdInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatIdInputStream.java?rev=902742&r1=902741&r2=902742&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatIdInputStream.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatIdInputStream.java
Mon Jan 25 09:21:53 2010
@@ -26,7 +26,6 @@
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.StreamCorruptedException;
-import org.apache.derby.iapi.reference.SQLState;
 import org.apache.derby.iapi.services.monitor.Monitor;
 import org.apache.derby.iapi.services.sanity.SanityManager;
 import org.apache.derby.iapi.error.StandardException;
@@ -43,7 +42,7 @@
   information about the streams format and capabilites.
   */
 public final class FormatIdInputStream extends DataInputStream
-	 implements ErrorObjectInput, Resetable
+    implements ErrorObjectInput, Resetable, CloneableStream
 {
 	protected ClassFactory cf;
 	private ErrorInfo errorInfo;
@@ -235,5 +234,14 @@
         ((Resetable) in).closeStream();
     }
 
-}
+    /*** CloneableStream interface ***/
+
+    public InputStream cloneStream() {
+        if (SanityManager.DEBUG)
+            SanityManager.ASSERT(in instanceof CloneableStream);
 
+        InputStream new_input_stream = ((CloneableStream) in).cloneStream();
+
+        return(new FormatIdInputStream(new_input_stream));
+    }
+}

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/ByteHolder.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/ByteHolder.java?rev=902742&r1=902741&r2=902742&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/ByteHolder.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/ByteHolder.java Mon
Jan 25 09:21:53 2010
@@ -21,12 +21,9 @@
 
 package org.apache.derby.impl.store.raw.data;
 
-import org.apache.derby.iapi.services.sanity.SanityManager;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.Vector;
 
 /**
   Holder for a growing sequence of bytes. The ByteHolder supports a
@@ -152,4 +149,14 @@
 	  Return true if this is in writing mode.
 	  */
 	public boolean writingMode();
+
+    /**
+     * Return a byte holder matching existing type and size of current
+     * ByteHolder, but don't bother to fill the bytes. Normal usage is expected
+     * to reset the holding stream to the beginning, so the copy of current
+     * state would be wasted.
+     *
+     * @return An empty <code>ByteHolder</code>.
+     */
+    public ByteHolder cloneEmpty();
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/MemByteHolder.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/MemByteHolder.java?rev=902742&r1=902741&r2=902742&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/MemByteHolder.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/MemByteHolder.java
Mon Jan 25 09:21:53 2010
@@ -417,6 +417,13 @@
 		return writing;
 	}
 
+    /**
+     * @see ByteHolder#cloneEmpty
+     */
+    public ByteHolder cloneEmpty() {
+        return(new MemByteHolder(bufSize));
+    }
+
 	/**
 	  Get the next buffer for writing bytes.
 	  @exception IOException	Thrown on error

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/OverflowInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/OverflowInputStream.java?rev=902742&r1=902741&r2=902742&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/OverflowInputStream.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/OverflowInputStream.java
Mon Jan 25 09:21:53 2010
@@ -24,12 +24,16 @@
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.reference.SQLState;
 
+import org.apache.derby.iapi.services.io.CloneableStream;
+
 import org.apache.derby.iapi.store.raw.RecordHandle;
 
 import org.apache.derby.iapi.types.Resetable;
+
 import org.apache.derby.iapi.store.raw.LockingPolicy;
 import org.apache.derby.iapi.store.access.TransactionController;
 
+import java.io.InputStream;
 import java.io.IOException;
 
 /**
@@ -48,7 +52,7 @@
 
 public class OverflowInputStream
 extends BufferedByteHolderInputStream
-implements Resetable
+implements Resetable, CloneableStream
 {
     /**************************************************************************
      * Fields of the class
@@ -104,9 +108,7 @@
     BaseContainerHandle owner,
     long                overflowPage, 
     int                 overflowId, 
-    RecordHandle        recordToLock)
-        throws IOException, StandardException
-    {
+    RecordHandle        recordToLock) {
         super(bh);
         this.owner              = owner;
         this.overflowPage       = overflowPage;
@@ -114,8 +116,6 @@
         this.firstOverflowPage  = overflowPage;
         this.firstOverflowId    = overflowId;
         this.recordToLock       = recordToLock;
-
-        fillByteHolder();
     }
 
     /**************************************************************************
@@ -156,7 +156,11 @@
             }
             catch (StandardException se)
             {
-                throw new IOException(se.toString());
+                // Simplify this code when we can use the Java 1.5 constructor
+                // taking the cause as an argument.
+                IOException ioe = new IOException(se.toString());
+                ioe.initCause(se);
+                throw ioe;
             }
 
             this.bh.startReading();
@@ -298,9 +302,6 @@
         // completely clear the byte holder
         this.bh.clear();
         this.bh.startReading();
-
-        // fill the byte holder
-        fillByteHolder();
     }
 
     /**
@@ -314,4 +315,32 @@
         owner.close();
         initialized = false;
     }
+
+    /**************************************************************************
+     * Public Methods of CloneableStream Interface
+     **************************************************************************/
+
+    /**
+     * Clone this object.
+     * <p>
+     * Creates a deep copy of this object. The returned stream has its own
+     * working buffers and can be initialized, reset and read independently
+     * from this stream.
+     * <p>
+     * The cloned stream is set back to the beginning of stream, no matter
+     * where the current stream happens to be positioned.
+     *
+     * @return Copy of this stream which can be used independently.
+     */
+    public InputStream cloneStream() {
+        OverflowInputStream ret_stream = 
+            new OverflowInputStream(
+                bh.cloneEmpty(),
+                owner, 
+                firstOverflowPage, 
+                firstOverflowId, 
+                recordToLock);
+
+        return(ret_stream);
+    }
 }



Mime
View raw message