Java Code Examples for org.eclipse.rdf4j.common.iteration.CloseableIteration#close()

The following examples show how to use org.eclipse.rdf4j.common.iteration.CloseableIteration#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JoinExecutorBase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public T getNextElement() throws QueryEvaluationException {
	// TODO check if we need to protect rightQueue from synchronized access
	// wasn't done in the original implementation either
	// if we see any weird behavior check here !!

	while (rightIter != null || rightQueue.hasNext()) {
		CloseableIteration<T, QueryEvaluationException> nextRightIter = rightIter;
		if (nextRightIter == null) {
			nextRightIter = rightIter = rightQueue.next();
		}
		if (nextRightIter != null) {
			if (nextRightIter.hasNext()) {
				return nextRightIter.next();
			} else {
				rightIter = null;
				nextRightIter.close();
			}
		}
	}

	return null;
}
 
Example 2
Source File: AccumuloRyaDAOTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testSameLiteralStringTypes() throws Exception {
    RyaIRI cpu = new RyaIRI(litdupsNS + "cpu");
    RyaIRI loadPerc = new RyaIRI(litdupsNS + "loadPerc");
    RyaType longLit = new RyaType(XMLSchema.LONG, "10");
    RyaType strLit = new RyaType(XMLSchema.STRING, new String(RyaContext.getInstance().serializeType(longLit)[0]));

    RyaStatement expected = new RyaStatement(cpu, loadPerc, longLit);
    dao.add(expected);
    dao.add(new RyaStatement(cpu, loadPerc, strLit));

    AccumuloRyaQueryEngine queryEngine = dao.getQueryEngine();

    CloseableIteration<RyaStatement, RyaDAOException> query = queryEngine.query(new RyaStatement(cpu, loadPerc, longLit), conf);
    assertTrue(query.hasNext());
    RyaStatement next = query.next();
    assertEquals(new Long(longLit.getData()), new Long(next.getObject().getData()));
    assertEquals(longLit.getDataType(), next.getObject().getDataType());
    assertFalse(query.hasNext());
    query.close();
}
 
Example 3
Source File: ParallelJoinCursor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public BindingSet getNextElement() throws QueryEvaluationException {
	BindingSet result = null;
	CloseableIteration<BindingSet, QueryEvaluationException> nextRightIter = rightIter;
	while (!isClosed() && (nextRightIter != null || rightQueue.hasNext())) {
		if (nextRightIter == null) {
			nextRightIter = rightIter = rightQueue.next();
		}
		if (nextRightIter != null) {
			if (nextRightIter.hasNext()) {
				result = nextRightIter.next();
				break;
			} else {
				nextRightIter.close();
				nextRightIter = rightIter = null;
			}
		}
	}

	return result;
}
 
Example 4
Source File: RepositoryConnectionTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private int getTotalStatementCount(RepositoryConnection connection) throws RepositoryException {
	CloseableIteration<? extends Statement, RepositoryException> iter = connection.getStatements(null, null, null,
			true);

	try {
		int size = 0;

		while (iter.hasNext()) {
			iter.next();
			++size;
		}

		return size;
	} finally {
		iter.close();
	}
}
 
Example 5
Source File: AccumuloRyaDAOTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd() throws Exception {
    RyaIRI cpu = RdfToRyaConversions.convertIRI(VF.createIRI(litdupsNS, "cpu"));
    RyaIRI loadPerc = RdfToRyaConversions.convertIRI(VF.createIRI(litdupsNS, "loadPerc"));
    RyaIRI uri1 = RdfToRyaConversions.convertIRI(VF.createIRI(litdupsNS, "uri1"));
    dao.add(new RyaStatement(cpu, loadPerc, uri1));

    CloseableIteration<RyaStatement, RyaDAOException> iter = dao.getQueryEngine().query(new RyaStatement(cpu, loadPerc, null), conf);
    int count = 0;
    while (iter.hasNext()) {
        assertTrue(uri1.equals(iter.next().getObject()));
        count++;
    }
    iter.close();
    assertEquals(1, count);

    dao.delete(new RyaStatement(cpu, loadPerc, null), conf);

    iter = dao.getQueryEngine().query(new RyaStatement(cpu, loadPerc, null), conf);
    count = 0;
    while (iter.hasNext()) {
        count++;
        iter.next();
    }
    iter.close();
    assertEquals(0, count);
}
 
Example 6
Source File: AccumuloRyaDAOTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testTTL() throws Exception {
    RyaIRI cpu = new RyaIRI(litdupsNS + "cpu");
    RyaIRI loadPerc = new RyaIRI(litdupsNS + "loadPerc");
    long current = System.currentTimeMillis();
    dao.add(new RyaStatement(cpu, loadPerc, new RyaIRI(litdupsNS + "uri1"), null, null, null, null, current));
    dao.add(new RyaStatement(cpu, loadPerc, new RyaIRI(litdupsNS + "uri2"), null, null, null, null, current - 1010l));
    dao.add(new RyaStatement(cpu, loadPerc, new RyaIRI(litdupsNS + "uri3"), null, null, null, null, current - 2010l));
    dao.add(new RyaStatement(cpu, loadPerc, new RyaIRI(litdupsNS + "uri4"), null, null, null, null, current - 3010l));
    dao.add(new RyaStatement(cpu, loadPerc, new RyaIRI(litdupsNS + "uri5"), null, null, null, null, current - 4010l));

    AccumuloRyaQueryEngine queryEngine = dao.getQueryEngine();
    AccumuloRdfConfiguration queryConf = conf.clone();
    queryConf.setTtl(3000l);

    CloseableIteration<RyaStatement, RyaDAOException> iter = queryEngine.query(new RyaStatement(cpu, loadPerc, null), queryConf);
    int count = 0;
    while (iter.hasNext()) {
        iter.next().getObject();
        count++;
    }
    iter.close();
    assertEquals(3, count);

    queryConf.setStartTime(current - 3000l);
    iter = queryEngine.query(new RyaStatement(cpu, loadPerc, null), queryConf);
    count = 0;
    while (iter.hasNext()) {
        iter.next().getObject();
        count++;
    }
    iter.close();
    assertEquals(2, count);
}
 
Example 7
Source File: TestUtils.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if a {@link RyaStatement} is in the specified instance's DAO.
 * @param description the description message for the statement.
 * @param verifyResultCount the expected number of matches.
 * @param matchStatement the {@link RyaStatement} to match.
 * @param dao the {@link AccumuloRyaDAO}.
 * @param config the {@link AccumuloRdfConfiguration} for the instance.
 * @throws RyaDAOException
 */
public static void assertStatementInInstance(final String description, final int verifyResultCount, final RyaStatement matchStatement, final AccumuloRyaDAO dao, final AccumuloRdfConfiguration config) throws RyaDAOException {
    final CloseableIteration<RyaStatement, RyaDAOException> iter = dao.getQueryEngine().query(matchStatement, config);
    int count = 0;
    while (iter.hasNext()) {
        final RyaStatement statement = iter.next();
        assertTrue(description + " - match subject: " + matchStatement,      matchStatement.getSubject().equals(statement.getSubject()));
        assertTrue(description + " - match predicate: " + matchStatement,    matchStatement.getPredicate().equals(statement.getPredicate()));
        assertTrue(description + " - match object match: " + matchStatement, matchStatement.getObject().equals(statement.getObject()));
        count++;
    }
    iter.close();
    assertEquals(description+" - Match Counts.", verifyResultCount, count);
}
 
Example 8
Source File: SailTripleSource.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<? extends Statement, QueryEvaluationException> getStatements(Resource subj, IRI pred,
		Value obj, Resource... contexts) throws QueryEvaluationException {
	CloseableIteration<? extends Statement, SailException> iter = null;
	CloseableIteration<? extends Statement, QueryEvaluationException> result = null;

	boolean allGood = false;
	try {
		iter = conn.getStatements(subj, pred, obj, includeInferred, contexts);
		result = new ExceptionConvertingIteration<Statement, QueryEvaluationException>(iter) {

			@Override
			protected QueryEvaluationException convert(Exception e) {
				return new QueryEvaluationException(e);
			}
		};
		allGood = true;
		return result;
	} catch (SailException e) {
		throw new QueryEvaluationException(e);
	} finally {
		if (!allGood) {
			try {
				if (result != null) {
					result.close();
				}
			} finally {
				if (iter != null) {
					iter.close();
				}
			}
		}
	}
}
 
Example 9
Source File: AccumuloRyaDAO.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public String getVersion() throws RyaDAOException {
    String version = null;
    final CloseableIteration<RyaStatement, RyaDAOException> versIter = queryEngine.query(new RyaStatement(RTS_SUBJECT_RYA, RTS_VERSION_PREDICATE_RYA, null), conf);
    if (versIter.hasNext()) {
        version = versIter.next().getObject().getData();
    }
    versIter.close();

    return version;
}
 
Example 10
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs7_2() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, RDFS.SUBPROPERTYOF, null);

	for (Statement nt : ntIter) {
		Resource aaa = nt.getSubject();
		Value bbb = nt.getObject();

		if (aaa instanceof IRI && bbb instanceof IRI) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (IRI) aaa, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource xxx = t1.getSubject();
				Value yyy = t1.getObject();

				boolean added = addInferredStatement(xxx, (IRI) bbb, yyy);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 11
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs5_1() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, RDFS.SUBPROPERTYOF, null);

	for (Statement nt : ntIter) {
		Resource aaa = nt.getSubject();
		Value bbb = nt.getObject();

		if (bbb instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements((Resource) bbb, RDFS.SUBPROPERTYOF, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value ccc = t1.getObject();
				if (ccc instanceof Resource) {
					boolean added = addInferredStatement(aaa, RDFS.SUBPROPERTYOF, ccc);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();

		}
	}

	return nofInferred;
}
 
Example 12
Source File: RDFStoreTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testValueRoundTrip(Resource subj, IRI pred, Value obj) throws Exception {
	con.begin();
	con.addStatement(subj, pred, obj);
	con.commit();

	CloseableIteration<? extends Statement, SailException> stIter = con.getStatements(null, null, null, false);

	try {
		Assert.assertTrue(stIter.hasNext());

		Statement st = stIter.next();
		Assert.assertEquals(subj, st.getSubject());
		Assert.assertEquals(pred, st.getPredicate());
		Assert.assertEquals(obj, st.getObject());
		Assert.assertTrue(!stIter.hasNext());
	} finally {
		stIter.close();
	}

	ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL,
			"SELECT S, P, O FROM {S} P {O} WHERE P = <" + pred.stringValue() + ">", null);

	CloseableIteration<? extends BindingSet, QueryEvaluationException> iter;
	iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false);

	try {
		Assert.assertTrue(iter.hasNext());

		BindingSet bindings = iter.next();
		Assert.assertEquals(subj, bindings.getValue("S"));
		Assert.assertEquals(pred, bindings.getValue("P"));
		Assert.assertEquals(obj, bindings.getValue("O"));
		Assert.assertTrue(!iter.hasNext());
	} finally {
		iter.close();
	}
}
 
Example 13
Source File: HashJoinImpl.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void handleClose() {
	resultQueue.close();
	for (CloseableIteration<BindingSet, QueryEvaluationException> iter : childIters) {
		iter.close();
	}
	super.handleClose();
}
 
Example 14
Source File: HashJoinTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleHashJoinPredicateOnly() throws Exception {
    //add data
    RyaIRI pred1 = new RyaIRI(litdupsNS, "pred1");
    RyaIRI pred2 = new RyaIRI(litdupsNS, "pred2");
    RyaType one = new RyaType("1");
    RyaIRI subj1 = new RyaIRI(litdupsNS, "subj1");
    RyaIRI subj2 = new RyaIRI(litdupsNS, "subj2");
    RyaIRI subj3 = new RyaIRI(litdupsNS, "subj3");
    RyaIRI subj4 = new RyaIRI(litdupsNS, "subj4");

    dao.add(new RyaStatement(subj1, pred1, one));
    dao.add(new RyaStatement(subj1, pred2, one));
    dao.add(new RyaStatement(subj2, pred1, one));
    dao.add(new RyaStatement(subj2, pred2, one));
    dao.add(new RyaStatement(subj3, pred1, one));
    dao.add(new RyaStatement(subj3, pred2, one));
    dao.add(new RyaStatement(subj4, pred1, one));
    dao.add(new RyaStatement(subj4, pred2, one));
    

    //1 join
    HashJoin ijoin = new HashJoin(dao.getQueryEngine());
    CloseableIteration<RyaStatement, RyaDAOException> join = ijoin.join(null, pred1, pred2);

    int count = 0;
    while (join.hasNext()) {
        RyaStatement next = join.next();
        count++;
    }
    assertEquals(4, count);
    join.close();
}
 
Example 15
Source File: RepositoryTripleSource.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<? extends Statement, QueryEvaluationException> getStatements(Resource subj, IRI pred,
		Value obj, Resource... contexts) throws QueryEvaluationException {
	CloseableIteration<? extends Statement, RepositoryException> iter = null;
	CloseableIteration<? extends Statement, QueryEvaluationException> result = null;

	boolean allGood = false;
	try {
		iter = repo.getStatements(subj, pred, obj, includeInferred, contexts);
		result = new ExceptionConvertingIteration<Statement, QueryEvaluationException>(iter) {

			@Override
			protected QueryEvaluationException convert(Exception exception) {
				return new QueryEvaluationException(exception);
			}
		};
		allGood = true;
		return result;
	} catch (RepositoryException e) {
		throw new QueryEvaluationException(e);
	} finally {
		if (!allGood) {
			try {
				if (result != null) {
					result.close();
				}
			} finally {
				if (iter != null) {
					iter.close();
				}
			}
		}
	}
}
 
Example 16
Source File: UnionExecutorBase.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void release(CloseableIteration<T, QueryEvaluationException> item) {
	item.close();
}
 
Example 17
Source File: StoreSerializationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void testSerialization() throws Exception {
	MemoryStore store = new MemoryStore(dataDir);
	store.initialize();

	ValueFactory factory = store.getValueFactory();
	IRI foo = factory.createIRI("http://www.foo.example/foo");
	IRI bar = factory.createIRI("http://www.foo.example/bar");

	SailConnection con = store.getConnection();
	con.begin();
	con.addStatement(foo, RDF.TYPE, bar);
	con.commit();

	ParsedTupleQuery query = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT X, P, Y FROM {X} P {Y}",
			null);
	TupleExpr tupleExpr = query.getTupleExpr();

	CloseableIteration<? extends BindingSet, QueryEvaluationException> iter = con.evaluate(tupleExpr, null,
			EmptyBindingSet.getInstance(), false);

	BindingSet bindingSet = iter.next();

	assertEquals(bindingSet.getValue("X"), foo);
	assertEquals(bindingSet.getValue("P"), RDF.TYPE);
	assertEquals(bindingSet.getValue("Y"), bar);
	iter.close();
	con.close();

	store.shutDown();

	store = new MemoryStore(dataDir);
	store.initialize();

	factory = store.getValueFactory();
	foo = factory.createIRI("http://www.foo.example/foo");
	bar = factory.createIRI("http://www.foo.example/bar");

	con = store.getConnection();

	iter = con.evaluate(tupleExpr, null, EmptyBindingSet.getInstance(), false);

	bindingSet = iter.next();

	assertEquals(bindingSet.getValue("X"), foo);
	assertEquals(bindingSet.getValue("P"), RDF.TYPE);
	assertEquals(bindingSet.getValue("Y"), bar);

	iter.close();
	con.begin();
	con.addStatement(bar, RDF.TYPE, foo);
	con.commit();
	con.close();

	store.shutDown();
}
 
Example 18
Source File: IteratorCombiner.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void close() throws QueryEvaluationException {
	for(CloseableIteration<BindingSet, QueryEvaluationException> iterator: iterators) {
		iterator.close();
	}
}
 
Example 19
Source File: QueueIteration.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void release(CloseableIteration<E, QueryEvaluationException> item) {
	item.close();
}
 
Example 20
Source File: IterativeJoin.java    From rya with Apache License 2.0 4 votes vote down vote up
protected CloseableIteration<RyaIRI, RyaDAOException> join(final CloseableIteration<RyaIRI, RyaDAOException> iteration,
                                                           final Map.Entry<RyaIRI, RyaType> predObj) {
    //TODO: configure batch
    //TODO: batch = 1, does not work
    final int batch = 100;
    return new CloseableIteration<RyaIRI, RyaDAOException>() {

        private CloseableIteration<Map.Entry<RyaStatement, BindingSet>, RyaDAOException> query;

        @Override
        public void close() throws RyaDAOException {
            iteration.close();
            if (query != null) {
                query.close();
            }
        }

        @Override
        public boolean hasNext() throws RyaDAOException {
            return !(query == null || !query.hasNext()) || batchNext();
        }

        @Override
        public RyaIRI next() throws RyaDAOException {
            if (query == null || !query.hasNext()) {
                if (!batchNext()) return null;
            }
            if (query != null && query.hasNext()) {
                return query.next().getKey().getSubject();
            } else {
                return null;
            }
        }

        private boolean batchNext() throws RyaDAOException {
            if (!iteration.hasNext()) {
                return false;
            }
            Collection<Map.Entry<RyaStatement, BindingSet>> batchedResults = new ArrayList<Map.Entry<RyaStatement, BindingSet>>();
            for (int i = 0; i < batch && iteration.hasNext(); i++) {
                batchedResults.add(new RdfCloudTripleStoreUtils.CustomEntry<RyaStatement, BindingSet>(
                        new RyaStatement(iteration.next(), predObj.getKey(), predObj.getValue()), null));
            }
            query = ryaQueryEngine.queryWithBindingSet(batchedResults, null);
            return query.hasNext();
        }

        @Override
        public void remove() throws RyaDAOException {
            this.next();
        }
    };
}