Example usage for org.apache.lucene.index DirectoryReader listCommits

List of usage examples for org.apache.lucene.index DirectoryReader listCommits

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader listCommits.

Prototype

public static List<IndexCommit> listCommits(Directory dir) throws IOException 

Source Link

Document

Returns all commit points that exist in the Directory.

Usage

From source file:org.elasticsearch.indices.recovery.PeerRecoveryTargetServiceTests.java

License:Apache License

public void testGetStartingSeqNo() throws Exception {
    final IndexShard replica = newShard(false);
    try {/*from  w  w  w . j a  va  2 s .  c  om*/
        // Empty store
        {
            recoveryEmptyReplica(replica);
            final RecoveryTarget recoveryTarget = new RecoveryTarget(replica, null, null, null);
            assertThat(PeerRecoveryTargetService.getStartingSeqNo(logger, recoveryTarget), equalTo(0L));
            recoveryTarget.decRef();
        }
        // Last commit is good - use it.
        final long initDocs = scaledRandomIntBetween(1, 10);
        {
            for (int i = 0; i < initDocs; i++) {
                indexDoc(replica, "doc", Integer.toString(i));
                if (randomBoolean()) {
                    flushShard(replica);
                }
            }
            flushShard(replica);
            replica.updateGlobalCheckpointOnReplica(initDocs - 1, "test");
            replica.getTranslog().sync();
            final RecoveryTarget recoveryTarget = new RecoveryTarget(replica, null, null, null);
            assertThat(PeerRecoveryTargetService.getStartingSeqNo(logger, recoveryTarget), equalTo(initDocs));
            recoveryTarget.decRef();
        }
        // Global checkpoint does not advance, last commit is not good - use the previous commit
        final int moreDocs = randomIntBetween(1, 10);
        {
            for (int i = 0; i < moreDocs; i++) {
                indexDoc(replica, "doc", Long.toString(i));
                if (randomBoolean()) {
                    flushShard(replica);
                }
            }
            flushShard(replica);
            final RecoveryTarget recoveryTarget = new RecoveryTarget(replica, null, null, null);
            assertThat(PeerRecoveryTargetService.getStartingSeqNo(logger, recoveryTarget), equalTo(initDocs));
            recoveryTarget.decRef();
        }
        // Advances the global checkpoint, a safe commit also advances
        {
            replica.updateGlobalCheckpointOnReplica(initDocs + moreDocs - 1, "test");
            replica.getTranslog().sync();
            final RecoveryTarget recoveryTarget = new RecoveryTarget(replica, null, null, null);
            assertThat(PeerRecoveryTargetService.getStartingSeqNo(logger, recoveryTarget),
                    equalTo(initDocs + moreDocs));
            recoveryTarget.decRef();
        }
        // Different translogUUID, fallback to file-based
        {
            replica.close("test", false);
            final List<IndexCommit> commits = DirectoryReader.listCommits(replica.store().directory());
            IndexWriterConfig iwc = new IndexWriterConfig(null).setCommitOnClose(false)
                    .setMergePolicy(NoMergePolicy.INSTANCE).setOpenMode(IndexWriterConfig.OpenMode.APPEND);
            try (IndexWriter writer = new IndexWriter(replica.store().directory(), iwc)) {
                final Map<String, String> userData = new HashMap<>(
                        commits.get(commits.size() - 1).getUserData());
                userData.put(Translog.TRANSLOG_UUID_KEY, UUIDs.randomBase64UUID());
                writer.setLiveCommitData(userData.entrySet());
                writer.commit();
            }
            final RecoveryTarget recoveryTarget = new RecoveryTarget(replica, null, null, null);
            assertThat(PeerRecoveryTargetService.getStartingSeqNo(logger, recoveryTarget),
                    equalTo(SequenceNumbers.UNASSIGNED_SEQ_NO));
            recoveryTarget.decRef();
        }
    } finally {
        closeShards(replica);
    }
}

From source file:org.eu.bitzone.Leia.java

License:Apache License

private void showCommits() throws Exception {
    final Object commitsTable = find("commitsTable");
    removeAll(commitsTable);/* w  w  w  . j a va2  s .c  o m*/
    if (dir == null) {
        final Object row = create("row");
        final Object cell = create("cell");
        setString(cell, "text", "<not available>");
        setBoolean(cell, "enabled", false);
        add(row, cell);
        add(commitsTable, row);
        return;
    }
    final List<IndexCommit> commits = DirectoryReader.listCommits(dir);
    final IndexCommit current = ir instanceof DirectoryReader ? ((DirectoryReader) ir).getIndexCommit() : null;
    // commits are ordered from oldest to newest ?
    final Iterator<IndexCommit> it = commits.iterator();
    int rowNum = 0;
    while (it.hasNext()) {
        final IndexCommit commit = it.next();
        // figure out the name of the segment files
        final Collection<String> files = commit.getFileNames();
        final Iterator<String> itf = files.iterator();
        final Object row = create("row");
        final boolean enabled = rowNum < commits.size() - 1;
        final Color color = Color.BLUE;
        rowNum++;
        add(commitsTable, row);
        putProperty(row, "commit", commit);
        if (enabled) {
            putProperty(row, "commitDeletable", Boolean.TRUE);
        }
        Object cell = create("cell");
        final String gen = String.valueOf(commit.getGeneration());
        setString(cell, "text", gen);
        add(row, cell);
        cell = create("cell");
        setString(cell, "text", commit.isDeleted() ? "Y" : "N");
        add(row, cell);
        cell = create("cell");
        setString(cell, "text", String.valueOf(commit.getSegmentCount()));
        add(row, cell);
        cell = create("cell");
        final Map userData = commit.getUserData();
        if (userData != null && !userData.isEmpty()) {
            setString(cell, "text", userData.toString());
        } else {
            setString(cell, "text", "");
        }
        add(row, cell);
        if (commit.equals(current)) {
            final Object[] cells = getItems(row);
            for (final Object c : cells) {
                setColor(c, "foreground", color);
            }
        }
    }
}

From source file:org.getopt.luke.Luke.java

License:Apache License

private void showCommits() throws Exception {
    Object commitsTable = find("commitsTable");
    removeAll(commitsTable);// w w  w .j ava 2 s  . c om
    if (dir == null) {
        Object row = create("row");
        Object cell = create("cell");
        setString(cell, "text", "<not available>");
        setBoolean(cell, "enabled", false);
        add(row, cell);
        add(commitsTable, row);
        return;
    }
    List<IndexCommit> commits = DirectoryReader.listCommits(dir);
    IndexCommit current = ir instanceof DirectoryReader ? ((DirectoryReader) ir).getIndexCommit() : null;
    // commits are ordered from oldest to newest ?
    Iterator<IndexCommit> it = commits.iterator();
    int rowNum = 0;
    while (it.hasNext()) {
        IndexCommit commit = (IndexCommit) it.next();
        // figure out the name of the segment files
        Collection<String> files = commit.getFileNames();
        Iterator<String> itf = files.iterator();
        Object row = create("row");
        boolean enabled = rowNum < commits.size() - 1;
        Color color = Color.BLUE;
        rowNum++;
        add(commitsTable, row);
        putProperty(row, "commit", commit);
        if (enabled) {
            putProperty(row, "commitDeletable", Boolean.TRUE);
        }
        Object cell = create("cell");
        String gen = String.valueOf(commit.getGeneration());
        setString(cell, "text", gen);
        add(row, cell);
        cell = create("cell");
        setString(cell, "text", commit.isDeleted() ? "Y" : "N");
        add(row, cell);
        cell = create("cell");
        setString(cell, "text", String.valueOf(commit.getSegmentCount()));
        add(row, cell);
        cell = create("cell");
        Map userData = commit.getUserData();
        if (userData != null && !userData.isEmpty()) {
            setString(cell, "text", userData.toString());
        } else {
            setString(cell, "text", "");
        }
        add(row, cell);
        if (commit.equals(current)) {
            Object[] cells = getItems(row);
            for (Object c : cells) {
                setColor(c, "foreground", color);
            }
        }
    }
}

From source file:org.neo4j.kernel.api.impl.index.backup.LuceneIndexSnapshots.java

License:Open Source License

/**
 * Create index snapshot iterator for a read only index.
 * @param indexFolder index location folder
 * @param directory index directory//from  ww w  . ja  v  a  2s  .c om
 * @return index file name resource iterator
 * @throws IOException
 */
public static ResourceIterator<File> forIndex(File indexFolder, Directory directory) throws IOException {
    if (!hasCommits(directory)) {
        return emptyIterator();
    }
    Collection<IndexCommit> indexCommits = DirectoryReader.listCommits(directory);
    IndexCommit indexCommit = Iterables.last(indexCommits);
    return new ReadOnlyIndexSnapshotFileIterator(indexFolder, indexCommit);
}

From source file:org.opengrok.suggest.SuggesterProjectData.java

License:Open Source License

private long getCommitVersion() throws IOException {
    List<IndexCommit> commits = DirectoryReader.listCommits(indexDir);
    if (commits.size() > 1) {
        throw new IllegalStateException(
                "IndexDeletionPolicy changed, normally only one commit should be stored");
    }/*from  w w w .j a v  a 2s  .c  o  m*/
    IndexCommit commit = commits.get(0);

    return commit.getGeneration();
}

From source file:org.pageseeder.flint.lucene.LuceneIndexIO.java

License:Apache License

/**
 * Sole constructor./*from www  .j  a v a 2  s . c  om*/
 *
 * @param dir       The index's directory
 * @param analyzer  The analyzer
 *
 * @throws IndexException if opening the index failed
 */
public LuceneIndexIO(Directory dir, Analyzer analyzer) throws IndexException {
    this._analyzer = analyzer;
    this._directory = dir;
    try {
        open();
    } catch (IndexFormatTooOldException ex) {
        // try to delete all files and retry
        if (this._directory != null)
            try {
                for (String n : this._directory.listAll()) {
                    this._directory.deleteFile(n);
                }
            } catch (IOException ex2) {
                throw new IndexException("Failed to delete index files from old index", ex);
            }
        // retry
        try {
            open();
        } catch (IOException ex2) {
            throw new IndexException("Failed to create writer on index", ex2);
        }
    } catch (IOException ex) {
        throw new IndexException("Failed to create writer on index", ex);
    }
    // get last commit data as last time used
    try {
        List<IndexCommit> commits = DirectoryReader.listCommits(dir);
        if (commits != null && !commits.isEmpty()) {
            String lastCommitDate = commits.get(commits.size() - 1).getUserData()
                    .get(LuceneIndexIO.LAST_COMMIT_DATE);
            if (lastCommitDate != null) {
                this.lastTimeUsed.set(Long.parseLong(lastCommitDate));
            }
        }
    } catch (IOException ex) {
        LOGGER.error("Failed to load last index commit date", ex);
    }
}

From source file:perf.PerfUtils.java

License:Apache License

public static IndexCommit findCommitPoint(String commit, Directory dir) throws IOException {
    List<IndexCommit> commits = DirectoryReader.listCommits(dir);
    Collections.reverse(commits);

    for (final IndexCommit ic : commits) {
        Map<String, String> map = ic.getUserData();
        String ud = null;//from   ww  w.  j  a  v a2 s . c  om
        if (map != null) {
            ud = map.get("userData");
            System.out.println("found commit=" + ud);
            if (ud != null && ud.equals(commit)) {
                return ic;
            }
        }
    }
    throw new RuntimeException("could not find commit '" + commit + "'");
}