Skip to content
This repository
tag: v0.1
Fetching contributors…

Cannot retrieve contributors at this time

file 279 lines (212 sloc) 6.275 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
#include <iostream>
#include <client/dbclient.h>
#include <client/gridfs.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);
extern void push_bsontype_table(lua_State* L, mongo::BSONType bsontype);
extern void lua_push_value(lua_State *L, const BSONElement &elem);

namespace {
    inline GridFile* userdata_to_gridfile(lua_State* L, int index) {
        void *ud = 0;

        ud = luaL_checkudata(L, index, LUAMONGO_GRIDFILE);
        GridFile *gridfile = *((GridFile **)ud);

        return gridfile;
    }
}

int gridfile_create(lua_State *L, GridFile gf) {
    GridFile **gridfile = (GridFile **)lua_newuserdata(L, sizeof(GridFile **));

    *gridfile = new GridFile(gf);

    luaL_getmetatable(L, LUAMONGO_GRIDFILE);
    lua_setmetatable(L, -2);

    return 1;
}

/*
* chunk, err = gridfile:chunk(chunk_num)
*/
static int gridfile_chunk(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);
    int num = luaL_checkint(L, 2);
    int resultcount = 1;

    try {
        CHUNK c = gridfile->getChunk(num);
        CHUNK *chunk_ptr = new CHUNK(c);

        CHUNK **chunk = (CHUNK **)lua_newuserdata(L, sizeof(CHUNK *));
        *chunk = chunk_ptr;

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

    return resultcount;
}

/*
* chunk_size = gridfile:chunk_size()
*/
static int gridfile_chunk_size(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    lua_pushinteger(L, gridfile->getChunkSize());

    return 1;
}

/*
* content_length = gridfile:content_length()
* __len
*/
static int gridfile_content_length(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    lua_pushnumber(L, gridfile->getContentLength());

    return 1;
}

/*
* bool = gridfile:exists()
*/
static int gridfile_exists(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    lua_pushboolean(L, gridfile->exists());

    return 1;
}

/*
*
*/

static int gridfile_field(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);
    const char *field_name = luaL_checkstring(L, 2);

    BSONElement elem = gridfile->getFileField(field_name);
    lua_push_value(L, elem);

    return 1;
}

/*
* filename_str = gridfile:filename()
*/
static int gridfile_filename(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    lua_pushstring(L, gridfile->getFilename().c_str());

    return 1;
}

/*
* md5_str = gridfile:md5()
*/
static int gridfile_md5(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    lua_pushstring(L, gridfile->getMD5().c_str());

    return 1;
}

/*
* metadata_table = gridfile:metadata()
*/
static int gridfile_metadata(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    bson_to_lua(L, gridfile->getMetadata());

    return 1;
}

/*
* num_chunks = gridfile:num_chunks()
*/
static int gridfile_num_chunks(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    lua_pushinteger(L, gridfile->getNumChunks());

    return 1;
}

/*
* date = gridfile:upload_date()
*/
static int gridfile_upload_date(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    Date_t upload_date = gridfile->getUploadDate();

    push_bsontype_table(L, mongo::Date);
    lua_pushnumber(L, upload_date);
    lua_rawseti(L, -2, 1);

    return 1;
}

/*
* success,err = gridfile:write(filename)
*/
static int gridfile_write(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);
    const char *where = luaL_checkstring(L, 2);

    try {
        gridfile->write(lua_tostring(L, 2));
    } catch (std::exception &e) {
        lua_pushboolean(L, 0);
        lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFILE, "write", e.what());
        return 2;
    }

    lua_pushboolean(L, 1);
    return 1;
}


/*
* string = gridfile:data()
*/
static int gridfile_data(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    std::stringstream data(std::stringstream::out | std::stringstream::binary);
    try {
        gridfile->write(data);
    } catch (std::exception &e) {
        lua_pushboolean(L, 0);
        lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFILE, "data", e.what());
        return 2;
    }

    std::string data_str = data.str();
    lua_pushlstring (L, data_str.c_str(), data_str.length());
    return 1;
}

/*
* __gc
*/
static int gridfile_gc(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    delete gridfile;

    return 0;
}

/*
* __tostring
*/
static int gridfile_tostring(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    lua_pushfstring(L, "%s: %p", LUAMONGO_GRIDFILE, gridfile);

    return 1;
}

int mongo_gridfile_register(lua_State *L) {
    static const luaL_Reg gridfile_methods[] = {
        {"chunk", gridfile_chunk},
        {"chunk_size", gridfile_chunk_size},
        {"content_length", gridfile_content_length},
        {"exists", gridfile_exists},
        {"field", gridfile_field},
        {"filename", gridfile_filename},
        {"md5", gridfile_md5},
        {"metadata", gridfile_metadata},
        {"num_chunks", gridfile_num_chunks},
        {"upload_date", gridfile_upload_date},
        {"write", gridfile_write},
        {"data", gridfile_data},
        {NULL, NULL}
    };

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

    luaL_newmetatable(L, LUAMONGO_GRIDFILE);
    luaL_register(L, 0, gridfile_methods);
    lua_pushvalue(L,-1);
    lua_setfield(L, -2, "__index");

    lua_pushcfunction(L, gridfile_gc);
    lua_setfield(L, -2, "__gc");

    lua_pushcfunction(L, gridfile_tostring);
    lua_setfield(L, -2, "__tostring");

    lua_pushcfunction(L, gridfile_content_length);
    lua_setfield(L, -2, "__len");

    luaL_register(L, LUAMONGO_GRIDFILE, gridfile_class_methods);

    return 1;
}
Something went wrong with that request. Please try again.