use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class SolrYard method update.
@Override
public final Iterable<Representation> update(Iterable<Representation> representations) throws YardException, IllegalArgumentException, NullPointerException {
if (representations == null) {
throw new IllegalArgumentException("The parsed Iterable over Representations MUST NOT be NULL!");
}
long start = System.currentTimeMillis();
Set<String> ids = new HashSet<String>();
for (Representation representation : representations) {
if (representation != null) {
ids.add(representation.getId());
}
}
if (closed) {
log.warn("The SolrYard '{}' was already closed!", config.getName());
}
// for debuging
int numDocs = ids.size();
try {
// returns the ids found in the solrIndex
ids = checkRepresentations(ids);
} catch (SolrServerException e) {
throw new YardException("Error while searching for alredy present documents " + "before executing the actual update for the parsed Representations", e);
} catch (IOException e) {
throw new YardException("Unable to access SolrServer", e);
}
long checked = System.currentTimeMillis();
List<SolrInputDocument> inputDocs = new ArrayList<SolrInputDocument>(ids.size());
List<Representation> updated = new ArrayList<Representation>();
for (Representation representation : representations) {
if (representation != null && ids.contains(representation.getId())) {
// null parsed or not
// already present
inputDocs.add(createSolrInputDocument(representation));
updated.add(representation);
}
}
long created = System.currentTimeMillis();
if (!inputDocs.isEmpty()) {
try {
final UpdateRequest update = new UpdateRequest();
if (!immediateCommit) {
update.setCommitWithin(commitWithin);
}
update.add(inputDocs);
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public UpdateResponse run() throws IOException, SolrServerException {
update.process(server);
if (immediateCommit) {
server.commit();
}
return null;
}
});
} catch (PrivilegedActionException pae) {
if (pae.getException() instanceof SolrServerException) {
throw new YardException("Error while adding updated Documents to the SolrServer", pae.getException());
} else if (pae.getException() instanceof IOException) {
throw new YardException("Unable to access SolrServer", pae.getException());
} else {
throw RuntimeException.class.cast(pae.getException());
}
}
}
long ready = System.currentTimeMillis();
log.info(String.format("Processed updateRequest for %d documents (%d in index " + "| %d updated) in %dms (checked %dms|created %dms| stored%dms)", numDocs, ids.size(), updated.size(), ready - start, checked - start, created - checked, ready - created));
return updated;
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class ClerezzaYard method findRepresentation.
@Override
public QueryResultList<Representation> findRepresentation(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
if (parsedQuery == null) {
throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
}
final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
final ResultSet result = executeSparqlFieldQuery(query);
//Note: An other possibility would be to first iterate over all results and add it to
// a list and create this Iterator than based on the List. This would
// be the preferenced way if changes in the graph could affect the
// Iteration over the SPARQL query results.
Iterator<Representation> representationIterator = new AdaptingIterator<SolutionMapping, Representation>(result, new AdaptingIterator.Adapter<SolutionMapping, Representation>() {
/**
* Adapter that gets the rootVariable of the Query (selecting the ID)
* and creates a Representation for it.
* @param solution a solution of the query
* @param type the type (no generics here)
* @return the representation or <code>null</code> if result is
* not an IRI or there is no Representation for the result.
*/
@Override
public Representation adapt(SolutionMapping solution, Class<Representation> type) {
RDFTerm resource = solution.get(query.getRootVariableName());
if (resource instanceof IRI) {
try {
return getRepresentation((IRI) resource, false);
} catch (IllegalArgumentException e) {
log.warn("Unable to create Representation for ID " + resource + "! -> ignore query result");
return null;
}
} else {
return null;
}
}
}, Representation.class);
// created before the method returns.
return new QueryResultListImpl<Representation>(query, representationIterator, Representation.class);
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class SesameYard method store.
protected final Iterable<Representation> store(Iterable<Representation> representations, boolean allowCreate) throws IllegalArgumentException, YardException {
RepositoryConnection con = null;
try {
con = repository.getConnection();
con.begin();
ArrayList<Representation> added = new ArrayList<Representation>();
for (Representation representation : representations) {
if (representation != null) {
//reassign
Representation stored = store(con, representation, allowCreate, false);
//to check if the store was successful
if (stored != null) {
added.add(stored);
} else {
//can only be the case if allowCreate==false (update was called)
log.warn(String.format("Unable to update Representation %s in Yard %s because it is not present!", representation.getId(), getId()));
}
}
//ignore null values in the parsed Iterable!
}
con.commit();
return added;
} catch (RepositoryException e) {
throw new YardException("Unable to remove parsed Representations", e);
} catch (IllegalArgumentException e) {
try {
//to avoid Exception logs in case store(..) throws an Exception
//in the case allowCreate and canNotCreateIsError do not allow
//the store operation
con.rollback();
} catch (RepositoryException ignore) {
}
throw e;
} finally {
if (con != null) {
try {
con.close();
} catch (RepositoryException ignore) {
}
}
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class SesameYard method findRepresentation.
@Override
public QueryResultList<Representation> findRepresentation(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
if (parsedQuery == null) {
throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
}
final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
RepositoryConnection con = null;
TupleQueryResult results = null;
try {
con = repository.getConnection();
con.begin();
//execute the query
int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
results = executeSparqlFieldQuery(con, query, limit, false);
//parse the results and generate the Representations
//create an own valueFactors so that all the data of the query results
//are added to the same Sesame Model
Model model = new TreeModel();
RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit) : new ArrayList<Representation>();
while (results.hasNext()) {
BindingSet result = results.next();
Value value = result.getValue(query.getRootVariableName());
if (value instanceof URI) {
//copy all data to the model and create the representation
RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI) value);
//link the result with the query result
model.add(queryRoot, queryResult, value);
representations.add(rep);
}
//ignore non URI results
}
con.commit();
return new SesameQueryResultList(model, query, representations);
} catch (RepositoryException e) {
throw new YardException("Unable to execute findReferences query", e);
} catch (QueryEvaluationException e) {
throw new YardException("Unable to execute findReferences query", e);
} finally {
if (results != null) {
//close the result if present
try {
results.close();
} catch (QueryEvaluationException ignore) {
/* ignore */
}
}
if (con != null) {
try {
con.close();
} catch (RepositoryException ignore) {
/* ignore */
}
}
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class SesameYard method store.
/**
* Generic store method used by store and update methods
* @param representation the representation to store/update
* @param allowCreate if new representation are allowed to be created
* @param canNotCreateIsError if updates to existing one are allowed
* @return the representation as added to the yard
* @throws IllegalArgumentException
* @throws YardException
*/
protected final Representation store(Representation representation, boolean allowCreate, boolean canNotCreateIsError) throws IllegalArgumentException, YardException {
RepositoryConnection con = null;
try {
con = repository.getConnection();
con.begin();
Representation added = store(con, representation, allowCreate, canNotCreateIsError);
con.commit();
return added;
} catch (RepositoryException e) {
throw new YardException("Unable to remove parsed Representations", e);
} catch (IllegalArgumentException e) {
try {
//to avoid Exception logs in case store(..) throws an Exception
//in the case allowCreate and canNotCreateIsError do not allow
//the store operation
con.rollback();
} catch (RepositoryException ignore) {
}
throw e;
} finally {
if (con != null) {
try {
con.close();
} catch (RepositoryException ignore) {
}
}
}
}
Aggregations