~ubuntu-branches/ubuntu/trusty/pylucene/trusty

« back to all changes in this revision

Viewing changes to lucene-java-2.3.1/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java

  • Committer: Package Import Robot
  • Author(s): Dmitry Nezhevenko
  • Date: 2012-04-23 16:43:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120423164355-grqtepnwtecdjfk2
Tags: 3.5.0-1
* New maintainer (closes: 670179)
* New upstream release
* Switch to dpkg-source 3.0 (quilt) format
* Switch to machine-readable debian/copyright
* Bump debian/compat to 8, drop debian/pycompat
* Switch from cdbs to dh
* Add watch file
* Build for all supported versions of python2 (closes: 581198, 632240)
* Rename binary package to python-lucene (closes: 581197)
* Add -dbg package

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
4
 
 
5
 
/**
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
12
 
 *
13
 
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 
 *
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.
20
 
 */
21
 
 
22
 
import java.util.Iterator;
23
 
import java.util.Collection;
24
 
import java.io.File;
25
 
import java.io.IOException;
26
 
 
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;
39
 
 
40
 
import org.apache.lucene.util.LuceneTestCase;
41
 
import org.apache.lucene.util._TestUtil;
42
 
 
43
 
//
44
 
// This was developed for Lucene In Action,
45
 
// http://lucenebook.com
46
 
//
47
 
 
48
 
public class TestSnapshotDeletionPolicy extends LuceneTestCase
49
 
{
50
 
  public static final String INDEX_PATH = "test.snapshots";
51
 
 
52
 
  public void testSnapshotDeletionPolicy() throws IOException {
53
 
    File dir = new File(System.getProperty("tempDir"), INDEX_PATH);
54
 
    try {
55
 
      Directory fsDir = FSDirectory.getDirectory(dir);
56
 
      runTest(fsDir);
57
 
      fsDir.close();
58
 
    } finally {
59
 
      _TestUtil.rmDir(dir);
60
 
    }
61
 
 
62
 
    MockRAMDirectory dir2 = new MockRAMDirectory();
63
 
    runTest(dir2);
64
 
  }
65
 
 
66
 
  private void runTest(Directory dir) throws IOException {
67
 
    // Run for ~7 seconds
68
 
    final long stopTime = System.currentTimeMillis() + 7000;
69
 
 
70
 
    SnapshotDeletionPolicy dp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
71
 
    final IndexWriter writer = new IndexWriter(dir, true, new StandardAnalyzer(), dp);
72
 
 
73
 
    // Force frequent commits
74
 
    writer.setMaxBufferedDocs(2);
75
 
 
76
 
    final Thread t = new Thread() {
77
 
        public void run() {
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++) {
82
 
              try {
83
 
                writer.addDocument(doc);
84
 
              } catch (IOException cie) {
85
 
                RuntimeException re = new RuntimeException("addDocument failed");
86
 
                re.initCause(cie);
87
 
                throw re;
88
 
              }
89
 
            }
90
 
            try {
91
 
              Thread.sleep(1);
92
 
            } catch (InterruptedException ie) {
93
 
              Thread.currentThread().interrupt();
94
 
            }
95
 
          }
96
 
        }
97
 
      };
98
 
 
99
 
    t.start();
100
 
 
101
 
    // While the above indexing thread is running, take many
102
 
    // backups:
103
 
    while(System.currentTimeMillis() < stopTime) {
104
 
      backupIndex(dir, dp);
105
 
      try {
106
 
        Thread.sleep(20);
107
 
      } catch (InterruptedException ie) {
108
 
        Thread.currentThread().interrupt();
109
 
      }
110
 
      if (!t.isAlive())
111
 
        break;
112
 
    }
113
 
 
114
 
    try {
115
 
      t.join();
116
 
    } catch (InterruptedException ie) {
117
 
      Thread.currentThread().interrupt();
118
 
    }
119
 
 
120
 
    // Add one more document to force writer to commit a
121
 
    // final segment, so deletion policy has a chance to
122
 
    // delete again:
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);
126
 
 
127
 
    // Make sure we don't have any leftover files in the
128
 
    // directory:
129
 
    writer.close();
130
 
    TestIndexWriter.assertNoUnreferencedFiles(dir, "some files were not deleted but should have been");
131
 
  }
132
 
 
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 {
139
 
 
140
 
    // To backup an index we first take a snapshot:
141
 
    IndexCommitPoint cp = dp.snapshot();
142
 
    try {
143
 
 
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
156
 
        // backup.
157
 
        readFile(dir, fileName);
158
 
      }
159
 
 
160
 
    } finally {
161
 
      // Make sure to release the snapshot, otherwise these
162
 
      // files will never be deleted during this IndexWriter
163
 
      // session:
164
 
      dp.release();
165
 
    }
166
 
  }
167
 
 
168
 
  byte[] buffer = new byte[4096];
169
 
 
170
 
  private void readFile(Directory dir, String name) throws IOException {
171
 
    IndexInput input = dir.openInput(name);
172
 
    try {
173
 
      long size = dir.fileLength(name);
174
 
      long bytesLeft = size;
175
 
      while (bytesLeft > 0) {
176
 
        final int numToRead;
177
 
        if (bytesLeft < buffer.length)
178
 
          numToRead = (int) bytesLeft;
179
 
        else
180
 
          numToRead = buffer.length;
181
 
        input.readBytes(buffer, 0, numToRead, false);
182
 
        bytesLeft -= numToRead;
183
 
      }
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.
189
 
      try {
190
 
        Thread.sleep(1);
191
 
      } catch (InterruptedException ie) {
192
 
        Thread.currentThread().interrupt();
193
 
      }
194
 
    } finally {
195
 
      input.close();
196
 
    }
197
 
  }
198
 
}
199