/**
   * Check if the named node type is in use in any workspace in the repository
   *
   * @param nodeTypeName the name of the node type to check
   * @return true if at least one node is using that type; false otherwise
   * @throws InvalidQueryException if there is an error searching for uses of the named node type
   */
  boolean isNodeTypeInUse(Name nodeTypeName) throws InvalidQueryException {

    String nodeTypeString = nodeTypeName.getString(context.getNamespaceRegistry());
    String expression = "SELECT * from [" + nodeTypeString + "] LIMIT 1";
    TypeSystem typeSystem = context.getValueFactories().getTypeSystem();
    // Parsing must be done now ...
    QueryCommand command = queryParser.parseQuery(expression, typeSystem);
    assert command != null : "Could not parse " + expression;

    Schemata schemata = getRepositorySchemata();

    // Now query the entire repository for any nodes that use this node type ...
    RepositoryCache repoCache = repository.repositoryCache();
    RepositoryQueryManager queryManager = repository.queryManager();
    Set<String> workspaceNames = repoCache.getWorkspaceNames();
    Map<String, NodeCache> overridden = null;
    NodeTypes nodeTypes = repository.nodeTypeManager().getNodeTypes();
    RepositoryIndexes indexDefns = repository.queryManager().getIndexes();
    CancellableQuery query =
        queryManager.query(
            context,
            repoCache,
            workspaceNames,
            overridden,
            command,
            schemata,
            indexDefns,
            nodeTypes,
            null,
            null);
    try {
      QueryResults result = query.execute();
      if (result.isEmpty()) return false;
      if (result.getRowCount() < 0) {
        // Try to get the first row ...
        NodeSequence seq = result.getRows();
        Batch batch = seq.nextBatch();
        while (batch != null) {
          if (batch.hasNext()) return true;
          // It's not common for the first batch may be empty, but it's possible. So try the next
          // batch ...
          batch = seq.nextBatch();
        }
        return false;
      }
      return result.getRowCount() > 0;
    } catch (RepositoryException e) {
      logger.error(e, JcrI18n.errorCheckingNodeTypeUsage, nodeTypeName, e.getLocalizedMessage());
      return true;
    }
  }
 protected RepositoryIndexes readIndexDefinitions() {
   // There were at least some changes ...
   NodeTypes nodeTypes = repository.nodeTypeManager().getNodeTypes();
   try {
     // Read the affected index definitions ...
     SessionCache systemCache = repository.createSystemSession(context, false);
     SystemContent system = new SystemContent(systemCache);
     Collection<IndexDefinition> indexDefns = system.readAllIndexDefinitions(providers.keySet());
     this.indexes = new Indexes(context, indexDefns, nodeTypes);
     return this.indexes;
   } catch (WorkspaceNotFoundException e) {
     // This happens occasionally when shutting down ...
   } catch (Throwable e) {
     logger.error(e, JcrI18n.errorRefreshingIndexDefinitions, repository.name());
   }
   return indexes;
 }
  protected ScanningTasks notify(ChangeSet changeSet) {
    if (changeSet.getWorkspaceName() == null) {
      // This is a change to the workspaces or repository metadata ...

      // Refresh the index definitions ...
      RepositoryIndexes indexes = readIndexDefinitions();
      ScanningTasks feedback = new ScanningTasks();
      if (!indexes.getIndexDefinitions().isEmpty()) {
        // Build up the names of the added and removed workspace names ...
        Set<String> addedWorkspaces = new HashSet<>();
        Set<String> removedWorkspaces = new HashSet<>();
        for (Change change : changeSet) {
          if (change instanceof WorkspaceAdded) {
            WorkspaceAdded added = (WorkspaceAdded) change;
            addedWorkspaces.add(added.getWorkspaceName());
          } else if (change instanceof WorkspaceRemoved) {
            WorkspaceRemoved removed = (WorkspaceRemoved) change;
            removedWorkspaces.add(removed.getWorkspaceName());
          }
        }
        if (!addedWorkspaces.isEmpty() || !removedWorkspaces.isEmpty()) {
          // Figure out which providers need to be called, and which definitions go with those
          // providers ...
          Map<String, List<IndexDefinition>> defnsByProvider = new HashMap<>();
          for (IndexDefinition defn : indexes.getIndexDefinitions().values()) {
            String providerName = defn.getProviderName();
            List<IndexDefinition> defns = defnsByProvider.get(providerName);
            if (defns == null) {
              defns = new ArrayList<>();
              defnsByProvider.put(providerName, defns);
            }
            defns.add(defn);
          }
          // Then for each provider ...
          for (Map.Entry<String, List<IndexDefinition>> entry : defnsByProvider.entrySet()) {
            String providerName = entry.getKey();
            WorkspaceIndexChanges changes =
                new WorkspaceIndexChanges(entry.getValue(), addedWorkspaces, removedWorkspaces);
            IndexProvider provider = providers.get(providerName);
            if (provider == null) continue;
            provider.notify(
                changes,
                repository.changeBus(),
                repository.nodeTypeManager(),
                repository.repositoryCache().getWorkspaceNames(),
                feedback.forProvider(providerName));
          }
        }
      }
      return feedback;
    }
    if (!systemWorkspaceName.equals(changeSet.getWorkspaceName())) {
      // The change does not affect the 'system' workspace, so skip it ...
      return null;
    }

    // It is simple to listen to all local and remote changes. Therefore, any changes made locally
    // to the index definitions
    // will be propagated through the cached representation via this listener.
    AtomicReference<Map<Name, IndexChangeInfo>> changesByProviderName = new AtomicReference<>();
    for (Change change : changeSet) {
      if (change instanceof NodeAdded) {
        NodeAdded added = (NodeAdded) change;
        Path addedPath = added.getPath();
        if (indexesPath.isAncestorOf(addedPath)) {
          // Get the name of the affected provider ...
          Name providerName = addedPath.getSegment(2).getName();
          if (addedPath.size() > 3) {
            // Adding an index (or column definition), but all we care about is the name of the
            // index
            Name indexName = addedPath.getSegment(3).getName();
            changeInfoForProvider(changesByProviderName, providerName).changed(indexName);
          }
        }
      } else if (change instanceof NodeRemoved) {
        NodeRemoved removed = (NodeRemoved) change;
        Path removedPath = removed.getPath();
        if (indexesPath.isAncestorOf(removedPath)) {
          // Get the name of the affected provider ...
          Name providerName = removedPath.getSegment(2).getName();
          if (removedPath.size() > 4) {
            // It's a column definition being removed, so the index is changed ...
            Name indexName = removedPath.getSegment(3).getName();
            changeInfoForProvider(changesByProviderName, providerName).removed(indexName);
          } else if (removedPath.size() > 3) {
            // Removing an index (or column definition), but all we care about is the name of the
            // index
            Name indexName = removedPath.getSegment(3).getName();
            changeInfoForProvider(changesByProviderName, providerName).removed(indexName);
          } else if (removedPath.size() == 3) {
            // The whole provider was removed ...
            changeInfoForProvider(changesByProviderName, providerName).removedAll();
          }
        }
      } else if (change instanceof PropertyChanged) {
        PropertyChanged propChanged = (PropertyChanged) change;
        Path changedPath = propChanged.getPathToNode();
        if (indexesPath.isAncestorOf(changedPath)) {
          if (changedPath.size() > 3) {
            // Adding an index (or column definition), but all we care about is the name of the
            // index
            Name providerName = changedPath.getSegment(2).getName();
            Name indexName = changedPath.getSegment(3).getName();
            changeInfoForProvider(changesByProviderName, providerName).changed(indexName);
          }
        }
      } // we don't care about node moves (don't happen) or property added/removed (handled by node
      // add/remove)
    }

    if (changesByProviderName.get() == null || changesByProviderName.get().isEmpty()) {
      // No changes to the indexes ...
      return null;
    }
    // Refresh the index definitions ...
    RepositoryIndexes indexes = readIndexDefinitions();

    // And notify the affected providers ...
    StringFactory strings = context.getValueFactories().getStringFactory();
    ScanningTasks feedback = new ScanningTasks();
    for (Map.Entry<Name, IndexChangeInfo> entry : changesByProviderName.get().entrySet()) {
      String providerName = strings.create(entry.getKey());
      IndexProvider provider = providers.get(providerName);
      if (provider == null) continue;

      IndexChanges changes = new IndexChanges();
      IndexChangeInfo info = entry.getValue();
      if (info.removedAll) {
        // Get all of the definitions for this provider ...
        for (IndexDefinition defn : indexes.getIndexDefinitions().values()) {
          if (defn.getProviderName().equals(providerName)) changes.remove(defn.getName());
        }
      }
      // Others might have been added or changed after the existing ones were removed ...
      for (Name name : info.removedIndexes) {
        changes.remove(strings.create(name));
      }
      for (Name name : info.changedIndexes) {
        IndexDefinition defn = indexes.getIndexDefinitions().get(strings.create(name));
        if (defn != null) changes.change(defn);
      }
      // Notify the provider ...
      try {
        provider.notify(
            changes,
            repository.changeBus(),
            repository.nodeTypeManager(),
            repository.repositoryCache().getWorkspaceNames(),
            feedback.forProvider(providerName));
      } catch (RuntimeException e) {
        logger.error(
            e,
            JcrI18n.errorNotifyingProviderOfIndexChanges,
            providerName,
            repository.name(),
            e.getMessage());
      }
    }

    // Finally swap the snapshot of indexes ...
    this.indexes = indexes;
    return feedback;
  }
  @Override
  public void registerIndexes(IndexDefinition[] indexDefinitions, boolean allowUpdate)
      throws InvalidIndexDefinitionException, IndexExistsException {
    CheckArg.isNotNull(indexDefinitions, "indexDefinitions");

    // Before we do anything, validate each of the index definitions and throw an exception ...
    RepositoryNodeTypeManager nodeTypeManager = repository.nodeTypeManager();
    List<IndexDefinition> validated = new ArrayList<>(indexDefinitions.length);
    Problems problems = new SimpleProblems();
    for (IndexDefinition defn : indexDefinitions) {
      String name = defn.getName();
      String providerName = defn.getProviderName();

      if (name == null) {
        problems.addError(JcrI18n.indexMustHaveName, defn, repository.name());
        continue;
      }
      if (indexes.getIndexDefinitions().containsKey(name) && !allowUpdate) {
        // Throw this one immediately ...
        String msg = JcrI18n.indexAlreadyExists.text(defn.getName(), repository.name());
        throw new IndexExistsException(msg);
      }
      if (providerName == null) {
        problems.addError(JcrI18n.indexMustHaveProviderName, defn.getName(), repository.name());
        continue;
      }
      if (defn.hasSingleColumn()) {
        IndexColumnDefinition columnDefn = defn.getColumnDefinition(0);
        Name propName =
            context.getValueFactories().getNameFactory().create(columnDefn.getPropertyName());
        switch (defn.getKind()) {
          case UNIQUE_VALUE:
            if (NON_UNIQUE_PROPERTY_NAMES.contains(propName)) {
              problems.addError(
                  JcrI18n.unableToCreateUniqueIndexForColumn,
                  defn.getName(),
                  columnDefn.getPropertyName());
            }
            break;
          case ENUMERATED_VALUE:
            if (NON_ENUMERATED_PROPERTY_NAMES.contains(propName)) {
              problems.addError(
                  JcrI18n.unableToCreateEnumeratedIndexForColumn,
                  defn.getName(),
                  columnDefn.getPropertyName());
            }
            break;
          case VALUE:
          case NODE_TYPE:
          case TEXT:
            break;
        }
      } else {
        // Mulitple columns ...
        if (defn.getKind() == IndexKind.NODE_TYPE) {
          // must be single-column indexes
          problems.addError(JcrI18n.nodeTypeIndexMustHaveOneColumn, defn.getName());
        }
      }
      IndexProvider provider = providers.get(providerName);
      if (provider == null) {
        problems.addError(JcrI18n.indexProviderDoesNotExist, defn.getName(), repository.name());
      } else {
        // Perform some default validations that should be applied to all providers...
        provider.validateDefaultColumnTypes(context, defn, problems);

        // Then have the provider perform any custom validations
        provider.validateProposedIndex(context, defn, nodeTypeManager, problems);

        // Create an instance of our own definition implementation ...
        defn = RepositoryIndexDefinition.createFrom(defn, true);

        validated.add(defn);
      }
    }
    if (problems.hasErrors()) {
      String msg = JcrI18n.invalidIndexDefinitions.text(repository.name(), problems);
      throw new InvalidIndexDefinitionException(new JcrProblems(problems), msg);
    }

    SessionCache systemCache = repository.createSystemSession(context, false);
    SystemContent system = new SystemContent(systemCache);
    for (IndexDefinition defn : validated) {
      String providerName = defn.getProviderName();

      // Determine if the index should be enabled ...
      defn = RepositoryIndexDefinition.createFrom(defn, providers.containsKey(providerName));

      // Write the definition to the system area ...
      system.store(defn, allowUpdate);
    }
    // Save the changes ...
    systemCache.save();

    // Refresh the immutable snapshot ...
    this.indexes = readIndexDefinitions();
  }
  /**
   * Initialize this manager by calling {@link IndexProvider#initialize()} on each of the
   * currently-registered providers.
   *
   * @return the information about the portions of the repository that need to be scanned to
   *     (re)build indexes; null if no scanning is required
   */
  protected synchronized ScanningTasks initialize() {
    if (initialized.get()) {
      // nothing to do ...
      return null;
    }

    // Initialize each of the providers, removing any that are not properly initialized ...
    for (Iterator<Map.Entry<String, IndexProvider>> providerIter = providers.entrySet().iterator();
        providerIter.hasNext(); ) {
      IndexProvider provider = providerIter.next().getValue();
      try {
        doInitialize(provider);
      } catch (Throwable t) {
        if (t.getCause() != null) {
          t = t.getCause();
        }
        repository.error(
            t,
            JcrI18n.unableToInitializeIndexProvider,
            provider.getName(),
            repository.name(),
            t.getMessage());
        providerIter.remove();
      }
    }
    // Re-read the index definitions in case there were disabled index definitions that used the
    // now-available provider ...
    RepositoryIndexes indexes = readIndexDefinitions();

    // Notify the providers of all the index definitions (which we'll treat as "new" since we're
    // just starting up) ...
    ScanningTasks feedback = new ScanningTasks();
    for (Iterator<Map.Entry<String, IndexProvider>> providerIter = providers.entrySet().iterator();
        providerIter.hasNext(); ) {
      IndexProvider provider = providerIter.next().getValue();
      if (provider == null) continue;
      final String providerName = provider.getName();

      IndexChanges changes = new IndexChanges();
      for (IndexDefinition indexDefn : indexes.getIndexDefinitions().values()) {
        if (!providerName.equals(indexDefn.getProviderName())) continue;
        changes.change(indexDefn);
      }
      // Even if there are no definitions, we still want to notify each of the providers ...
      try {
        provider.notify(
            changes,
            repository.changeBus(),
            repository.nodeTypeManager(),
            repository.repositoryCache().getWorkspaceNames(),
            feedback.forProvider(providerName));
      } catch (RuntimeException e) {
        logger.error(
            e,
            JcrI18n.errorNotifyingProviderOfIndexChanges,
            providerName,
            repository.name(),
            e.getMessage());
      }
    }

    // Refresh the index writer ...
    refreshIndexWriter();
    initialized.set(true);
    return feedback;
  }