All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.request.SolrQueryRequestBase Maven / Gradle / Ivy

There is a newer version: 9.7.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.request;

import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.noop.NoopSpan;
import io.opentracing.util.GlobalTracer;
import java.io.Closeable;
import java.security.Principal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.solr.api.ApiBag;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.CommandOperation;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.JsonSchemaValidator;
import org.apache.solr.common.util.ObjectReleaseTracker;
import org.apache.solr.common.util.SuppressForbidden;
import org.apache.solr.common.util.ValidatingJsonMap;
import org.apache.solr.core.SolrCore;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.servlet.HttpSolrCall;
import org.apache.solr.util.RTimerTree;
import org.apache.solr.util.RefCounted;

/**
 * Base implementation of SolrQueryRequest that provides some convenience methods for
 * accessing parameters, and manages an IndexSearcher reference.
 *
 * 

The close() method must be called on any instance of this class once it is no * longer in use. */ public abstract class SolrQueryRequestBase implements SolrQueryRequest, Closeable { protected final SolrCore core; protected final SolrParams origParams; protected volatile IndexSchema schema; protected SolrParams params; protected Map context; protected Iterable streams; protected Map json; private final RTimerTree requestTimer; protected final long startTime; @SuppressForbidden( reason = "Need currentTimeMillis to get start time for request (to be used for stats/debugging)") public SolrQueryRequestBase(SolrCore core, SolrParams params, RTimerTree requestTimer) { this.core = core; this.schema = null == core ? null : core.getLatestSchema(); this.params = this.origParams = params; this.requestTimer = requestTimer; this.startTime = System.currentTimeMillis(); } public SolrQueryRequestBase(SolrCore core, SolrParams params) { this(core, params, new RTimerTree()); } @Override public Map getContext() { // SolrQueryRequest as a whole isn't thread safe, and this isn't either. if (context == null) context = new HashMap<>(); return context; } @Override public SolrParams getParams() { return params; } @Override public SolrParams getOriginalParams() { return origParams; } @Override public void setParams(SolrParams params) { this.params = params; } // Get the start time of this request in milliseconds @Override public long getStartTime() { return startTime; } @Override public RTimerTree getRequestTimer() { return requestTimer; } // The index searcher associated with this request protected RefCounted searcherHolder; @Override public SolrIndexSearcher getSearcher() { if (core == null) return null; // a request for a core admin will not have a core // should this reach out and get a searcher from the core singleton, or // should the core populate one in a factory method to create requests? // or there could be a setSearcher() method that Solr calls if (searcherHolder == null) { searcherHolder = core.getSearcher(); // We start tracking here instead of at construction, because if getSearcher is never called, // it's not fatal to forget close(), and lots of test code is sloppy about it. However, when // we get another searcher reference, having this tracked may be a good hint about where the // leak comes from. assert ObjectReleaseTracker.track(this); } return searcherHolder.get(); } // The solr core (coordinator, etc) associated with this request @Override public SolrCore getCore() { return core; } // The index schema associated with this request @Override public IndexSchema getSchema() { // a request for a core admin will no have a core return schema; } @Override public Tracer getTracer() { final HttpSolrCall call = getHttpSolrCall(); if (call != null) { final Tracer tracer = (Tracer) call.getReq().getAttribute(Tracer.class.getName()); if (tracer != null) { return tracer; } } if (core != null) { return core.getCoreContainer().getTracer(); } return GlobalTracer.get(); // this way is not ideal (particularly in tests) but it's okay } @Override public Span getSpan() { final HttpSolrCall call = getHttpSolrCall(); if (call != null) { final Span span = (Span) call.getReq().getAttribute(Span.class.getName()); if (span != null) { return span; } } return NoopSpan.INSTANCE; } @Override public void updateSchemaToLatest() { schema = core.getLatestSchema(); } /** * Frees resources associated with this request, this method must be called when the object * is no longer in use. */ @Override public void close() { if (searcherHolder != null) { assert ObjectReleaseTracker.release(this); searcherHolder.decref(); searcherHolder = null; } } /** A Collection of ContentStreams passed to the request */ @Override public Iterable getContentStreams() { return streams; } public void setContentStreams(Iterable s) { streams = s; } @Override public String getParamString() { return origParams.toString(); } @Override public String toString() { return this.getClass().getSimpleName() + '{' + params + '}'; } @Override public Map getJSON() { return json; } @Override public void setJSON(Map json) { this.json = json; } @Override public Principal getUserPrincipal() { return null; } List parsedCommands; @Override public List getCommands(boolean validateInput) { if (parsedCommands == null) { Iterable contentStreams = getContentStreams(); if (contentStreams == null) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No content stream"); for (ContentStream contentStream : contentStreams) { parsedCommands = ApiBag.getCommandOperations(contentStream, getValidators(), validateInput); } } return CommandOperation.clone(parsedCommands); } protected ValidatingJsonMap getSpec() { return null; } protected Map getValidators() { return Collections.emptyMap(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy