crunch-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwi...@apache.org
Subject git commit: CRUNCH-181: UUID support added to o.a.c.types.PTypes. Contributd by Micah Whitacre.
Date Thu, 21 Mar 2013 05:07:35 GMT
Updated Branches:
  refs/heads/master 96f5e9f8c -> 0b8677579


CRUNCH-181: UUID support added to o.a.c.types.PTypes. Contributd by Micah Whitacre.


Project: http://git-wip-us.apache.org/repos/asf/crunch/repo
Commit: http://git-wip-us.apache.org/repos/asf/crunch/commit/0b867757
Tree: http://git-wip-us.apache.org/repos/asf/crunch/tree/0b867757
Diff: http://git-wip-us.apache.org/repos/asf/crunch/diff/0b867757

Branch: refs/heads/master
Commit: 0b8677579dc7fed083a1d46eb448b9c75f78763a
Parents: 96f5e9f
Author: Josh Wills <jwills@apache.org>
Authored: Wed Mar 20 13:08:52 2013 -0700
Committer: Josh Wills <jwills@apache.org>
Committed: Wed Mar 20 22:07:01 2013 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/crunch/types/PTypes.java  |   21 +++++++++
 .../java/org/apache/crunch/types/PTypesTest.java   |   34 +++++++++++++++
 2 files changed, 55 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/crunch/blob/0b867757/crunch/src/main/java/org/apache/crunch/types/PTypes.java
----------------------------------------------------------------------
diff --git a/crunch/src/main/java/org/apache/crunch/types/PTypes.java b/crunch/src/main/java/org/apache/crunch/types/PTypes.java
index 886d96c..546719c 100644
--- a/crunch/src/main/java/org/apache/crunch/types/PTypes.java
+++ b/crunch/src/main/java/org/apache/crunch/types/PTypes.java
@@ -19,6 +19,7 @@ package org.apache.crunch.types;
 
 import java.math.BigInteger;
 import java.nio.ByteBuffer;
+import java.util.UUID;
 
 import org.apache.crunch.CrunchRuntimeException;
 import org.apache.crunch.MapFn;
@@ -44,6 +45,10 @@ public class PTypes {
     return typeFamily.derived(BigInteger.class, BYTE_TO_BIGINT, BIGINT_TO_BYTE, typeFamily.bytes());
   }
 
+  public static PType<UUID> uuid(PTypeFamily ptf) {
+    return ptf.derived(UUID.class, BYTE_TO_UUID, UUID_TO_BYTE, ptf.bytes());
+  }
+  
   public static <T> PType<T> jsonString(Class<T> clazz, PTypeFamily typeFamily)
{
     return typeFamily
         .derived(clazz, new JacksonInputMapFn<T>(clazz), new JacksonOutputMapFn<T>(),
typeFamily.strings());
@@ -228,4 +233,20 @@ public class PTypes {
       return input.name();
     }
   };
+  
+  private static MapFn<ByteBuffer, UUID> BYTE_TO_UUID = new MapFn<ByteBuffer, UUID>()
{
+    @Override
+    public UUID map(ByteBuffer input) {
+      return new UUID(input.getLong(), input.getLong());
+    }
+  };
+  
+  private static MapFn<UUID, ByteBuffer> UUID_TO_BYTE = new MapFn<UUID, ByteBuffer>()
{
+    @Override
+    public ByteBuffer map(UUID input) {
+      ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
+      bb.asLongBuffer().put(input.getMostSignificantBits()).put(input.getLeastSignificantBits());
+      return bb;
+    }
+  };
 }

http://git-wip-us.apache.org/repos/asf/crunch/blob/0b867757/crunch/src/test/java/org/apache/crunch/types/PTypesTest.java
----------------------------------------------------------------------
diff --git a/crunch/src/test/java/org/apache/crunch/types/PTypesTest.java b/crunch/src/test/java/org/apache/crunch/types/PTypesTest.java
new file mode 100644
index 0000000..d7c8811
--- /dev/null
+++ b/crunch/src/test/java/org/apache/crunch/types/PTypesTest.java
@@ -0,0 +1,34 @@
+/**
+ * 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.crunch.types;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.UUID;
+
+import org.apache.crunch.types.avro.AvroTypeFamily;
+import org.junit.Test;
+
+public class PTypesTest {
+  @Test
+  public void testUUID() throws Exception {
+    UUID uuid = UUID.randomUUID();
+    PType<UUID> ptype = PTypes.uuid(AvroTypeFamily.getInstance());
+    assertEquals(uuid, ptype.getInputMapFn().map(ptype.getOutputMapFn().map(uuid)));
+  }
+}


Mime
View raw message