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 public void onCommit(List<? extends IndexCommit> indexCommits) {
070 _lastIndexCommit = indexCommits.get(indexCommits.size() - 1);
071
072 for (int i = 0; i < indexCommits.size() - 1; i++) {
073 IndexCommit indexCommit = indexCommits.get(i);
074
075 if (!_segmentsFileNames.contains(
076 indexCommit.getSegmentsFileName())) {
077
078 indexCommit.delete();
079 }
080 }
081 }
082
083 public void onInit(List<? extends IndexCommit> indexCommits) {
084 onCommit(indexCommits);
085 }
086
087 private volatile IndexCommit _lastIndexCommit;
088 private List<String> _segmentsFileNames =
089 new CopyOnWriteArrayList<String>();
090
091 }