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.request;
017:
018: import org.apache.solr.search.SolrIndexSearcher;
019: import org.apache.solr.util.ContentStream;
020: import org.apache.solr.util.RefCounted;
021: import org.apache.solr.schema.IndexSchema;
022: import org.apache.solr.core.SolrCore;
023: import org.apache.solr.core.SolrException;
024:
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.HashMap;
028:
029: /**
030: * Base implementation of <code>SolrQueryRequest</code> that provides some
031: * convenience methods for accessing parameters, and manages an IndexSearcher
032: * reference.
033: *
034: * <p>
035: * The <code>close()</code> method must be called on any instance of this
036: * class once it is no longer in use.
037: * </p>
038: *
039: *
040: * @author yonik
041: * @version $Id: SolrQueryRequestBase.java 542679 2007-05-29 22:28:21Z ryan $
042: */
043: public abstract class SolrQueryRequestBase implements SolrQueryRequest {
044: @Deprecated
045: public static final String QUERY_NAME = "q";
046: @Deprecated
047: public static final String START_NAME = "start";
048: @Deprecated
049: public static final String ROWS_NAME = "rows";
050: @Deprecated
051: public static final String XSL_NAME = "xsl";
052: @Deprecated
053: public static final String QUERYTYPE_NAME = "qt";
054:
055: protected final SolrCore core;
056: protected final SolrParams origParams;
057: protected SolrParams params;
058: protected Map<Object, Object> context;
059: protected Iterable<ContentStream> streams;
060:
061: public SolrQueryRequestBase(SolrCore core, SolrParams params) {
062: this .core = core;
063: this .params = this .origParams = params;
064: }
065:
066: public Map<Object, Object> getContext() {
067: // SolrQueryRequest as a whole isn't thread safe, and this isn't either.
068: if (context == null)
069: context = new HashMap<Object, Object>();
070: return context;
071: }
072:
073: public SolrParams getParams() {
074: return params;
075: }
076:
077: public SolrParams getOriginalParams() {
078: return origParams;
079: }
080:
081: public void setParams(SolrParams params) {
082: this .params = params;
083: }
084:
085: public String getParam(String name) {
086: return params.get(name);
087: }
088:
089: public String[] getParams(String name) {
090: return params.getParams(name);
091: }
092:
093: /**
094: * use getParams().required().getInt( name ) instead
095: */
096: @Deprecated
097: public int getIntParam(String name) {
098: String s = getParam(name);
099: if (s == null) {
100: throw new SolrException(
101: SolrException.ErrorCode.SERVER_ERROR,
102: "Missing required parameter '" + name + "' from "
103: + this );
104: }
105: return Integer.parseInt(s);
106: }
107:
108: /**
109: * use getParams().required().getInt( name ) instead
110: */
111: @Deprecated
112: public int getIntParam(String name, int defval) {
113: String s = getParam(name);
114: return s == null ? defval : Integer.parseInt(s);
115: }
116:
117: /**
118: * use getParams().required().getParam( name ) instead
119: */
120: @Deprecated
121: public String getStrParam(String name) {
122: String s = getParam(name);
123: if (s == null) {
124: throw new SolrException(
125: SolrException.ErrorCode.SERVER_ERROR,
126: "Missing required parameter '" + name + "' from "
127: + this );
128: }
129: return s;
130: }
131:
132: /**
133: * use getParams().required().getParam( name ) instead
134: */
135: @Deprecated
136: public String getStrParam(String name, String defval) {
137: String s = getParam(name);
138: return s == null ? defval : s;
139: }
140:
141: @Deprecated
142: public String getQueryString() {
143: return params.get(SolrParams.Q);
144: }
145:
146: @Deprecated
147: public String getQueryType() {
148: return params.get(SolrParams.QT);
149: }
150:
151: // starting position in matches to return to client
152: @Deprecated
153: public int getStart() {
154: return params.getInt(SolrParams.START, 0);
155: }
156:
157: // number of matching documents to return
158: @Deprecated
159: public int getLimit() {
160: return params.getInt(SolrParams.ROWS, 10);
161: }
162:
163: protected final long startTime = System.currentTimeMillis();
164:
165: // Get the start time of this request in milliseconds
166: public long getStartTime() {
167: return startTime;
168: }
169:
170: // The index searcher associated with this request
171: protected RefCounted<SolrIndexSearcher> searcherHolder;
172:
173: public SolrIndexSearcher getSearcher() {
174: // should this reach out and get a searcher from the core singleton, or
175: // should the core populate one in a factory method to create requests?
176: // or there could be a setSearcher() method that Solr calls
177:
178: if (searcherHolder == null) {
179: searcherHolder = core.getSearcher();
180: }
181:
182: return searcherHolder.get();
183: }
184:
185: // The solr core (coordinator, etc) associated with this request
186: public SolrCore getCore() {
187: return core;
188: }
189:
190: // The index schema associated with this request
191: public IndexSchema getSchema() {
192: return core.getSchema();
193: }
194:
195: /**
196: * Frees resources associated with this request, this method <b>must</b>
197: * be called when the object is no longer in use.
198: */
199: public void close() {
200: if (searcherHolder != null) {
201: searcherHolder.decref();
202: }
203: }
204:
205: /** A Collection of ContentStreams passed to the request
206: */
207: public Iterable<ContentStream> getContentStreams() {
208: return streams;
209: }
210:
211: public void setContentStreams(Iterable<ContentStream> s) {
212: streams = s;
213: }
214:
215: public String getParamString() {
216: return origParams.toString();
217: }
218:
219: public String toString() {
220: return this .getClass().getSimpleName() + '{' + params + '}';
221: }
222:
223: }
|