jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sal...@apache.org
Subject svn commit: r1325586 - in /incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http: QuadEntity.java QuadIteratorEntity.java TripleEntity.java TripleIteratorEntity.java UpdateRemote.java
Date Fri, 13 Apr 2012 01:48:03 GMT
Author: sallen
Date: Fri Apr 13 01:48:03 2012
New Revision: 1325586

URL: http://svn.apache.org/viewvc?rev=1325586&view=rev
Log:
More changes for streaming update support.

Added:
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java
Removed:
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadEntity.java
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleEntity.java
Modified:
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java

Added: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java?rev=1325586&view=auto
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java
(added)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java
Fri Apr 13 01:48:03 2012
@@ -0,0 +1,142 @@
+package org.apache.jena.client.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.util.Iterator;
+
+import org.apache.http.entity.AbstractHttpEntity;
+import org.openjena.atlas.io.IO;
+import org.openjena.atlas.iterator.PeekIterator;
+import org.openjena.atlas.lib.NotImplemented;
+import org.openjena.riot.out.NodeFormatter;
+import org.openjena.riot.out.NodeFormatterNT;
+
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.sparql.core.Quad;
+
+class QuadIteratorEntity extends AbstractHttpEntity
+{
+    private final boolean insert;
+    private final Iterator<Quad> it;
+    private final NodeFormatter nodeFmt = new NodeFormatterNT() ;
+    
+    public QuadIteratorEntity(boolean insert, Iterator<Quad> it)
+    {
+        this.insert = insert;
+        this.it = it;
+    }
+
+    @Override
+    public boolean isRepeatable()
+    {
+        return false;
+    }
+
+    @Override
+    public long getContentLength()
+    {
+        return -1;
+    }
+    
+    @Override
+    public boolean isStreaming()
+    {
+        return true;
+    }
+
+    @Override
+    public InputStream getContent() throws IOException, IllegalStateException
+    {
+        throw new NotImplemented("TripleIteratorEntity is only meant to be used as a Request
entity.");
+    }
+
+    @Override
+    public void writeTo(OutputStream outstream) throws IOException
+    {
+        if (outstream == null)
+        {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+        
+        if (!it.hasNext()) return;
+        
+        // TODO Move this to UpdateWriter
+        // TODO Would be cool to use a Trig writer here
+        Writer out = IO.asBufferedUTF8(outstream) ;
+        try
+        {
+            String instruction = insert ? "INSERT" : "DELETE";
+            out.write(String.format("%1$s DATA {\n", instruction));
+            
+            PeekIterator<Quad> pit = new PeekIterator<Quad>(it);
+            while (pit.hasNext())
+            {
+                Quad quad = pit.next();
+                Node graphName = quad.isDefaultGraph() ? null : quad.getGraph();
+                
+                if (null != graphName)
+                {
+                    out.write(String.format("  graph <%1$s> {\n", graphName.getURI()));
+                }
+                
+                // This is a little optimization to not repeat the graph block if consecutive
quads have the same graph
+                while (true)
+                {
+                    out.write(null != graphName ? "    " : "  ");
+                    write(out, quad);
+                    
+                    if (!pit.hasNext()) break;
+                    
+                    Quad nextQuad = pit.peek();
+                    Node nextGraphName = nextQuad.isDefaultGraph() ? null : nextQuad.getGraph();
+                    
+                    if (!nodesAreEqual(graphName, nextGraphName)) break;
+                    
+                    quad = pit.next();
+                    
+                }
+                
+                if (null != graphName)
+                {
+                    out.write("\n  }");
+                }
+            }
+            
+            out.write("\n}");
+        }
+        finally
+        {
+            if (null != out)
+            {
+                out.flush();
+            }
+        }
+        
+    }
+    
+    private boolean nodesAreEqual(Node a, Node b)
+    {
+        if (null == a && null == b) return true;
+        if (null != a && null == b) return false;
+        return a.equals(b);
+    }
+    
+    // really just writing the triple part of the quad
+    private void write(Writer out, Quad quad) throws IOException
+    {
+        Node s = quad.getSubject() ;
+        Node p = quad.getPredicate() ;
+        Node o = quad.getObject() ;
+
+        nodeFmt.format(out, s) ;
+        out.write(" ") ;
+        nodeFmt.format(out, p) ;
+        out.write(" ") ;
+        nodeFmt.format(out, o) ;
+        out.write(" .\n") ;
+        
+    }
+
+}

Added: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java?rev=1325586&view=auto
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java
(added)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java
Fri Apr 13 01:48:03 2012
@@ -0,0 +1,88 @@
+package org.apache.jena.client.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Iterator;
+
+import org.apache.http.entity.AbstractHttpEntity;
+import org.openjena.atlas.lib.NotImplemented;
+import org.openjena.riot.RiotWriter;
+
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.Triple;
+import com.hp.hpl.jena.sparql.core.Quad;
+
+class TripleIteratorEntity extends AbstractHttpEntity
+{
+    private final Node graphName;
+    private final boolean insert;
+    private final Iterator<Triple> it;
+    
+    public TripleIteratorEntity(Node graphName, boolean insert, Iterator<Triple> it)
+    {
+        if (Quad.isDefaultGraph(graphName))
+        {
+            graphName = null;
+        }
+        this.graphName = graphName;
+        this.insert = insert;
+        this.it = it;
+    }
+
+    @Override
+    public boolean isRepeatable()
+    {
+        return false;
+    }
+
+    @Override
+    public long getContentLength()
+    {
+        return -1;
+    }
+    
+    @Override
+    public boolean isStreaming()
+    {
+        return true;
+    }
+
+    @Override
+    public InputStream getContent() throws IOException, IllegalStateException
+    {
+        throw new NotImplemented("TripleIteratorEntity is only meant to be used as a Request
entity.");
+    }
+
+    @Override
+    public void writeTo(OutputStream outstream) throws IOException
+    {
+        if (outstream == null)
+        {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+        
+        if (!it.hasNext()) return;
+        
+        // TODO Move this to UpdateWriter
+        String instruction = insert ? "INSERT" : "DELETE";
+        write(outstream, String.format("%1$s DATA {\n", instruction));
+        if (null != graphName)
+        {
+            write(outstream, String.format("graph <%1$s> {\n", graphName.getURI()));
+        }
+        
+        RiotWriter.writeTriples(outstream, it);
+        
+        if (null != graphName)
+        {
+            write(outstream, "\n}");
+        }
+        write(outstream, "\n}");
+    }
+
+    private void write(OutputStream out, String s) throws IOException
+    {
+        out.write(s.getBytes("UTF-8"));
+    }
+}

Modified: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java?rev=1325586&r1=1325585&r2=1325586&view=diff
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java
(original)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java
Fri Apr 13 01:48:03 2012
@@ -18,29 +18,29 @@
 
 package org.apache.jena.client.http;
 
-import java.io.ByteArrayOutputStream ;
-import java.io.IOException ;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.util.Iterator;
 
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse ;
-import org.apache.http.client.HttpClient ;
-import org.apache.http.client.methods.HttpPost ;
-import org.apache.http.entity.AbstractHttpEntity ;
-import org.apache.http.entity.ByteArrayEntity ;
-import org.apache.http.entity.InputStreamEntity;
-import org.apache.http.impl.client.DefaultHttpClient ;
-import org.apache.http.protocol.HTTP ;
-import org.openjena.atlas.io.IndentedWriter ;
-import org.openjena.riot.WebContent ;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.AbstractHttpEntity;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.protocol.HTTP;
+import org.openjena.atlas.io.IndentedWriter;
+import org.openjena.riot.WebContent;
 
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.Triple;
 import com.hp.hpl.jena.sparql.core.Quad;
-import com.hp.hpl.jena.sparql.modify.request.Target ;
-import com.hp.hpl.jena.sparql.modify.request.UpdateDrop ;
-import com.hp.hpl.jena.sparql.modify.request.UpdateWriter ;
-import com.hp.hpl.jena.update.Update ;
-import com.hp.hpl.jena.update.UpdateException ;
-import com.hp.hpl.jena.update.UpdateRequest ;
+import com.hp.hpl.jena.sparql.modify.request.Target;
+import com.hp.hpl.jena.sparql.modify.request.UpdateDrop;
+import com.hp.hpl.jena.sparql.modify.request.UpdateWriter;
+import com.hp.hpl.jena.update.Update;
+import com.hp.hpl.jena.update.UpdateException;
+import com.hp.hpl.jena.update.UpdateRequest;
 
 public class UpdateRemote
 {
@@ -71,24 +71,34 @@ public class UpdateRemote
         out.flush() ;
         byte[] bytes = b_out.toByteArray() ;
         AbstractHttpEntity reqEntity = new ByteArrayEntity(bytes) ;
-        reqEntity.setContentType(WebContent.contentTypeSPARQLUpdate) ;
-        reqEntity.setContentEncoding(HTTP.UTF_8) ;
-        execute(reqEntity);            
+        execute(reqEntity) ;
     }
     
     
+    public void add(Node graphName, Iterator<Triple> triples)
+    {
+        TripleIteratorEntity reqEntity = new TripleIteratorEntity(graphName, true, triples)
;
+        execute(reqEntity) ;
+    }
+    
     public void add(Iterator<Quad> quads)
     {
-        
+        QuadIteratorEntity reqEntity = new QuadIteratorEntity(true, quads) ;
+        execute(reqEntity) ;
     }
     
+    public void delete(Node graphName, Iterator<Triple> triples)
+    {
+        TripleIteratorEntity reqEntity = new TripleIteratorEntity(graphName, false, triples)
;
+        execute(reqEntity) ;
+    }
     
     public void delete(Iterator<Quad> quads)
     {
-        
+        QuadIteratorEntity reqEntity = new QuadIteratorEntity(false, quads) ;
+        execute(reqEntity) ;
     }
     
-    
     protected void execute(AbstractHttpEntity reqEntity)
     {
         HttpPost httpPost = new HttpPost(serviceURL) ;



Mime
View raw message