Content of file PathAwareEntityFinderDelegate.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>PathAwareEntityFinderDelegate.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> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-model</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.model.impl</a> &gt; <span class="el_source">PathAwareEntityFinderDelegate.java</span></div><h1>PathAwareEntityFinderDelegate.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * 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.model.impl;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.nio.file.Path;
import it.tidalwave.util.As;
import it.tidalwave.util.Finder;
import it.tidalwave.util.SupplierBasedFinder;
import it.tidalwave.util.spi.FinderSupport;
import it.tidalwave.role.SimpleComposite;
import it.tidalwave.bluemarine2.model.MediaFolder;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import it.tidalwave.bluemarine2.model.spi.PathAwareFinder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static java.util.Collections.singletonList;
import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
import static lombok.AccessLevel.PRIVATE;

/***********************************************************************************************************************
 *
 * A decorator of an {@link Finder} of {@link PathAwareEntity} that creates a virtual tree of entities. Each entity is
 * given a path, which starts with the path of a {@link MediaFolder} and continues with the id of the entity.
 *
 * This {@code Finder} can filtered by path. If a filter path is provided, the filtering happens in memory: this means
 * that even when the delegate queries a native store, all the data are first retrieved in memory.
 *
 * @stereotype  Finder
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L65">@RequiredArgsConstructor(access = PRIVATE) @Slf4j</span>
public class PathAwareEntityFinderDelegate extends FinderSupport&lt;PathAwareEntity, PathAwareFinder&gt; implements PathAwareFinder
  {
    private static final long serialVersionUID = 4429676480224742813L;

    @Nonnull
    private final MediaFolder mediaFolder;

    @Nonnull
    private final Finder&lt;PathAwareEntity&gt; delegate;

    @Nonnull
    private final Optional&lt;Path&gt; optionalPath;

    /*******************************************************************************************************************
     *
     * Creates an instance associated to a given {@link MediaFolder} and a delegate finder.
     *
     * @see #PathAwareEntityFinderDelegate(it.tidalwave.bluemarine2.model.MediaFolder, java.util.function.Function)
     *
     * @param   mediaFolder     the folder associated to this finder
     * @param   delegate        the delegate finder to provide data
     *
     ******************************************************************************************************************/
    public PathAwareEntityFinderDelegate (@Nonnull final MediaFolder mediaFolder,
                                          @Nonnull final Finder&lt;PathAwareEntity&gt; delegate)
      {
<span class="nc" id="L92">        this(mediaFolder, delegate, Optional.empty());</span>
<span class="nc" id="L93">      }</span>

    /*******************************************************************************************************************
     *
     * Creates an instance associated to a given {@link MediaFolder} and a function for providing children. This
     * constructor is typically used when the children are already present in memory (e.g. they are
     * {@link VirtualMediaFolder}s. Because the function doesn't have the full semantics of a {@link Finder} - it can't
     * optimise a query in function of search parameters, nor optimise the count of results - when a
     * {@code PathAwareEntityFinderDelegate} is created in this way all operations will be performed in memory. If one
     * can provide data from a native store and enjoy optimised queries, instead of this constructor use
     * {@link #PathAwareEntityFinderDelegate(it.tidalwave.bluemarine2.model.MediaFolder, it.tidalwave.util.Finder)}
     *
     * @see #PathAwareEntityFinderDelegate(it.tidalwave.bluemarine2.model.MediaFolder, it.tidalwave.util.Finder)
     *
     * @param   mediaFolder     the folder associated to this finder
     * @param   function        the function that provides children
     *
     ******************************************************************************************************************/
    public PathAwareEntityFinderDelegate (@Nonnull final MediaFolder mediaFolder,
                                          @Nonnull final Function&lt;MediaFolder, Collection&lt;? extends PathAwareEntity&gt;&gt; function)
      {
<span class="fc" id="L114">        this(mediaFolder, new SupplierBasedFinder&lt;&gt;(() -&gt; function.apply(mediaFolder)), Optional.empty());</span>
<span class="fc" id="L115">      }</span>

    /*******************************************************************************************************************
     *
     * Clone constructor.
     *
     ******************************************************************************************************************/
    public PathAwareEntityFinderDelegate (@Nonnull final PathAwareEntityFinderDelegate other,
                                          @Nonnull final Object override)
      {
<span class="fc" id="L125">        super(other, override);</span>
<span class="fc" id="L126">        final PathAwareEntityFinderDelegate source = getSource(PathAwareEntityFinderDelegate.class, other, override);</span>
<span class="fc" id="L127">        this.mediaFolder = source.mediaFolder;</span>
<span class="fc" id="L128">        this.delegate = source.delegate;</span>
<span class="fc" id="L129">        this.optionalPath = source.optionalPath;</span>
<span class="fc" id="L130">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public PathAwareFinder withPath (@Nonnull final Path path)
      {
<span class="fc" id="L140">        return clonedWith(new PathAwareEntityFinderDelegate(mediaFolder, delegate, Optional.of(path)));</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    protected List&lt;? extends PathAwareEntity&gt; computeResults()
      {
<span class="fc" id="L151">        return new CopyOnWriteArrayList&lt;&gt;(optionalPath.flatMap(path -&gt; filteredByPath(path).map(e -&gt; singletonList(e)))</span>
<span class="fc" id="L152">                                                      .orElse((List)delegate.results()));</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnegative
    public int count()
      {
<span class="nc" id="L163">        optionalPath.ifPresent(path -&gt; log.warn(&quot;Path present: {} - count won't be a native query&quot;, path));</span>
<span class="nc" id="L164">        return optionalPath.map(path -&gt; filteredByPath(path).map(entity -&gt; 1).orElse(0))</span>
<span class="nc" id="L165">                           .orElse(delegate.count());</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private Optional&lt;? extends PathAwareEntity&gt; filteredByPath (@Nonnull final Path path)
      {
<span class="fc" id="L176">        log.debug(&quot;filteredByPath({})&quot;, path);</span>
<span class="fc bfc" id="L177" title="All 2 branches covered.">        return mediaFolder.getPath().equals(path)</span>
<span class="fc" id="L178">                                        ? Optional.of(mediaFolder)</span>
<span class="fc bfc" id="L179" title="All 2 branches covered.">                                        : childMatchingPathHead(path).flatMap(entity -&gt; path.equals(entity.getPath())</span>
<span class="fc" id="L180">                                                ? Optional.of(entity)</span>
<span class="fc" id="L181">                                                : childMatchingPath(entity, path));</span>
      }

    /*******************************************************************************************************************
     *
     * Returns the child entity that matches the first element of the path, if present. The path can be exactly the one
     * of the found entity, or it can be of one of its children.
     *
     * This method performs a bulk query of all children and then filters by path in memory. It is not possible to
     * use a query to the native store for the path - which would be good for performance reasons - , because even
     * though each segment of the path is function of some attribute of the related {@code PathAwareEntity} - typically
     * the id - it is not a matter of the native store. Performance of this section relies upon memory caching. Some
     * experiment showed that it's not useful to add another caching layer here, and the one in
     * {@code RepositoryFinderSupport} is enough.
     *
     * @param   path    the path
     * @return          the entity, if present
     *
     ******************************************************************************************************************/
    @Nonnull
    private Optional&lt;PathAwareEntity&gt; childMatchingPathHead (@Nonnull final Path path)
      {
//                assert filtered.size() == 1 or 0;
<span class="fc" id="L204">        log.debug(&quot;&gt;&gt;&gt;&gt; bulk query to {}, filtering in memory&quot;, delegate);</span>
<span class="fc" id="L205">        return (Optional&lt;PathAwareEntity&gt;)delegate.results().stream()</span>
<span class="fc" id="L206">                                                  .filter(entity -&gt; sameHead(relative(path), relative(entity.getPath())))</span>
<span class="fc" id="L207">                                                  .findFirst();</span>
      }

    /*******************************************************************************************************************
     *
     * @param   entity
     * @param   path    the path
     * @return          the entity, if present
     *
     ******************************************************************************************************************/
    @Nonnull
    private static Optional&lt;PathAwareEntity&gt; childMatchingPath (@Nonnull final PathAwareEntity entity,
                                                                @Nonnull final Path path)
      {
<span class="fc" id="L221">        return ((PathAwareFinder)asSimpleComposite(entity).findChildren()).withPath(path).optionalResult();</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull // FIXME: this should be normally done by as()
this should be normally done by as()
private static SimpleComposite asSimpleComposite (@Nonnull final As object) { <span class="pc bpc" id="L232" title="1 of 2 branches missed."> return (object instanceof SimpleComposite) ? (SimpleComposite)object : object.as(_SimpleComposite_);</span> } /******************************************************************************************************************* * * Relativizes a path against the finder path, that is it removes the parent path. If the path can't be * relativized, that is it doesn't start with the finder path, returns null. * ******************************************************************************************************************/ @Nullable private Path relative (@Nonnull final Path path) { <span class="fc bfc" id="L244" title="All 2 branches covered."> return mediaFolder.getParent().isEmpty() ? path :</span> <span class="pc bpc" id="L245" title="1 of 2 branches missed."> path.startsWith(mediaFolder.getPath()) ? path.subpath(mediaFolder.getPath().getNameCount(), path.getNameCount())</span> <span class="nc" id="L246"> : null;</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ private static boolean sameHead (@Nullable final Path path1, @Nullable final Path path2) { <span class="pc bpc" id="L256" title="2 of 6 branches missed."> return (path1 != null) &amp;&amp; (path2 != null) &amp;&amp; path1.getName(0).equals(path2.getName(0));</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>