All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sirix.index.name.NameIndexBuilder Maven / Gradle / Ivy

package org.sirix.index.name;

import java.util.Optional;
import java.util.Set;
import org.brackit.xquery.atomic.QNm;
import org.sirix.api.visitor.VisitResultType;
import org.sirix.exception.SirixIOException;
import org.sirix.index.SearchMode;
import org.sirix.index.avltree.AVLTreeReader.MoveCursor;
import org.sirix.index.avltree.AVLTreeWriter;
import org.sirix.index.avltree.keyvalue.NodeReferences;
import org.sirix.node.interfaces.immutable.ImmutableNode;
import org.sirix.utils.LogWrapper;
import org.slf4j.LoggerFactory;

public final class NameIndexBuilder {
  private static final LogWrapper LOGGER = new LogWrapper(LoggerFactory.getLogger(NameIndexBuilder.class));

  public Set mIncludes;
  public Set mExcludes;
  public AVLTreeWriter mAVLTreeWriter;

  public NameIndexBuilder(final Set includes, final Set excludes,
      final AVLTreeWriter avlTreeWriter) {
    mIncludes = includes;
    mExcludes = excludes;
    mAVLTreeWriter = avlTreeWriter;
  }

  public VisitResultType build(QNm name, ImmutableNode node) {
    final boolean included = (mIncludes.isEmpty() || mIncludes.contains(name));
    final boolean excluded = (!mExcludes.isEmpty() && mExcludes.contains(name));

    if (!included || excluded) {
      return VisitResultType.CONTINUE;
    }

    final Optional textReferences = mAVLTreeWriter.get(name, SearchMode.EQUAL);

    try {
      textReferences.ifPresentOrElse(nodeReferences -> setNodeReferences(node, nodeReferences, name),
          () -> setNodeReferences(node, new NodeReferences(), name));
    } catch (final SirixIOException e) {
      LOGGER.error(e.getMessage(), e);
    }

    return VisitResultType.CONTINUE;
  }

  private void setNodeReferences(final ImmutableNode node, final NodeReferences references, final QNm name) {
    mAVLTreeWriter.index(name, references.addNodeKey(node.getNodeKey()), MoveCursor.NO_MOVE);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy