Search in sources :

Example 1 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class RequestHandlerUtils method handleCommit.

/**
   * Check the request parameters and decide if it should commit or optimize.
   * If it does, it will check other related parameters such as "waitFlush" and "waitSearcher"
   */
public static boolean handleCommit(SolrQueryRequest req, UpdateRequestProcessor processor, SolrParams params, boolean force) throws IOException {
    if (params == null) {
        params = new MapSolrParams(new HashMap<String, String>());
    }
    boolean optimize = params.getBool(UpdateParams.OPTIMIZE, false);
    boolean commit = params.getBool(UpdateParams.COMMIT, false);
    boolean softCommit = params.getBool(UpdateParams.SOFT_COMMIT, false);
    boolean prepareCommit = params.getBool(UpdateParams.PREPARE_COMMIT, false);
    if (optimize || commit || softCommit || prepareCommit || force) {
        CommitUpdateCommand cmd = new CommitUpdateCommand(req, optimize);
        updateCommit(cmd, params);
        processor.processCommit(cmd);
        return true;
    }
    return false;
}
Also used : MapSolrParams(org.apache.solr.common.params.MapSolrParams) CommitUpdateCommand(org.apache.solr.update.CommitUpdateCommand)

Example 2 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class RequestHandlerUtils method handleRollback.

/**
   * @since Solr 1.4
   */
public static boolean handleRollback(SolrQueryRequest req, UpdateRequestProcessor processor, SolrParams params, boolean force) throws IOException {
    if (params == null) {
        params = new MapSolrParams(new HashMap<String, String>());
    }
    boolean rollback = params.getBool(UpdateParams.ROLLBACK, false);
    if (rollback || force) {
        RollbackUpdateCommand cmd = new RollbackUpdateCommand(req);
        processor.processRollback(cmd);
        return true;
    }
    return false;
}
Also used : MapSolrParams(org.apache.solr.common.params.MapSolrParams) RollbackUpdateCommand(org.apache.solr.update.RollbackUpdateCommand)

Example 3 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class BlobHandler method handleRequestBody.

@Override
public void handleRequestBody(final SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    String httpMethod = req.getHttpMethod();
    String path = (String) req.getContext().get("path");
    SolrConfigHandler.setWt(req, JSON);
    List<String> pieces = StrUtils.splitSmart(path, '/');
    String blobName = null;
    if (pieces.size() >= 3)
        blobName = pieces.get(2);
    if ("POST".equals(httpMethod)) {
        if (blobName == null || blobName.isEmpty()) {
            rsp.add("error", "Name not found");
            return;
        }
        String err = SolrConfigHandler.validateName(blobName);
        if (err != null) {
            log.warn("no blob name");
            rsp.add("error", err);
            return;
        }
        if (req.getContentStreams() == null) {
            log.warn("no content stream");
            rsp.add("error", "No stream");
            return;
        }
        for (ContentStream stream : req.getContentStreams()) {
            ByteBuffer payload = SimplePostTool.inputStreamToByteArray(stream.getStream(), maxSize);
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(payload.array(), payload.position(), payload.limit());
            String md5 = new BigInteger(1, m.digest()).toString(16);
            TopDocs duplicate = req.getSearcher().search(new TermQuery(new Term("md5", md5)), 1);
            if (duplicate.totalHits > 0) {
                rsp.add("error", "duplicate entry");
                forward(req, null, new MapSolrParams((Map) makeMap("q", "md5:" + md5, "fl", "id,size,version,timestamp,blobName")), rsp);
                log.warn("duplicate entry for blob :" + blobName);
                return;
            }
            TopFieldDocs docs = req.getSearcher().search(new TermQuery(new Term("blobName", blobName)), 1, new Sort(new SortField("version", SortField.Type.LONG, true)));
            long version = 0;
            if (docs.totalHits > 0) {
                Document doc = req.getSearcher().doc(docs.scoreDocs[0].doc);
                Number n = doc.getField("version").numericValue();
                version = n.longValue();
            }
            version++;
            String id = blobName + "/" + version;
            Map<String, Object> doc = makeMap(ID, id, "md5", md5, "blobName", blobName, VERSION, version, "timestamp", new Date(), "size", payload.limit(), "blob", payload);
            verifyWithRealtimeGet(blobName, version, req, doc);
            log.info(StrUtils.formatString("inserting new blob {0} ,size {1}, md5 {2}", doc.get(ID), String.valueOf(payload.limit()), md5));
            indexMap(req, rsp, doc);
            log.info(" Successfully Added and committed a blob with id {} and size {} ", id, payload.limit());
            break;
        }
    } else {
        int version = -1;
        if (pieces.size() > 3) {
            try {
                version = Integer.parseInt(pieces.get(3));
            } catch (NumberFormatException e) {
                rsp.add("error", "Invalid version" + pieces.get(3));
                return;
            }
        }
        if (ReplicationHandler.FILE_STREAM.equals(req.getParams().get(CommonParams.WT))) {
            if (blobName == null) {
                throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Please send the request in the format /blob/<blobName>/<version>");
            } else {
                String q = "blobName:{0}";
                if (version != -1)
                    q = "id:{0}/{1}";
                QParser qparser = QParser.getParser(StrUtils.formatString(q, blobName, version), req);
                final TopDocs docs = req.getSearcher().search(qparser.parse(), 1, new Sort(new SortField("version", SortField.Type.LONG, true)));
                if (docs.totalHits > 0) {
                    rsp.add(ReplicationHandler.FILE_STREAM, new SolrCore.RawWriter() {

                        @Override
                        public void write(OutputStream os) throws IOException {
                            Document doc = req.getSearcher().doc(docs.scoreDocs[0].doc);
                            IndexableField sf = doc.getField("blob");
                            FieldType fieldType = req.getSchema().getField("blob").getType();
                            ByteBuffer buf = (ByteBuffer) fieldType.toObject(sf);
                            if (buf == null) {
                                //should never happen unless a user wrote this document directly
                                throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Invalid document . No field called blob");
                            } else {
                                os.write(buf.array(), 0, buf.limit());
                            }
                        }
                    });
                } else {
                    throw new SolrException(SolrException.ErrorCode.NOT_FOUND, StrUtils.formatString("Invalid combination of blobName {0} and version {1}", blobName, version));
                }
            }
        } else {
            String q = "*:*";
            if (blobName != null) {
                q = "blobName:{0}";
                if (version != -1) {
                    q = "id:{0}/{1}";
                }
            }
            forward(req, null, new MapSolrParams((Map) makeMap("q", StrUtils.formatString(q, blobName, version), "fl", "id,size,version,timestamp,blobName,md5", SORT, "version desc")), rsp);
        }
    }
}
Also used : SolrCore(org.apache.solr.core.SolrCore) OutputStream(java.io.OutputStream) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) TopDocs(org.apache.lucene.search.TopDocs) ContentStream(org.apache.solr.common.util.ContentStream) Sort(org.apache.lucene.search.Sort) MessageDigest(java.security.MessageDigest) SolrException(org.apache.solr.common.SolrException) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Date(java.util.Date) FieldType(org.apache.solr.schema.FieldType) IndexableField(org.apache.lucene.index.IndexableField) MapSolrParams(org.apache.solr.common.params.MapSolrParams) QParser(org.apache.solr.search.QParser) BigInteger(java.math.BigInteger) Map(java.util.Map) Utils.makeMap(org.apache.solr.common.util.Utils.makeMap) Collections.singletonMap(java.util.Collections.singletonMap)

Example 4 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class UpdateRequestProcessorChain method init.

/**
   * Initializes the chain using the factories specified by the <code>PluginInfo</code>.
   * if the chain includes the <code>RunUpdateProcessorFactory</code>, but
   * does not include an implementation of the 
   * <code>DistributingUpdateProcessorFactory</code> interface, then an 
   * instance of <code>DistributedUpdateProcessorFactory</code> will be 
   * injected immediately prior to the <code>RunUpdateProcessorFactory</code>.
   *
   * @see DistributingUpdateProcessorFactory
   * @see RunUpdateProcessorFactory
   * @see DistributedUpdateProcessorFactory
   */
@Override
public void init(PluginInfo info) {
    final String infomsg = "updateRequestProcessorChain \"" + (null != info.name ? info.name : "") + "\"" + (info.isDefault() ? " (default)" : "");
    log.debug("creating " + infomsg);
    // wrap in an ArrayList so we know we know we can do fast index lookups 
    // and that add(int,Object) is supported
    List<UpdateRequestProcessorFactory> list = new ArrayList<>(solrCore.initPlugins(info.getChildren("processor"), UpdateRequestProcessorFactory.class, null));
    if (list.isEmpty()) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, infomsg + " require at least one processor");
    }
    int numDistrib = 0;
    int runIndex = -1;
    // (no idea why someone might use multiple run instances, but just in case)
    for (int i = list.size() - 1; 0 <= i; i--) {
        UpdateRequestProcessorFactory factory = list.get(i);
        if (factory instanceof DistributingUpdateProcessorFactory) {
            numDistrib++;
        }
        if (factory instanceof RunUpdateProcessorFactory) {
            runIndex = i;
        }
    }
    if (1 < numDistrib) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, infomsg + " may not contain more then one " + "instance of DistributingUpdateProcessorFactory");
    }
    if (0 <= runIndex && 0 == numDistrib) {
        // by default, add distrib processor immediately before run
        DistributedUpdateProcessorFactory distrib = new DistributedUpdateProcessorFactory();
        distrib.init(new NamedList());
        list.add(runIndex, distrib);
        log.debug("inserting DistributedUpdateProcessorFactory into " + infomsg);
    }
    chain = list;
    ProcessorInfo processorInfo = new ProcessorInfo(new MapSolrParams(info.attributes));
    if (processorInfo.isEmpty())
        return;
    UpdateRequestProcessorChain newChain = constructChain(this, processorInfo, solrCore);
    chain = newChain.chain;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 5 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class ZookeeperInfoHandler method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    final SolrParams params = req.getParams();
    Map<String, String> map = new HashMap<>(1);
    map.put(WT, "raw");
    map.put(OMIT_HEADER, "true");
    req.setParams(SolrParams.wrapDefaults(new MapSolrParams(map), params));
    synchronized (this) {
        if (pagingSupport == null) {
            pagingSupport = new PagedCollectionSupport();
            ZkController zkController = cores.getZkController();
            if (zkController != null) {
                // get notified when the ZK session expires (so we can clear the cached collections and rebuild)
                zkController.addOnReconnectListener(pagingSupport);
            }
        }
    }
    String path = params.get(PATH);
    String addr = params.get("addr");
    if (addr != null && addr.length() == 0) {
        addr = null;
    }
    String detailS = params.get("detail");
    boolean detail = detailS != null && detailS.equals("true");
    String dumpS = params.get("dump");
    boolean dump = dumpS != null && dumpS.equals("true");
    int start = params.getInt("start", 0);
    int rows = params.getInt("rows", -1);
    String filterType = params.get("filterType");
    if (filterType != null) {
        filterType = filterType.trim().toLowerCase(Locale.ROOT);
        if (filterType.length() == 0)
            filterType = null;
    }
    FilterType type = (filterType != null) ? FilterType.valueOf(filterType) : FilterType.none;
    String filter = (type != FilterType.none) ? params.get("filter") : null;
    if (filter != null) {
        filter = filter.trim();
        if (filter.length() == 0)
            filter = null;
    }
    ZKPrinter printer = new ZKPrinter(cores.getZkController(), addr);
    printer.detail = detail;
    printer.dump = dump;
    boolean isGraphView = "graph".equals(params.get("view"));
    printer.page = (isGraphView && "/clusterstate.json".equals(path)) ? new PageOfCollections(start, rows, type, filter) : null;
    printer.pagingSupport = pagingSupport;
    try {
        printer.print(path);
    } finally {
        printer.close();
    }
    rsp.getValues().add(RawResponseWriter.CONTENT, printer);
}
Also used : MapSolrParams(org.apache.solr.common.params.MapSolrParams) HashMap(java.util.HashMap) ZkController(org.apache.solr.cloud.ZkController) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams)

Aggregations

MapSolrParams (org.apache.solr.common.params.MapSolrParams)50 HashMap (java.util.HashMap)33 SolrCore (org.apache.solr.core.SolrCore)24 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)20 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)19 SolrParams (org.apache.solr.common.params.SolrParams)14 Test (org.junit.Test)14 LinkedHashMap (java.util.LinkedHashMap)11 ArrayList (java.util.ArrayList)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)10 SolrException (org.apache.solr.common.SolrException)6 UpdateRequestHandler (org.apache.solr.handler.UpdateRequestHandler)6 SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)6 Map (java.util.Map)4 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 Utils.makeMap (org.apache.solr.common.util.Utils.makeMap)4 IOException (java.io.IOException)3 SchemaField (org.apache.solr.schema.SchemaField)3 StringWriter (java.io.StringWriter)2 Collections.singletonMap (java.util.Collections.singletonMap)2