Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
F
freeswitch
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
张华
freeswitch
Commits
30917dd1
提交
30917dd1
authored
2月 11, 2015
作者:
Chris Rienzo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
FS-7265 #resolve #comment [mod_mongo] add mongo_find_n API
上级
592d0737
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
106 行增加
和
8 行删除
+106
-8
mod_mongo.c
src/mod/applications/mod_mongo/mod_mongo.c
+106
-8
没有找到文件。
src/mod/applications/mod_mongo/mod_mongo.c
浏览文件 @
30917dd1
/*
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-201
4
, Anthony Minessale II <anthm@freeswitch.org>
* Copyright (C) 2005-201
5
, Anthony Minessale II <anthm@freeswitch.org>
*
*
* Version: MPL 1.1
* Version: MPL 1.1
*
*
...
@@ -44,6 +44,7 @@
...
@@ -44,6 +44,7 @@
#define DELIMITER ';'
#define DELIMITER ';'
#define FIND_ONE_SYNTAX "mongo_find_one ns; query; fields; options"
#define FIND_ONE_SYNTAX "mongo_find_one ns; query; fields; options"
#define FIND_N_SYNTAX "mongo_find_n ns; query; fields; options; n"
#define MAPREDUCE_SYNTAX "mongo_mapreduce ns; query"
#define MAPREDUCE_SYNTAX "mongo_mapreduce ns; query"
SWITCH_MODULE_LOAD_FUNCTION
(
mod_mongo_load
);
SWITCH_MODULE_LOAD_FUNCTION
(
mod_mongo_load
);
...
@@ -184,6 +185,102 @@ SWITCH_STANDARD_API(mongo_mapreduce_function)
...
@@ -184,6 +185,102 @@ SWITCH_STANDARD_API(mongo_mapreduce_function)
return
status
;
return
status
;
}
}
SWITCH_STANDARD_API
(
mongo_find_n_function
)
{
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
char
*
db
=
NULL
,
*
collection
=
NULL
,
*
json_query
=
NULL
,
*
json_fields
=
NULL
,
*
query_options_str
=
NULL
;
int
query_options
=
0
;
int
n
=
1
;
db
=
strdup
(
cmd
);
switch_assert
(
db
!=
NULL
);
if
((
collection
=
strchr
(
db
,
'.'
)))
{
*
collection
++
=
'\0'
;
if
((
json_query
=
strchr
(
collection
,
DELIMITER
)))
{
*
json_query
++
=
'\0'
;
if
((
json_fields
=
strchr
(
json_query
,
DELIMITER
)))
{
*
json_fields
++
=
'\0'
;
if
((
query_options_str
=
strchr
(
json_fields
,
DELIMITER
)))
{
char
*
n_str
;
*
query_options_str
++
=
'\0'
;
if
(
!
zstr
(
query_options_str
))
{
query_options
=
parse_query_options
(
query_options_str
);
}
if
((
n_str
=
strchr
(
query_options_str
,
DELIMITER
)))
{
*
n_str
++
=
'\0'
;
if
(
switch_is_number
(
n_str
))
{
n
=
atoi
(
n_str
);
if
(
n
<
1
)
{
n
=
1
;
}
}
}
}
}
}
}
if
(
!
zstr
(
db
)
&&
!
zstr
(
collection
)
&&
!
zstr
(
json_query
)
&&
!
zstr
(
json_fields
))
{
bson_error_t
error
;
mongoc_client_t
*
conn
=
get_connection
();
if
(
conn
)
{
mongoc_collection_t
*
col
=
mongoc_client_get_collection
(
conn
,
db
,
collection
);
if
(
col
)
{
bson_t
*
query
=
bson_new_from_json
((
uint8_t
*
)
json_query
,
strlen
(
json_query
),
&
error
);
bson_t
*
fields
=
bson_new_from_json
((
uint8_t
*
)
json_fields
,
strlen
(
json_fields
),
&
error
);
if
(
query
&&
fields
)
{
/* send query */
mongoc_cursor_t
*
cursor
=
mongoc_collection_find
(
col
,
query_options
,
0
,
n
,
0
,
query
,
fields
,
NULL
);
if
(
cursor
&&
!
mongoc_cursor_error
(
cursor
,
&
error
))
{
/* get results from cursor */
const
bson_t
*
result
;
stream
->
write_function
(
stream
,
"-OK
\n
["
);
if
(
mongoc_cursor_more
(
cursor
)
&&
mongoc_cursor_next
(
cursor
,
&
result
))
{
char
*
json_result
;
json_result
=
bson_as_json
(
result
,
NULL
);
stream
->
write_function
(
stream
,
"%s"
,
json_result
);
bson_free
(
json_result
);
}
while
(
mongoc_cursor_more
(
cursor
)
&&
mongoc_cursor_next
(
cursor
,
&
result
))
{
char
*
json_result
;
json_result
=
bson_as_json
(
result
,
NULL
);
stream
->
write_function
(
stream
,
",%s"
,
json_result
);
bson_free
(
json_result
);
}
stream
->
write_function
(
stream
,
"]
\n
"
);
}
else
{
stream
->
write_function
(
stream
,
"-ERR
\n
query failed!
\n
"
);
}
if
(
cursor
)
{
mongoc_cursor_destroy
(
cursor
);
}
}
else
{
stream
->
write_function
(
stream
,
"-ERR
\n
missing query or fields!
\n
%s
\n
"
,
FIND_ONE_SYNTAX
);
}
if
(
query
)
{
bson_destroy
(
query
);
}
if
(
fields
)
{
bson_destroy
(
fields
);
}
mongoc_collection_destroy
(
col
);
}
else
{
stream
->
write_function
(
stream
,
"-ERR
\n
unknown collection: %s
\n
"
,
collection
);
}
connection_done
(
conn
);
}
else
{
stream
->
write_function
(
stream
,
"-ERR
\n
failed to get connection!
\n
"
);
}
}
else
{
stream
->
write_function
(
stream
,
"-ERR
\n
%s
\n
"
,
FIND_N_SYNTAX
);
}
switch_safe_free
(
db
);
return
status
;
}
SWITCH_STANDARD_API
(
mongo_find_one_function
)
SWITCH_STANDARD_API
(
mongo_find_one_function
)
{
{
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
...
@@ -218,21 +315,21 @@ SWITCH_STANDARD_API(mongo_find_one_function)
...
@@ -218,21 +315,21 @@ SWITCH_STANDARD_API(mongo_find_one_function)
bson_t
*
query
=
bson_new_from_json
((
uint8_t
*
)
json_query
,
strlen
(
json_query
),
&
error
);
bson_t
*
query
=
bson_new_from_json
((
uint8_t
*
)
json_query
,
strlen
(
json_query
),
&
error
);
bson_t
*
fields
=
bson_new_from_json
((
uint8_t
*
)
json_fields
,
strlen
(
json_fields
),
&
error
);
bson_t
*
fields
=
bson_new_from_json
((
uint8_t
*
)
json_fields
,
strlen
(
json_fields
),
&
error
);
if
(
query
&&
fields
)
{
if
(
query
&&
fields
)
{
int
ok
=
0
;
/* send query */
/* send query */
mongoc_cursor_t
*
cursor
=
mongoc_collection_find
(
col
,
query_options
,
0
,
1
,
0
,
query
,
fields
,
NULL
);
mongoc_cursor_t
*
cursor
=
mongoc_collection_find
(
col
,
query_options
,
0
,
1
,
0
,
query
,
fields
,
NULL
);
if
(
cursor
&&
mongoc_cursor_more
(
cursor
)
&&
!
mongoc_cursor_error
(
cursor
,
&
error
))
{
if
(
cursor
&&
!
mongoc_cursor_error
(
cursor
,
&
error
))
{
/* get result from cursor */
/* get result from cursor */
const
bson_t
*
result
;
const
bson_t
*
result
;
if
(
mongoc_cursor_next
(
cursor
,
&
result
))
{
if
(
mongoc_cursor_
more
(
cursor
)
&&
mongoc_cursor_
next
(
cursor
,
&
result
))
{
char
*
json_result
;
char
*
json_result
;
json_result
=
bson_as_json
(
result
,
NULL
);
json_result
=
bson_as_json
(
result
,
NULL
);
stream
->
write_function
(
stream
,
"-OK
\n
%s
\n
"
,
json_result
);
stream
->
write_function
(
stream
,
"-OK
\n
%s
\n
"
,
json_result
);
bson_free
(
json_result
);
bson_free
(
json_result
);
ok
=
1
;
}
else
{
/* empty set */
stream
->
write_function
(
stream
,
"-OK
\n
{}
\n
"
);
}
}
}
}
else
{
if
(
!
ok
)
{
stream
->
write_function
(
stream
,
"-ERR
\n
query failed!
\n
"
);
stream
->
write_function
(
stream
,
"-ERR
\n
query failed!
\n
"
);
}
}
if
(
cursor
)
{
if
(
cursor
)
{
...
@@ -256,7 +353,7 @@ SWITCH_STANDARD_API(mongo_find_one_function)
...
@@ -256,7 +353,7 @@ SWITCH_STANDARD_API(mongo_find_one_function)
stream
->
write_function
(
stream
,
"-ERR
\n
failed to get connection!
\n
"
);
stream
->
write_function
(
stream
,
"-ERR
\n
failed to get connection!
\n
"
);
}
}
}
else
{
}
else
{
stream
->
write_function
(
stream
,
"-ERR
\n
%s
\n
"
,
FIND_ONE_SYNTAX
);
stream
->
write_function
(
stream
,
"-ERR
\n
%s
\n
"
,
FIND_ONE_SYNTAX
);
}
}
switch_safe_free
(
db
);
switch_safe_free
(
db
);
...
@@ -349,6 +446,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_mongo_load)
...
@@ -349,6 +446,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_mongo_load)
}
}
SWITCH_ADD_API
(
api_interface
,
"mongo_find_one"
,
"findOne"
,
mongo_find_one_function
,
FIND_ONE_SYNTAX
);
SWITCH_ADD_API
(
api_interface
,
"mongo_find_one"
,
"findOne"
,
mongo_find_one_function
,
FIND_ONE_SYNTAX
);
SWITCH_ADD_API
(
api_interface
,
"mongo_find_n"
,
"find"
,
mongo_find_n_function
,
FIND_N_SYNTAX
);
SWITCH_ADD_API
(
api_interface
,
"mongo_mapreduce"
,
"Map/Reduce"
,
mongo_mapreduce_function
,
MAPREDUCE_SYNTAX
);
SWITCH_ADD_API
(
api_interface
,
"mongo_mapreduce"
,
"Map/Reduce"
,
mongo_mapreduce_function
,
MAPREDUCE_SYNTAX
);
return
SWITCH_STATUS_SUCCESS
;
return
SWITCH_STATUS_SUCCESS
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论