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.util.NamedList;
019: import org.apache.solr.util.SimpleOrderedMap;
020:
021: import java.util.*;
022:
023: /**
024: * <code>SolrQueryResponse</code> is used by a query handler to return
025: * the response to a query request.
026: *
027: * <p>
028: * <a name="returnable_data" /><b>Note On Returnable Data...</b><br/>
029: * A <code>SolrQueryResponse</code> may contain the following types of
030: * Objects generated by the <code>SolrRequestHandler</code> that processed
031: * the request.
032: * </p>
033: * <ul>
034: * <li>{@link String}</li>
035: * <li>{@link Integer}</li>
036: * <li>{@link Long}</li>
037: * <li>{@link Float}</li>
038: * <li>{@link Double}</li>
039: * <li>{@link Boolean}</li>
040: * <li>{@link Date}</li>
041: * <li>{@link org.apache.solr.search.DocList}</li>
042: * <li>{@link Map} containing any of the items in this list</li>
043: * <li>{@link NamedList} containing any of the items in this list</li>
044: * <li>{@link Collection} containing any of the items in this list</li>
045: * <li>Array containing any of the items in this list</li>
046: * <li>null</li>
047: * </ul>
048: *
049: * @author yonik
050: * @version $Id: SolrQueryResponse.java 501512 2007-01-30 18:36:32Z yonik $
051: * @since solr 0.9
052: */
053: public class SolrQueryResponse {
054:
055: /**
056: * Container for user defined values
057: * @see #getValues
058: * @see #add
059: * @see #setAllValues
060: * @see <a href="#returnable_data">Note on Returnable Data</a>
061: */
062: protected NamedList values = new SimpleOrderedMap();
063:
064: protected Set<String> defaultReturnFields;
065:
066: // error if this is set...
067: protected Exception err;
068:
069: /***
070: // another way of returning an error
071: int errCode;
072: String errMsg;
073: ***/
074:
075: /**
076: * Gets data to be returned in this response
077: * @see <a href="#returnable_data">Note on Returnable Data</a>
078: */
079: public NamedList getValues() {
080: return values;
081: }
082:
083: /**
084: * Sets data to be returned in this response
085: * @see <a href="#returnable_data">Note on Returnable Data</a>
086: */
087: public void setAllValues(NamedList nameValuePairs) {
088: values = nameValuePairs;
089: }
090:
091: /**
092: * Sets the document field names of fields to return by default when
093: * returning DocLists
094: */
095: public void setReturnFields(Set<String> fields) {
096: defaultReturnFields = fields;
097: }
098:
099: // TODO: should this be represented as a String[] such
100: // that order can be maintained if needed?
101:
102: /**
103: * Gets the document field names of fields to return by default when
104: * returning DocLists
105: */
106: public Set<String> getReturnFields() {
107: return defaultReturnFields;
108: }
109:
110: /**
111: * Appends a named value to the list of named values to be returned.
112: * @param name the name of the value - may be null if unnamed
113: * @param val the value to add - also may be null since null is a legal value
114: * @see <a href="#returnable_data">Note on Returnable Data</a>
115: */
116: public void add(String name, Object val) {
117: values.add(name, val);
118: }
119:
120: /**
121: * Causes an error to be returned instead of the results.
122: */
123: public void setException(Exception e) {
124: err = e;
125: }
126:
127: /**
128: * Returns an Exception if there was a fatal error in processing the request.
129: * Returns null if the request succeeded.
130: */
131: public Exception getException() {
132: return err;
133: }
134:
135: /**
136: * The endtime of the request in milliseconds.
137: * Used to calculate query time.
138: * @see #setEndTime(long)
139: * @see #getEndTime()
140: */
141: protected long endtime;
142:
143: /**
144: * Get the time in milliseconds when the response officially finished.
145: */
146: public long getEndTime() {
147: if (endtime == 0) {
148: setEndTime();
149: }
150: return endtime;
151: }
152:
153: /**
154: * Stop the timer for how long this query took.
155: * @see #setEndTime(long)
156: */
157: public long setEndTime() {
158: return setEndTime(System.currentTimeMillis());
159: }
160:
161: /**
162: * Set the in milliseconds when the response officially finished.
163: * @see #setEndTime()
164: */
165: public long setEndTime(long endtime) {
166: if (endtime != 0) {
167: this.endtime = endtime;
168: }
169: return this.endtime;
170: }
171:
172: }
|