[Mulgara-svn] r1041 - in trunk/src/jar: query/java/org/mulgara/connection query/java/org/mulgara/server resolver/java/org/mulgara/resolver server-beep/java/org/mulgara/server/beep server-rmi/java/org/mulgara/server/rmi

pag at mulgara.org pag at mulgara.org
Wed Jul 2 21:35:56 UTC 2008


Author: pag
Date: 2008-07-02 14:35:55 -0700 (Wed, 02 Jul 2008)
New Revision: 1041

Modified:
   trunk/src/jar/query/java/org/mulgara/connection/CommandExecutor.java
   trunk/src/jar/query/java/org/mulgara/connection/Connection.java
   trunk/src/jar/query/java/org/mulgara/server/Session.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java
   trunk/src/jar/server-beep/java/org/mulgara/server/beep/BEEPSession.java
   trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/JRDFSessionWrapperRemoteJRDFSession.java
   trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java
   trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java
   trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java
Log:
Updated all sessions to handle different query types

Modified: trunk/src/jar/query/java/org/mulgara/connection/CommandExecutor.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/connection/CommandExecutor.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/query/java/org/mulgara/connection/CommandExecutor.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -12,6 +12,10 @@
 package org.mulgara.connection;
 
 import org.mulgara.query.Answer;
+import org.mulgara.query.AskQuery;
+import org.mulgara.query.BooleanAnswer;
+import org.mulgara.query.ConstructQuery;
+import org.mulgara.query.GraphAnswer;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
 import org.mulgara.query.TuplesException;
@@ -50,4 +54,18 @@
     return (Answer)cmd.execute(this);
   }
 
+  /**
+   * @see org.mulgara.connection.Connection#execute(org.mulgara.query.AskQuery)
+   */
+  public BooleanAnswer execute(AskQuery cmd) throws QueryException, TuplesException {
+    return (BooleanAnswer)cmd.execute(this);
+  }
+
+  /**
+   * @see org.mulgara.connection.Connection#execute(org.mulgara.query.AskQuery)
+   */
+  public GraphAnswer execute(ConstructQuery cmd) throws QueryException, TuplesException {
+    return (GraphAnswer)cmd.execute(this);
+  }
+
 }

Modified: trunk/src/jar/query/java/org/mulgara/connection/Connection.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/connection/Connection.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/query/java/org/mulgara/connection/Connection.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -14,6 +14,8 @@
 import java.net.URI;
 
 import org.mulgara.query.Answer;
+import org.mulgara.query.AskQuery;
+import org.mulgara.query.BooleanAnswer;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
 import org.mulgara.query.TuplesException;
@@ -163,4 +165,11 @@
    */
   public Answer execute(Query cmd) throws QueryException, TuplesException;
 
+  /**
+   * Issues an ASK query on the connection.
+   * @param cmd The ASK command to issue the query.
+   * @return A BooleanAnswer with the true/false result of the query.
+   */
+  public BooleanAnswer execute(AskQuery cmd) throws QueryException, TuplesException;
+
 }
\ No newline at end of file

Modified: trunk/src/jar/query/java/org/mulgara/server/Session.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/server/Session.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/query/java/org/mulgara/server/Session.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -38,6 +38,9 @@
 // Locally written packages
 import org.jrdf.graph.Triple;
 import org.mulgara.query.Answer;
+import org.mulgara.query.AskQuery;
+import org.mulgara.query.ConstructQuery;
+import org.mulgara.query.GraphAnswer;
 import org.mulgara.query.ModelExpression;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
@@ -186,6 +189,25 @@
   public Answer query(Query query) throws QueryException;
 
   /**
+   * Perform an ASK query.
+   *
+   * @param query the query
+   * @return <code>true</code> if the query results in data, <code>false</code> if
+   *         it results in the empty set.
+   * @throws QueryException if <var>query</var> can't be answered
+   */
+  public boolean query(AskQuery query) throws QueryException;
+
+  /**
+   * Perform a CONSTRUCT query.
+   *
+   * @param query the query
+   * @return An Answer that contains triples valid for a graph.
+   * @throws QueryException if <var>query</var> can't be answered
+   */
+  public GraphAnswer query(ConstructQuery query) throws QueryException;
+
+  /**
    * Performs multiple queries storing the results, answers, into the returned
    * list.
    *

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -789,7 +789,7 @@
   }
 
   public Answer doQuery(Query query) throws Exception {
-    TransactionalAnswer result;
+    Answer result;
 
     query = transform(query);
 
@@ -797,8 +797,16 @@
 
     // Complete the numerical phase of resolution
     Tuples tuples = localQuery.resolveE(query);
-    result = new TransactionalAnswer(transaction, new SubqueryAnswer(this, systemResolver, tuples, query.getVariableList()));
-    answers.put(result, null);
+    if (query instanceof AskQuery) {
+      // strip the answer down to true/false
+      result = new BooleanAnswer(tuples.getRowCardinality() != 0);
+    } else {
+      result = new TransactionalAnswer(transaction, new SubqueryAnswer(this, systemResolver, tuples, query.getVariableList()));
+      answers.put((TransactionalAnswer)result, null);
+
+      // check if the query was a CONSTRUCT, and wrap in a graph filter if needed
+      if (query instanceof ConstructQuery) result = new GraphAnswer(result);
+    }
     tuples.close();
 
     return result;

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -393,6 +393,10 @@
   }
 
 
+  /**
+   * Execute a SELECT query on the database.
+   * @see org.mulgara.server.Session#query(org.mulgara.query.Query)
+   */
   public Answer query(Query query) throws QueryException {
     if (logger.isDebugEnabled()) logger.debug("QUERY: " + query);
 
@@ -402,6 +406,32 @@
   }
 
 
+  /**
+   * Tests the database to see if a query will return any data.
+   * @see org.mulgara.server.Session#query(org.mulgara.query.AskQuery)
+   */
+  public boolean query(AskQuery query) throws QueryException {
+    if (logger.isDebugEnabled()) logger.debug("ASK QUERY: " + query);
+
+    QueryOperation queryOperation = new QueryOperation(query, this);
+    execute(queryOperation, "Query failed");
+    return ((BooleanAnswer)queryOperation.getAnswer()).getResult();
+  }
+
+
+  /**
+   * Queries the database for data that will be structured as a graph.
+   * @see org.mulgara.server.Session#query(org.mulgara.query.ConstructQuery)
+   */
+  public GraphAnswer query(ConstructQuery query) throws QueryException {
+    if (logger.isDebugEnabled()) logger.debug("CONSTRUCT QUERY: " + query);
+
+    QueryOperation queryOperation = new QueryOperation(query, this);
+    execute(queryOperation, "Query failed");
+    return (GraphAnswer)queryOperation.getAnswer();
+  }
+
+
   public List<Answer> query(List<Query> queryList) throws QueryException {
     if (logger.isDebugEnabled()) {
       StringBuffer log = new StringBuffer("QUERYING LIST: ");

Modified: trunk/src/jar/server-beep/java/org/mulgara/server/beep/BEEPSession.java
===================================================================
--- trunk/src/jar/server-beep/java/org/mulgara/server/beep/BEEPSession.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/server-beep/java/org/mulgara/server/beep/BEEPSession.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -50,9 +50,13 @@
 import org.beepcore.beep.lib.Reply;
 import org.jrdf.graph.Triple;
 import org.mulgara.query.Answer;
+import org.mulgara.query.AskQuery;
+import org.mulgara.query.ConstructQuery;
+import org.mulgara.query.GraphAnswer;
 import org.mulgara.query.ModelExpression;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
+import org.mulgara.query.TuplesException;
 import org.mulgara.rules.RulesRef;
 import org.mulgara.server.Session;
 import org.mulgara.sparql.protocol.StreamFormatException;
@@ -332,6 +336,33 @@
   }
 
   /**
+   * @see org.mulgara.server.Session#query(org.mulgara.query.AskQuery)
+   */
+  public boolean query(AskQuery query) throws QueryException {
+    Answer ans = query((Query)query);
+    try {
+      ans.beforeFirst();
+      ans.next();
+      return (Boolean)ans.getObject(0);
+    } catch (TuplesException e) {
+      throw new QueryException("Unable to access a boolean answer", e);
+    } finally {
+      try {
+        ans.close();
+      } catch (TuplesException e) {
+        throw new QueryException("Unable to release a boolean answer", e);
+      }
+    }
+  }
+
+  /**
+   * @see org.mulgara.server.Session#query(org.mulgara.query.ConstructQuery)
+   */
+  public GraphAnswer query(ConstructQuery query) throws QueryException {
+    return new GraphAnswer(query((Query)query));
+  }
+
+  /**
    * Make a list of TQL queries.
    *
    * @param queries the list of queries

Modified: trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/JRDFSessionWrapperRemoteJRDFSession.java
===================================================================
--- trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/JRDFSessionWrapperRemoteJRDFSession.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/JRDFSessionWrapperRemoteJRDFSession.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -205,24 +205,26 @@
     // Generate the answer locally
     Answer localAnswer = jrdfSession.query(query);
 
-    RemoteAnswer remoteAnswer;
+    return convertToRemoteAnswer(localAnswer);
+  }
 
-    try {
-      // determine if this answer can be fully serialised or if it needs paging
-      if (localAnswer.getRowUpperBound() <= RemoteAnswer.MARSHALL_SIZE_LIMIT) {
-        remoteAnswer = new AnswerWrapperRemoteAnswerSerialised(new ArrayAnswer(localAnswer));
-        localAnswer.close();
-      } else {
-        remoteAnswer = new AnswerWrapperRemoteAnswer(localAnswer);
-      }
-    }
-    catch (TuplesException e) {
-      throw new QueryException("Unable to resolve answer: ", e);
-    }
+  public boolean query(AskQuery query) throws QueryException, RemoteException {
+    return jrdfSession.query(query);
+  }
 
-    return remoteAnswer;
+  /**
+   * Queries the local server and returns a remote reference to an Answer.
+   *
+   * @param query The query to perform.
+   * @return A remote reference to an Answer.
+   * @throws QueryException The query caused an exception.
+   * @throws RemoteException Thrown when there is a network error.
+   */
+  public RemoteAnswer query(ConstructQuery query) throws QueryException, RemoteException {
+    return convertToRemoteAnswer(jrdfSession.query(query));
   }
 
+
   public void insert(URI modelURI, Set statements)
       throws QueryException, RemoteException {
 
@@ -361,4 +363,19 @@
     if (t instanceof GraphException) return (GraphException) t;
     return new GraphException(t.toString(), t);
   }
+
+  private RemoteAnswer convertToRemoteAnswer(Answer ans) throws QueryException, RemoteException {
+    try {
+      if (ans.getRowUpperBound() <= RemoteAnswer.MARSHALL_SIZE_LIMIT) {
+        RemoteAnswer serialAnswer = new AnswerWrapperRemoteAnswerSerialised(new ArrayAnswer(ans));
+        ans.close();
+        return serialAnswer;
+      } else {
+        return new AnswerWrapperRemoteAnswer(ans);
+      }
+    } catch (TuplesException e) {
+      throw new QueryException("Unable to resolve answer", e);
+    }
+  }
+
 }

Modified: trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java
===================================================================
--- trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -36,6 +36,8 @@
 
 // Locally written packages
 import org.jrdf.graph.Triple;
+import org.mulgara.query.AskQuery;
+import org.mulgara.query.ConstructQuery;
 import org.mulgara.query.ModelExpression;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
@@ -249,15 +251,15 @@
   public void commit() throws QueryException, RemoteException;
 
   /**
-   * METHOD TO DO
+   * Roll back the current transaction.
    *
-   * @throws QueryException EXCEPTION TO DO
-   * @throws RemoteException EXCEPTION TO DO
+   * @throws QueryException An error occured in the abandoning of the transaction.
+   * @throws RemoteException An exception coming from the network connection.
    */
   public void rollback() throws QueryException, RemoteException;
 
   /**
-   * Make a TQL query.
+   * Make a query.
    *
    * @param query the query
    * @return a non-<code>null</code> answer to the <var>query</var>
@@ -267,6 +269,26 @@
   public RemoteAnswer query(Query query) throws QueryException, RemoteException;
 
   /**
+   * Make an ASK query.
+   *
+   * @param query the query
+   * @return <code>true</code> if the query returns a result.
+   * @throws QueryException if <var>query</var> can't be answered
+   * @throws RemoteException if the remote connection fails
+   */
+  public boolean query(AskQuery query) throws QueryException, RemoteException;
+
+  /**
+   * Make a CONSTRUCT query.
+   *
+   * @param query the query
+   * @return an Answer containing the triples for a graph.
+   * @throws QueryException if <var>query</var> can't be answered
+   * @throws RemoteException if the remote connection fails
+   */
+  public RemoteAnswer query(ConstructQuery query) throws QueryException, RemoteException;
+
+  /**
    * Make a list of TQL query.
    *
    * @param queries A list of queries

Modified: trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java
===================================================================
--- trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -43,6 +43,9 @@
 // Locally written packages
 import org.jrdf.graph.Triple;
 import org.mulgara.query.Answer;
+import org.mulgara.query.AskQuery;
+import org.mulgara.query.ConstructQuery;
+import org.mulgara.query.GraphAnswer;
 import org.mulgara.query.ModelExpression;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
@@ -507,7 +510,37 @@
 
   /**
    * {@inheritDoc}
+   * Provices a lightweight GraphAnswer wrapper over the returned Answer (which is already
+   * a GraphAnswer at the server end).
    */
+  public GraphAnswer query(ConstructQuery query) throws QueryException {
+    try {
+      RemoteAnswer ans = remoteSession.query(query);
+      resetRetries();
+      return new GraphAnswer(new RemoteAnswerWrapperAnswer(ans));
+    } catch (RemoteException e) {
+      testRetry(e);
+      return query(query);
+    }
+  }
+
+
+  /**
+   * {@inheritDoc}
+   */
+  public boolean query(AskQuery query) throws QueryException {
+    try {
+      return remoteSession.query(query);
+    } catch (RemoteException e) {
+      testRetry(e);
+      return query(query);
+    }
+  }
+
+
+  /**
+   * {@inheritDoc}
+   */
   public void close() throws QueryException {
 
     try {

Modified: trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java
===================================================================
--- trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java	2008-07-02 21:33:34 UTC (rev 1040)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java	2008-07-02 21:35:55 UTC (rev 1041)
@@ -42,6 +42,8 @@
 import org.jrdf.graph.Triple;
 import org.mulgara.query.Answer;
 import org.mulgara.query.ArrayAnswer;
+import org.mulgara.query.AskQuery;
+import org.mulgara.query.ConstructQuery;
 import org.mulgara.query.ModelExpression;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
@@ -356,27 +358,38 @@
    * @throws RemoteException Thrown when there is a network error.
    */
   public RemoteAnswer query(Query query) throws QueryException, RemoteException {
+    return convertToRemoteAnswer(session.query(query));
+  }
 
+  /**
+   * Queries the local server and returns the boolean result.
+   *
+   * @param query The query to perform.
+   * @return <code>true</code> if the query returns a non-empty result.
+   * @throws QueryException The query caused an exception.
+   * @throws RemoteException Thrown when there is a network error.
+   */
+  public boolean query(AskQuery query) throws QueryException, RemoteException {
     try {
-      Answer ans = session.query(query);
-      try {
-        if (ans.getRowUpperBound() <= RemoteAnswer.MARSHALL_SIZE_LIMIT) {
-          RemoteAnswer serialAnswer = new AnswerWrapperRemoteAnswerSerialised(new
-              ArrayAnswer(ans));
-          ans.close();
-          return serialAnswer;
-        } else {
-          return new AnswerWrapperRemoteAnswer(ans);
-        }
-      } catch (TuplesException e) {
-        throw new QueryException("Error getting information for answer", e);
-      }
+      return session.query(query);
     } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
 
   /**
+   * Queries the local server and returns a remote reference to an Answer.
+   *
+   * @param query The query to perform.
+   * @return A remote reference to an Answer.
+   * @throws QueryException The query caused an exception.
+   * @throws RemoteException Thrown when there is a network error.
+   */
+  public RemoteAnswer query(ConstructQuery query) throws QueryException, RemoteException {
+    return convertToRemoteAnswer(session.query(query));
+  }
+
+  /**
    * Queries a local server for a list of queries.  Wraps the resulting answers in remote
    * objects before returning them.
    *
@@ -562,6 +575,32 @@
   }
 
   /**
+   * Converts an Answer to a RemoteAnswer. Closure of the original Answer is handled.
+   * @param ans The Answer to convert.
+   * @return A new RemoteAnswer containing the same data as the original Answer. This
+   *         needs to be closed when it is finished with.
+   * @throws QueryException Accessing the data caused an exception.
+   * @throws RemoteException Thrown when there is a network error.
+   */
+  private RemoteAnswer convertToRemoteAnswer(Answer ans) throws QueryException, RemoteException {
+    try {
+      try {
+        if (ans.getRowUpperBound() <= RemoteAnswer.MARSHALL_SIZE_LIMIT) {
+          RemoteAnswer serialAnswer = new AnswerWrapperRemoteAnswerSerialised(new ArrayAnswer(ans));
+          ans.close();
+          return serialAnswer;
+        } else {
+          return new AnswerWrapperRemoteAnswer(ans);
+        }
+      } catch (TuplesException e) {
+        throw new QueryException("Error getting information for answer", e);
+      }
+    } catch (Throwable t) {
+      throw convertToQueryException(t);
+    }
+  }
+
+  /**
    * Return t if it is already a QueryException or wrap it as one.
    *
    * @return t if it is already a QueryException or wrap it as one.




More information about the Mulgara-svn mailing list