/** * 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); }
/** Moves onto the next result possibility. */ @Override public QuerySolution nextSolution() { return new ResultBinding(model, nextBinding()) ; }
/** Moves onto the next result possibility. * The returned object should be of class QuerySolution */ @Override public QuerySolution next() { return nextSolution() ; }
@Override public ResultSet apply(ResultSet input) { ResultSetMem tmp = (ResultSetMem)input; //ResultSet result = ResultSetFactory.makeRewindable(input); ResultSetMem result = new ResultSetMem(tmp); tmp.rewind(); return result; }
final ResultSetMem resultMem = new ResultSetMem(queryResults); final int count = resultMem.size();
/** Reset this result set back to the beginning */ public void rewind( ) { reset() ; }
@Override public QuerySolution peek() { return new ResultBinding(model, peekBinding()); }
public ResultSetMem() { this.varNames = new ArrayList<>(); reset(); }
/** * 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); }
/** * Create an in-memory result set from another one * * @param imrs2 * The other ResultSetMem object * @param takeCopy * Should we copy the rows? */ public ResultSetMem(ResultSetMem imrs2, boolean takeCopy) { varNames = imrs2.varNames; if ( takeCopy ) rows.addAll(imrs2.rows); else // Share results (not the iterator). rows = imrs2.rows; reset(); }
/** * 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); }
/** * 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 ResultSet objects to concatenate. */ public ResultSetMem(ResultSet... sets) { varNames = sets[0].getResultVars(); for ( ResultSet rs : sets ) { if ( !varNames.equals(rs.getResultVars()) ) throw new ResultSetException("ResultSet must have the same variables."); if ( rs instanceof ResultSetMem ) rows.addAll(((ResultSetMem)rs).rows); else while (rs.hasNext()) rows.add(rs.nextBinding()); } reset(); }
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(); } }
/** * Create an in-memory result set from any ResultSet object. If the * ResultSet is an in-memory one already, then no copying is done - the * necessary internal datastructures are shared. This operation destroys * (uses up) a ResultSet object that is not an in-memory one. */ public ResultSetMem(ResultSet qr) { model = qr.getResourceModel(); if ( qr instanceof ResultSetMem ) { ResultSetMem qrm = (ResultSetMem)qr; this.rows = qrm.rows; this.varNames = qrm.varNames; } else { varNames = qr.getResultVars(); List<Var> vars = Var.varList(varNames); while (qr.hasNext()) { Binding rb = BindingFactory.copy(qr.nextBinding()); rows.add(rb); } } reset(); }
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)) ; }
@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_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)) ; }