avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1607197 - in /avro/trunk: ./ doc/src/content/xdocs/ lang/java/avro/src/main/java/org/apache/avro/io/ lang/java/avro/src/main/java/org/apache/avro/io/parsing/ lang/java/avro/src/test/java/org/apache/avro/io/
Date Tue, 01 Jul 2014 21:33:48 GMT
Author: cutting
Date: Tue Jul  1 21:33:48 2014
New Revision: 1607197

URL: http://svn.apache.org/r1607197
Log:
AVRO-1533. Java: In schema resolution, permit conversion between bytes and string.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/doc/src/content/xdocs/spec.xml
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/ResolvingDecoder.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestResolvingIO.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1607197&r1=1607196&r2=1607197&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Jul  1 21:33:48 2014
@@ -27,6 +27,9 @@ Trunk (not yet released)
     AVRO-1352. Schema for fixed types corrupted when writing out in
     JSON format (Steve Roehrs via thiru)
 
+    AVRO-1533. Java: In schema resolution, permit conversion between
+    bytes and string. (cutting)
+
   OPTIMIZATIONS
 
     AVRO-1455. Deep copy does not need to create new instances for primitives.

Modified: avro/trunk/doc/src/content/xdocs/spec.xml
URL: http://svn.apache.org/viewvc/avro/trunk/doc/src/content/xdocs/spec.xml?rev=1607197&r1=1607196&r2=1607197&view=diff
==============================================================================
--- avro/trunk/doc/src/content/xdocs/spec.xml (original)
+++ avro/trunk/doc/src/content/xdocs/spec.xml Tue Jul  1 21:33:48 2014
@@ -1079,6 +1079,8 @@
                 <li>int is promotable to long, float, or double</li>
                 <li>long is promotable to float or double</li>
                 <li>float is promotable to double</li>
+                <li>string is promotable to bytes</li>
+                <li>bytes is promotable to string</li>
                 </ul>
             </li>
           </ul>

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/ResolvingDecoder.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/ResolvingDecoder.java?rev=1607197&r1=1607196&r2=1607197&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/ResolvingDecoder.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/ResolvingDecoder.java Tue Jul
 1 21:33:48 2014
@@ -18,11 +18,14 @@
 package org.apache.avro.io;
 
 import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
 
 import org.apache.avro.AvroTypeException;
 import org.apache.avro.Schema;
 import org.apache.avro.io.parsing.ResolvingGrammarGenerator;
 import org.apache.avro.io.parsing.Symbol;
+import org.apache.avro.util.Utf8;
 
 /**
  * {@link Decoder} that performs type-resolution between the reader's and
@@ -189,6 +192,64 @@ public class ResolvingDecoder extends Va
   }
   
   @Override
+  public Utf8 readString(Utf8 old) throws IOException {
+    Symbol actual = parser.advance(Symbol.STRING);
+    if (actual == Symbol.BYTES) {
+      return new Utf8(in.readBytes(null).array());
+    } else {
+      assert actual == Symbol.STRING;
+      return in.readString(old);
+    }
+  }
+
+  private static final Charset UTF8 = Charset.forName("UTF-8");
+
+  @Override
+  public String readString() throws IOException {
+    Symbol actual = parser.advance(Symbol.STRING);
+    if (actual == Symbol.BYTES) {
+      return new String(in.readBytes(null).array(), UTF8);
+    } else {
+      assert actual == Symbol.STRING;
+      return in.readString();
+    }
+  }
+
+  @Override
+  public void skipString() throws IOException {
+    Symbol actual = parser.advance(Symbol.STRING);
+    if (actual == Symbol.BYTES) {
+      in.skipBytes();
+    } else {
+      assert actual == Symbol.STRING;
+      in.skipString();
+    }
+  }
+
+  @Override
+  public ByteBuffer readBytes(ByteBuffer old) throws IOException {
+    Symbol actual = parser.advance(Symbol.BYTES);
+    if (actual == Symbol.STRING) {
+      Utf8 s = in.readString(null);
+      return ByteBuffer.wrap(s.getBytes(), 0, s.getByteLength());
+    } else {
+      assert actual == Symbol.BYTES;
+      return in.readBytes(old);
+    }
+  }
+
+  @Override
+  public void skipBytes() throws IOException {
+    Symbol actual = parser.advance(Symbol.BYTES);
+    if (actual == Symbol.STRING) {
+      in.skipString();
+    } else {
+      assert actual == Symbol.BYTES;
+      in.skipBytes();
+    }
+  }
+
+  @Override
   public int readEnum() throws IOException {
     parser.advance(Symbol.ENUM);
     Symbol.EnumAdjustAction top = (Symbol.EnumAdjustAction) parser.popSymbol();

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java?rev=1607197&r1=1607196&r2=1607197&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
(original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
Tue Jul  1 21:33:48 2014
@@ -151,6 +151,20 @@ public class ResolvingGrammarGenerator e
         }
         break;
   
+      case BYTES:
+        switch (writerType) {
+        case STRING:
+          return Symbol.resolve(super.generate(writer, seen), Symbol.BYTES);
+        }
+        break;
+  
+      case STRING:
+        switch (writerType) {
+        case BYTES:
+          return Symbol.resolve(super.generate(writer, seen), Symbol.STRING);
+        }
+        break;
+  
       case UNION:
         int j = bestBranch(reader, writer);
         if (j >= 0) {
@@ -161,8 +175,6 @@ public class ResolvingGrammarGenerator e
       case NULL:
       case BOOLEAN:
       case INT:
-      case STRING:
-      case BYTES:
       case ENUM:
       case ARRAY:
       case MAP:
@@ -449,6 +461,18 @@ public class ResolvingGrammarGenerator e
             return j;
           }
           break;
+        case STRING:
+          switch (b.getType()) {
+          case BYTES:
+            return j;
+          }
+          break;
+        case BYTES:
+          switch (b.getType()) {
+          case STRING:
+            return j;
+          }
+          break;
         }
         j++;
       }

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestResolvingIO.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestResolvingIO.java?rev=1607197&r1=1607196&r2=1607197&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestResolvingIO.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestResolvingIO.java Tue Jul
 1 21:33:48 2014
@@ -184,12 +184,14 @@ public class TestResolvingIO {
           + "{\"name\":\"f0\", \"type\":\"boolean\"},"
           + "{\"name\":\"f1\", \"type\":\"int\"},"
           + "{\"name\":\"f2\", \"type\":\"float\"},"
-          + "{\"name\":\"f3\", \"type\":\"string\"}]}", "BIFS",
+          + "{\"name\":\"f3\", \"type\":\"bytes\"},"
+          + "{\"name\":\"f4\", \"type\":\"string\"}]}", "BIFbS",
           "{\"type\":\"record\",\"name\":\"r\",\"fields\":["
           + "{\"name\":\"f0\", \"type\":\"boolean\"},"
           + "{\"name\":\"f1\", \"type\":\"long\"},"
           + "{\"name\":\"f2\", \"type\":\"double\"},"
-          + "{\"name\":\"f3\", \"type\":\"string\"}]}", "BLDS" },
+          + "{\"name\":\"f3\", \"type\":\"string\"},"
+          + "{\"name\":\"f4\", \"type\":\"bytes\"}]}", "BLDSb" },
 
         { "[\"int\"]", "U0I",
               "[\"long\"]", "U0L" },



Mime
View raw message