forked from moai/luamongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_gridfilebuilder.cpp
169 lines (139 loc) · 4.86 KB
/
mongo_gridfilebuilder.cpp
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
#include <iostream>
#include <client/dbclient.h>
#include "mongo_cxx_extension.h"
#include "utils.h"
#include "common.h"
// FIXME: client pointer is keep, review Lua binding in order to avoid
// unexpected garbage collection of DBClientBase
using namespace mongo;
extern void lua_to_bson(lua_State *L, int stackpos, BSONObj &obj);
extern void bson_to_lua(lua_State *L, const BSONObj &obj);
extern int gridfile_create(lua_State *L, GridFile gf);
extern DBClientBase* userdata_to_dbclient(lua_State *L, int stackpos);
namespace {
inline mongo_cxx_extension::GridFileBuilder* userdata_to_gridfilebuilder(lua_State* L,
int index) {
void *ud = 0;
ud = luaL_checkudata(L, index, LUAMONGO_GRIDFILEBUILDER);
mongo_cxx_extension::GridFileBuilder *gridfilebuilder;
gridfilebuilder = *((mongo_cxx_extension::GridFileBuilder **)ud);
return gridfilebuilder;
}
} // anonymous namespace
/*
* builder, err = mongo.GridFileBuilder.New(connection, dbname[, chunksize[, prefix]])
*/
static int gridfilebuilder_new(lua_State *L) {
int n = lua_gettop(L);
int resultcount = 1;
try {
DBClientBase *connection = userdata_to_dbclient(L, 1);
const char *dbname = lua_tostring(L, 2);
mongo_cxx_extension::GridFileBuilder **builder;
builder = (mongo_cxx_extension::GridFileBuilder **)
lua_newuserdata(L, sizeof(mongo_cxx_extension::GridFileBuilder *));
if (n >= 4) {
unsigned int chunksize = static_cast<unsigned int>(luaL_checkint(L, 3));
const char *prefix = luaL_checkstring(L, 4);
*builder = new mongo_cxx_extension::GridFileBuilder(connection,
dbname,
chunksize,
prefix);
} else if (n == 3) {
unsigned int chunksize = static_cast<unsigned int>(luaL_checkint(L, 3));
*builder = new mongo_cxx_extension::GridFileBuilder(connection,
dbname, chunksize);
} else {
*builder = new mongo_cxx_extension::GridFileBuilder(connection, dbname);
}
luaL_getmetatable(L, LUAMONGO_GRIDFILEBUILDER);
lua_setmetatable(L, -2);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_CONNECTION_FAILED, e.what());
resultcount = 2;
}
return resultcount;
}
/*
* ok, err = builder:append(data_string)
*/
static int gridfilebuilder_append(lua_State *L) {
mongo_cxx_extension::GridFileBuilder *builder;
builder = userdata_to_gridfilebuilder(L, 1);
int resultcount = 1;
try {
size_t length = 0;
const char *data = luaL_checklstring(L, 2, &length);
builder->appendChunk(data, length);
lua_pushboolean(L, 1);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFILEBUILDER,
"append", e.what());
resultcount = 2;
}
return resultcount;
}
/*
* bson, err = builder:build(remote_file[, content_type])
*/
static int gridfilebuilder_build(lua_State *L) {
int resultcount = 1;
mongo_cxx_extension::GridFileBuilder *builder;
builder = userdata_to_gridfilebuilder(L, 1);
const char *remote = luaL_checkstring(L, 2);
const char *content_type = luaL_optstring(L, 3, "");
try {
BSONObj res = builder->buildFile(remote, content_type);
bson_to_lua(L, res);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFILEBUILDER,
"build", e.what());
resultcount = 2;
}
return resultcount;
}
/*
* __gc
*/
static int gridfilebuilder_gc(lua_State *L) {
mongo_cxx_extension::GridFileBuilder *builder;
builder = userdata_to_gridfilebuilder(L, 1);
delete builder;
return 0;
}
/*
* __tostring
*/
static int gridfilebuilder_tostring(lua_State *L) {
mongo_cxx_extension::GridFileBuilder *builder;
builder = userdata_to_gridfilebuilder(L, 1);
lua_pushfstring(L, "%s: %p", LUAMONGO_GRIDFILEBUILDER, builder);
return 1;
}
int mongo_gridfilebuilder_register(lua_State *L) {
static const luaL_Reg gridfilebuilder_methods[] = {
{"append", gridfilebuilder_append},
{"write", gridfilebuilder_append},
{"build", gridfilebuilder_build},
{NULL, NULL}
};
static const luaL_Reg gridfilebuilder_class_methods[] = {
{"New", gridfilebuilder_new},
{NULL, NULL}
};
luaL_newmetatable(L, LUAMONGO_GRIDFILEBUILDER);
//luaL_register(L, 0, gridfs_methods);
luaL_setfuncs(L, gridfilebuilder_methods, 0);
lua_pushvalue(L,-1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, gridfilebuilder_gc);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, gridfilebuilder_tostring);
lua_setfield(L, -2, "__tostring");
lua_pop(L,1);
luaL_newlib(L, gridfilebuilder_class_methods);
return 1;
}