def db_cursor_next(res_id, collection_name): parse_get_json() result = {} batch_size = current_app.config['CURSOR_BATCH_SIZE'] with UseResId(res_id, db=get_keepalive_db()) as db: coll = db[collection_name] cursor_id = int(request.json.get('cursor_id')) retrieved = request.json.get('retrieved', 0) drain_cursor = request.json.get('drain_cursor', False) batch_size = -1 if drain_cursor else current_app.config['CURSOR_BATCH_SIZE'] cursor = recreate_cursor(coll, cursor_id, retrieved, batch_size) try: result['result'] = [] for i in range(batch_size): try: result['result'].append(cursor.next()) except StopIteration: result['empty_cursor'] = True break except OperationFailure as e: return MWSServerError(400, 'Cursor not found') # kill cursor on server if all results are returned if result.get('empty_cursor'): kill_cursor(coll, long(cursor_id)) return to_json(result)
def db_collection_find(res_id, collection_name): parse_get_json() result = {} batch_size = current_app.config['CURSOR_BATCH_SIZE'] with UseResId(res_id, db=get_keepalive_db()) as db: limit = request.json.get('limit', 0) coll = db[collection_name] query = request.json.get('query') projection = request.json.get('projection') skip = request.json.get('skip', 0) sort = request.json.get('sort', {}) sort = sort.items() cursor = coll.find(spec=query, fields=projection, skip=skip, limit=limit) cursor.batch_size(batch_size) if len(sort) > 0: cursor.sort(sort) # count is only available before cursor is read so we include it # in the first response result['count'] = cursor.count(with_limit_and_skip=True) count = result['count'] num_to_return = min(limit, batch_size) if limit else batch_size try: result['result'] = [] for i in range(num_to_return): try: result['result'].append(cursor.next()) except StopIteration: break except OperationFailure as e: return MWSServerError(400, 'Cursor not found') # cursor_id is too big as a number, use a string instead result['cursor_id'] = str(cursor.cursor_id) # close the Cursor object, but keep the cursor alive on the server del cursor return to_json(result)