Return-Path: X-Original-To: apmail-marmotta-commits-archive@minotaur.apache.org Delivered-To: apmail-marmotta-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7F0A317604 for ; Mon, 27 Oct 2014 08:01:38 +0000 (UTC) Received: (qmail 39806 invoked by uid 500); 27 Oct 2014 08:01:38 -0000 Delivered-To: apmail-marmotta-commits-archive@marmotta.apache.org Received: (qmail 39727 invoked by uid 500); 27 Oct 2014 08:01:38 -0000 Mailing-List: contact commits-help@marmotta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@marmotta.apache.org Delivered-To: mailing list commits@marmotta.apache.org Received: (qmail 39685 invoked by uid 99); 27 Oct 2014 08:01:38 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Oct 2014 08:01:38 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id F1A7E90916D; Mon, 27 Oct 2014 08:01:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: wikier@apache.org To: commits@marmotta.apache.org Date: Mon, 27 Oct 2014 08:01:38 -0000 Message-Id: <2e8006c767ae4f57b59af0b2fad252d3@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/9] git commit: MARMOTTA-556: added basic service implementation of Linked Data Fragments MARMOTTA-556: added basic service implementation of Linked Data Fragments Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/d0dbb7d5 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/d0dbb7d5 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/d0dbb7d5 Branch: refs/heads/MARMOTTA-556 Commit: d0dbb7d59c63780ac5acfb1f18a49769b3c8e550 Parents: 1a96333 Author: Sergio Fernández Authored: Fri Oct 24 18:31:25 2014 +0200 Committer: Sergio Fernández Committed: Fri Oct 24 18:31:25 2014 +0200 ---------------------------------------------------------------------- platform/marmotta-ldf/pom.xml | 4 + .../marmotta/platform/ldf/api/LdfService.java | 65 +++++++++++ .../platform/ldf/logging/LdfLoggingModule.java | 55 +++++++++ .../platform/ldf/services/LdfServiceImpl.java | 62 ++++++++++ .../platform/ldf/sesame/PagedRDFHandler.java | 114 +++++++++++++++++++ 5 files changed, 300 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/d0dbb7d5/platform/marmotta-ldf/pom.xml ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldf/pom.xml b/platform/marmotta-ldf/pom.xml index cc3fd49..de1ae45 100644 --- a/platform/marmotta-ldf/pom.xml +++ b/platform/marmotta-ldf/pom.xml @@ -165,6 +165,10 @@ ${project.version} + org.openrdf.sesame + sesame-rio-api + + org.jboss.resteasy resteasy-cdi http://git-wip-us.apache.org/repos/asf/marmotta/blob/d0dbb7d5/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/api/LdfService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/api/LdfService.java b/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/api/LdfService.java new file mode 100644 index 0000000..2d7e3e2 --- /dev/null +++ b/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/api/LdfService.java @@ -0,0 +1,65 @@ +package org.apache.marmotta.platform.ldf.api; + +import org.openrdf.model.Model; +import org.openrdf.model.Resource; +import org.openrdf.model.URI; +import org.openrdf.rio.RDFFormat; +/* + * 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. + */ +import org.openrdf.model.Value; +import org.openrdf.repository.RepositoryException; + +import java.io.OutputStream; + +/** + * Linked Media Fragments service + * + * @author Sergio Fernández + */ +public interface LdfService { + + /** + * Writes a fragment matching the specified triple fragment pattern + * specified (null values are wildcards). + * + * @param subject fragment subject + * @param predicate fragmnent predicate + * @param object fragment object + * @param offset index at which to start the page + * @param limit maximum number of triples + * @param format RDF serialization + * @param out output stream where write the fragment + */ + void writeFragment(URI subject, URI predicate, Value object, int offset, int limit, RDFFormat format, OutputStream out) throws RepositoryException; + + /** + * Writes a fragment matching the specified quad fragment pattern + * specified (null values are wildcards). + * + * @param subject fragment subject + * @param predicate fragmnent predicate + * @param object fragment object + * @param context named graph + * @param offset index at which to start the page + * @param limit maximum number of triples + * @param format RDF serialization + * @param out output stream where write the fragment + */ + void writeFragment(URI subject, URI predicate, Value object, Resource context, int offset, int limit, RDFFormat format, OutputStream out) throws RepositoryException; + +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/d0dbb7d5/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/logging/LdfLoggingModule.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/logging/LdfLoggingModule.java b/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/logging/LdfLoggingModule.java new file mode 100644 index 0000000..54a6495 --- /dev/null +++ b/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/logging/LdfLoggingModule.java @@ -0,0 +1,55 @@ +/* + * 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.marmotta.platform.ldf.logging; + +import ch.qos.logback.classic.Level; +import org.apache.marmotta.platform.core.logging.BaseLoggingModule; + +import javax.enterprise.context.ApplicationScoped; +import java.util.Collection; +import java.util.Collections; + +/** + * Logging Module for LDF + * + * @author Sergio Fernández + */ +@ApplicationScoped +public class LdfLoggingModule extends BaseLoggingModule { + + @Override + public String getId() { + return "ldf"; + } + + @Override + public String getName() { + return "LDF"; + } + + @Override + public Collection getPackages() { + return Collections.singleton("org.apache.marmotta.platform.ldf"); + } + + @Override + public Level getDefaultLevel() { + return Level.DEBUG; + } + +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/d0dbb7d5/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/services/LdfServiceImpl.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/services/LdfServiceImpl.java b/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/services/LdfServiceImpl.java new file mode 100644 index 0000000..49ca8ff --- /dev/null +++ b/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/services/LdfServiceImpl.java @@ -0,0 +1,62 @@ +/* + * 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.marmotta.platform.ldf.services; + +import org.apache.marmotta.commons.sesame.repository.ResultUtils; +import org.apache.marmotta.platform.core.api.triplestore.SesameService; +import org.apache.marmotta.platform.ldf.api.LdfService; +import org.apache.marmotta.platform.ldf.sesame.PagedRDFHandler; +import org.openrdf.model.*; +import org.openrdf.repository.RepositoryConnection; +import org.openrdf.repository.RepositoryException; +import org.openrdf.repository.RepositoryResult; +import org.openrdf.rio.*; + +import javax.inject.Inject; +import java.io.OutputStream; + +/** + * Linked Media Fragments service implementation + * + * @author Sergio Fernández + */ +public class LdfServiceImpl implements LdfService { + + @Inject + private SesameService sesameService; + + @Override + public void writeFragment(URI subject, URI predicate, Value object, int offset, int limit, RDFFormat format, OutputStream out) throws RepositoryException { + writeFragment(subject, predicate, object, null, offset, limit, format, out); + } + + @Override + public void writeFragment(URI subject, URI predicate, Value object, Resource context, int offset, int limit, RDFFormat format, OutputStream out) throws RepositoryException { + final RepositoryConnection conn = sesameService.getConnection(); + try { + conn.begin(); + RepositoryResult statements = conn.getStatements(subject, predicate, object, true, context); + RDFHandler handler = new PagedRDFHandler(Rio.createWriter(format, out), offset, limit); + Rio.write(ResultUtils.iterable(statements), handler); + } catch (RDFHandlerException e) { + e.printStackTrace(); + } finally { + conn.close(); + } + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/d0dbb7d5/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/sesame/PagedRDFHandler.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/sesame/PagedRDFHandler.java b/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/sesame/PagedRDFHandler.java new file mode 100644 index 0000000..36f59cf --- /dev/null +++ b/platform/marmotta-ldf/src/main/java/org/apache/marmotta/platform/ldf/sesame/PagedRDFHandler.java @@ -0,0 +1,114 @@ +/* + * 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.marmotta.platform.ldf.sesame; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import org.openrdf.model.Literal; +import org.openrdf.model.Resource; +import org.openrdf.model.Statement; +import org.openrdf.rio.RDFHandler; +import org.openrdf.rio.RDFHandlerException; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * Paginates statements before sending them to the delegated RDFHandler. + * (TODO: find a more performance solution) + * + * @author Sergio Fernández + */ +public class PagedRDFHandler implements RDFHandler { + + private List statements; + private RDFHandler handler; + private int offset; + private int limit; + + /** + * Constructs a PagedRDFHandler with a delegate handler + * + * @param handler The handler to delegate the calls to + */ + public PagedRDFHandler(RDFHandler handler, int offset, int limit) { + super(); + this.statements = new ArrayList<>(); + this.handler = handler; + this.offset = offset; + this.limit = limit; + } + + @Override + public void startRDF() throws RDFHandlerException { + handler.startRDF(); + } + + @Override + public void endRDF() throws RDFHandlerException { + //first order by a fixed criteria + Collections.sort(statements, new Comparator() { + @Override + public int compare(Statement s1, Statement s2) { + int subjectComparison = s1.getSubject().stringValue().compareTo(s2.getSubject().stringValue()); + int predicatedComparison = s1.getPredicate().stringValue().compareTo(s2.getPredicate().stringValue()); + + if (subjectComparison != 0) { + return subjectComparison; + } else if (predicatedComparison != 0) { + return predicatedComparison; + } else if((s1.getObject() instanceof Literal) && (s2.getObject() instanceof Resource)) { + return 1; + } else if((s1.getObject() instanceof Resource) && (s2.getObject() instanceof Literal)) { + return -1; + } else { + return s1.getObject().stringValue().compareTo(s2.getObject().stringValue()); + } + } + }); + + //then filter + List filteredStatements = Lists.newArrayList(Iterables.limit(statements, 20)); + + //send statements to delegate writer + for (Statement statement : filteredStatements) { + handler.handleStatement(statement); + } + + //and actually end the rdf + handler.endRDF(); + } + + @Override + public void handleNamespace(String prefix, String uri) throws RDFHandlerException { + handler.handleNamespace(prefix, uri); + } + + @Override + public void handleStatement(Statement statement) throws RDFHandlerException { + statements.add(statement); + } + + @Override + public void handleComment(String comment) throws RDFHandlerException { + handler.handleComment(comment); + } + +}