Example usage for org.apache.lucene.index IndexWriter rollback

List of usage examples for org.apache.lucene.index IndexWriter rollback

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriter rollback.

Prototype

@Override
public void rollback() throws IOException 

Source Link

Document

Close the IndexWriter without committing any changes that have occurred since the last commit (or since it was opened, if commit hasn't been called).

Usage

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void create(IndexObject indexObject) {

    IndexWriter indexWriter = null;
    try {//from w ww  . j av  a2s  .c o  m
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        indexWriter.addDocument(DocumentUtil.IndexObject2Document(indexObject));
        indexWriter.commit();
    } catch (Exception e) {
        e.printStackTrace();
        try {
            indexWriter.rollback();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void deleteAll() {
    IndexWriter indexWriter = null;
    try {/* w  w w . jav  a 2  s. c  o m*/
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        Long result = indexWriter.deleteAll();
        /**/
        indexWriter.forceMergeDeletes();
        log.info("deleted:{}", result);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            indexWriter.rollback();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void update(IndexObject indexObject) {

    IndexWriter indexWriter = null;

    try {//  ww  w .  j  a v a  2 s.c  o m

        Term term = new Term("id", indexObject.getId().toString());
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        indexWriter.updateDocument(term, DocumentUtil.IndexObject2Document(indexObject));

    } catch (Exception e) {
        e.printStackTrace();
        try {
            indexWriter.rollback();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void delete(IndexObject indexObject) {
    IndexWriter indexWriter = null;
    try {//from  w  ww.  j a v  a  2 s. c o  m
        Term term = new Term("id", indexObject.getId().toString());
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        indexWriter.deleteDocuments(term);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            indexWriter.rollback();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:net.bobah.mail.Indexer.java

License:Apache License

private void runEx() throws Exception {
    final File dir = new File(config.getProperty("net.bobah.mail.local.folder"));
    if (!dir.exists() || !dir.isDirectory()) {
        throw new IllegalArgumentException(String.format("\"%s\" does not exist or is not a directory", dir));
    }/*from   w w  w  .  j a v a 2s. c o  m*/

    Collection<File> files = findFiles(dir, new FileFilter() {
        @Override
        public boolean accept(File file) {
            return file.getName().endsWith(".eml");
        }
    }, new Comparator<File>() {
        @Override
        public int compare(File l, File r) {
            return Long.compare(l.lastModified(), r.lastModified());
        }
    });

    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
    iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
    final File indexDir = new File(dir, "index");

    final boolean indexExisted = indexDir.exists();
    if (!indexExisted)
        indexDir.mkdirs();

    final Directory idx = FSDirectory.open(indexDir);
    final IndexWriter writer = new IndexWriter(idx, iwc);

    final IndexReader reader = indexExisted ? DirectoryReader.open(idx) : null;
    final IndexSearcher searcher = indexExisted ? new IndexSearcher(reader) : null;

    //final AtomicLong counter = new AtomicLong(0l);
    try {
        for (final File file : files) {
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        index(file, writer, searcher);
                        //if (counter.incrementAndGet() % 100 == 0) writer.commit(); // TODO: VL: make batch size configurable
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }

        shutdownExecutor(executor, log);

        // TODO: VL: delete stale documents from the index

        writer.commit();
        log.info("committed index updates");

        searcher.search(new MatchAllDocsQuery(), new Collector() {
            @Override
            public void setScorer(Scorer scorer) throws IOException {
            }

            @Override
            public void setNextReader(AtomicReaderContext unused) throws IOException {
            }

            @Override
            public void collect(int docID) throws IOException {
                Document doc = reader.document(docID);
                final String path = doc.get("path");
                if (path != null) {
                    try {
                        final File file = new File(path);
                        if (!file.exists()) {
                            log.info("deleting index for {}", doc.get("id"));
                            writer.deleteDocuments(new Term("id", doc.get("id")));
                        }
                    } catch (SecurityException e) {
                        log.error("exception", e);
                    }
                }
            }

            @Override
            public boolean acceptsDocsOutOfOrder() {
                return true;
            }
        });

        writer.commit();
        log.info("committed index deletions");

    } finally {
        try {
            // close writer without commit (see explicit commits above)
            writer.rollback();
        } catch (IOException e) {
            log.error("exception while closing writer", e);
        }
    }
}

From source file:net.sf.lucis.core.impl.DefaultWriter.java

License:Apache License

private void rollback(IndexWriter writer) {
    try {
        writer.rollback();
    } catch (Exception e) {
    }
}

From source file:nl.strohalm.cyclos.utils.lucene.IndexOperationRunner.java

License:Open Source License

private synchronized void rollback(final Class<? extends Indexable> entityType, final IndexWriter writer) {
    if (writer == null) {
        return;//from  ww w .ja v a  2s . c o  m
    }
    try {
        writer.rollback();
    } catch (Exception e) {
        LOG.error("Error while rolling back index writer for " + ClassHelper.getClassName(entityType), e);
    }
    // The index writer is closed by rollback. Invalidate it.
    cachedWriters.remove(entityType);
}

From source file:org.apache.jena.query.text.TextIndexLucene.java

License:Apache License

@Override
public void rollback() {
    IndexWriter idx = indexWriter;
    indexWriter = null;//from   ww w.  j a  va  2 s.co  m
    try {
        idx.rollback();
    } catch (IOException e) {
        throw new TextIndexException(e);
    }

    // The rollback will close the indexWriter, so we need to reopen it
    openIndexWriter();
}

From source file:org.apache.solr.uninverting.TestFieldCacheVsDocValues.java

License:Apache License

public void testHugeBinaryValues() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random());
    // FSDirectory because SimpleText will consume gobbs of
    // space when storing big binary values:
    Directory d = newFSDirectory(createTempDir("hugeBinaryValues"));
    boolean doFixed = random().nextBoolean();
    int numDocs;/*  w ww  .java 2 s  .c  o  m*/
    int fixedLength = 0;
    if (doFixed) {
        // Sometimes make all values fixed length since some
        // codecs have different code paths for this:
        numDocs = TestUtil.nextInt(random(), 10, 20);
        fixedLength = TestUtil.nextInt(random(), 65537, 256 * 1024);
    } else {
        numDocs = TestUtil.nextInt(random(), 100, 200);
    }
    IndexWriter w = new IndexWriter(d, newIndexWriterConfig(analyzer));
    List<byte[]> docBytes = new ArrayList<>();
    long totalBytes = 0;
    for (int docID = 0; docID < numDocs; docID++) {
        // we don't use RandomIndexWriter because it might add
        // more docvalues than we expect !!!!

        // Must be > 64KB in size to ensure more than 2 pages in
        // PagedBytes would be needed:
        int numBytes;
        if (doFixed) {
            numBytes = fixedLength;
        } else if (docID == 0 || random().nextInt(5) == 3) {
            numBytes = TestUtil.nextInt(random(), 65537, 3 * 1024 * 1024);
        } else {
            numBytes = TestUtil.nextInt(random(), 1, 1024 * 1024);
        }
        totalBytes += numBytes;
        if (totalBytes > 5 * 1024 * 1024) {
            break;
        }
        byte[] bytes = new byte[numBytes];
        random().nextBytes(bytes);
        docBytes.add(bytes);
        Document doc = new Document();
        BytesRef b = new BytesRef(bytes);
        b.length = bytes.length;
        doc.add(new BinaryDocValuesField("field", b));
        doc.add(new StringField("id", "" + docID, Field.Store.YES));
        try {
            w.addDocument(doc);
        } catch (IllegalArgumentException iae) {
            if (iae.getMessage().indexOf("is too large") == -1) {
                throw iae;
            } else {
                // OK: some codecs can't handle binary DV > 32K
                assertFalse(codecAcceptsHugeBinaryValues("field"));
                w.rollback();
                d.close();
                return;
            }
        }
    }

    DirectoryReader r;
    try {
        r = DirectoryReader.open(w);
    } catch (IllegalArgumentException iae) {
        if (iae.getMessage().indexOf("is too large") == -1) {
            throw iae;
        } else {
            assertFalse(codecAcceptsHugeBinaryValues("field"));

            // OK: some codecs can't handle binary DV > 32K
            w.rollback();
            d.close();
            return;
        }
    }
    w.close();

    LeafReader ar = SlowCompositeReaderWrapper.wrap(r);
    TestUtil.checkReader(ar);

    BinaryDocValues s = FieldCache.DEFAULT.getTerms(ar, "field");
    for (int docID = 0; docID < docBytes.size(); docID++) {
        Document doc = ar.document(docID);
        assertEquals(docID, s.nextDoc());
        BytesRef bytes = s.binaryValue();
        byte[] expected = docBytes.get(Integer.parseInt(doc.get("id")));
        assertEquals(expected.length, bytes.length);
        assertEquals(new BytesRef(expected), bytes);
    }

    assertTrue(codecAcceptsHugeBinaryValues("field"));

    ar.close();
    d.close();
}

From source file:org.apache.solr.update.DirectUpdateHandler2.java

License:Apache License

@Override
public void closeWriter(IndexWriter writer) throws IOException {
    boolean clearRequestInfo = false;
    solrCoreState.getCommitLock().lock();
    try {//from  w ww  .  j av a  2s.c om
        SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());
        SolrQueryResponse rsp = new SolrQueryResponse();
        if (SolrRequestInfo.getRequestInfo() == null) {
            clearRequestInfo = true;
            SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp)); // important for debugging
        }

        if (!commitOnClose) {
            if (writer != null) {
                writer.rollback();
            }

            // we shouldn't close the transaction logs either, but leaving them open
            // means we can't delete them on windows (needed for tests)
            if (ulog != null)
                ulog.close(false);

            return;
        }

        // do a commit before we quit?     
        boolean tryToCommit = writer != null && ulog != null && ulog.hasUncommittedChanges()
                && ulog.getState() == UpdateLog.State.ACTIVE;

        try {
            if (tryToCommit) {

                CommitUpdateCommand cmd = new CommitUpdateCommand(req, false);
                cmd.openSearcher = false;
                cmd.waitSearcher = false;
                cmd.softCommit = false;

                // TODO: keep other commit callbacks from being called?
                //  this.commit(cmd);        // too many test failures using this method... is it because of callbacks?

                synchronized (solrCoreState.getUpdateLock()) {
                    ulog.preCommit(cmd);
                }

                // todo: refactor this shared code (or figure out why a real CommitUpdateCommand can't be used)
                final Map<String, String> commitData = new HashMap<String, String>();
                commitData.put(SolrIndexWriter.COMMIT_TIME_MSEC_KEY,
                        String.valueOf(System.currentTimeMillis()));
                writer.setCommitData(commitData);
                writer.commit();

                synchronized (solrCoreState.getUpdateLock()) {
                    ulog.postCommit(cmd);
                }
            }
        } catch (Throwable th) {
            log.error("Error in final commit", th);
        }

        // we went through the normal process to commit, so we don't have to artificially
        // cap any ulog files.
        try {
            if (ulog != null)
                ulog.close(false);
        } catch (Throwable th) {
            log.error("Error closing log files", th);
        }

        if (writer != null)
            writer.close();

    } finally {
        solrCoreState.getCommitLock().unlock();
        if (clearRequestInfo)
            SolrRequestInfo.clearRequestInfo();
    }
}