Example usage for com.mongodb BasicDBObject getDate

List of usage examples for com.mongodb BasicDBObject getDate

Introduction

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

Prototype

public Date getDate(final String field, final Date def) 

Source Link

Document

Returns the date or def if not set.

Usage

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

License:Open Source License

private BasicDBObject getCache(String url, SourceFederatedQueryConfigPojo endpoint) {
    if ((null != endpoint.cacheTime_days) && (endpoint.cacheTime_days <= 0)) { // cache disabled
        return null;
    }//from www  .ja v a  2s  . com

    DBCollection endpointCacheCollection = getCacheCollection();

    if (!_staticInitializer) {
        _staticInitializer = true;
        endpointCacheCollection.ensureIndex(new BasicDBObject(SimpleFederatedCache.expiryDate_, 1));
    }
    BasicDBObject cacheObj = (BasicDBObject) endpointCacheCollection
            .findOne(new BasicDBObject(SimpleFederatedCache._id_, url));
    if (null == cacheObj) {
        return null;
    }
    // else found something, means there's stuff in the DB
    // so check it's not too big:
    Date now = new Date();
    if ((-1 == _lastChecked) || (now.getTime() > (_lastChecked + (600L * 1000L)))) { // (only check every 10 minutes) 

        if (endpointCacheCollection.count() > SimpleFederatedCache.QUERY_FEDERATION_CACHE_CLEANSE_SIZE) {
            _lastChecked = now.getTime();
            // Remove everything with expiry date older than now            
            endpointCacheCollection.remove(new BasicDBObject(SimpleFederatedCache.expiryDate_,
                    new BasicDBObject(DbManager.lt_, new Date())));
        }
    } //TESTED (4.3)
    Date expiryDate = cacheObj.getDate(SimpleFederatedCache.expiryDate_, now);
    if (now.getTime() < expiryDate.getTime()) {
        return cacheObj;
    } else {
        return null;
    } //TESTED (4.2)
}

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

License:Open Source License

public static boolean checkDocCache_isExpired(BasicDBObject cachedDoc,
        SourceFederatedQueryConfigPojo endpoint) {
    if (null == endpoint.cacheTime_days)
        endpoint.cacheTime_days = DEFAULT_CACHE_TIME_DAYS;

    Date now = new Date();
    long cacheThreshold = cachedDoc.getDate(DocumentPojo.created_, now).getTime()
            + endpoint.cacheTime_days * 3600L * 24L * 1000L;

    if (cacheThreshold < now.getTime()) // (ie doc-creation-time + cache is earlier than now => time to decache)
    {/*from  w  w  w .jav a 2 s .  c om*/
        //DEBUG
        if (_DEBUG)
            _logger.debug("DEB: preQA6zz: Cache expired: " + cachedDoc.getString(DocumentPojo.url_) + ": "
                    + new Date(cacheThreshold) + " vs " + now);

        return true;
    } else
        return false;
}

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

License:Open Source License

public void test_CheckIfDocAdded(String testName, List<BasicDBObject> docs) {

    // 1) Did a doc get added?

    if (2 != docs.size()) {
        System.out.println("*** " + testName + ": didn't add doc? " + docs.size());
        System.exit(-1);/*from w  ww.j  a v  a  2 s. c  om*/
    }

    // 2) Was it the right doc?

    BasicDBObject doc = docs.get(0);
    doc.remove(DocumentPojo._id_);
    doc.remove(DocumentPojo.created_);
    doc.remove(DocumentPojo.modified_);
    doc.remove(DocumentPojo.publishedDate_);

    String docToCheck = "{ \"displayUrl\" : \"http://test3_1\" , \"url\" : \"inf://federated/fakeendpoint.123/test3_1/testentityin\" , \"sourceKey\" : [ \"fakeendpoint.123\"] , \"source\" : [ \"fakeendpoint\"] , \"communityId\" : [ \"4c927585d591d31d7b37097a\"] , \"mediaType\" : [ \"Report\"] , \"metadata\" : { \"json\" : [ { \"test\" : { \"field\" : [ \"test3_1\" , \"test3_1\"] , \"field2\" : \"http://test3_1\"}}]} , \"title\" : \"fake endpoint: : test3_1: test3_1\" , \"entities\" : [ { \"disambiguated_name\" : \"test3_1\" , \"type\" : \"TestEntityOut\" , \"dimension\" : \"What\" , \"relevance\" : 1.0 , \"doccount\" : 1 , \"averageFreq\" : 1.0 , \"datasetSignificance\" : 10.0 , \"significance\" : 10.0 , \"frequency\" : 1.0 , \"index\" : \"test3_1/testentityout\" , \"queryCoverage\" : 100.0 , \"totalfrequency\" : 1.0}] , \"description\" : \"[\\n  {\\n    \\\"test\\\": {\\n      \\\"field\\\": [\\n        \\\"test3_1\\\",\\n        \\\"test3_1\\\"\\n      ],\\n      \\\"field2\\\": \\\"http://test3_1\\\"\\n    }\\n  }\\n]\" , \"aggregateSignif\" : 115.0 , \"queryRelevance\" : 105.0 , \"score\" : 110.0}";

    if (!docToCheck.equals(doc.toString())) {
        System.out.println(
                "*** " + testName + ": document incorrect:\n" + docToCheck + "\nVS\n" + doc.toString());
        System.exit(-1);
    }

    // 3) Did the doc get cached?

    DBCollection endpointCacheCollection = getCacheCollection();

    String hardwiredCacheId = "http://localhost:8186/test/test3_1/?param1=test3_1";
    BasicDBObject cachedVal = (BasicDBObject) endpointCacheCollection
            .findOne(new BasicDBObject(SimpleFederatedCache._id_, hardwiredCacheId));

    if (null == cachedVal) {
        System.out.println(
                "*** " + testName + ": no cache for: " + doc.get(DocumentPojo.url_) + " / " + hardwiredCacheId);
        System.exit(-1);
    } else {
        Date expiryDate = cachedVal.getDate(SimpleFederatedCache.expiryDate_, null);
        if ((null == expiryDate) || ((new Date().getTime() - expiryDate.getTime()) > 10 * 1000L)) {
            System.out.println("*** " + testName + ": expiry date for: " + doc.get(DocumentPojo.url_) + ": "
                    + expiryDate + " vs now= " + new Date());
            System.exit(-1);
        }
        BasicDBObject cachedJson = (BasicDBObject) cachedVal.get(SimpleFederatedCache.cachedJson_);
        Object docVal = null;
        try {
            Object[] docVals = (Object[]) ((BasicDBObject) doc.get(DocumentPojo.metadata_)).get("json");
            docVal = docVals[0];
        } catch (Exception e) {
            @SuppressWarnings("rawtypes")
            Collection docVals = (Collection) ((BasicDBObject) doc.get(DocumentPojo.metadata_)).get("json");
            docVal = docVals.iterator().next();

        }
        if ((null == cachedJson) || !cachedJson.equals(docVal)) {
            System.out.println("*** " + testName + ": cache: " + doc.get(DocumentPojo.url_)
                    + ": cached val incorrect:\n" + docVal + "\nVS\n" + cachedJson);
            System.exit(-1);
        }
    }
}

From source file:com.ikanow.infinit.e.processing.custom.launcher.CustomSavedQueryQueueLauncher.java

License:Open Source License

public static void createAlertDocSummary(StringBuffer alertEmailText, int docNum, int numDocSummaries,
        BasicDBObject doc, String rootUrl) {
    if (docNum < numDocSummaries) {
        // Preamble on the first doc
        if (0 == docNum) {
            alertEmailText.append("<p>");
            alertEmailText.append("Top ").append(numDocSummaries);
            if (0 == numDocSummaries) {
                alertEmailText.append(" document:");
            } else {
                alertEmailText.append(" documents:");
            }/*from  www .j  a va 2 s.  c om*/
            alertEmailText.append("</p>");
            alertEmailText.append("\n");
            alertEmailText.append("<ol>");
            alertEmailText.append("\n");
        }
        // Docs:         
        StringBuffer guiQuery = new StringBuffer("{\"qt\":[{\"ftext\":\"_id:")
                .append(doc.getObjectId(DocumentPojo._id_)).append("\"}]}");
        String url = doc.getString(DocumentPojo.displayUrl_, doc.getString(DocumentPojo.url_));
        String title = doc.getString(DocumentPojo.title_, url);
        alertEmailText.append("<li/>");
        alertEmailText.append(title);
        alertEmailText.append(" [");
        alertEmailText.append(doc.getDate(DocumentPojo.publishedDate_, doc.getDate(DocumentPojo.created_)));
        alertEmailText.append("]");
        alertEmailText.append(" (");
        alertEmailText.append("<a href=\"").append(rootUrl);
        try {
            alertEmailText.append("?query=");
            alertEmailText.append(URLEncoder.encode(guiQuery.toString(), "UTF-8"));
            alertEmailText.append("&communityIds=").append(
                    doc.getObjectId(DocumentPojo.communityId_, new ObjectId("4c927585d591d31d7b37097a")));
        } catch (Exception e) {
        } // (just carry on)
        alertEmailText.append("\">");
        alertEmailText.append("GUI</a>)");
        if ((null != url) && (url.startsWith("http"))) {
            alertEmailText.append(" (");
            alertEmailText.append("<a href=\"").append(url).append("\">");
            alertEmailText.append("External Link</a>)");
        }
        alertEmailText.append("\n");
    }
}