001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.update;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.HashMap;
021:
022: import org.apache.solr.core.SolrCore;
023: import org.apache.solr.handler.XmlUpdateRequestHandler;
024: import org.apache.solr.util.ContentStream;
025: import org.apache.solr.request.MapSolrParams;
026: import org.apache.solr.request.SolrQueryRequestBase;
027: import org.apache.solr.request.SolrQueryResponse;
028: import org.apache.solr.util.AbstractSolrTestCase;
029: import org.apache.solr.util.ContentStreamBase;
030:
031: /**
032: *
033: * @author ryan
034: *
035: */
036: public class AutoCommitTest extends AbstractSolrTestCase {
037:
038: public String getSchemaFile() {
039: return "schema.xml";
040: }
041:
042: public String getSolrConfigFile() {
043: return "solrconfig.xml";
044: }
045:
046: /**
047: * Take a string and make it an iterable ContentStream
048: *
049: * This should be moved to a helper class. (it is useful for the client too!)
050: */
051: public static Collection<ContentStream> toContentStreams(
052: final String str, final String contentType) {
053: ArrayList<ContentStream> streams = new ArrayList<ContentStream>();
054: ContentStreamBase stream = new ContentStreamBase.StringStream(
055: str);
056: stream.setContentType(contentType);
057: streams.add(stream);
058: return streams;
059: }
060:
061: public void testMaxDocs() throws Exception {
062:
063: DirectUpdateHandler2 updater = (DirectUpdateHandler2) SolrCore
064: .getSolrCore().getUpdateHandler();
065: DirectUpdateHandler2.CommitTracker tracker = updater.tracker;
066: tracker.timeUpperBound = -1;
067: tracker.docsUpperBound = 14;
068:
069: XmlUpdateRequestHandler handler = new XmlUpdateRequestHandler();
070: handler.init(null);
071:
072: SolrCore core = SolrCore.getSolrCore();
073: MapSolrParams params = new MapSolrParams(
074: new HashMap<String, String>());
075:
076: // Add a single document
077: SolrQueryResponse rsp = new SolrQueryResponse();
078: SolrQueryRequestBase req = new SolrQueryRequestBase(core,
079: params) {
080: };
081: for (int i = 0; i < 14; i++) {
082: req.setContentStreams(toContentStreams(adoc("id", "A" + i,
083: "subject", "info"), null));
084: handler.handleRequest(req, rsp);
085: }
086: // It should not be there right away
087: assertQ("shouldn't find any", req("id:A1"),
088: "//result[@numFound=0]");
089: assertEquals(0, tracker.autoCommitCount);
090:
091: req.setContentStreams(toContentStreams(adoc("id", "A14",
092: "subject", "info"), null));
093: handler.handleRequest(req, rsp);
094: // Wait longer then the autocommit time
095: Thread.sleep(500);
096: // blocks until commit is complete
097: req.setContentStreams(toContentStreams(adoc("id", "A15",
098: "subject", "info"), null));
099: handler.handleRequest(req, rsp);
100:
101: // Now make sure we can find it
102: assertQ("should find one", req("id:A14"),
103: "//result[@numFound=1]");
104: assertEquals(1, tracker.autoCommitCount);
105: // But not the one added afterward
106: assertQ("should find one", req("id:A15"),
107: "//result[@numFound=0]");
108: assertEquals(1, tracker.autoCommitCount);
109:
110: // Now add some more
111: for (int i = 0; i < 14; i++) {
112: req.setContentStreams(toContentStreams(adoc("id", "B" + i,
113: "subject", "info"), null));
114: handler.handleRequest(req, rsp);
115: }
116: // It should not be there right away
117: assertQ("shouldn't find any", req("id:B1"),
118: "//result[@numFound=0]");
119: assertEquals(1, tracker.autoCommitCount);
120:
121: req.setContentStreams(toContentStreams(adoc("id", "B14",
122: "subject", "info"), null));
123: handler.handleRequest(req, rsp);
124: Thread.sleep(500);
125:
126: // add request will block if commit has already started or completed
127: req.setContentStreams(toContentStreams(adoc("id", "B15",
128: "subject", "info"), null));
129: handler.handleRequest(req, rsp);
130:
131: assertQ("should find one", req("id:B14"),
132: "//result[@numFound=1]");
133: assertEquals(2, tracker.autoCommitCount);
134: assertQ("should find none", req("id:B15"),
135: "//result[@numFound=0]");
136: assertEquals(2, tracker.autoCommitCount);
137: }
138:
139: public void testMaxTime() throws Exception {
140:
141: DirectUpdateHandler2 updater = (DirectUpdateHandler2) SolrCore
142: .getSolrCore().getUpdateHandler();
143: DirectUpdateHandler2.CommitTracker tracker = updater.tracker;
144: tracker.timeUpperBound = 500;
145: tracker.docsUpperBound = -1;
146:
147: XmlUpdateRequestHandler handler = new XmlUpdateRequestHandler();
148: handler.init(null);
149:
150: SolrCore core = SolrCore.getSolrCore();
151: MapSolrParams params = new MapSolrParams(
152: new HashMap<String, String>());
153:
154: // Add a single document
155: SolrQueryResponse rsp = new SolrQueryResponse();
156: SolrQueryRequestBase req = new SolrQueryRequestBase(core,
157: params) {
158: };
159: req.setContentStreams(toContentStreams(adoc("id", "529",
160: "field_t", "what's inside?", "subject", "info"), null));
161: handler.handleRequest(req, rsp);
162:
163: // Check it it is in the index
164: assertQ("shouldn't find any", req("id:529"),
165: "//result[@numFound=0]");
166:
167: // Wait longer then the autocommit time
168: Thread.sleep(1000);
169: req.setContentStreams(toContentStreams(adoc("id", "530",
170: "field_t", "what's inside?", "subject", "info"), null));
171: handler.handleRequest(req, rsp);
172:
173: // Now make sure we can find it
174: assertQ("should find one", req("id:529"),
175: "//result[@numFound=1]");
176: // But not this one
177: assertQ("should find none", req("id:530"),
178: "//result[@numFound=0]");
179:
180: // now make the call 10 times really fast and make sure it
181: // only commits once
182: req
183: .setContentStreams(toContentStreams(adoc("id", "500"),
184: null));
185: for (int i = 0; i < 10; i++) {
186: handler.handleRequest(req, rsp);
187: }
188: assertQ("should not be there yet", req("id:500"),
189: "//result[@numFound=0]");
190: assertEquals(1, tracker.autoCommitCount);
191:
192: // Wait longer then the autocommit time
193: Thread.sleep(1000);
194: req.setContentStreams(toContentStreams(adoc("id", "531",
195: "field_t", "what's inside?", "subject", "info"), null));
196:
197: assertQ("now it should", req("id:500"), "//result[@numFound=1]");
198: assertQ("but not this", req("id:531"), "//result[@numFound=0]");
199: assertEquals(2, tracker.autoCommitCount);
200: }
201: }
|