On 8/24/2011 4:15 PM, Shawn Heisey wrote:
It might not be the prettiest code, but I'll take it. Thank you. I paraphrased quite a bit and have ended up with the following:

I put all this into a somewhat generic method. Hopefully it will prove useful to someone else on the list. There are some minimal comments to explain what it does:

    /**
     * Gets the DataImportHandler status.
     *
* @return Long.MIN_VALUE: an error occurred, or the import never started. * Negative value: Import in progress, invert the sign to see how
     *         many documents added so far. Zero or positive value: Import
     *         complete, total number of documents added.
     * @throws SolrServerException
     * @throws IOException
     */
    public long getDIHStatus() throws SolrServerException, IOException
    {
        Long processed = null;
        String tmpProcessed = null;
        String tmpFetched = null;
        String elapsed = null;
        String aborted = null;
        String msg = null;

        SolrRequest req = new DirectXmlRequest("/dataimport", null);
        NamedList<Object> nl = solrCore.request(req);

        String status = (String) nl.get("status");
        @SuppressWarnings("unchecked")
        Map<String, String> msgs = (Map<String, String>) nl
                .get("statusMessages");
        if (msgs != null)
        {
            tmpProcessed = (String) msgs.get("Total Documents Processed");
            tmpFetched = (String) msgs.get("Total Rows Fetched");
            elapsed = (String) msgs.get("Time taken ");
            aborted = (String) msgs.get("Aborted");
            msg = (String) msgs.get("");
        }

        /**
* The "Total Documents Processed" field disappears between the time the * actual import is done and the DIH finishes indexing, committing, and * optimizing. If it's not there, try to pull it from the "" field. As a
         * last-ditch effort, get the (possibly inaccurate) value from the
         * "Total Rows Fetched" field.
         */
        if (tmpProcessed != null)
        {
            processed = Long.parseLong(tmpProcessed);
        }
        else if (msg != null)
        {
            /**
             * Pull up to two numbers out of the message. Example: Indexing
* completed. Added/Updated: 370055 documents. Deleted 0 documents.
             */
            Pattern p = Pattern.compile("(\\d+)");
            Matcher m = p.matcher(msg);
            if (m.find())
            {
                tmpProcessed = m.group();
                processed = Long.parseLong(tmpProcessed);
            }
            if (m.find())
            {
                tmpProcessed = m.group();
                processed += Long.parseLong(tmpProcessed);
            }
        }
        else if (tmpFetched != null)
        {
            processed = Long.parseLong(tmpFetched);
        }

        /**
* All available info has been gathered from the response. Now we parse
         * what we have and determine the return value.
         */
        if (aborted != null || processed == null)
        {
            return Long.MIN_VALUE;
        }

        if (status.equals("busy"))
        {
            if (processed == 0)
            {
                processed = -1L;
            }
            else
            {
                processed = -processed;
            }
            return processed;
        }

        if (status.equals("idle"))
        {
            if (elapsed == null)
            {
                return Long.MIN_VALUE;
            }
            return processed;
        }
        return Long.MIN_VALUE;
    }

Reply via email to