pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestPersistentSnapshotDeletionPolicy.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 this
6  * work for additional information regarding copyright ownership. The ASF
7  * licenses this file to You under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance with the License.
9  * 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, WITHOUT
15  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16  * License for the specific language governing permissions and limitations under
17  * the License.
18  */
19
20 import java.io.IOException;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.store.LockObtainFailedException;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPolicy {
33
34   // Keep it a class member so that getDeletionPolicy can use it
35   private Directory snapshotDir;
36   
37   // so we can close it if called by SDP tests
38   private PersistentSnapshotDeletionPolicy psdp;
39   
40   @Before
41   @Override
42   public void setUp() throws Exception {
43     super.setUp();
44     snapshotDir = newDirectory();
45   }
46   
47   @After
48   @Override
49   public void tearDown() throws Exception {
50     if (psdp != null) psdp.close();
51     snapshotDir.close();
52     super.tearDown();
53   }
54   
55   @Override
56   protected SnapshotDeletionPolicy getDeletionPolicy() throws IOException {
57     if (psdp != null) psdp.close();
58     snapshotDir.close();
59     snapshotDir = newDirectory();
60     return psdp = new PersistentSnapshotDeletionPolicy(
61         new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.CREATE,
62         TEST_VERSION_CURRENT);
63   }
64
65   @Override
66   protected SnapshotDeletionPolicy getDeletionPolicy(Map<String, String> snapshots) throws IOException {
67     SnapshotDeletionPolicy sdp = getDeletionPolicy();
68     if (snapshots != null) {
69       for (Entry<String, String> e: snapshots.entrySet()) {
70         sdp.registerSnapshotInfo(e.getKey(), e.getValue(), null);
71       }
72     }
73     return sdp;
74   }
75
76   @Override
77   @Test
78   public void testExistingSnapshots() throws Exception {
79     int numSnapshots = 3;
80     Directory dir = newDirectory();
81     PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();
82     IndexWriter writer = new IndexWriter(dir, getConfig(random, psdp));
83     prepareIndexAndSnapshots(psdp, writer, numSnapshots, "snapshot");
84     writer.close();
85     psdp.close();
86
87     // Re-initialize and verify snapshots were persisted
88     psdp = new PersistentSnapshotDeletionPolicy(
89         new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
90         TEST_VERSION_CURRENT);
91     new IndexWriter(dir, getConfig(random, psdp)).close();
92
93     assertSnapshotExists(dir, psdp, numSnapshots);
94     assertEquals(numSnapshots, psdp.getSnapshots().size());
95     psdp.close();
96     dir.close();
97   }
98
99   @Test(expected=IllegalArgumentException.class)
100   public void testIllegalSnapshotId() throws Exception {
101     getDeletionPolicy().snapshot("$SNAPSHOTS_DOC$");
102   }
103   
104   @Test
105   public void testInvalidSnapshotInfos() throws Exception {
106     // Add the correct number of documents (1), but without snapshot information
107     IndexWriter writer = new IndexWriter(snapshotDir, getConfig(random, null));
108     writer.addDocument(new Document());
109     writer.close();
110     try {
111       new PersistentSnapshotDeletionPolicy(
112           new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
113           TEST_VERSION_CURRENT);
114       fail("should not have succeeded to read from an invalid Directory");
115     } catch (IllegalStateException e) {
116     }
117   }
118
119   @Test
120   public void testNoSnapshotInfos() throws Exception {
121     // Initialize an empty index in snapshotDir - PSDP should initialize successfully.
122     new IndexWriter(snapshotDir, getConfig(random, null)).close();
123     new PersistentSnapshotDeletionPolicy(
124         new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
125         TEST_VERSION_CURRENT).close();
126   }
127
128   @Test(expected=IllegalStateException.class)
129   public void testTooManySnapshotInfos() throws Exception {
130     // Write two documents to the snapshots directory - illegal.
131     IndexWriter writer = new IndexWriter(snapshotDir, getConfig(random, null));
132     writer.addDocument(new Document());
133     writer.addDocument(new Document());
134     writer.close();
135     
136     new PersistentSnapshotDeletionPolicy(
137         new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
138         TEST_VERSION_CURRENT).close();
139     fail("should not have succeeded to open an invalid directory");
140   }
141
142   @Test
143   public void testSnapshotRelease() throws Exception {
144     Directory dir = newDirectory();
145     PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();
146     IndexWriter writer = new IndexWriter(dir, getConfig(random, psdp));
147     prepareIndexAndSnapshots(psdp, writer, 1, "snapshot");
148     writer.close();
149
150     psdp.release("snapshot0");
151     psdp.close();
152
153     psdp = new PersistentSnapshotDeletionPolicy(
154         new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
155         TEST_VERSION_CURRENT);
156     assertEquals("Should have no snapshots !", 0, psdp.getSnapshots().size());
157     psdp.close();
158     dir.close();
159   }
160
161   @Test
162   public void testStaticRead() throws Exception {
163     // While PSDP is open, it keeps a lock on the snapshots directory and thus
164     // prevents reading the snapshots information. This test checks that the 
165     // static read method works.
166     int numSnapshots = 1;
167     Directory dir = newDirectory();
168     PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();
169     IndexWriter writer = new IndexWriter(dir, getConfig(random, psdp));
170     prepareIndexAndSnapshots(psdp, writer, numSnapshots, "snapshot");
171     writer.close();
172     dir.close();
173     
174     try {
175       // This should fail, since the snapshots directory is locked - we didn't close it !
176       new PersistentSnapshotDeletionPolicy(
177           new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
178           TEST_VERSION_CURRENT);
179      fail("should not have reached here - the snapshots directory should be locked!");
180     } catch (LockObtainFailedException e) {
181       // expected
182     } finally {
183       psdp.close();
184     }
185     
186     // Reading the snapshots info should succeed though
187     Map<String, String> snapshots = PersistentSnapshotDeletionPolicy.readSnapshotsInfo(snapshotDir);
188     assertEquals("expected " + numSnapshots + " snapshots, got " + snapshots.size(), numSnapshots, snapshots.size());
189   }
190   
191 }