mardi 4 juillet 2017

mongocxx::cursor stores only 100 records, C++11

I'm running a query on a table containing more than 100 records.

mongocxx::cursor cursor = collection.find(document{} << finalize, opts);

In real life, my application has to generate this query on some location, and pass the cursor to a "reader" class. This is required to manage different databases (MySql, MsSql, MongoDB, etc.)

class TMongoReader :
{
public:
    TMongoReader(mongocxx::cursor & p_Cursor)
        : m_Cursor(std::move(p_Cursor)), m_It(m_Cursor.begin()) {}

    bool Next()
    {
        if (m_It == m_Cursor.end()) {   //  no more data to process
            return false;
        }
        //  get the next item
        m_Obj = *m_It;
        //  increment the cursor iterator
        ++m_It;
        return true;    //  OK, continue processing data
    }

    bsoncxx::document::view & Obj() { return m_Obj; }

protected:
    mongocxx::cursor m_Cursor;
    mongocxx::cursor::iterator m_It;

    bsoncxx::document::view m_Obj;
};

Surprisingly, when using the reader class to scan the records, I get a crash at record #101:

    int32_t n = 0;
    TMongoReader reader(cursor);
    while(reader.Next())
    {
        cout << "Record[" << ++n << "]: " << bsoncxx::to_json(reader.Obj()) << endl;
    }

It seems that the driver loads the records by batch of 100, but I can't figure out why the cursor m_Cursor and the iterator m_It are corrupt after that. Any idea ?
Thanks a lot in advance.

Aucun commentaire:

Enregistrer un commentaire