01: package org.compass.core.lucene.engine.indexdeletionpolicy;
02:
03: import java.io.IOException;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: import org.apache.lucene.index.IndexCommitPoint;
08: import org.apache.lucene.index.IndexDeletionPolicy;
09: import org.apache.lucene.store.Directory;
10: import org.compass.core.CompassException;
11: import org.compass.core.config.CompassConfigurable;
12: import org.compass.core.config.CompassSettings;
13: import org.compass.core.lucene.LuceneEnvironment;
14:
15: /**
16: * An index deletion policy that keeps all commits that have not expired. Expiration time (in seconds)
17: * can be set using {@link org.compass.core.lucene.LuceneEnvironment.IndexDeletionPolicy.ExpirationTime}
18: * settings.
19: *
20: * @author kimchy
21: * @see org.compass.core.lucene.engine.indexdeletionpolicy.IndexDeletionPolicyFactory
22: */
23: public class ExpirationTimeDeletionPolicy implements
24: IndexDeletionPolicy, DirectoryConfigurable, CompassConfigurable {
25:
26: private double expirationTimeSeconds;
27:
28: private Directory dir;
29:
30: public void setDirectory(Directory directory) {
31: this .dir = directory;
32: }
33:
34: public void configure(CompassSettings settings)
35: throws CompassException {
36: this .expirationTimeSeconds = settings
37: .getSettingAsDouble(
38: LuceneEnvironment.IndexDeletionPolicy.ExpirationTime.EXPIRATION_TIME_IN_SECONDS,
39: 60);
40: }
41:
42: public void onInit(List commits) throws IOException {
43: onCommit(commits);
44: }
45:
46: public void onCommit(List commits) throws IOException {
47: IndexCommitPoint lastCommit = (IndexCommitPoint) commits
48: .get(commits.size() - 1);
49:
50: // Any commit older than expireTime should be deleted:
51: double expireTime = dir.fileModified(lastCommit
52: .getSegmentsFileName())
53: / 1000.0 - expirationTimeSeconds;
54:
55: Iterator it = commits.iterator();
56:
57: while (it.hasNext()) {
58: IndexCommitPoint commit = (IndexCommitPoint) it.next();
59: double modTime = dir.fileModified(commit
60: .getSegmentsFileName()) / 1000.0;
61: if (commit != lastCommit && modTime < expireTime) {
62: commit.delete();
63: }
64: }
65: }
66: }
|