~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/index/TestIndexModifier.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.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 org.apache.lucene.util.LuceneTestCase;
21
 
import org.apache.lucene.analysis.Analyzer;
22
 
import org.apache.lucene.analysis.SimpleAnalyzer;
23
 
import org.apache.lucene.analysis.standard.StandardAnalyzer;
24
 
import org.apache.lucene.document.Document;
25
 
import org.apache.lucene.document.Field;
26
 
import org.apache.lucene.document.Field.Index;
27
 
import org.apache.lucene.document.Field.Store;
28
 
import org.apache.lucene.store.Directory;
29
 
import org.apache.lucene.store.FSDirectory;
30
 
import org.apache.lucene.store.RAMDirectory;
31
 
 
32
 
import java.io.File;
33
 
import java.io.IOException;
34
 
import java.util.EmptyStackException;
35
 
import java.util.Random;
36
 
import java.util.Stack;
37
 
 
38
 
/**
39
 
 * Tests for the "IndexModifier" class, including accesses from two threads at the
40
 
 * same time.
41
 
 * 
42
 
 * @author Daniel Naber
43
 
 * @deprecated
44
 
 */
45
 
public class TestIndexModifier extends LuceneTestCase {
46
 
 
47
 
  private int docCount = 0;
48
 
  
49
 
  private final Term allDocTerm = new Term("all", "x");
50
 
 
51
 
  public void testIndex() throws IOException {
52
 
    Directory ramDir = new RAMDirectory();
53
 
    IndexModifier i = new IndexModifier(ramDir, new StandardAnalyzer(), true);
54
 
    i.addDocument(getDoc());
55
 
    assertEquals(1, i.docCount());
56
 
    i.flush();
57
 
    i.addDocument(getDoc(), new SimpleAnalyzer());
58
 
    assertEquals(2, i.docCount());
59
 
    i.optimize();
60
 
    assertEquals(2, i.docCount());
61
 
    i.flush();
62
 
    i.deleteDocument(0);
63
 
    assertEquals(1, i.docCount());
64
 
    i.flush();
65
 
    assertEquals(1, i.docCount());
66
 
    i.addDocument(getDoc());
67
 
    i.addDocument(getDoc());
68
 
    i.flush();
69
 
    // depend on merge policy - assertEquals(3, i.docCount());
70
 
    i.deleteDocuments(allDocTerm);
71
 
    assertEquals(0, i.docCount());
72
 
    i.optimize();
73
 
    assertEquals(0, i.docCount());
74
 
    
75
 
    //  Lucene defaults:
76
 
    assertNull(i.getInfoStream());
77
 
    assertTrue(i.getUseCompoundFile());
78
 
    assertEquals(IndexWriter.DISABLE_AUTO_FLUSH, i.getMaxBufferedDocs());
79
 
    assertEquals(10000, i.getMaxFieldLength());
80
 
    assertEquals(10, i.getMergeFactor());
81
 
    // test setting properties:
82
 
    i.setMaxBufferedDocs(100);
83
 
    i.setMergeFactor(25);
84
 
    i.setMaxFieldLength(250000);
85
 
    i.addDocument(getDoc());
86
 
    i.setUseCompoundFile(false);
87
 
    i.flush();
88
 
    assertEquals(100, i.getMaxBufferedDocs());
89
 
    assertEquals(25, i.getMergeFactor());
90
 
    assertEquals(250000, i.getMaxFieldLength());
91
 
    assertFalse(i.getUseCompoundFile());
92
 
 
93
 
    // test setting properties when internally the reader is opened:
94
 
    i.deleteDocuments(allDocTerm);
95
 
    i.setMaxBufferedDocs(100);
96
 
    i.setMergeFactor(25);
97
 
    i.setMaxFieldLength(250000);
98
 
    i.addDocument(getDoc());
99
 
    i.setUseCompoundFile(false);
100
 
    i.optimize();
101
 
    assertEquals(100, i.getMaxBufferedDocs());
102
 
    assertEquals(25, i.getMergeFactor());
103
 
    assertEquals(250000, i.getMaxFieldLength());
104
 
    assertFalse(i.getUseCompoundFile());
105
 
 
106
 
    i.close();
107
 
    try {
108
 
      i.docCount();
109
 
      fail();
110
 
    } catch (IllegalStateException e) {
111
 
      // expected exception
112
 
    }
113
 
  }
114
 
 
115
 
  public void testExtendedIndex() throws IOException {
116
 
    Directory ramDir = new RAMDirectory();
117
 
    PowerIndex powerIndex = new PowerIndex(ramDir, new StandardAnalyzer(), true);
118
 
    powerIndex.addDocument(getDoc());
119
 
    powerIndex.addDocument(getDoc());
120
 
    powerIndex.addDocument(getDoc());
121
 
    powerIndex.addDocument(getDoc());
122
 
    powerIndex.addDocument(getDoc());
123
 
    powerIndex.flush();
124
 
    assertEquals(5, powerIndex.docFreq(allDocTerm));
125
 
    powerIndex.close();
126
 
  }
127
 
  
128
 
  private Document getDoc() {
129
 
    Document doc = new Document();
130
 
    doc.add(new Field("body", Integer.toString(docCount), Field.Store.YES, Field.Index.UN_TOKENIZED));
131
 
    doc.add(new Field("all", "x", Field.Store.YES, Field.Index.UN_TOKENIZED));
132
 
    docCount++;
133
 
    return doc;
134
 
  }
135
 
  
136
 
  public void testIndexWithThreads() throws IOException {
137
 
    testIndexInternal(0);
138
 
    testIndexInternal(10);
139
 
    testIndexInternal(50);
140
 
  }
141
 
  
142
 
  private void testIndexInternal(int maxWait) throws IOException {
143
 
    final boolean create = true;
144
 
    //Directory rd = new RAMDirectory();
145
 
    // work on disk to make sure potential lock problems are tested:
146
 
    String tempDir = System.getProperty("java.io.tmpdir");
147
 
    if (tempDir == null)
148
 
      throw new IOException("java.io.tmpdir undefined, cannot run test");
149
 
    File indexDir = new File(tempDir, "lucenetestindex");
150
 
    Directory rd = FSDirectory.getDirectory(indexDir);
151
 
    IndexThread.id = 0;
152
 
    IndexThread.idStack.clear();
153
 
    IndexModifier index = new IndexModifier(rd, new StandardAnalyzer(), create);
154
 
    IndexThread thread1 = new IndexThread(index, maxWait, 1);
155
 
    thread1.start();
156
 
    IndexThread thread2 = new IndexThread(index, maxWait, 2);
157
 
    thread2.start();
158
 
    while(thread1.isAlive() || thread2.isAlive()) {
159
 
      try {
160
 
        Thread.sleep(100);
161
 
      } catch (InterruptedException e) {
162
 
        throw new RuntimeException(e);
163
 
      }
164
 
    }
165
 
    index.optimize();
166
 
    int added = thread1.added + thread2.added;
167
 
    int deleted = thread1.deleted + thread2.deleted;
168
 
    assertEquals(added-deleted, index.docCount());
169
 
    index.close();
170
 
    
171
 
    try {
172
 
      index.close();
173
 
      fail();
174
 
    } catch(IllegalStateException e) {
175
 
      // expected exception
176
 
    }
177
 
    rmDir(indexDir);
178
 
  }
179
 
  
180
 
  private void rmDir(File dir) {
181
 
    File[] files = dir.listFiles();
182
 
    for (int i = 0; i < files.length; i++) {
183
 
      files[i].delete();
184
 
    }
185
 
    dir.delete();
186
 
  }
187
 
  
188
 
  private class PowerIndex extends IndexModifier {
189
 
    public PowerIndex(Directory dir, Analyzer analyzer, boolean create) throws IOException {
190
 
      super(dir, analyzer, create);
191
 
    }
192
 
    public int docFreq(Term term) throws IOException {
193
 
      synchronized(directory) {
194
 
        assureOpen();
195
 
        createIndexReader();
196
 
        return indexReader.docFreq(term);
197
 
      }
198
 
    }
199
 
  }
200
 
  
201
 
}
202
 
 
203
 
class IndexThread extends Thread {
204
 
 
205
 
  private final static int ITERATIONS = 500;       // iterations of thread test
206
 
 
207
 
  static int id = 0;
208
 
  static Stack idStack = new Stack();
209
 
 
210
 
  int added = 0;
211
 
  int deleted = 0;
212
 
 
213
 
  private int maxWait = 10;
214
 
  private IndexModifier index;
215
 
  private int threadNumber;
216
 
  private Random random;
217
 
  
218
 
  IndexThread(IndexModifier index, int maxWait, int threadNumber) {
219
 
    this.index = index;
220
 
    this.maxWait = maxWait;
221
 
    this.threadNumber = threadNumber;
222
 
    // TODO: test case is not reproducible despite pseudo-random numbers:
223
 
    random = new Random(101+threadNumber);        // constant seed for better reproducability
224
 
  }
225
 
  
226
 
  public void run() {
227
 
    try {
228
 
      for(int i = 0; i < ITERATIONS; i++) {
229
 
        int rand = random.nextInt(101);
230
 
        if (rand < 5) {
231
 
          index.optimize();
232
 
        } else if (rand < 60) {
233
 
          Document doc = getDocument();
234
 
          index.addDocument(doc);
235
 
          idStack.push(doc.get("id"));
236
 
          added++;
237
 
        } else {
238
 
          // we just delete the last document added and remove it
239
 
          // from the id stack so that it won't be removed twice:
240
 
          String delId = null;
241
 
          try {
242
 
            delId = (String)idStack.pop();
243
 
          } catch (EmptyStackException e) {
244
 
            continue;
245
 
          }
246
 
          Term delTerm = new Term("id", new Integer(delId).toString());
247
 
          int delCount = index.deleteDocuments(delTerm);
248
 
          if (delCount != 1) {
249
 
            throw new RuntimeException("Internal error: " + threadNumber + " deleted " + delCount + 
250
 
                " documents, term=" + delTerm);
251
 
          }
252
 
          deleted++;
253
 
        }
254
 
        if (maxWait > 0) {
255
 
          try {
256
 
            rand = random.nextInt(maxWait);
257
 
            //System.out.println("waiting " + rand + "ms");
258
 
            Thread.sleep(rand);
259
 
          } catch (InterruptedException e) {
260
 
            throw new RuntimeException(e);
261
 
          }
262
 
        }
263
 
      }
264
 
    } catch (IOException e) {
265
 
      throw new RuntimeException(e);
266
 
    }
267
 
  }
268
 
 
269
 
  private Document getDocument() {
270
 
    Document doc = new Document();
271
 
    synchronized (getClass()) {
272
 
      doc.add(new Field("id", Integer.toString(id), Field.Store.YES,
273
 
          Field.Index.UN_TOKENIZED));
274
 
      id++;
275
 
    }
276
 
    // add random stuff:
277
 
    doc.add(new Field("content", Integer.toString(random.nextInt(1000)), Field.Store.YES,
278
 
        Field.Index.TOKENIZED));
279
 
    doc.add(new Field("content", Integer.toString(random.nextInt(1000)), Field.Store.YES,
280
 
        Field.Index.TOKENIZED));
281
 
    doc.add(new Field("all", "x", Field.Store.YES, Field.Index.TOKENIZED));
282
 
    return doc;
283
 
  }
284
 
  
285
 
}