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 :: Media Scanner</a> > <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-persistence</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.persistence.impl</a> > <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 "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.
*
* *********************************************************************************************************************
*
* 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("onPowerOnNotification({})", notification);</span>
<span class="fc" id="L104"> final TypeSafeMap properties = notification.getProperties();</span>
<span class="fc" id="L106"> final Optional<Path> importFile = properties.getOptional(IMPORT_FILE);</span>
<span class="fc" id="L107"> final Optional<Path> 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("No storage path: working in memory");</span>
<span class="fc" id="L112"> sail = new MemoryStore();</span>
}
else
{
<span class="fc" id="L116"> log.info("Disk storage at {}", storageFolder);</span>
<span class="pc bpc" id="L118" title="1 of 4 branches missed."> if (importFile.isPresent() && Files.exists(importFile.get()))</span>
{
<span class="fc" id="L120"> log.warn("Scratching store ...");</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() && 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() + "~"));</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("onPowerOffNotification({})", 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("exportToFile({})", 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("bio", "http://purl.org/vocab/bio/0.1/");</span>
<span class="fc" id="L188"> writer.handleNamespace("bmmo", "http://bluemarine.tidalwave.it/2015/04/mo/");</span>
<span class="fc" id="L189"> writer.handleNamespace("dc", "http://purl.org/dc/elements/1.1/");</span>
<span class="fc" id="L190"> writer.handleNamespace("foaf", "http://xmlns.com/foaf/0.1/");</span>
<span class="fc" id="L191"> writer.handleNamespace("owl", "http://www.w3.org/2002/07/owl#");</span>
<span class="fc" id="L192"> writer.handleNamespace("mo", "http://purl.org/ontology/mo/");</span>
<span class="fc" id="L193"> writer.handleNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");</span>
<span class="fc" id="L194"> writer.handleNamespace("rel", "http://purl.org/vocab/relationship/");</span>
<span class="fc" id="L195"> writer.handleNamespace("vocab", "http://dbtune.org/musicbrainz/resource/vocab/");</span>
<span class="fc" id="L196"> writer.handleNamespace("xs", "http://www.w3.org/2001/XMLSchema#");</span>
<span class="fc" id="L198"> connection.export(writer);</span>
}
<span class="fc" id="L200"> }</span>
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
public <E extends Exception> void runInTransaction (@Nonnull final TransactionalTask<E> task)
throws E, RepositoryException
{
<span class="fc" id="L210"> log.info("runInTransaction({})", task);</span>
<span class="fc" id="L211"> waitForPowerOn();</span>
<span class="fc" id="L212"> final long baseTime = System.nanoTime();</span>
<span class="fc" id="L214"> try (final RepositoryConnection connection = repository.getConnection()) // TODO: pool?</span>
{
<span class="fc" id="L216"> task.run(connection);</span>
<span class="fc" id="L217"> connection.commit();</span>
}
<span class="nc" id="L219"> catch (Exception e)</span>
{
<span class="nc" id="L221"> log.error("Transaction failed: {}", e.toString());</span>
<span class="fc" id="L222"> }</span>
<span class="pc bpc" id="L224" title="1 of 2 branches missed."> if (log.isDebugEnabled())</span>
{
<span class="fc" id="L226"> log.debug(">>>> done in {} ms", (System.nanoTime() - baseTime) * 1E-6);</span>
}
<span class="fc" 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("Importing repository from {} ...", 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="pc bpc" id="L260" title="1 of 2 branches missed."> if (!initialized.await(10, SECONDS))</span>
{
<span class="nc" id="L262"> throw new IllegalStateException("Did not receive PowerOnNotification");</span>
}
}
<span class="nc" id="L265"> catch (InterruptedException ex)</span>
{
<span class="nc" id="L267"> throw new IllegalStateException("Interrupted while waiting for PowerOnNotification");</span>
<span class="fc" id="L268"> }</span>
<span class="fc" 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>