[ 
https://issues.apache.org/jira/browse/COMMONSRDF-35?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15543915#comment-15543915
 ] 

ASF GitHub Bot commented on COMMONSRDF-35:
------------------------------------------

Github user ansell commented on a diff in the pull request:

    https://github.com/apache/incubator-commonsrdf/pull/24#discussion_r81667185
  
    --- Diff: 
rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
 ---
    @@ -0,0 +1,192 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements. See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership. The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.commons.rdf.rdf4j.impl;
    +
    +import java.util.Optional;
    +import java.util.stream.Stream;
    +
    +import org.apache.commons.rdf.api.BlankNodeOrIRI;
    +import org.apache.commons.rdf.api.Dataset;
    +import org.apache.commons.rdf.api.Graph;
    +import org.apache.commons.rdf.api.IRI;
    +import org.apache.commons.rdf.api.Quad;
    +import org.apache.commons.rdf.api.RDFTerm;
    +import org.apache.commons.rdf.rdf4j.RDF4JDataset;
    +import org.apache.commons.rdf.rdf4j.RDF4JQuad;
    +import org.eclipse.rdf4j.common.iteration.Iterations;
    +import org.eclipse.rdf4j.model.Resource;
    +import org.eclipse.rdf4j.model.Statement;
    +import org.eclipse.rdf4j.model.Value;
    +import org.eclipse.rdf4j.repository.Repository;
    +import org.eclipse.rdf4j.repository.RepositoryConnection;
    +import org.eclipse.rdf4j.repository.RepositoryResult;
    +
    +public class RepositoryDatasetImpl extends 
AbstractRepositoryGraphLike<Quad> implements RDF4JDataset, Dataset {
    +
    +   public RepositoryDatasetImpl(Repository repository, boolean 
includeInferred) {
    +           super(repository, includeInferred);
    +   }
    +
    +   public RepositoryDatasetImpl(Repository repository) {
    +           super(repository);
    +   }
    +
    +
    +   @Override
    +   public void add(Quad tripleLike) {
    +           Statement statement = rdf4jTermFactory.asStatement(tripleLike);
    +           try (RepositoryConnection conn = getRepositoryConnection()) {
    +                   conn.add(statement);
    +                   conn.commit();
    +           }
    +   }
    +
    +
    +   @Override
    +   public boolean contains(Quad tripleLike) {
    +           Statement statement = rdf4jTermFactory.asStatement(tripleLike);
    +           try (RepositoryConnection conn = getRepositoryConnection()) {
    +                   return conn.hasStatement(statement, includeInferred);
    +           }
    +   }
    +
    +   @Override
    +   public void remove(Quad tripleLike) {
    +           Statement statement = rdf4jTermFactory.asStatement(tripleLike);
    +           try (RepositoryConnection conn = getRepositoryConnection()) {
    +                   conn.remove(statement);
    +                   conn.commit();
    +           }
    +   }
    +
    +   @Override
    +   public void clear() {
    +           try (RepositoryConnection conn = getRepositoryConnection()) {
    +                   conn.clear();
    +                   conn.commit();
    +           }
    +   }
    +
    +   @Override
    +   public long size() {
    +           if (includeInferred) { 
    +                   // We'll need to count them all
    +                   return stream().count();
    +           } 
    +           // else: Ask directly
    +           try (RepositoryConnection conn = getRepositoryConnection()) {
    +                   return conn.size();
    +           }
    +   }
    +
    +   
    +   @Override
    +   public void add(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI 
predicate, RDFTerm object) {
    +           Resource context = (Resource) 
rdf4jTermFactory.asValue(graphName);
    +           Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
    +           org.eclipse.rdf4j.model.IRI pred = 
(org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
    +           Value obj = rdf4jTermFactory.asValue(object);
    +           try (RepositoryConnection conn = getRepositoryConnection()) {
    +                   conn.add(subj, pred, obj,  context);
    +                   conn.commit();
    +           }
    +   }
    +
    +   @Override
    +   public boolean contains(Optional<BlankNodeOrIRI> graphName, 
BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
    +           Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
    +           org.eclipse.rdf4j.model.IRI pred = 
(org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
    +           Value obj = rdf4jTermFactory.asValue(object);
    +           Resource[] contexts = asContexts(graphName);
    +           
    +           try (RepositoryConnection conn = getRepositoryConnection()) {
    +                   return conn.hasStatement(subj, pred, obj, 
includeInferred, contexts);
    +           }
    +   }
    +
    +   private Resource[] asContexts(Optional<BlankNodeOrIRI> graphName) {
    +           Resource[] contexts;
    +           if (graphName == null) {
    +                   // no contexts == any contexts
    +                    contexts = new Resource[0];
    +           } else {        
    +                   BlankNodeOrIRI g = graphName.orElse(null);
    +                   Resource context = (Resource) 
rdf4jTermFactory.asValue(g);
    +                   contexts = new Resource[] { context };
    +           }
    +           return contexts;
    +   }
    +
    +   @Override
    +   public void remove(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI 
subject, IRI predicate, RDFTerm object) {
    +           Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
    +           org.eclipse.rdf4j.model.IRI pred = 
(org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
    +           Value obj = rdf4jTermFactory.asValue(object);
    +           Resource[] contexts = asContexts(graphName);
    +
    +           try (RepositoryConnection conn = getRepositoryConnection()) {
    +                   conn.remove(subj, pred, obj, contexts);
    +                   conn.commit();
    --- End diff --
    
    I am not sure how to do it efficiently without explicit begin/end in the 
Graph/Dataset model. However, as it stands, this method will not be useful for 
any but the most trivial cases, given the potential overhead for commit each 
time so it needs work to get into circulation.
    
    If you added begin/end then you could also optimise to reduce the number of 
calls to getRepositoryConnection that may be resource or time intensive 
depending on the Repository implementation.


> rdf4j integration
> -----------------
>
>                 Key: COMMONSRDF-35
>                 URL: https://issues.apache.org/jira/browse/COMMONSRDF-35
>             Project: Apache Commons RDF
>          Issue Type: New Feature
>            Reporter: Stian Soiland-Reyes
>            Assignee: Stian Soiland-Reyes
>              Labels: integration, rdf4j, sesame
>             Fix For: 0.3.0
>
>
> Add a new rdf4j module with implementation for Eclipse rdf4j
> See https://github.com/apache/incubator-commonsrdf/tree/rdf4j/rdf4j
> A legacy sesame branch could then be added by mainly copy/paste and change 
> the import



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to