1
package org.apache.lucene;
2
// Intentionally not in org.apache.lucene.index, to assert
3
// that we do not require any package private access.
6
* Licensed to the Apache Software Foundation (ASF) under one or more
7
* contributor license agreements. See the NOTICE file distributed with
8
* this work for additional information regarding copyright ownership.
9
* The ASF licenses this file to You under the Apache License, Version 2.0
10
* (the "License"); you may not use this file except in compliance with
11
* the License. You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
22
import java.util.Iterator;
23
import java.util.Collection;
25
import java.io.IOException;
27
import org.apache.lucene.document.Document;
28
import org.apache.lucene.document.Field;
29
import org.apache.lucene.store.Directory;
30
import org.apache.lucene.store.FSDirectory;
31
import org.apache.lucene.store.IndexInput;
32
import org.apache.lucene.store.MockRAMDirectory;
33
import org.apache.lucene.analysis.standard.StandardAnalyzer;
34
import org.apache.lucene.index.IndexCommitPoint;
35
import org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy;
36
import org.apache.lucene.index.IndexWriter;
37
import org.apache.lucene.index.TestIndexWriter;
38
import org.apache.lucene.index.SnapshotDeletionPolicy;
40
import org.apache.lucene.util.LuceneTestCase;
41
import org.apache.lucene.util._TestUtil;
44
// This was developed for Lucene In Action,
45
// http://lucenebook.com
48
public class TestSnapshotDeletionPolicy extends LuceneTestCase
50
public static final String INDEX_PATH = "test.snapshots";
52
public void testSnapshotDeletionPolicy() throws IOException {
53
File dir = new File(System.getProperty("tempDir"), INDEX_PATH);
55
Directory fsDir = FSDirectory.getDirectory(dir);
62
MockRAMDirectory dir2 = new MockRAMDirectory();
66
private void runTest(Directory dir) throws IOException {
68
final long stopTime = System.currentTimeMillis() + 7000;
70
SnapshotDeletionPolicy dp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
71
final IndexWriter writer = new IndexWriter(dir, true, new StandardAnalyzer(), dp);
73
// Force frequent commits
74
writer.setMaxBufferedDocs(2);
76
final Thread t = new Thread() {
78
Document doc = new Document();
79
doc.add(new Field("content", "aaa", Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
80
while(System.currentTimeMillis() < stopTime) {
81
for(int i=0;i<27;i++) {
83
writer.addDocument(doc);
84
} catch (IOException cie) {
85
RuntimeException re = new RuntimeException("addDocument failed");
92
} catch (InterruptedException ie) {
93
Thread.currentThread().interrupt();
101
// While the above indexing thread is running, take many
103
while(System.currentTimeMillis() < stopTime) {
104
backupIndex(dir, dp);
107
} catch (InterruptedException ie) {
108
Thread.currentThread().interrupt();
116
} catch (InterruptedException ie) {
117
Thread.currentThread().interrupt();
120
// Add one more document to force writer to commit a
121
// final segment, so deletion policy has a chance to
123
Document doc = new Document();
124
doc.add(new Field("content", "aaa", Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
125
writer.addDocument(doc);
127
// Make sure we don't have any leftover files in the
130
TestIndexWriter.assertNoUnreferencedFiles(dir, "some files were not deleted but should have been");
133
/** Example showing how to use the SnapshotDeletionPolicy
134
* to take a backup. This method does not really do a
135
* backup; instead, it reads every byte of every file
136
* just to test that the files indeed exist and are
137
* readable even while the index is changing. */
138
public void backupIndex(Directory dir, SnapshotDeletionPolicy dp) throws IOException {
140
// To backup an index we first take a snapshot:
141
IndexCommitPoint cp = dp.snapshot();
144
// While we hold the snapshot, and nomatter how long
145
// we take to do the backup, the IndexWriter will
146
// never delete the files in the snapshot:
147
Collection files = cp.getFileNames();
148
Iterator it = files.iterator();
149
while(it.hasNext()) {
150
final String fileName = (String) it.next();
151
// NOTE: in a real backup you would not use
152
// readFile; you would need to use something else
153
// that copies the file to a backup location. This
154
// could even be a spawned shell process (eg "tar",
155
// "zip") that takes the list of files and builds a
157
readFile(dir, fileName);
161
// Make sure to release the snapshot, otherwise these
162
// files will never be deleted during this IndexWriter
168
byte[] buffer = new byte[4096];
170
private void readFile(Directory dir, String name) throws IOException {
171
IndexInput input = dir.openInput(name);
173
long size = dir.fileLength(name);
174
long bytesLeft = size;
175
while (bytesLeft > 0) {
177
if (bytesLeft < buffer.length)
178
numToRead = (int) bytesLeft;
180
numToRead = buffer.length;
181
input.readBytes(buffer, 0, numToRead, false);
182
bytesLeft -= numToRead;
184
// Don't do this in your real backups! This is just
185
// to force a backup to take a somewhat long time, to
186
// make sure we are exercising the fact that the
187
// IndexWriter should not delete this file even when I
188
// take my time reading it.
191
} catch (InterruptedException ie) {
192
Thread.currentThread().interrupt();