add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / index / IndexCommit.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19
20 import java.util.Collection;
21 import java.util.Map;
22 import java.io.IOException;
23
24 import org.apache.lucene.store.Directory;
25
26 /**
27  * <p>Expert: represents a single commit into an index as seen by the
28  * {@link IndexDeletionPolicy} or {@link IndexReader}.</p>
29  *
30  * <p> Changes to the content of an index are made visible
31  * only after the writer who made that change commits by
32  * writing a new segments file
33  * (<code>segments_N</code>). This point in time, when the
34  * action of writing of a new segments file to the directory
35  * is completed, is an index commit.</p>
36  *
37  * <p>Each index commit point has a unique segments file
38  * associated with it. The segments file associated with a
39  * later index commit point would have a larger N.</p>
40  *
41  * @lucene.experimental
42 */
43
44 public abstract class IndexCommit implements Comparable<IndexCommit> {
45
46   /**
47    * Get the segments file (<code>segments_N</code>) associated 
48    * with this commit point.
49    */
50   public abstract String getSegmentsFileName();
51
52   /**
53    * Returns all index files referenced by this commit point.
54    */
55   public abstract Collection<String> getFileNames() throws IOException;
56
57   /**
58    * Returns the {@link Directory} for the index.
59    */
60   public abstract Directory getDirectory();
61   
62   /**
63    * Delete this commit point.  This only applies when using
64    * the commit point in the context of IndexWriter's
65    * IndexDeletionPolicy.
66    * <p>
67    * Upon calling this, the writer is notified that this commit 
68    * point should be deleted. 
69    * <p>
70    * Decision that a commit-point should be deleted is taken by the {@link IndexDeletionPolicy} in effect
71    * and therefore this should only be called by its {@link IndexDeletionPolicy#onInit onInit()} or 
72    * {@link IndexDeletionPolicy#onCommit onCommit()} methods.
73   */
74   public abstract void delete();
75
76   public abstract boolean isDeleted();
77
78   /** Returns true if this commit is an optimized index. */
79   public abstract boolean isOptimized();
80
81   /** Two IndexCommits are equal if both their Directory and versions are equal. */
82   @Override
83   public boolean equals(Object other) {
84     if (other instanceof IndexCommit) {
85       IndexCommit otherCommit = (IndexCommit) other;
86       return otherCommit.getDirectory().equals(getDirectory()) && otherCommit.getVersion() == getVersion();
87     } else
88       return false;
89   }
90
91   @Override
92   public int hashCode() {
93     return (int) (getDirectory().hashCode() + getVersion());
94   }
95
96   /** Returns the version for this IndexCommit.  This is the
97    *  same value that {@link IndexReader#getVersion} would
98    *  return if it were opened on this commit. */
99   public abstract long getVersion();
100
101   /** Returns the generation (the _N in segments_N) for this
102    *  IndexCommit */
103   public abstract long getGeneration();
104
105   /** Convenience method that returns the last modified time
106    *  of the segments_N file corresponding to this index
107    *  commit, equivalent to
108    *  getDirectory().fileModified(getSegmentsFileName()). */
109   public long getTimestamp() throws IOException {
110     return getDirectory().fileModified(getSegmentsFileName());
111   }
112
113   /** Returns userData, previously passed to {@link
114    *  IndexWriter#commit(Map)} for this commit.  Map is
115    *  String -> String. */
116   public abstract Map<String,String> getUserData() throws IOException;
117   
118   public int compareTo(IndexCommit commit) {
119     long gen = getGeneration();
120     long comgen = commit.getGeneration();
121     if (gen < comgen) {
122       return -1;
123     } else if (gen > comgen) {
124       return 1;
125     } else {
126       return 0;
127     }
128   }
129
130 }