You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2014/09/11 12:18:00 UTC

svn commit: r1624250 - in /jena/Experimental/hadoop-rdf/hadoop-rdf-common/src: main/java/org/apache/jena/hadoop/rdf/types/ main/java/org/apache/jena/hadoop/rdf/types/comparators/ main/java/org/apache/jena/hadoop/rdf/types/compators/ test/java/org/apach...

Author: rvesse
Date: Thu Sep 11 10:18:00 2014
New Revision: 1624250

URL: http://svn.apache.org/r1624250
Log:
Add binary comparators for QuadWritable and TripleWritable, make NodeWritable more lazily instantiated

Added:
    jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/comparators/
      - copied from r1623994, jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/compators/
    jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/comparators/SimpleBinaryComparator.java
Removed:
    jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/comparators/NodeComparator.java
    jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/compators/
Modified:
    jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/NodeWritable.java
    jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/QuadWritable.java
    jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/TripleWritable.java
    jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/test/java/org/apache/jena/hadoop/rdf/io/types/RdfTypesTest.java

Modified: jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/NodeWritable.java
URL: http://svn.apache.org/viewvc/jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/NodeWritable.java?rev=1624250&r1=1624249&r2=1624250&view=diff
==============================================================================
--- jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/NodeWritable.java (original)
+++ jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/NodeWritable.java Thu Sep 11 10:18:00 2014
@@ -23,7 +23,7 @@ import java.io.DataOutput;
 import java.io.IOException;
 import org.apache.hadoop.io.WritableComparable;
 import org.apache.hadoop.io.WritableComparator;
-import org.apache.jena.hadoop.rdf.types.compators.NodeComparator;
+import org.apache.jena.hadoop.rdf.types.comparators.SimpleBinaryComparator;
 import org.apache.jena.hadoop.rdf.types.converters.ThriftConverter;
 import org.apache.jena.riot.thrift.TRDF;
 import org.apache.jena.riot.thrift.ThriftConvert;
@@ -35,11 +35,18 @@ import com.hp.hpl.jena.sparql.util.NodeU
 
 /**
  * A writable for {@link Node} instances
+ * <p>
+ * This uses <a
+ * href="http://afs.github.io/rdf-thrift/rdf-binary-thrift.html">RDF Thrift</a>
+ * for the binary encoding of terms. The in-memory storage for this type is both
+ * a {@link Node} and a {@link RDF_Term} with lazy conversion between the two
+ * forms as necessary.
+ * </p>
  */
 public class NodeWritable implements WritableComparable<NodeWritable> {
 
     static {
-        WritableComparator.define(NodeWritable.class, new NodeComparator());
+        WritableComparator.define(NodeWritable.class, new SimpleBinaryComparator());
     }
 
     private Node node;
@@ -82,6 +89,13 @@ public class NodeWritable implements Wri
      * @return Node
      */
     public Node get() {
+        // We may not have yet loaded the node
+        if (this.node == null) {
+            // If term is set to undefined then node is supposed to be null
+            if (this.term.isSet() && !this.term.isSetUndefined()) {
+                this.node = ThriftConvert.convert(this.term);
+            }
+        }
         return this.node;
     }
 
@@ -94,13 +108,18 @@ public class NodeWritable implements Wri
     public void set(Node n) {
         this.node = n;
         // Clear the term for now
-        // Only convert the Node to it as and when we want to write it out
+        // We only convert the Node to a term as and when we want to write it
+        // out in order to not waste effort if the value is never written out
         this.term.clear();
     }
 
     @Override
     public void readFields(DataInput input) throws IOException {
+        // Clear previous value
+        this.node = null;
         this.term.clear();
+
+        // Read in the new value
         int termLength = input.readInt();
         byte[] buffer = new byte[termLength];
         input.readFully(buffer);
@@ -109,7 +128,8 @@ public class NodeWritable implements Wri
         } catch (TException e) {
             throw new IOException(e);
         }
-        this.node = ThriftConvert.convert(this.term);
+
+        // Note that we don't convert it back into a Node at this time
     }
 
     @Override
@@ -123,6 +143,7 @@ public class NodeWritable implements Wri
             }
         }
 
+        // Write out the Thrift term
         byte[] buffer;
         try {
             buffer = ThriftConverter.toBytes(this.term);
@@ -135,19 +156,27 @@ public class NodeWritable implements Wri
 
     @Override
     public int compareTo(NodeWritable other) {
-        return NodeUtils.compareRDFTerms(this.node, other.node);
+        // Use get() rather than accessing the field directly because the node
+        // field is lazily instantiated from the Thrift term
+        return NodeUtils.compareRDFTerms(this.get(), other.get());
     }
 
     @Override
     public String toString() {
-        if (this.node == null)
+        // Use get() rather than accessing the field directly because the node
+        // field is lazily instantiated from the Thrift term
+        Node n = this.get();
+        if (n == null)
             return "";
-        return this.node.toString();
+        return n.toString();
     }
 
     @Override
     public int hashCode() {
-        return this.node.hashCode();
+        // Use get() rather than accessing the field directly because the node
+        // field is lazily instantiated from the Thrift term
+        Node n = this.get();
+        return n != null ? this.get().hashCode() : 0;
     }
 
     @Override

Modified: jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/QuadWritable.java
URL: http://svn.apache.org/viewvc/jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/QuadWritable.java?rev=1624250&r1=1624249&r2=1624250&view=diff
==============================================================================
--- jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/QuadWritable.java (original)
+++ jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/QuadWritable.java Thu Sep 11 10:18:00 2014
@@ -22,6 +22,8 @@ import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.hadoop.io.WritableComparator;
+import org.apache.jena.hadoop.rdf.types.comparators.SimpleBinaryComparator;
 import org.apache.jena.hadoop.rdf.types.converters.ThriftConverter;
 import org.apache.jena.riot.thrift.ThriftConvert;
 import org.apache.jena.riot.thrift.wire.RDF_Quad;
@@ -32,12 +34,13 @@ import com.hp.hpl.jena.sparql.core.Quad;
 
 /**
  * A writable quad
- * 
- * 
- * 
  */
 public class QuadWritable extends AbstractNodeTupleWritable<Quad> {
 
+    static {
+        WritableComparator.define(QuadWritable.class, new SimpleBinaryComparator());
+    }
+
     private RDF_Quad quad = new RDF_Quad();
 
     /**
@@ -97,7 +100,7 @@ public class QuadWritable extends Abstra
         if (this.get() == null)
             throw new IOException(
                     "Null quads cannot be written using this class, consider using NodeTupleWritable instead");
-        
+
         // May not have yet prepared the Thrift triple
         if (!this.quad.isSetS()) {
             Quad tuple = this.get();

Modified: jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/TripleWritable.java
URL: http://svn.apache.org/viewvc/jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/TripleWritable.java?rev=1624250&r1=1624249&r2=1624250&view=diff
==============================================================================
--- jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/TripleWritable.java (original)
+++ jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/TripleWritable.java Thu Sep 11 10:18:00 2014
@@ -22,6 +22,8 @@ import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.hadoop.io.WritableComparator;
+import org.apache.jena.hadoop.rdf.types.comparators.SimpleBinaryComparator;
 import org.apache.jena.hadoop.rdf.types.converters.ThriftConverter;
 import org.apache.jena.riot.thrift.ThriftConvert;
 import org.apache.jena.riot.thrift.wire.RDF_Triple;
@@ -37,6 +39,10 @@ import com.hp.hpl.jena.graph.Triple;
  * 
  */
 public class TripleWritable extends AbstractNodeTupleWritable<Triple> {
+    
+    static {
+        WritableComparator.define(TripleWritable.class, new SimpleBinaryComparator());
+    }
 
     private RDF_Triple triple = new RDF_Triple();
 

Added: jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/comparators/SimpleBinaryComparator.java
URL: http://svn.apache.org/viewvc/jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/comparators/SimpleBinaryComparator.java?rev=1624250&view=auto
==============================================================================
--- jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/comparators/SimpleBinaryComparator.java (added)
+++ jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/main/java/org/apache/jena/hadoop/rdf/types/comparators/SimpleBinaryComparator.java Thu Sep 11 10:18:00 2014
@@ -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.jena.hadoop.rdf.types.comparators;
+
+import org.apache.hadoop.io.WritableComparator;
+
+/**
+ * A general purpose comparator that may be used with any types which can be
+ * compared directly on their binary encodings
+ */
+public class SimpleBinaryComparator extends WritableComparator {
+
+    @Override
+    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
+        return WritableComparator.compareBytes(b1, s1, l1, b2, s2, l2);
+    }
+
+}

Modified: jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/test/java/org/apache/jena/hadoop/rdf/io/types/RdfTypesTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/test/java/org/apache/jena/hadoop/rdf/io/types/RdfTypesTest.java?rev=1624250&r1=1624249&r2=1624250&view=diff
==============================================================================
--- jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/test/java/org/apache/jena/hadoop/rdf/io/types/RdfTypesTest.java (original)
+++ jena/Experimental/hadoop-rdf/hadoop-rdf-common/src/test/java/org/apache/jena/hadoop/rdf/io/types/RdfTypesTest.java Thu Sep 11 10:18:00 2014
@@ -108,6 +108,51 @@ public class RdfTypesTest {
         // Check equivalent
         Assert.assertEquals(0, expected.compareTo(actual));
     }
+    
+    /**
+     * Basic node writable round tripping test
+     * 
+     * @throws IOException
+     * @throws InstantiationException
+     * @throws IllegalAccessException
+     * @throws ClassNotFoundException
+     */
+    @Test
+    public void node_writable_null() throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
+        Node n = null;
+        NodeWritable nw = new NodeWritable(n);
+        testWriteRead(nw, nw);
+    }
+    
+    /**
+     * Basic node writable round tripping test
+     * 
+     * @throws IOException
+     * @throws InstantiationException
+     * @throws IllegalAccessException
+     * @throws ClassNotFoundException
+     */
+    @Test
+    public void node_writable_variable_01() throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
+        Node n = NodeFactory.createVariable("x");
+        NodeWritable nw = new NodeWritable(n);
+        testWriteRead(nw, nw);
+    }
+    
+    /**
+     * Basic node writable round tripping test
+     * 
+     * @throws IOException
+     * @throws InstantiationException
+     * @throws IllegalAccessException
+     * @throws ClassNotFoundException
+     */
+    @Test
+    public void node_writable_variable_02() throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
+        Node n = NodeFactory.createVariable("really-log-variable-name-asddsfr4545egfdgdfgfdgdtgvdg-dfgfdgdfgdfgdfg4-dfvdfgdfgdfgfdgfdgdfgdfgfdg");
+        NodeWritable nw = new NodeWritable(n);
+        testWriteRead(nw, nw);
+    }
 
     /**
      * Basic node writable round tripping test