# Path: virtool/types.py



from contextlib import asynccontextmanager
from typing import Any, Dict, Optional, Sequence, Set, Union
from motor.motor_asyncio import AsyncIOMotorCollection
from pymongo import InsertOne, UpdateOne
from virtool.types import Projection
import virtool.utils
    async def flush(self):
        """
        Flush the buffered write requests to MongoDB.

        """
        if self._buffer:
            await self.collection.bulk_write(self._buffer)
            self._buffer = list()


@asynccontextmanager
async def buffered_bulk_writer(collection, batch_size=100):
    """
    A context manager for bulk writing to MongoDB.

    Returns a :class:``BufferedBulkWriter`` object. Automatically flushes the buffer
    when the context manager exits.

    :param collection: the MongoDB collection to write against
    :param batch_size: the number of requests to be sent in each bulk operation

    """
    writer = BufferedBulkWriter(collection, batch_size)

    try:
        yield writer
    finally:
        await writer.flush()



# Path: virtool/types.py



# Path: virtool/db/utils.py
from contextlib import asynccontextmanager
from typing import Any, Dict, Optional, Sequence, Set, Union
from motor.motor_asyncio import AsyncIOMotorCollection
from pymongo import InsertOne, UpdateOne
from virtool.types import Projection
import virtool.utils

    async def flush(self):
        """
        Flush the buffered write requests to MongoDB.

        """
        if self._buffer:
            await self.collection.bulk_write(self._buffer)
            self._buffer = list()


@asynccontextmanager
async def buffered_bulk_writer(collection, batch_size=100):
    """
    A context manager for bulk writing to MongoDB.

    Returns a :class:``BufferedBulkWriter`` object. Automatically flushes the buffer
    when the context manager exits.

    :param collection: the MongoDB collection to write against
    :param batch_size: the number of requests to be sent in each bulk operation

    """
    writer = BufferedBulkWriter(collection, batch_size)

    try:
        yield writer
    finally:
        await writer.flush()



def apply_projection(document: Dict, projection: Projection):