Content of file DefaultPersistence.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>DefaultPersistence.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">blueMarine II :: Persistence</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.persistence.impl</a> &gt; <span class="el_source">DefaultPersistence.java</span></div><h1>DefaultPersistence.java</h1><pre class="source lang-java linenums"><span class="fc" id="L1">/*</span>
 * *********************************************************************************************************************
 *
 * blueMarine II: Semantic Media Centre
 * http://tidalwave.it/projects/bluemarine2
 *
 * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); 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 &quot;AS IS&quot; 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.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/bluemarine2-src
 * git clone https://github.com/tidalwave-it/bluemarine2-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.bluemarine2.persistence.impl;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandler;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.n3.N3Writer;
import org.eclipse.rdf4j.sail.Sail;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.eclipse.rdf4j.sail.nativerdf.NativeStore;
import it.tidalwave.util.TypeSafeMap;
import it.tidalwave.util.annotation.VisibleForTesting;
import it.tidalwave.messagebus.MessageBus;
import it.tidalwave.messagebus.annotation.ListensTo;
import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
import it.tidalwave.bluemarine2.util.SortingRDFHandler;
import it.tidalwave.bluemarine2.message.PersistenceInitializedNotification;
import it.tidalwave.bluemarine2.message.PowerOffNotification;
import it.tidalwave.bluemarine2.message.PowerOnNotification;
import it.tidalwave.bluemarine2.persistence.Persistence;
import lombok.extern.slf4j.Slf4j;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.nio.charset.StandardCharsets.UTF_8;
import static it.tidalwave.bluemarine2.persistence.PersistencePropertyNames.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L72">@SimpleMessageSubscriber @Slf4j</span>
<span class="fc" id="L73">public class DefaultPersistence implements Persistence</span>
  {
    @Inject
    private MessageBus messageBus;

<span class="fc" id="L78">    private final CountDownLatch initialized = new CountDownLatch(1);</span>

    private Repository repository;

    @VisibleForTesting Sail sail;

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Repository getRepository()
      {
<span class="nc" id="L92">        waitForPowerOn();</span>
<span class="nc" id="L93">        return repository;</span>
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @VisibleForTesting void onPowerOnNotification (@ListensTo @Nonnull final PowerOnNotification notification)
      throws RepositoryException, IOException, RDFParseException
      {
<span class="fc" id="L103">        log.info(&quot;onPowerOnNotification({})&quot;, notification);</span>
<span class="fc" id="L104">        final TypeSafeMap properties = notification.getProperties();</span>

<span class="fc" id="L106">        final Optional&lt;Path&gt; importFile = properties.getOptional(IMPORT_FILE);</span>
<span class="fc" id="L107">        final Optional&lt;Path&gt; storageFolder = properties.getOptional(STORAGE_FOLDER);</span>

<span class="fc bfc" id="L109" title="All 2 branches covered.">        if (storageFolder.isEmpty())</span>
          {
<span class="fc" id="L111">            log.warn(&quot;No storage path: working in memory&quot;);</span>
<span class="fc" id="L112">            sail = new MemoryStore();</span>
          }
        else
          {
<span class="fc" id="L116">            log.info(&quot;Disk storage at {}&quot;, storageFolder);</span>

<span class="pc bpc" id="L118" title="1 of 4 branches missed.">            if (importFile.isPresent() &amp;&amp; Files.exists(importFile.get()))</span>
              {
<span class="fc" id="L120">                log.warn(&quot;Scratching store ...&quot;);</span>
<span class="fc" id="L121">                FileUtils.deleteDirectory(storageFolder.get().toFile()); // FIXME: rename to backup folder with timestamp</span>
rename to backup folder with timestamp
} <span class="fc" id="L124"> sail = new NativeStore(storageFolder.get().toFile());</span> } <span class="fc" id="L127"> repository = new SailRepository(sail);</span> <span class="fc" id="L128"> repository.initialize();</span> <span class="pc bpc" id="L130" title="1 of 4 branches missed."> if (importFile.isPresent() &amp;&amp; Files.exists(importFile.get()))</span> { <span class="fc" id="L132"> importFromFile(importFile.get());</span> <span class="fc bfc" id="L134" title="All 2 branches covered."> if (properties.getOptional(RENAME_IMPORT_FILE).orElse(false))</span> { <span class="fc" id="L136"> Files.move(importFile.get(), Paths.get(importFile.get().toString() + &quot;~&quot;));</span> } } <span class="fc" id="L140"> initialized.countDown();</span> <span class="fc" id="L141"> messageBus.publish(new PersistenceInitializedNotification());</span> <span class="fc" id="L142"> }</span> /******************************************************************************************************************* * * ******************************************************************************************************************/ @VisibleForTesting void onPowerOffNotification (@ListensTo @Nonnull final PowerOffNotification notification) throws RepositoryException, IOException, RDFParseException { <span class="nc" id="L151"> log.info(&quot;onPowerOffNotification({})&quot;, notification);</span> <span class="nc bnc" id="L153" title="All 2 branches missed."> if (repository != null)</span> { <span class="nc" id="L155"> repository.shutDown();</span> } <span class="nc" id="L157"> }</span> /******************************************************************************************************************* * * Exports the repository to the given file. * * @param path where to export the data to * @throws RDFHandlerException * @throws IOException * @throws RepositoryException * ******************************************************************************************************************/ @Override public void exportToFile (@Nonnull final Path path) throws RDFHandlerException, IOException, RepositoryException { <span class="fc" id="L173"> log.info(&quot;exportToFile({})&quot;, path);</span> <span class="fc" id="L174"> Files.createDirectories(path.getParent());</span> <span class="fc" id="L176"> try (final Writer w = Files.newBufferedWriter(path, UTF_8);</span> <span class="fc" id="L177"> final RepositoryConnection connection = repository.getConnection())</span> { <span class="fc" id="L179"> final RDFHandler writer = new SortingRDFHandler(new N3Writer(w));</span> // FIXME: use Iterations - and sort // for (final Namespace namespace : connection.getNamespaces().asList()) // { // writer.handleNamespace(namespace.getPrefix(), namespace.getName()); // } <span class="fc" id="L187"> writer.handleNamespace(&quot;bio&quot;, &quot;http://purl.org/vocab/bio/0.1/&quot;);</span> <span class="fc" id="L188"> writer.handleNamespace(&quot;bmmo&quot;, &quot;http://bluemarine.tidalwave.it/2015/04/mo/&quot;);</span> <span class="fc" id="L189"> writer.handleNamespace(&quot;dc&quot;, &quot;http://purl.org/dc/elements/1.1/&quot;);</span> <span class="fc" id="L190"> writer.handleNamespace(&quot;foaf&quot;, &quot;http://xmlns.com/foaf/0.1/&quot;);</span> <span class="fc" id="L191"> writer.handleNamespace(&quot;owl&quot;, &quot;http://www.w3.org/2002/07/owl#&quot;);</span> <span class="fc" id="L192"> writer.handleNamespace(&quot;mo&quot;, &quot;http://purl.org/ontology/mo/&quot;);</span> <span class="fc" id="L193"> writer.handleNamespace(&quot;rdfs&quot;, &quot;http://www.w3.org/2000/01/rdf-schema#&quot;);</span> <span class="fc" id="L194"> writer.handleNamespace(&quot;rel&quot;, &quot;http://purl.org/vocab/relationship/&quot;);</span> <span class="fc" id="L195"> writer.handleNamespace(&quot;vocab&quot;, &quot;http://dbtune.org/musicbrainz/resource/vocab/&quot;);</span> <span class="fc" id="L196"> writer.handleNamespace(&quot;xs&quot;, &quot;http://www.w3.org/2001/XMLSchema#&quot;);</span> <span class="fc" id="L198"> connection.export(writer);</span> } <span class="fc" id="L200"> }</span> /******************************************************************************************************************* * * ******************************************************************************************************************/ @Override public &lt;E extends Exception&gt; void runInTransaction (@Nonnull final TransactionalTask&lt;E&gt; task) throws E, RepositoryException { <span class="nc" id="L210"> log.info(&quot;runInTransaction({})&quot;, task);</span> <span class="nc" id="L211"> waitForPowerOn();</span> <span class="nc" id="L212"> final long baseTime = System.nanoTime();</span> <span class="nc" id="L214"> try (final RepositoryConnection connection = repository.getConnection()) // TODO: pool?</span> { <span class="nc" id="L216"> task.run(connection);</span> <span class="nc" id="L217"> connection.commit();</span> } <span class="nc" id="L219"> catch (Exception e)</span> { <span class="nc" id="L221"> log.error(&quot;Transaction failed: {}&quot;, e.toString());</span> <span class="nc" id="L222"> }</span> <span class="nc bnc" id="L224" title="All 2 branches missed."> if (log.isDebugEnabled())</span> { <span class="nc" id="L226"> log.debug(&quot;&gt;&gt;&gt;&gt; done in {} ms&quot;, (System.nanoTime() - baseTime) * 1E-6);</span> } <span class="nc" id="L228"> }</span> /******************************************************************************************************************* * * Imports the repository from the given file. * * @param path where to import the data from * @throws RDFHandlerException * @throws IOException * @throws RepositoryException * ******************************************************************************************************************/ private void importFromFile (@Nonnull final Path path) throws IOException, RepositoryException, RDFParseException { <span class="fc" id="L243"> try (final RepositoryConnection connection = repository.getConnection();</span> <span class="fc" id="L244"> final Reader reader = Files.newBufferedReader(path, UTF_8))</span> { <span class="fc" id="L246"> log.info(&quot;Importing repository from {} ...&quot;, path);</span> <span class="fc" id="L247"> connection.add(reader, path.toUri().toString(), RDFFormat.N3);</span> <span class="fc" id="L248"> connection.commit();</span> } <span class="fc" id="L250"> }</span> /******************************************************************************************************************* * * ******************************************************************************************************************/ private void waitForPowerOn() { try { <span class="nc bnc" id="L260" title="All 2 branches missed."> if (!initialized.await(10, SECONDS))</span> { <span class="nc" id="L262"> throw new IllegalStateException(&quot;Did not receive PowerOnNotification&quot;);</span> } } <span class="nc" id="L265"> catch (InterruptedException ex)</span> { <span class="nc" id="L267"> throw new IllegalStateException(&quot;Interrupted while waiting for PowerOnNotification&quot;);</span> <span class="nc" id="L268"> }</span> <span class="nc" id="L269"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>