001
014
015 package com.liferay.portal.search.lucene.dump;
016
017 import java.io.IOException;
018 import java.io.OutputStream;
019
020 import java.util.List;
021 import java.util.concurrent.CopyOnWriteArrayList;
022 import java.util.concurrent.locks.Lock;
023
024 import org.apache.lucene.index.IndexCommit;
025 import org.apache.lucene.index.IndexDeletionPolicy;
026 import org.apache.lucene.index.IndexWriter;
027
028
031 public class DumpIndexDeletionPolicy implements IndexDeletionPolicy {
032
033 public void dump(
034 OutputStream outputStream, IndexWriter indexWriter, Lock commitLock)
035 throws IOException {
036
037 IndexCommit indexCommit = null;
038
039 String segmentsFileName = null;
040
041 commitLock.lock();
042
043 try {
044 indexWriter.commit();
045
046 indexCommit = _lastIndexCommit;
047
048 segmentsFileName = indexCommit.getSegmentsFileName();
049
050 _segmentsFileNames.add(segmentsFileName);
051 }
052 finally {
053 commitLock.unlock();
054 }
055
056 try {
057 IndexCommitSerializationUtil.serializeIndex(
058 indexCommit, outputStream);
059 }
060 finally {
061 _segmentsFileNames.remove(segmentsFileName);
062 }
063 }
064
065 public long getLastGeneration() {
066 return _lastIndexCommit.getGeneration();
067 }
068
069 @Override
070 public void onCommit(List<? extends IndexCommit> indexCommits) {
071 _lastIndexCommit = indexCommits.get(indexCommits.size() - 1);
072
073 for (int i = 0; i < indexCommits.size() - 1; i++) {
074 IndexCommit indexCommit = indexCommits.get(i);
075
076 if (!_segmentsFileNames.contains(
077 indexCommit.getSegmentsFileName())) {
078
079 indexCommit.delete();
080 }
081 }
082 }
083
084 @Override
085 public void onInit(List<? extends IndexCommit> indexCommits) {
086 onCommit(indexCommits);
087 }
088
089 private volatile IndexCommit _lastIndexCommit;
090 private List<String> _segmentsFileNames =
091 new CopyOnWriteArrayList<String>();
092
093 }