<commit_before>#include <iostream>
#include <client/dbclient.h>

extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include <compat-5.1.h>
#endif
};

#include "utils.h"
#include "common.h"

using namespace mongo;

extern void bson_to_lua(lua_State *L, const BSONObj &obj);

int cursor_create(lua_State *L, DBClientConnection *connection, const char *ns, const Query &query) {
    int resultcount = 1;

    try {
        DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
	auto_ptr<DBClientCursor> autocursor = connection->query(ns, query);
        *cursor = autocursor.get();
	autocursor.release();

        luaL_getmetatable(L, LUAMONGO_CURSOR);
        lua_setmetatable(L, -2);
    } catch (std::exception &e) {
        lua_pushnil(L);
        lua_pushfstring(L, LUAMONGO_ERR_QUERY_FAILED, e.what());
        resultcount = 2;
    }

    return resultcount;
}

static int cursor_next(lua_State *L) {
    void *ud = 0;

    ud = luaL_checkudata(L, 1, LUAMONGO_CURSOR);
    DBClientCursor *cursor = *((DBClientCursor **)ud);

    if (cursor->more()) {
	bson_to_lua(L, cursor->next());	
    } else {
	lua_pushnil(L);
    }

    return 1;
}

static int row_iterator(lua_State *L) {
    void *ud = 0;

    ud = luaL_checkudata(L, lua_upvalueindex(1), LUAMONGO_CURSOR);
    DBClientCursor *cursor = *((DBClientCursor **)ud);

    if (cursor->more()) {
	bson_to_lua(L, cursor->next());	
    } else {
	lua_pushnil(L);
    }

    return 1;
}

static int cursor_rows(lua_State *L) {
    lua_pushcclosure(L, row_iterator, 1);
    return 1;
}

static int cursor_gc(lua_State *L) {
    void *ud = 0;

    ud = luaL_checkudata(L, 1, LUAMONGO_CURSOR);
    DBClientCursor *cursor = *((DBClientCursor **)ud);

    delete cursor;

    return 0;
}

static int cursor_tostring(lua_State *L) {
    return 0;
}

int mongo_cursor_register(lua_State *L) {
    static const luaL_Reg cursor_methods[] = {
	{"next", cursor_next},
	{"rows", cursor_rows},
        {NULL, NULL}
    };

    static const luaL_Reg cursor_class_methods[] = {
        {NULL, NULL}
    };

    luaL_newmetatable(L, LUAMONGO_CURSOR);
    luaL_register(L, 0, cursor_methods);
    lua_pushvalue(L,-1);
    lua_setfield(L, -2, "__index");

    lua_pushcfunction(L, cursor_gc);<commit_msg>rename rows method to results<commit_after>#include <iostream>
#include <client/dbclient.h>

extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include <compat-5.1.h>
#endif
};

#include "utils.h"
#include "common.h"

using namespace mongo;

extern void bson_to_lua(lua_State *L, const BSONObj &obj);

int cursor_create(lua_State *L, DBClientConnection *connection, const char *ns, const Query &query) {
    int resultcount = 1;

    try {
        DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
	auto_ptr<DBClientCursor> autocursor = connection->query(ns, query);
        *cursor = autocursor.get();
	autocursor.release();

        luaL_getmetatable(L, LUAMONGO_CURSOR);
        lua_setmetatable(L, -2);
    } catch (std::exception &e) {
        lua_pushnil(L);
        lua_pushfstring(L, LUAMONGO_ERR_QUERY_FAILED, e.what());
        resultcount = 2;
    }

    return resultcount;
}

static int cursor_next(lua_State *L) {
    void *ud = 0;

    ud = luaL_checkudata(L, 1, LUAMONGO_CURSOR);
    DBClientCursor *cursor = *((DBClientCursor **)ud);

    if (cursor->more()) {
	bson_to_lua(L, cursor->next());	
    } else {
	lua_pushnil(L);
    }

    return 1;
}

static int result_iterator(lua_State *L) {
    void *ud = 0;

    ud = luaL_checkudata(L, lua_upvalueindex(1), LUAMONGO_CURSOR);
    DBClientCursor *cursor = *((DBClientCursor **)ud);

    if (cursor->more()) {
	bson_to_lua(L, cursor->next());	
    } else {
	lua_pushnil(L);
    }

    return 1;
}

static int cursor_results(lua_State *L) {
    lua_pushcclosure(L, result_iterator, 1);
    return 1;
}

static int cursor_gc(lua_State *L) {
    void *ud = 0;

    ud = luaL_checkudata(L, 1, LUAMONGO_CURSOR);
    DBClientCursor *cursor = *((DBClientCursor **)ud);

    delete cursor;

    return 0;
}

static int cursor_tostring(lua_State *L) {
    return 0;
}

int mongo_cursor_register(lua_State *L) {
    static const luaL_Reg cursor_methods[] = {
	{"next", cursor_next},
	{"results", cursor_results},
        {NULL, NULL}
    };

    static const luaL_Reg cursor_class_methods[] = {
        {NULL, NULL}
    };

    luaL_newmetatable(L, LUAMONGO_CURSOR);
    luaL_register(L, 0, cursor_methods);
    lua_pushvalue(L,-1);
    lua_setfield(L, -2, "__index");

    lua_pushcfunction(L, cursor_gc);
{'fraction_non_alphanumeric': 0.14107708553326292, 'fraction_numerical': 0.009714889123548046, 'mean_word_length': 3.6385896180215473, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '867578', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1785, 'n_words': 373}