1
package org.apache.solr.update;
3
* Licensed to the Apache Software Foundation (ASF) under one or more
4
* contributor license agreements. See the NOTICE file distributed with
5
* this work for additional information regarding copyright ownership.
6
* The ASF licenses this file to You under the Apache License, Version 2.0
7
* (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
import org.apache.lucene.document.Document;
20
import org.apache.lucene.document.Field;
21
import org.apache.solr.core.SolrCore;
22
import org.apache.solr.util.AbstractSolrTestCase;
25
import java.io.FileFilter;
32
public class DirectUpdateHandlerOptimizeTest extends AbstractSolrTestCase {
35
public String getSchemaFile() {
36
return "schema12.xml";
40
public String getSolrConfigFile() {
41
return "solrconfig-duh-optimize.xml";
45
public void testOptimize() throws Exception {
46
SolrCore core = h.getCore();
48
UpdateHandler updater = core.getUpdateHandler();
49
AddUpdateCommand cmd = new AddUpdateCommand();
50
cmd.overwriteCommitted = true;
51
cmd.overwritePending = true;
52
cmd.allowDups = false;
53
//add just under the merge factor, so no segments are merged
54
//the merge factor is 100 and the maxBufferedDocs is 2, so there should be 50 segments
55
for (int i = 0; i < 99; i++) {
56
// Add a valid document
57
cmd.doc = new Document();
58
cmd.doc.add(new Field("id", "id_" + i, Field.Store.YES, Field.Index.NOT_ANALYZED));
59
cmd.doc.add(new Field("subject", "subject_" + i, Field.Store.NO, Field.Index.ANALYZED));
63
CommitUpdateCommand cmtCmd = new CommitUpdateCommand(false);
64
updater.commit(cmtCmd);
65
updater.commit(cmtCmd); // commit twice to give systems such as windows a chance to delete the old files
67
String indexDir = core.getIndexDir();
68
assertNumSegments(indexDir, 50);
71
cmtCmd = new CommitUpdateCommand(true);
72
cmtCmd.maxOptimizeSegments = 25;
73
updater.commit(cmtCmd);
74
updater.commit(cmtCmd);
75
assertNumSegments(indexDir, 25);
77
cmtCmd.maxOptimizeSegments = -1;
79
updater.commit(cmtCmd);
81
} catch (IllegalArgumentException e) {
83
cmtCmd.maxOptimizeSegments = 1;
84
updater.commit(cmtCmd);
85
updater.commit(cmtCmd);
86
assertNumSegments(indexDir, 1);
89
private void assertNumSegments(String indexDir, int numSegs) {
90
File file = new File(indexDir);
91
File[] segs = file.listFiles(new FileFilter() {
92
public boolean accept(File file) {
93
return file.getName().endsWith("cfs");
98
// TODO: we need a method that does not rely on physical inspection of the directory.
100
// assertTrue("Wrong number of segments: " + segs.length + " does not equal: " + numSegs, segs.length == numSegs);