mongo_clients
简介
通过格式化配置创建mongo数据库的操作客户端对象
需要预先加载格式化配置的mongo数据库(组织方式以及访问方式);
访问方式有两种:一是通过pkg.mongoc直接操作数据库,需要指明一种是通过pkg.remote_mongoc间接数据库;
组织方式主要是:数据库有哪些表;哪些字段需要创建唯一索引、哪些字段需要创建普通索引、哪些字段需要参与校验和计算等等;
组件接口
MongoClient.gs
通过pkg.mongoc操作mongo数据库的客户端对象
| 函数原型 | 函数作用 |
|---|---|
| string desc() | 获取描述信息 |
| string get_name() | 获取本客户端的名称 |
| string get_db() | 获取所操作的数据库的名字 |
| array get_checksum_fields(string coll_name) | 获取指定名称的集合的参与校验和计算的字段列表 |
| bool start(MongoClientsConfig db_config) | 开始服务 |
| void stop() | 停止服务 |
| bool restart() | 重启服务 |
| object get_or_create_collection(string name, mixed collection_config = nil, array checksum_fields = nil) | 获取指定名称的集合的操作对象,如果不存在则创建 |
| object get_coll(string name) | 获取指定名称的集合的操作对象 |
| object get_gridfs(string name) | 获取指定名称的gridfs的操作对象 |
| mixed transaction(function fn, ...) | 执行一个事务 |
| object client() | 获取本对象封装的数据库客户端连接对象 |
| bool is_master() | 检查接受命令的这个节点是不是主节点 |
| bool wait_for_writable() | 等待当前节点可写 |
mongo_clients.gs
| 函数原型 | 函数作用 |
|---|---|
| void setup(map mongoc_cfg, array dbs = nil) | 模块初始化并启动服务 |
| void restart() | 重启mongodb服务 |
| void iterate_dbs(function fn) | 遍历所有数据库配置 |
| MongoClientsConfig new_db_config(map db_info, map collections, map para) | 根据数据库配置信息生成一个最终的数据库配置 |
| string add_db(mixed db_config) | 加载一个格式化的数据库配置 |
| MongoClientsConfig get_db_config(string name) | 获取指定名称的数据库配置 |
| object new_client_from_config(mixed db_config) | 通过一个数据库的格式化配置创建一个操作客户端对象 |
| object new_client(map db_info, map collections, map para = ) | 通过一个数据库的格式化配置创建一个通过pkg.mongoc操作的客户端对象 |
| object create_client_by_config(MongoClientsConfig db_config) | 通过一个数据库的格式化配置创建客户端对象 |
| void name_client(string name, object client) | 为一个操作对象命名 |
| object find_client(string name) | 根据名称找到操作对象 |
| void dumps() | 输出调试信息 |
| void close_clients() | 关闭所有mongo客户端 |
| map parse_uri(string uri) | 解析uri |
类
MongoClientsConfig
继承自 MongoClientsConfigBase, MongoClientsConfigCollections
数据库配置
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|
成员方法
| 函数原型 | 函数作用 |
|---|
MongoClientsConfigBase
数据库基础配置
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| name | string | nil | 可选 | 数据库基础配置名称 |
| db | string | nil | 可选 | 数据库的实际物理名称 |
| uri | string | nil | 可选 | 数据库连接信息 |
| host | string | nil | 可选 | 数据库主机地址 |
| port | mixed | nil | 可选 | 数据库主机端口 可以是int型或string型 |
| auth | map | nil | 可选 | 数据库连接验证信息 |
| opts | map | nil | 可选 | 数据库连接的额外选项 |
| collection_checksum | map | nil | 可选 | 集合上参与校验和计算的字段列表 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| void merge(map target) | 合并数据库基础配置和额外配置 |
| array get_checksum_fields(string collection_name) | 获取集合上参与校验和计算的字段列表 |
MongoClientsConfigCollections
数据库集合配置词典class
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| collections | map | nil | 可选 | 数据库集合配置 key: 集合名称 value: 集合配置MongoClientsConfigCollection |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| void iterate_collection(function func) | 遍历所有集合配置 |
MongoClientsConfigCollection
数据库集合配置
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| name | string | nil | 可选 | 集合名称 |
| gridfs | map | nil | 可选 | gridFS设置词典 |
| index_list | array | nil | 可选 | 索引列表 [ [ 索引名称, 索引字段列表, 是否唯一, 索引过期时长(单位:秒) ] ] |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| void iterate_index(function func) | 遍历索引 |
IndexBuilder
索引创建器
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| name | string | nil | 可选 | 索引名称 |
| unique | bool | false | 可选 | 是不是unique类型的索引 |
| expire_after_seconds | mixed | nil | 可选 | 索引过期时长(单位:秒) |
| key_list | array | nil | 可选 | 索引包含的字段列表 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| bool equal(IndexBuilder other) | 判断两个索引是否相同 |
| string desc() | 获取索引描述 |
| bool drop_index(object coll_ob) | 删除索引 |
| bool create_index(object coll_ob) | 创建索引 |
样例
#pragma parallel
import gs.lang.*;
import gs.util.*;
import pkg.mongo_clients;
public void sample()
{
map mongoc_cfg = {
"thread_num" : 2,
};
array dbs = [
];
map db_config = {
"db" : {
"name" : "base",
"db" : "base",
"host" : "127.0.0.1",
"port" : 27017,
"auth" : {},
"collection_checksum" : {
"users" : ["account_id"],
},
},
"collections" : {
"users" : {
"account_id" : false, // =false, normal索引
"account" : false, // =false, normal索引
"rid" : true, // =true, unique索引
"name" : false, // =false, normal索引
"__unique_index" : [ // 其他unique索引
[ "account", "rid" ],
[ "account_id", "rid" ]
],
"__normal_index" : [ // 其他normal索引
[ "rid", "name" ]
]
},
},
"para" : {
"name" : "base@local", // 比"db"字段中的name优先
"db" : "local_base", // 比"db"字段中的"db"优先
}
};
dbs.push_back(db_config);
// 启动服务
mongo_clients=>setup(mongoc_cfg, dbs);
// 通过名称获取操作对象
object local_client = mongo_clients.find_client("base@local");
printf("name=%M\n", local_client.get_name());
printf("db=%M\n", local_client.get_db());
// 获取集合操作对象
object coll_users = local_client.get_coll("users");
printf("coll_users: %M\n", coll_users);
// 通过集合对象(pkg.mongoc.async_collection)操作数据
//
}