avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1435347 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/file/ lang/java/avro/src/test/java/org/apache/avro/file/ lang/java/avro/src/test/java/org/apache/avro/file/codec/
Date Fri, 18 Jan 2013 21:15:13 GMT
Author: cutting
Date: Fri Jan 18 21:15:13 2013
New Revision: 1435347

URL: http://svn.apache.org/viewvc?rev=1435347&view=rev
Log:
AVRO-970. Java: Make Codec API public.

Added:
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/TestCustomCodec.java   (with
props)
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/codec/
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/codec/CustomCodec.java  
(with props)
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/Codec.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/DeflateCodec.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/NullCodec.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/SnappyCodec.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1435347&r1=1435346&r2=1435347&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Jan 18 21:15:13 2013
@@ -17,6 +17,8 @@ Trunk (not yet released)
     AVRO-1008. Java: Improve support for forcing connection
     handshakes. (jbaldassari & cutting)
 
+    AVRO-970. Java: Make Codec API public. (Rui Pereira via cutting)
+
   BUG FIXES
 
     AVRO-1231. Java: Fix Trevni shredder to work on non-recursive

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/Codec.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/Codec.java?rev=1435347&r1=1435346&r2=1435347&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/Codec.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/Codec.java Fri Jan 18 21:15:13
2013
@@ -22,16 +22,14 @@ import java.nio.ByteBuffer;
 
 /** 
  * Interface for Avro-supported compression codecs for data files.
- *
- * This is currently exclusively an internal-facing API.
  */
-abstract class Codec {
+public abstract class Codec {
   /** Name of the codec; written to the file's metadata. */
-  abstract String getName();
+  public abstract String getName();
   /** Compresses the input data */
-  abstract ByteBuffer compress(ByteBuffer uncompressedData) throws IOException;
+  public abstract ByteBuffer compress(ByteBuffer uncompressedData) throws IOException;
   /** Decompress the data  */
-  abstract ByteBuffer decompress(ByteBuffer compressedData) throws IOException;
+  public abstract ByteBuffer decompress(ByteBuffer compressedData) throws IOException;
   /** 
    * Codecs must implement an equals() method.  Two codecs, A and B are equal
    * if: the result of A and B decompressing content compressed by A is the same

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/DeflateCodec.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/DeflateCodec.java?rev=1435347&r1=1435346&r2=1435347&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/DeflateCodec.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/DeflateCodec.java Fri Jan
18 21:15:13 2013
@@ -63,12 +63,12 @@ class DeflateCodec extends Codec {
   }
 
   @Override
-  String getName() {
+  public String getName() {
     return DataFileConstants.DEFLATE_CODEC;
   }
 
   @Override
-  ByteBuffer compress(ByteBuffer data) throws IOException {
+  public ByteBuffer compress(ByteBuffer data) throws IOException {
     ByteArrayOutputStream baos = getOutputBuffer(data.remaining());
     DeflaterOutputStream ios = new DeflaterOutputStream(baos, getDeflater());
     writeAndClose(data, ios);
@@ -77,7 +77,7 @@ class DeflateCodec extends Codec {
   }
 
   @Override
-  ByteBuffer decompress(ByteBuffer data) throws IOException {
+  public ByteBuffer decompress(ByteBuffer data) throws IOException {
     ByteArrayOutputStream baos = getOutputBuffer(data.remaining());
     InflaterOutputStream ios = new InflaterOutputStream(baos, getInflater());
     writeAndClose(data, ios);

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/NullCodec.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/NullCodec.java?rev=1435347&r1=1435346&r2=1435347&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/NullCodec.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/NullCodec.java Fri Jan 18
21:15:13 2013
@@ -36,17 +36,17 @@ final class NullCodec extends Codec {
   public static final CodecFactory OPTION = new Option();
 
   @Override
-  String getName() {
+  public String getName() {
     return DataFileConstants.NULL_CODEC;
   }
 
   @Override
-  ByteBuffer compress(ByteBuffer buffer) throws IOException {
+  public ByteBuffer compress(ByteBuffer buffer) throws IOException {
     return buffer;
   }
 
   @Override
-  ByteBuffer decompress(ByteBuffer data) throws IOException {
+  public ByteBuffer decompress(ByteBuffer data) throws IOException {
     return data;
   }
 

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/SnappyCodec.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/SnappyCodec.java?rev=1435347&r1=1435346&r2=1435347&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/SnappyCodec.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/file/SnappyCodec.java Fri Jan
18 21:15:13 2013
@@ -36,10 +36,10 @@ class SnappyCodec extends Codec {
 
   private SnappyCodec() {}
 
-  @Override String getName() { return DataFileConstants.SNAPPY_CODEC; }
+  @Override public String getName() { return DataFileConstants.SNAPPY_CODEC; }
 
   @Override
-  ByteBuffer compress(ByteBuffer in) throws IOException {
+  public ByteBuffer compress(ByteBuffer in) throws IOException {
     ByteBuffer out =
       ByteBuffer.allocate(Snappy.maxCompressedLength(in.remaining())+4);
     int size = Snappy.compress(in.array(), in.position(), in.remaining(),
@@ -54,7 +54,7 @@ class SnappyCodec extends Codec {
   }
 
   @Override
-  ByteBuffer decompress(ByteBuffer in) throws IOException {
+  public ByteBuffer decompress(ByteBuffer in) throws IOException {
     ByteBuffer out = ByteBuffer.allocate
       (Snappy.uncompressedLength(in.array(),in.position(),in.remaining()-4));
     int size = Snappy.uncompress(in.array(),in.position(),in.remaining()-4,

Added: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/TestCustomCodec.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/TestCustomCodec.java?rev=1435347&view=auto
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/TestCustomCodec.java (added)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/TestCustomCodec.java Fri
Jan 18 21:15:13 2013
@@ -0,0 +1,58 @@
+/**
+ * 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.avro.file;
+
+
+import org.apache.avro.file.codec.CustomCodec;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class TestCustomCodec {
+
+  @Test
+  public void testCustomCodec() {
+    CustomCodec customCodec = new CustomCodec();
+    Codec snappyCodec = new SnappyCodec.Option().createInstance();
+    assertTrue(customCodec.equals(new CustomCodec()));
+    assertFalse(customCodec.equals(snappyCodec));
+
+    String testString = "Testing 123";
+    ByteBuffer original = ByteBuffer.allocate(testString.getBytes().length);
+    original.put(testString.getBytes());
+    original.rewind();
+    ByteBuffer decompressed = null;
+    try {
+      ByteBuffer compressed = customCodec.compress(original);
+      compressed.rewind();
+      decompressed = customCodec.decompress(compressed);
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+
+    assertEquals(testString, new String(decompressed.array()));
+
+  }
+
+}
\ No newline at end of file

Propchange: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/TestCustomCodec.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/codec/CustomCodec.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/codec/CustomCodec.java?rev=1435347&view=auto
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/codec/CustomCodec.java (added)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/codec/CustomCodec.java Fri
Jan 18 21:15:13 2013
@@ -0,0 +1,94 @@
+/**
+ * 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.avro.file.codec;
+
+import org.apache.avro.file.Codec;
+
+import java.io.IOException;
+import java.nio.*;
+
+/**
+ * Simple Custom Codec to validate making Codec Public
+ * Compress and Decompress operations are just bitwise-NOT of data
+ */
+public class CustomCodec extends Codec {
+
+  private static final String CODECNAME = "CUSTOMCODEC";
+
+  @Override
+  public String getName() {
+    return CODECNAME;
+  }
+
+  @Override
+  public ByteBuffer compress(ByteBuffer in) throws IOException {
+    ByteBuffer out = ByteBuffer.allocate(in.remaining());
+    while (in.position() < in.capacity())
+      out.put((byte) ~in.get());
+    return out;
+  }
+
+  @Override
+  public ByteBuffer decompress(ByteBuffer in) throws IOException {
+    ByteBuffer out = ByteBuffer.allocate(in.remaining());
+    while (in.position() < in.capacity())
+      out.put((byte) ~in.get());
+    return out;
+  }
+
+  @Override
+  public boolean equals(Object other) {
+    if (this == other)
+      return true;
+    if (other instanceof Codec) {
+      ByteBuffer original = ByteBuffer.allocate(getName().getBytes().length);
+      original.put(getName().getBytes());
+      original.rewind();
+      try {
+        return compareDecompress((Codec) other, original);
+      } catch (IOException e) {
+        return false;
+      }
+    } else
+      return false;
+  }
+
+  /**
+   * Codecs must implement an equals() method.  Two codecs, A and B are equal
+   * if: the result of A and B decompressing content compressed by A is the same
+   * AND the retult of A and B decompressing content compressed by B is the same
+   */
+  private boolean compareDecompress(Codec other, ByteBuffer original) throws IOException
{
+    ByteBuffer compressedA = this.compress(original);
+    original.rewind();
+    ByteBuffer compressedB = other.compress(original);
+
+    if (this.decompress(compressedA).equals(other.decompress((ByteBuffer) compressedA.rewind()))
&&
+      this.decompress(compressedB).equals(other.decompress((ByteBuffer) compressedB.rewind()))
+      ) {
+      return true;
+    }
+    return false;
+  }
+
+  @Override
+  public int hashCode() {
+    return getName().hashCode();
+  }
+}

Propchange: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/file/codec/CustomCodec.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message