Example usage for com.mongodb BasicDBObject getDouble

List of usage examples for com.mongodb BasicDBObject getDouble

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject getDouble.

Prototype

public double getDouble(final String key, final double def) 

Source Link

Document

Returns the value of a field as an double .

Usage

From source file:com.ikanow.infinit.e.api.knowledge.federated.SimpleFederatedQueryEngine.java

License:Open Source License

@Override
public void postQueryActivities(ObjectId queryId, List<BasicDBObject> docs, ResponsePojo response) {
    boolean grabbedScores = false;
    double aggregateSignif = 100.0;
    double queryRelevance = 100.0;
    double score = 100.0;

    if (null != _asyncRequestsPerQuery) {
        int added = 0;
        BasicDBList bsonArray = new BasicDBList();
        PeekingIterator<FederatedRequest> it = Iterators.peekingIterator(_asyncRequestsPerQuery.iterator());
        while (it.hasNext()) {
            // loop state:
            BasicDBObject[] docOrDocs = new BasicDBObject[1];
            docOrDocs[0] = null;//from   ww  w .  j  ava  2  s  .c o  m

            FederatedRequest request = it.next();
            boolean isComplexSource = isComplexSource(request.endpointInfo.parentSource);
            if (null == request.cachedDoc) { // no cached doc, simple source processing (OR ANY COMPLEX CASE BY CONSTRUCTION)
                try {
                    if ((null == request.cachedResult) || isComplexSource) { // no cached api response, or complex         
                        if (null != request.importThread) {
                            // 1) wait for the thread to finish
                            if (null == request.endpointInfo.queryTimeout_secs) {
                                request.endpointInfo.queryTimeout_secs = 300;
                            }
                            for (int timer = 0; timer < request.endpointInfo.queryTimeout_secs; timer++) {
                                try {
                                    request.importThread.join(1000L);
                                    if (!request.importThread.isAlive()) {
                                        break;
                                    }
                                } //TESTED (by hand)
                                catch (Exception e) {
                                    //(carry on)
                                }
                            }
                            if (request.importThread.isAlive()) {
                                request.errorMessage = new RuntimeException("Script timed out");
                            } //TESTED (by hand)

                            // 2) Get the results
                            if (null != request.errorMessage) {
                                if (_testMode) {
                                    throw new RuntimeException(request.errorMessage);
                                }
                            } else if (isComplexSource) {
                                //DEBUG 
                                if (_DEBUG)
                                    _logger.debug("DEB: postQA0: " + request.complexSourceProcResults.size());

                                handleComplexDocCaching(request, _cacheMode, _scoreStats);

                                // Get a list of docs
                                docOrDocs = ((BasicDBList) DocumentPojo
                                        .listToDb(request.complexSourceProcResults, DocumentPojo.listType()))
                                                .toArray(new BasicDBObject[0]);

                                // (_API_ caching is exactly the same between cache and non-cache cases)
                                // (note that if null != complexSourceProcResults then follows that null != scriptResult)
                                String url = buildScriptUrl(request.mergeKey, request.queryIndex);

                                if (!(request.importThread instanceof FederatedSimpleHarvest) && _cacheMode) { // (don't cache python federated queries in test mode)
                                    // (simple harvest caching is done separately)
                                    this.cacheApiResponse(url, request.scriptResult, request.endpointInfo);
                                }
                            } //TESTED (by hand - single and multiple doc mode)               
                            else if (null == request.scriptResult) {
                                if (_testMode) {
                                    throw new RuntimeException("Script mode: no cached result found from: "
                                            + request.requestParameter);
                                }
                            } else {
                                // (_API_ caching is exactly the same between cache and non-cache cases)
                                String url = buildScriptUrl(request.mergeKey, request.queryIndex);
                                if (_cacheMode) { // (don't cache python federated queries in test mode)
                                    this.cacheApiResponse(url, request.scriptResult, request.endpointInfo);
                                }
                                bsonArray.add(request.scriptResult);
                            }
                        } // end script mode
                        else { // HTTP mode (also: must be simple source builder)
                            Response endpointResponse = request.responseFuture.get();
                            request.asyncClient.close();
                            request.asyncClient = null;

                            String jsonStr = endpointResponse.getResponseBody();
                            String url = endpointResponse.getUri().toURL().toString();

                            Object bsonUnknownType = com.mongodb.util.JSON.parse(jsonStr);
                            BasicDBObject bson = null;
                            if (bsonUnknownType instanceof BasicDBObject) {
                                bson = (BasicDBObject) bsonUnknownType;
                            } else if (bsonUnknownType instanceof BasicDBList) {
                                bson = new BasicDBObject(SimpleFederatedCache.array_, bsonUnknownType);
                            } else if (bsonUnknownType instanceof String) {
                                bson = new BasicDBObject(SimpleFederatedCache.value_, bsonUnknownType);
                            }

                            //DEBUG
                            if (_DEBUG)
                                _logger.debug("DEB: postQA1: " + url + ": " + jsonStr);

                            if (null != bson) {
                                MongoDbUtil.enforceTypeNamingPolicy(bson, 0);
                                this.cacheApiResponse(url, bson, request.endpointInfo);
                                bsonArray.add(bson);
                            }
                        } //(end script vs request method)
                    } //TESTED (3.1, 4.2)
                    else { // (just used cached value)
                        //DEBUG 
                        if (_DEBUG)
                            _logger.debug("DEB: postQA2: " + request.cachedResult.toString());

                        bsonArray.add(
                                (BasicDBObject) request.cachedResult.get(SimpleFederatedCache.cachedJson_));
                    } //TESTED (4.1, 4.3)
                } catch (Exception e) {
                    //DEBUG
                    if (null == request.subRequest) {
                        _logger.error("Error with script: " + e.getMessage());
                        if (_testMode) {
                            throw new RuntimeException("Error with script: " + e.getMessage(), e);
                        }
                    } else {
                        _logger.error("Error with " + request.subRequest.endPointUrl + ": " + e.getMessage());
                        if (_testMode) {
                            throw new RuntimeException(
                                    "Error with " + request.subRequest.endPointUrl + ": " + e.getMessage(), e);
                        }
                    }
                }

                if (null == docOrDocs[0]) {
                    // (this next bit of logic can only occur in simple source cases by construction, phew)
                    if (!it.hasNext() || (request.mergeKey != it.peek().mergeKey)) { // deliberate ptr arithmetic
                        String url = buildScriptUrl(request.mergeKey, request.queryIndex);

                        //DEBUG
                        if (_DEBUG)
                            _logger.debug("DEB: postQA3: " + url + ": " + bsonArray);

                        docOrDocs[0] = createDocFromJson(bsonArray, url, request, request.endpointInfo);
                    }
                }
            } // (end if no cached doc)
            else { // cached doc, bypass lots of processing because no merging and doc already built (simple source processing)
                docOrDocs[0] = request.cachedDoc;
            } //TESTED (by hand)

            if (null != docOrDocs[0])
                for (BasicDBObject doc : docOrDocs) {

                    // Cache the document unless already cached (or caching disabled)
                    if ((null == request.cachedDoc) && _cacheMode && !isComplexSource
                            && ((null == request.endpointInfo.cacheTime_days)
                                    || (request.endpointInfo.cacheTime_days >= 0))) {
                        simpleDocCache(request, doc);
                    } //TESTED (by hand, 3 cases: cached not expired, cached expired first time, cached expired multiple times)

                    if (!grabbedScores) {
                        if (!docs.isEmpty()) {
                            BasicDBObject topDoc = docs.get(0);
                            aggregateSignif = topDoc.getDouble(DocumentPojo.aggregateSignif_, aggregateSignif);
                            queryRelevance = topDoc.getDouble(DocumentPojo.queryRelevance_, queryRelevance);
                            score = topDoc.getDouble(DocumentPojo.score_, score);
                            grabbedScores = true;

                            // OK would also like to grab the original matching entity, if it exists
                            if (!isComplexSource) {
                                BasicDBList ents = (BasicDBList) topDoc.get(DocumentPojo.entities_);
                                if (null != ents) {
                                    for (Object entObj : ents) {
                                        BasicDBObject ent = (BasicDBObject) entObj;
                                        String entIndex = ent.getString(EntityPojo.index_, "");
                                        if (entIndex.equals(request.queryIndex)) {
                                            ents = (BasicDBList) doc.get(DocumentPojo.entities_);
                                            if (null != ents) {
                                                ents.add(ent);
                                            }
                                            break;
                                        }
                                    }
                                } //TESTED (by hand)
                            }
                        }
                    }
                    doc.put(DocumentPojo.aggregateSignif_, aggregateSignif);
                    doc.put(DocumentPojo.queryRelevance_, queryRelevance);
                    doc.put(DocumentPojo.score_, score);

                    // Swap id and updateId, everything's been cached now:
                    // Handle update ids vs normal ids:
                    ObjectId updateId = (ObjectId) doc.get(DocumentPojo.updateId_);
                    if (null != updateId) { // swap the 2...
                        doc.put(DocumentPojo.updateId_, doc.get(DocumentPojo._id_));
                        doc.put(DocumentPojo._id_, updateId);
                    } //TESTED (by hand)            

                    // If we're returning to a query then we'll adjust the doc format (some of the atomic fields become arrays)
                    if (!_testMode) {
                        convertDocToQueryFormat(doc, request.communityIdStrs);
                    } //TESTED (by hand)

                    docs.add(0, doc);
                    added++;
                    //(doc auto reset at top of loop)

                    //(end if built a doc from the last request/set of requests)
                } //TESTED (3.1)      

        } //(end loop over federated requests)

        if (null != response.getStats()) {
            response.getStats().found += added;
        } //TESTED (by hand)         
    }
}

From source file:com.ikanow.infinit.e.api.test.QueryExtensionTestCode.java

License:Open Source License

@Override
public void postQueryActivities(ObjectId queryId, List<BasicDBObject> docs, ResponsePojo response) {

    System.out.println("**QUERY ID=" + queryId);
    BasicDBObject topDoc = null;
    if ((null != docs) && !docs.isEmpty()) {
        topDoc = docs.iterator().next();
        System.out.println("**TOP DOC=" + topDoc);
    }//from  w ww  . j av a2 s  .c om
    DocumentPojo newDoc = new DocumentPojo();
    newDoc.setTitle("test1 title: " + queryId);
    newDoc.setUrl("http://www.bbc.com");
    newDoc.setDescription("test1 desc");
    newDoc.setCreated(new Date());
    newDoc.setModified(new Date());
    newDoc.setPublishedDate(new Date());
    newDoc.setId(queryId);
    newDoc.setMediaType("Social");
    newDoc.addToMetadata("query", _query);
    newDoc.setSourceKey("test1");
    newDoc.setCommunityId(new ObjectId(_savedCommIdStrs[0]));
    if (null != topDoc) {
        newDoc.setScore(topDoc.getDouble(DocumentPojo.score_, 100.0));
        newDoc.setAggregateSignif(topDoc.getDouble(DocumentPojo.aggregateSignif_, 100.0));
    } else {
        newDoc.setScore(100.0);
        newDoc.setAggregateSignif(100.0);
    }
    if (null != docs) {
        docs.add(0, (BasicDBObject) newDoc.toDb());
    }
    response.getStats().found++;
}