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 org.apache.lucene.analysis.MockAnalyzer;
21 import org.apache.lucene.document.*;
22 import org.apache.lucene.store.*;
23 import org.apache.lucene.util.*;
24 import org.junit.Test;
26 public class TestRollingUpdates extends LuceneTestCase {
28 // Just updates the same set of N docs over and over, to
29 // stress out deletions
32 public void testRollingUpdates() throws Exception {
33 final Directory dir = newDirectory();
35 final LineFileDocs docs = new LineFileDocs(random);
37 final IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
38 final int SIZE = atLeast(20);
41 final int numUpdates = (int) (SIZE * (2+random.nextDouble()));
42 for(int docIter=0;docIter<numUpdates;docIter++) {
43 final Document doc = docs.nextDoc();
44 final String myID = ""+id;
50 doc.getField("docid").setValue(myID);
51 w.updateDocument(new Term("docid", myID), doc);
53 if (docIter >= SIZE && random.nextInt(50) == 17) {
57 final boolean applyDeletions = random.nextBoolean();
58 r = w.getReader(applyDeletions);
59 assertTrue("applyDeletions=" + applyDeletions + " r.numDocs()=" + r.numDocs() + " vs SIZE=" + SIZE, !applyDeletions || r.numDocs() == SIZE);
68 assertEquals(SIZE, w.numDocs());
77 public void testUpdateSameDoc() throws Exception {
78 final Directory dir = newDirectory();
80 final LineFileDocs docs = new LineFileDocs(random);
81 for (int r = 0; r < 3; r++) {
82 final IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
83 TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2));
84 final int numUpdates = atLeast(20);
85 int numThreads = _TestUtil.nextInt(random, 2, 6);
86 IndexingThread[] threads = new IndexingThread[numThreads];
87 for (int i = 0; i < numThreads; i++) {
88 threads[i] = new IndexingThread(docs, w, numUpdates);
92 for (int i = 0; i < numThreads; i++) {
99 IndexReader open = IndexReader.open(dir);
100 assertEquals(1, open.numDocs());
106 static class IndexingThread extends Thread {
107 final LineFileDocs docs;
108 final IndexWriter writer;
111 public IndexingThread(LineFileDocs docs, IndexWriter writer, int num) {
114 this.writer = writer;
120 IndexReader open = null;
121 for (int i = 0; i < num; i++) {
122 Document doc = new Document();// docs.nextDoc();
123 doc.add(newField("id", "test", Field.Index.NOT_ANALYZED));
124 writer.updateDocument(new Term("id", "test"), doc);
125 if (random.nextInt(3) == 0) {
127 open = IndexReader.open(writer, true);
129 IndexReader reader = open.reopen();
130 if (reader != open) {
134 assertEquals("iter: " + i + " numDocs: "+ open.numDocs() + " del: " + open.numDeletedDocs() + " max: " + open.maxDoc(), 1, open.numDocs());
140 } catch (Exception e) {
141 throw new RuntimeException(e);