Exemplo n.º 1
0
def _find_by_common(collection, fields=None, source=None, message_id=None,
                    start_date=None, end_date=None, ffrom=None, count=50,
                    page=0, **kw):
    """A `find` query handling system for the `email` collection.

    `source` should be either 'lucene' or 'maildir'.

    `message_id` should be a string that looks like this:

        <24283319.1075842034234.JavaMail.evans@thyme>

    `start_date` and `end_date` are milliseconds since January 1, 1970. If a
    string is provided for either, a conversion will take place. It can cover
    just about whatever you throw at it, but read the documentation for
    `gen_date_args` for more detail.

    The `count` flag defaults to 50, but you can set it to 0 to have no limit
    applied.

    Paging starts counting pages at 0.
    """
    query_dict = copy.deepcopy(kw)

    # At least one of these is required.
    if not source and not ffrom and not message_id:
        return None

    # Arrange fields to optimize index usage
    if source:
        query_dict['source'] = source
    if message_id:
        query_dict['message_id'] = message_id

    date_args = gen_date_args(start_date, end_date)
    if len(date_args.keys()) > 0:
        query_dict = {
            'date': date_args,
        }

    # Support less prominent indexes
    if ffrom:
        query_dict['ffrom'] = ffrom

    # run query and apply paging 
    db_cursor = collection.find(query_dict)
    db_cursor.limit(count) # limit(0) = no limit
    db_cursor.skip(page * count)
    
    return db_cursor
Exemplo n.º 2
0
def find_samples(db, some_string=None, start_date=None, end_date=None,
                 count=50, page=0):
    """An example of how a Mongo Query might look for the sample project.
    """
    query_dict = {}

    if some_string:
        query_dict['some_string'] = some_string

    # Date arguments have some complexity. 
    date_args = gen_date_args(start_date, end_date)
    if len(date_args.keys()) > 0:
        query_dict = {
            'timestamp': date_args,
        }

    # Run the query on Mongo and get a cursor
    db_cursor = collection.find(query_dict)
    db_cursor.limit(count) # limit(0) = no limit
    db_cursor.skip(page * count)

    # The cursor is iterable for a caller, so just hand it over
    return db_cursor