/** * Create an in-memory result set from an array of * ResulSets. It is assumed that all the ResultSets * from the array have the same variables. * * @param sets the ResultSets to concatenate. */ public static ResultSet union(ResultSet... sets) { return new ResultSetMem(sets); }
/** * Take a copy of a result set - the result set returns is an in-memory * copy. It is not attached to the original query execution object which can * be closed. * * @param results * @return ResultSet */ static public ResultSetRewindable copyResults(ResultSet results) { return new ResultSetMem(results); }
@Override public ResultSet apply(ResultSet input) { ResultSetMem tmp = (ResultSetMem)input; //ResultSet result = ResultSetFactory.makeRewindable(input); ResultSetMem result = new ResultSetMem(tmp); tmp.rewind(); return result; }
/** * Turn an existing result set into a rewindable one. * May take a copy but this is not guaranteed * Uses up the result set passed in which is no longer valid as a ResultSet. * * @param resultSet * @return ResultSetRewindable */ static public ResultSetRewindable makeRewindable(ResultSet resultSet) { if ( resultSet instanceof ResultSetRewindable ) { ResultSetRewindable rsw = (ResultSetRewindable)resultSet ; rsw.reset() ; return rsw ; } return new ResultSetMem(resultSet); }
protected ResultSet executeSelectQuery(String query) { logger.trace("Sending query\n{} ...", query); QueryExecution qe = qef.createQueryExecution(query); try { ResultSet rs = qe.execSelect(); timeout = false; return rs; } catch (QueryExceptionHTTP e) { if(e.getCause() instanceof SocketTimeoutException){ if(timeout){ logger.warn("Got timeout"); throw e; } else { logger.trace("Got local timeout"); } } else { logger.error("Exception executing query", e); } return new ResultSetMem(); } }
return new ResultSetMem();
private ResultSet execSelect(String queryStr) { // long startTime1 = System.currentTimeMillis(); // try { // // RepositoryConnection conn = getConnection(); // try { // GraphQuery q = conn.prepareGraphQuery(QueryLanguage.SPARQL, queryStr); // q.evaluate(); // } catch (MalformedQueryException e) { // throw new RuntimeException(e); // } finally { // conn.close(); // } // } catch (Exception re) { // //log.info(re,re); // } // log.info((System.currentTimeMillis() - startTime1) + " to execute via sesame"); Query askQuery = QueryFactory.create(queryStr); try (QueryExecution qe = QueryExecutionFactory.sparqlService(endpointURI, askQuery)) { return new ResultSetMem(qe.execSelect()); } }
@Test public void test_RS_1() { ResultSetRewindable rs1 = new ResultSetMem() ; ByteArrayOutputStream arr = new ByteArrayOutputStream() ; ResultSetFormatter.outputAsXML(arr, rs1) ; rs1.reset() ; ByteArrayInputStream ins = new ByteArrayInputStream(arr.toByteArray()) ; ResultSet rs2 = ResultSetFactory.fromXML(ins) ; assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
@Test public void test_RS_5() { ResultSetRewindable rs1 = new ResultSetMem() ; ByteArrayOutputStream arr = new ByteArrayOutputStream() ; ResultSetFormatter.outputAsJSON(arr, rs1) ; rs1.reset() ; ByteArrayInputStream ins = new ByteArrayInputStream(arr.toByteArray()) ; ResultSet rs2 = ResultSetFactory.fromJSON(ins) ; assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
final ResultSetMem resultMem = new ResultSetMem(queryResults);
@Test public void test_RS_cmp_1() { ResultSetRewindable rs1 = new ResultSetMem() ; ResultSetRewindable rs2 = new ResultSetMem() ; assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; rs1.reset() ; rs2.reset() ; assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
@Test public void test_RS_cmp_3() { ResultSet rs1 = make("x", org.apache.jena.graph.NodeFactory.createURI("tag:local")) ; ResultSet rs2 = new ResultSetMem() ; assertFalse(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
@Test public void test_RS_1_str() { ResultSetRewindable rs1 = new ResultSetMem() ; String x = ResultSetFormatter.asXMLString(rs1) ; rs1.reset() ; InputStream in = new ByteArrayInputStream(StrUtils.asUTF8bytes(x)); ResultSet rs2 = ResultSetFactory.fromXML(in) ; assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
@Test public void test_RS_cmp_2() { ResultSet rs1 = make("x", org.apache.jena.graph.NodeFactory.createURI("tag:local")) ; ResultSet rs2 = new ResultSetMem() ; assertFalse(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
@Test public void test_RS_3() { ResultSetRewindable rs1 = new ResultSetMem() ; Model model = RDFOutput.encodeAsModel(rs1) ; rs1.reset() ; ResultSet rs2 = RDFInput.fromRDF(model) ; assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
@Test public void test_RS_peeking_5() { // Peeking should be able to cope with people moving on the underlying result set independently ResultSet inner = new ResultSetMem(make("x", NodeFactory.createURI("tag:local")), make("x", NodeFactory.createURI("tag:local"))); ResultSetPeekable rs = ResultSetFactory.makePeekable(inner); assertTrue(rs.hasNext()); assertNotNull(rs.peek()); // Move on the inner result set independently inner.next(); // Since we fiddled with the underlying result set there won't be further elements available anymore assertFalse(rs.hasNext()); }
@Test public void test_RS_peeking_6() { // Peeking should be able to cope with people moving on the underlying result set independently ResultSet inner = new ResultSetMem(make("x", NodeFactory.createURI("tag:local")), make("x", NodeFactory.createURI("tag:local")), make("x", NodeFactory.createURI("tag:local"))); ResultSetPeekable rs = ResultSetFactory.makePeekable(inner); assertTrue(rs.hasNext()); assertNotNull(rs.peek()); // Move on the inner result set independently inner.next(); // Since we fiddled with the underlying result set we'll be out of sync // but there should still be further data available assertTrue(rs.hasNext()); }
@Test(expected=IllegalStateException.class) public void test_RS_peeking_8() { // Peeking may fail if someone moves backwards in the result set // If we had moved past the first item this should be an error ResultSetRewindable inner = new ResultSetMem(make("x", NodeFactory.createURI("tag:local")), make("x", NodeFactory.createURI("tag:local"))); ResultSetPeekable rs = ResultSetFactory.makePeekable(inner); assertTrue(rs.hasNext()); assertNotNull(rs.peek()); assertNotNull(rs.next()); // Reset the inner result set independently inner.reset(); // Since we moved the underlying result set backwards and had moved somewhere we // are now in an illegal state rs.hasNext(); }
@Test public void test_RS_peeking_9() { // Check that peeking causes the correct row to be returned when we actually access the rows Node first = NodeFactory.createURI("tag:first"); Node second = NodeFactory.createURI("tag:second"); Var x = Var.alloc("x"); ResultSet inner = new ResultSetMem(make("x", first), make("x", second)); ResultSetPeekable rs = ResultSetFactory.makePeekable(inner); assertTrue(rs.hasNext()); // Peek and check row is as expected Binding peeked = rs.peekBinding(); assertNotNull(peeked); assertTrue(first.equals(peeked.get(x))); // Check first row is as expected Binding next = rs.nextBinding(); assertNotNull(next); assertTrue(first.equals(next.get(x))); // Repeat for second row peeked = rs.peekBinding(); assertNotNull(peeked); assertTrue(second.equals(peeked.get(x))); next = rs.nextBinding(); assertNotNull(next); assertTrue(second.equals(next.get(x))); }