Example usage for org.apache.lucene.index IndexCommit getSegmentsFileName

List of usage examples for org.apache.lucene.index IndexCommit getSegmentsFileName

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexCommit getSegmentsFileName.

Prototype

public abstract String getSegmentsFileName();

Source Link

Document

Get the segments file (segments_N) associated with this commit point.

Usage

From source file:cn.hbu.cs.esearch.index.ZoieIndexDeletionPolicy.java

License:Apache License

private synchronized void processCommits(List<? extends IndexCommit> commits) {
    int size = commits.size();
    if (size == 0) {
        return;/*from ww w.  j  a v a2s. c o  m*/
    }

    IndexCommit indexCommit = null;
    for (Object commit : commits) {
        indexCommit = (IndexCommit) commit;
        if (--size > 0 && !currentSnapshots.containsKey(indexCommit.getSegmentsFileName())) {
            indexCommit.delete();
        }
    }
    lastCommit = indexCommit;
}

From source file:com.liferay.portal.search.lucene.dump.DumpIndexDeletionPolicy.java

License:Open Source License

public void dump(OutputStream outputStream, IndexWriter indexWriter, Lock commitLock) throws IOException {

    IndexCommit indexCommit = null;

    String segmentsFileName = null;

    commitLock.lock();//from   w w  w.j a v  a2s.  c  o  m

    try {
        indexWriter.commit();

        indexCommit = _lastIndexCommit;

        segmentsFileName = indexCommit.getSegmentsFileName();

        _segmentsFileNames.add(segmentsFileName);
    } finally {
        commitLock.unlock();
    }

    try {
        IndexCommitSerializationUtil.serializeIndex(indexCommit, outputStream);
    } finally {
        _segmentsFileNames.remove(segmentsFileName);
    }
}

From source file:com.liferay.portal.search.lucene.dump.DumpIndexDeletionPolicy.java

License:Open Source License

public void onCommit(List<? extends IndexCommit> indexCommits) {
    _lastIndexCommit = indexCommits.get(indexCommits.size() - 1);

    for (int i = 0; i < indexCommits.size() - 1; i++) {
        IndexCommit indexCommit = indexCommits.get(i);

        if (!_segmentsFileNames.contains(indexCommit.getSegmentsFileName())) {

            indexCommit.delete();//w  w w  .  j  a  v a 2  s .c  o m
        }
    }
}

From source file:org.apache.blur.mapreduce.lib.BlurInputFormat.java

License:Apache License

public static List<BlurInputSplit> getSplitForDirectory(Path shardDir, Configuration configuration, Text table,
        Text snapshot, Directory directory) throws IOException {
    List<BlurInputSplit> splits = new ArrayList<BlurInputSplit>();
    SnapshotIndexDeletionPolicy policy = new SnapshotIndexDeletionPolicy(configuration,
            SnapshotIndexDeletionPolicy.getGenerationsPath(shardDir));

    Long generation = policy.getGeneration(snapshot.toString());
    if (generation == null) {
        throw new IOException("Snapshot [" + snapshot + "] not found in shard [" + shardDir + "]");
    }//  ww  w .ja v a2 s  .c  om

    List<IndexCommit> listCommits = DirectoryReader.listCommits(directory);
    IndexCommit indexCommit = findIndexCommit(listCommits, generation, shardDir);

    String segmentsFileName = indexCommit.getSegmentsFileName();
    SegmentInfos segmentInfos = new SegmentInfos();
    segmentInfos.read(directory, segmentsFileName);
    for (SegmentInfoPerCommit commit : segmentInfos) {
        SegmentInfo segmentInfo = commit.info;
        if (commit.getDelCount() == segmentInfo.getDocCount()) {
            LOG.info("Segment [{0}] in dir [{1}] has all records deleted.", segmentInfo.name, shardDir);
        } else {
            String name = segmentInfo.name;
            Collection<String> files = commit.files();
            long fileLength = 0;
            for (String file : files) {
                fileLength += directory.fileLength(file);
            }
            List<String> dirFiles = new ArrayList<String>(files);
            dirFiles.add(segmentsFileName);
            splits.add(new BlurInputSplit(shardDir, segmentsFileName, name, fileLength, table, dirFiles));
        }
    }
    return splits;
}

From source file:org.apache.jackrabbit.core.query.lucene.IndexDeletionPolicyImpl.java

License:Apache License

public void onCommit(List<? extends IndexCommit> commits) throws IOException {
    checkCommits(commits);//from  w  ww.j  a  v a2  s.com

    // report back current generation
    IndexCommit current = commits.get(commits.size() - 1);
    String name = current.getSegmentsFileName();
    if (name.equals(SEGMENTS)) {
        index.setCurrentGeneration(0);
    } else {
        index.setCurrentGeneration(Long.parseLong(name.substring(SEGMENTS.length() + 1), Character.MAX_RADIX));
    }
}

From source file:org.apache.jackrabbit.core.query.lucene.IndexDeletionPolicyImpl.java

License:Apache License

private void checkCommits(List<? extends IndexCommit> commits) throws IOException {
    long currentTime = System.currentTimeMillis();
    for (int i = 0; i < commits.size() - 1; i++) {
        IndexCommit ic = commits.get(i);
        long lastModified = index.getDirectory().fileModified(ic.getSegmentsFileName());
        if (currentTime - lastModified > maxAge) {
            ic.delete();//ww w  .  j  a v a2  s.c  om
        } else {
            // following commits are younger, no need to check
            break;
        }
    }
}

From source file:org.apache.solr.handler.IndexFetcher.java

License:Apache License

private boolean hasUnusedFiles(Directory indexDir, IndexCommit commit) throws IOException {
    String segmentsFileName = commit.getSegmentsFileName();
    SegmentInfos infos = SegmentInfos.readCommit(indexDir, segmentsFileName);
    Set<String> currentFiles = new HashSet<>(infos.files(true));
    String[] allFiles = indexDir.listAll();
    for (String file : allFiles) {
        if (!file.equals(segmentsFileName) && !currentFiles.contains(file) && !file.endsWith(".lock")) {
            LOG.info("Found unused file: " + file);
            return true;
        }/*from  w w  w.  ja va2  s . com*/
    }
    return false;
}

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

License:Apache License

/**
 * Initialize index overview and other GUI elements. This method is called always when a new index is open.
 *//*w w w  . j a  va  2 s .  co m*/
private void initOverview() {
    try {
        initSolrTypes();
        courier = new Font("Courier", getFont().getStyle(), getFont().getSize());
        lastST = find("lastST");
        setBoolean(find("bReload"), "enabled", true);
        setBoolean(find("bClose"), "enabled", true);
        setBoolean(find("bCommit"), "enabled", true);
        final Object sTable = find("sTable");
        removeAll(sTable);
        final Object docTable = find("docTable");
        removeAll(docTable);
        final Object cbType = find("cbType");
        populateAnalyzers(cbType);
        final Object pOver = find("pOver");
        Object iName = find("idx");
        String idxName;
        if (pName.length() > 40) {
            idxName = pName.substring(0, 10) + "..." + pName.substring(pName.length() - 27);
        } else {
            idxName = pName;
        }
        setString(iName, "text", idxName + (readOnly ? " (R)" : ""));
        iName = find(pOver, "iName");
        setString(iName, "text", pName + (readOnly ? " (Read-Only)" : ""));
        final Object dirImpl = find("dirImpl");
        String implName = "N/A";
        if (dir == null) {
            if (ir != null) {
                implName = "N/A (reader is " + ir.getClass().getName() + ")";
            }
        } else {
            implName = dir.getClass().getName();
        }
        setString(dirImpl, "text", implName);
        final Object fileSize = find("iFileSize");
        final long totalFileSize = Util.calcTotalFileSize(pName, dir);
        setString(fileSize, "text", Util.normalizeSize(totalFileSize) + Util.normalizeUnit(totalFileSize));
        final Object iFormat = find(pOver, "iFormat");
        final Object iCaps = find(pOver, "iCaps");
        String formatText = "N/A";
        String formatCaps = "N/A";
        if (dir != null) {
            final IndexGate.FormatDetails formatDetails = IndexGate.getIndexFormat(dir);
            formatText = formatDetails.genericName;
            formatCaps = formatDetails.capabilities;
        }
        setString(iFormat, "text", formatText);
        setString(iCaps, "text", formatCaps);
        if (ir == null) {
            return;
        }
        // we need IndexReader from now on
        idxInfo = new IndexInfo(ir, pName);
        Object iDocs = find(pOver, "iDocs");
        final String numdocs = String.valueOf(ir.numDocs());
        setString(iDocs, "text", numdocs);
        iDocs = find("iDocs1");
        setString(iDocs, "text", String.valueOf(ir.maxDoc() - 1));
        final Object iFields = find(pOver, "iFields");
        fn = idxInfo.getFieldNames();
        if (fn.size() == 0) {
            showStatus("Empty index.");
        }
        showFiles(dir, null);
        if (ir instanceof CompositeReader) {
            ar = SlowCompositeReaderWrapper.wrap((CompositeReader) ir);
        } else if (ir instanceof AtomicReader) {
            ar = (AtomicReader) ir;
        }
        if (ar != null) {
            infos = ar.getFieldInfos();
        }
        showCommits();
        final Object fList = find(pOver, "fList");
        final Object defFld = find("defFld");
        final Object fCombo = find("fCombo");
        final TreeSet<String> fields = new TreeSet<String>(fn);
        idxFields = fields.toArray(new String[fields.size()]);
        setString(iFields, "text", String.valueOf(idxFields.length));
        final Object iTerms = find(pOver, "iTerms");
        if (!slowAccess) {
            final Thread t = new Thread() {

                @Override
                public void run() {
                    final Object r = create("row");
                    final Object cell = create("cell");
                    add(r, cell);
                    add(fList, r);
                    setBoolean(cell, "enabled", false);
                    setString(cell, "text", "..wait..");
                    try {
                        numTerms = idxInfo.getNumTerms();
                        termCounts = idxInfo.getFieldTermCounts();
                        setString(iTerms, "text", String.valueOf(numTerms));
                        initFieldList(fList, fCombo, defFld);
                    } catch (final Exception e) {
                        e.printStackTrace();
                        numTerms = -1;
                        termCounts = Collections.emptyMap();
                        showStatus("ERROR: can't count terms per field");
                    }
                }
            };
            t.start();
        } else {
            setString(iTerms, "text", "N/A");
            initFieldList(fList, fCombo, defFld);
        }
        final Object iDel = find(pOver, "iDelOpt");
        final String sDel = ir.hasDeletions() ? "Yes (" + ir.numDeletedDocs() + ")" : "No";
        final IndexCommit ic = ir instanceof DirectoryReader ? ((DirectoryReader) ir).getIndexCommit() : null;
        final String opt = ic != null ? ic.getSegmentCount() == 1 ? "Yes" : "No" : "?";
        final String sDelOpt = sDel + " / " + opt;
        setString(iDel, "text", sDelOpt);
        final Object iVer = find(pOver, "iVer");
        String verText = "N/A";
        if (ic != null) {
            verText = Long.toHexString(((DirectoryReader) ir).getVersion());
        }
        setString(iVer, "text", verText);
        final Object iTiiDiv = find(pOver, "iTiiDiv");
        String divText = "N/A";
        if (ir instanceof DirectoryReader) {
            final List<AtomicReaderContext> contexts = ir.leaves();
            if (contexts.size() > 0 && contexts.get(0).reader() instanceof SegmentReader) {
                divText = String.valueOf(((SegmentReader) contexts.get(0).reader()).getTermInfosIndexDivisor());
            }
        }
        setString(iTiiDiv, "text", divText);
        final Object iCommit = find(pOver, "iCommit");
        String commitText = "N/A";
        if (ic != null) {
            commitText = ic.getSegmentsFileName() + " (generation=" + ic.getGeneration() + ", segs="
                    + ic.getSegmentCount() + ")";
        }
        setString(iCommit, "text", commitText);
        final Object iUser = find(pOver, "iUser");
        String userData = null;
        if (ic != null) {
            final Map<String, String> userDataMap = ic.getUserData();
            if (userDataMap != null && !userDataMap.isEmpty()) {
                userData = userDataMap.toString();
            } else {
                userData = "--";
            }
        } else {
            userData = "(not supported)";
        }
        setString(iUser, "text", userData);
        final Object nTerms = find("nTerms");
        if (!slowAccess) {
            final Thread t = new Thread() {

                @Override
                public void run() {
                    actionTopTerms(nTerms);
                }
            };
            t.start();
        }
    } catch (final Exception e) {
        e.printStackTrace();
        errorMsg(e.getMessage());
    }
}

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

License:Apache License

public void showSegments(final Object commitsTable) throws Exception {
    final Object segTable = find("segmentsTable");
    removeAll(segTable);/*from w  ww  .  j  a  va2s  . c  o  m*/
    final Object[] rows = getSelectedItems(commitsTable);
    if (rows == null || rows.length == 0) {
        showStatus("No commit point selected.");
        return;
    }
    final Object row = rows[0];
    final IndexCommit commit = (IndexCommit) getProperty(row, "commit");
    if (commit == null) {
        showStatus("Can't retrieve commit point (application error)");
        return;
    }
    final Object segGen = find("segGen");
    setString(segGen, "text", commit.getSegmentsFileName() + " (gen " + commit.getGeneration() + ")");
    final String segName = commit.getSegmentsFileName();
    final SegmentInfos infos = new SegmentInfos();
    try {
        infos.read(dir, segName);
    } catch (final Exception e) {
        e.printStackTrace();
        errorMsg("Error reading segment infos for '" + segName + ": " + e.toString());
        return;
    }
    for (final SegmentCommitInfo si : infos.asList()) {
        final Object r = create("row");
        add(segTable, r);
        Object cell = create("cell");
        add(r, cell);
        setString(cell, "text", si.info.name);
        cell = create("cell");
        add(r, cell);
        setString(cell, "text", String.valueOf(si.getDelGen()));
        setChoice(cell, "alignment", "right");
        cell = create("cell");
        add(r, cell);
        setString(cell, "text", String.valueOf(si.getDelCount()));
        setChoice(cell, "alignment", "right");
        cell = create("cell");
        add(r, cell);
        setString(cell, "text", String.valueOf(si.info.getDocCount()));
        setChoice(cell, "alignment", "right");
        cell = create("cell");
        add(r, cell);
        setString(cell, "text", si.info.getVersion());
        cell = create("cell");
        add(r, cell);
        setString(cell, "text", si.info.getCodec().getName());
        final long size = si.sizeInBytes();
        cell = create("cell");
        add(r, cell);
        setString(cell, "text", Util.normalizeSize(size) + Util.normalizeUnit(size));
        setChoice(cell, "alignment", "right");
        cell = create("cell");
        add(r, cell);
        setString(cell, "text", si.info.getUseCompoundFile() ? "Y" : "N");

        putProperty(r, "si", si);
    }
    final Object diagsTable = find("diagsTable");
    removeAll(diagsTable);
}

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

License:Apache License

/**
 * Initialize index overview and other GUI elements. This method is called always
 * when a new index is open./* ww  w . j  a va 2s  .  c o m*/
 *
 */
private void initOverview() {
    try {
        initSolrTypes();
        courier = new Font("Courier", getFont().getStyle(), getFont().getSize());
        lastST = find("lastST");
        setBoolean(find("bReload"), "enabled", true);
        setBoolean(find("bClose"), "enabled", true);
        setBoolean(find("bCommit"), "enabled", true);
        Object sTable = find("sTable");
        removeAll(sTable);
        Object docTable = find("docTable");
        removeAll(docTable);
        Object cbType = find("cbType");
        populateAnalyzers(cbType);
        Object pOver = find("pOver");
        Object iName = find("idx");
        String idxName;
        if (pName.length() > 40) {
            idxName = pName.substring(0, 10) + "..." + pName.substring(pName.length() - 27);
        } else {
            idxName = pName;
        }
        setString(iName, "text", idxName + (readOnly ? " (R)" : ""));
        iName = find(pOver, "iName");
        setString(iName, "text", pName + (readOnly ? " (Read-Only)" : ""));
        Object dirImpl = find("dirImpl");
        String implName = "N/A";
        if (dir == null) {
            if (ir != null) {
                implName = "N/A (reader is " + ir.getClass().getName() + ")";
            }
        } else {
            implName = dir.getClass().getName();
        }
        setString(dirImpl, "text", implName);
        Object fileSize = find("iFileSize");
        long totalFileSize = Util.calcTotalFileSize(pName, dir);
        setString(fileSize, "text", Util.normalizeSize(totalFileSize) + Util.normalizeUnit(totalFileSize));
        Object iFormat = find(pOver, "iFormat");
        Object iCaps = find(pOver, "iCaps");
        String formatText = "N/A";
        String formatCaps = "N/A";
        if (dir != null) {
            IndexGate.FormatDetails formatDetails = IndexGate.getIndexFormat(dir);
            formatText = formatDetails.genericName;
            formatCaps = formatDetails.capabilities;
        }
        setString(iFormat, "text", formatText);
        setString(iCaps, "text", formatCaps);
        if (ir == null) {
            return;
        }
        // we need IndexReader from now on
        idxInfo = new IndexInfo(ir, pName);
        Object iDocs = find(pOver, "iDocs");
        String numdocs = String.valueOf(ir.numDocs());
        setString(iDocs, "text", numdocs);
        iDocs = find("iDocs1");
        setString(iDocs, "text", String.valueOf(ir.maxDoc() - 1));
        Object iFields = find(pOver, "iFields");
        fn = idxInfo.getFieldNames();
        if (fn.size() == 0) {
            showStatus("Empty index.");
        }
        showFiles(dir, null);
        if (ir instanceof CompositeReader) {
            ar = SlowCompositeReaderWrapper.wrap(ir);
        } else if (ir instanceof AtomicReader) {
            ar = (AtomicReader) ir;
        }
        if (ar != null) {
            infos = ar.getFieldInfos();
        }
        showCommits();
        final Object fList = find(pOver, "fList");
        final Object defFld = find("defFld");
        final Object fCombo = find("fCombo");
        idxFields = fn.toArray(new String[fn.size()]);
        setString(iFields, "text", String.valueOf(idxFields.length));
        final Object iTerms = find(pOver, "iTerms");
        if (!slowAccess) {
            Thread t = new Thread() {
                public void run() {
                    Object r = create("row");
                    Object cell = create("cell");
                    add(r, cell);
                    add(fList, r);
                    setBoolean(cell, "enabled", false);
                    setString(cell, "text", "..wait..");
                    try {
                        numTerms = idxInfo.getNumTerms();
                        termCounts = idxInfo.getFieldTermCounts();
                        setString(iTerms, "text", String.valueOf(numTerms));
                        initFieldList(fList, fCombo, defFld);
                    } catch (Exception e) {
                        e.printStackTrace();
                        numTerms = -1;
                        termCounts = Collections.emptyMap();
                        showStatus("ERROR: can't count terms per field");
                        setString(cell, "text", e.getMessage());
                    }
                }
            };
            t.start();
        } else {
            setString(iTerms, "text", "N/A");
            initFieldList(fList, fCombo, defFld);
        }
        Object iDel = find(pOver, "iDelOpt");
        String sDel = ir.hasDeletions() ? "Yes (" + ir.numDeletedDocs() + ")" : "No";
        IndexCommit ic = ir instanceof DirectoryReader ? ((DirectoryReader) ir).getIndexCommit() : null;
        String opt = ic != null ? (ic.getSegmentCount() == 1 ? "Yes" : "No") : "?";
        String sDelOpt = sDel + " / " + opt;
        setString(iDel, "text", sDelOpt);
        Object iVer = find(pOver, "iVer");
        String verText = "N/A";
        if (ic != null) {
            verText = Long.toHexString(((DirectoryReader) ir).getVersion());
        }
        setString(iVer, "text", verText);
        Object iTiiDiv = find(pOver, "iTiiDiv");
        String divText = "N/A";
        if (ir instanceof DirectoryReader) {
            List<AtomicReaderContext> readers = ((DirectoryReader) ir).leaves(); //.getSequentialSubReaders();
            if (readers.size() > 0) {
                if (readers.get(0).reader() instanceof SegmentReader) {
                    divText = String
                            .valueOf(((SegmentReader) readers.get(0).reader()).getTermInfosIndexDivisor());
                }
            }
        }
        setString(iTiiDiv, "text", divText);
        Object iCommit = find(pOver, "iCommit");
        String commitText = "N/A";
        if (ic != null) {
            commitText = ic.getSegmentsFileName() + " (generation=" + ic.getGeneration() + ", segs="
                    + ic.getSegmentCount() + ")";
        }
        setString(iCommit, "text", commitText);
        Object iUser = find(pOver, "iUser");
        String userData = null;
        if (ic != null) {
            Map<String, String> userDataMap = ic.getUserData();
            if (userDataMap != null && !userDataMap.isEmpty()) {
                userData = userDataMap.toString();
            } else {
                userData = "--";
            }
        } else {
            userData = "(not supported)";
        }
        setString(iUser, "text", userData);
        final Object nTerms = find("nTerms");
        if (!slowAccess) {
            Thread t = new Thread() {
                public void run() {
                    actionTopTerms(nTerms);
                }
            };
            t.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
        errorMsg(e.getMessage());
    }
}