def find(self, type_id, unit_key): """ Find entries in the content catalog using the specified unit type_id and unit_key. The catalog may contain more than one entry matching the locator for a given content source. In this case, only the newest entry for each source is included in the result set. :param type_id: The unit type ID. :type type_id: str :param unit_key: The unit key. :type unit_key: dict :return: A list of matching entries. :rtype: list """ collection = ContentCatalog.get_collection() locator = ContentCatalog.get_locator(type_id, unit_key) query = { 'locator': locator, 'expiration': { '$gte': ContentCatalog.get_expiration(0) } } newest_by_source = {} for entry in collection.find(query, sort=[('_id', ASCENDING)]): newest_by_source[entry['source_id']] = entry return newest_by_source.values()
def purge_expired(self, grace_period=GRACE_PERIOD): """ Purge (delete) expired entries from the content catalog belonging to the specified content source by ID. :param grace_period: The grace period in seconds. The grace_period defines how long an entry is permitted to remain in the catalog after it has expired. The default is 1 hour. :type grace_period: int """ collection = ContentCatalog.get_collection() now = ContentCatalog.get_expiration(0) timestamp = now - grace_period query = {'expiration': {'$lt': timestamp}} collection.remove(query, safe=True)
def has_entries(self, source_id): """ Get whether the specified content source has entries in the catalog. :param source_id: A content source ID. :type source_id: str :return: True if has entries. :rtype: bool """ collection = ContentCatalog.get_collection() query = { 'source_id': source_id, 'expiration': {'$gte': ContentCatalog.get_expiration(0)} } cursor = collection.find(query) return cursor.count() > 0
def find(self, type_id, unit_key): """ Find entries in the content catalog using the specified unit type_id and unit_key. The catalog may contain more than one entry matching the locator for a given content source. In this case, only the newest entry for each source is included in the result set. :param type_id: The unit type ID. :type type_id: str :param unit_key: The unit key. :type unit_key: dict :return: A list of matching entries. :rtype: list """ collection = ContentCatalog.get_collection() locator = ContentCatalog.get_locator(type_id, unit_key) query = { 'locator': locator, 'expiration': {'$gte': ContentCatalog.get_expiration(0)} } newest_by_source = {} for entry in collection.find(query, sort=[('_id', ASCENDING)]): newest_by_source[entry['source_id']] = entry return newest_by_source.values()