~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/TestIndexWriterMerging.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
 
 * Copyright 2006 The Apache Software Foundation
4
 
 *
5
 
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 
 * you may not use this file except in compliance with the License.
7
 
 * You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
import org.apache.lucene.store.Directory;
19
 
import org.apache.lucene.store.MockRAMDirectory;
20
 
import org.apache.lucene.analysis.standard.StandardAnalyzer;
21
 
import org.apache.lucene.document.Document;
22
 
import org.apache.lucene.document.Field;
23
 
import org.apache.lucene.util.LuceneTestCase;
24
 
 
25
 
import java.io.IOException;
26
 
 
27
 
 
28
 
public class TestIndexWriterMerging extends LuceneTestCase
29
 
{
30
 
 
31
 
  /**
32
 
   * Tests that index merging (specifically addIndexes()) doesn't
33
 
   * change the index order of documents.
34
 
   */
35
 
  public void testLucene() throws IOException
36
 
  {
37
 
 
38
 
    int num=100;
39
 
 
40
 
    Directory indexA = new MockRAMDirectory();
41
 
    Directory indexB = new MockRAMDirectory();
42
 
 
43
 
    fillIndex(indexA, 0, num);
44
 
    boolean fail = verifyIndex(indexA, 0);
45
 
    if (fail)
46
 
    {
47
 
      fail("Index a is invalid");
48
 
    }
49
 
 
50
 
    fillIndex(indexB, num, num);
51
 
    fail = verifyIndex(indexB, num);
52
 
    if (fail)
53
 
    {
54
 
      fail("Index b is invalid");
55
 
    }
56
 
 
57
 
    Directory merged = new MockRAMDirectory();
58
 
 
59
 
    IndexWriter writer = new IndexWriter(merged, new StandardAnalyzer(), true);
60
 
    writer.setMergeFactor(2);
61
 
 
62
 
    writer.addIndexes(new Directory[]{indexA, indexB});
63
 
    writer.close();
64
 
 
65
 
    fail = verifyIndex(merged, 0);
66
 
    merged.close();
67
 
 
68
 
    assertFalse("The merged index is invalid", fail);
69
 
  }
70
 
 
71
 
  private boolean verifyIndex(Directory directory, int startAt) throws IOException
72
 
  {
73
 
    boolean fail = false;
74
 
    IndexReader reader = IndexReader.open(directory);
75
 
 
76
 
    int max = reader.maxDoc();
77
 
    for (int i = 0; i < max; i++)
78
 
    {
79
 
      Document temp = reader.document(i);
80
 
      //System.out.println("doc "+i+"="+temp.getField("count").stringValue());
81
 
      //compare the index doc number to the value that it should be
82
 
      if (!temp.getField("count").stringValue().equals((i + startAt) + ""))
83
 
      {
84
 
        fail = true;
85
 
        System.out.println("Document " + (i + startAt) + " is returning document " + temp.getField("count").stringValue());
86
 
      }
87
 
    }
88
 
    reader.close();
89
 
    return fail;
90
 
  }
91
 
 
92
 
  private void fillIndex(Directory dir, int start, int numDocs) throws IOException
93
 
  {
94
 
 
95
 
    IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
96
 
    writer.setMergeFactor(2);
97
 
    writer.setMaxBufferedDocs(2);
98
 
 
99
 
    for (int i = start; i < (start + numDocs); i++)
100
 
    {
101
 
      Document temp = new Document();
102
 
      temp.add(new Field("count", (""+i), Field.Store.YES, Field.Index.UN_TOKENIZED));
103
 
 
104
 
      writer.addDocument(temp);
105
 
    }
106
 
    writer.close();
107
 
  }
108
 
}