Esempio n. 1
0
def geocodeFromCacheById(cacheId, inMemoryOnly=None):
    if inMemoryOnly is None:
        inMemoryOnly = False

    if 'importance_rating' in cacheId:
        importanceRating = cacheId['importance_rating']
        cacheId = dict(cacheId) # don't modify what was passed in.
        del cacheId['importance_rating'] # remove so doesn't conflict with mongoDB query.
    else:
        importanceRating = None

    if isinstance(cacheId, list):
        success = False
        for item in cacheId:
            if isIntendedForDirectUse(GeocodeResultAbstract.getProviderIdFromCacheId(item)):
                cacheId = item
                success = True
                break

        if not success:
            logger.error('Could not find useful ID from ID list %s' % (unicode(cacheId)))
            return None

    if isIntendedForDirectUse(GeocodeResultAbstract.getProviderIdFromCacheId(cacheId)):
        tup = GeocodeResultAbstract.buildTupleFromCacheId(cacheId)
        returnVal = inMemoryCacheGeocodeData.get(tup,None)

        if returnVal is None:
            if not inMemoryOnly:
                db = getDatabase()
                assert isinstance(db, Database)
                result = db.place.find_one({'_id' : cacheId})
                if result is None:
                    logger.warn('Could not find place cache ID in database: %s' % unicode(cacheId))
                    return None
            else:
                return None

            returnVal = buildGeocodeResult(result['place_data'], cacheId['providerId'], importanceRating)

        # Always update, this will move the item to the back of the ordered dict
        # meaning we have a 'least recently used' cache.
        if returnVal is not None:
            inMemoryCacheGeocodeData[tup] = returnVal

        return returnVal
    else:
        geocode = GeocodeResultAbstract.getGnsByPlaceId(GeocodeResultAbstract.getPlaceIdFromCacheId(cacheId))
        if geocode is None:
            logger.error('Failed to retrieve GNS data with cache ID: %s' % unicode(cacheId))

        return geocode
Esempio n. 2
0
            def addTemporalEntryForCurrentUser(follower):
                timeId = getTimeIdFromTimestamp(startTime,
                                                Configuration.TEMPORAL_STEP,
                                                getEpochMs())

                userCacheIds = user.location_geocode.all_geocode_results_cache_id
                followerGeocodeResults = follower.location_geocode.all_geocode_results

                for userCacheId in userCacheIds:
                    userPlaceId = GeocodeResultAbstract.getPlaceIdFromCacheId(
                        userCacheId)
                    userProviderId = GeocodeResultAbstract.getProviderIdFromCacheId(
                        userCacheId)

                    for followerGeocodeResult in followerGeocodeResults:
                        followerPlaceId = followerGeocodeResult.place_id
                        followerProviderId = followerGeocodeResult.provider_id
                        followerPlaceType = followerGeocodeResult.place_type

                        instance.addTemporalEntry(temporalCollection, timeId,
                                                  userProviderId, userPlaceId,
                                                  followerProviderId,
                                                  followerPlaceId,
                                                  followerPlaceType)