private void checkAndUpdateIndexDescriptor()

in indexer-core/src/main/java/org/apache/maven/index/context/DefaultIndexingContext.java [315:363]


    private void checkAndUpdateIndexDescriptor(boolean reclaimIndex)
            throws IOException, ExistingLuceneIndexMismatchException {
        if (reclaimIndex) {
            // forcefully "reclaiming" the ownership of the index as ours
            storeDescriptor();
            return;
        }

        // check for descriptor if this is not a "virgin" index
        if (getSize() > 0) {
            final TopScoreDocCollector collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
            final IndexSearcher indexSearcher = acquireIndexSearcher();
            try {
                indexSearcher.search(new TermQuery(DESCRIPTOR_TERM), collector);

                if (collector.getTotalHits() == 0) {
                    throw new ExistingLuceneIndexMismatchException("The existing index has no NexusIndexer descriptor");
                }

                if (collector.getTotalHits() > 1) {
                    // eh? this is buggy index it seems, just iron it out then
                    storeDescriptor();
                } else {
                    // good, we have one descriptor as should
                    Document descriptor = indexSearcher.storedFields().document(collector.topDocs().scoreDocs[0].doc);
                    String[] h = StringUtils.split(descriptor.get(FLD_IDXINFO), ArtifactInfo.FS);
                    // String version = h[0];
                    String repoId = h[1];

                    // // compare version
                    // if ( !VERSION.equals( version ) )
                    // {
                    // throw new UnsupportedExistingLuceneIndexException(
                    // "The existing index has version [" + version + "] and not [" + VERSION + "] version!" );
                    // }

                    if (getRepositoryId() == null) {
                        repositoryId = repoId;
                    } else if (!getRepositoryId().equals(repoId)) {
                        throw new ExistingLuceneIndexMismatchException(
                                "The existing index is for repository " //
                                        + "[" + repoId + "] and not for repository [" + getRepositoryId() + "]");
                    }
                }
            } finally {
                releaseIndexSearcher(indexSearcher);
            }
        }
    }