1 package org.apache.lucene.index;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.util.Collection;
22 import java.io.IOException;
24 import org.apache.lucene.store.Directory;
27 * <p>Expert: represents a single commit into an index as seen by the
28 * {@link IndexDeletionPolicy} or {@link IndexReader}.</p>
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>
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>
41 * @lucene.experimental
44 public abstract class IndexCommit implements Comparable<IndexCommit> {
47 * Get the segments file (<code>segments_N</code>) associated
48 * with this commit point.
50 public abstract String getSegmentsFileName();
53 * Returns all index files referenced by this commit point.
55 public abstract Collection<String> getFileNames() throws IOException;
58 * Returns the {@link Directory} for the index.
60 public abstract Directory getDirectory();
63 * Delete this commit point. This only applies when using
64 * the commit point in the context of IndexWriter's
65 * IndexDeletionPolicy.
67 * Upon calling this, the writer is notified that this commit
68 * point should be deleted.
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.
74 public abstract void delete();
76 public abstract boolean isDeleted();
78 /** Returns true if this commit is an optimized index. */
79 public abstract boolean isOptimized();
81 /** Two IndexCommits are equal if both their Directory and versions are equal. */
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();
92 public int hashCode() {
93 return (int) (getDirectory().hashCode() + getVersion());
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();
101 /** Returns the generation (the _N in segments_N) for this
103 public abstract long getGeneration();
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());
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;
118 public int compareTo(IndexCommit commit) {
119 long gen = getGeneration();
120 long comgen = commit.getGeneration();
123 } else if (gen > comgen) {