跳到主要内容

db_agent

简介

为接入不同的数据库服务提供统一操作接口,默认支持以下几种:

操作基于dba server的mongodb/mysql

直接操作的sqlite/reids

组件接口

AgentDbaBase.gs

基于dba server的数据库服务的基础组件

函数原型函数作用
mixed send_request(string cmd, array args, map extend_info = nil, int timeout = 45)发送操作请求并且等待请求结果
void post_request(string cmd, array args, map extend_info = nil)发送操作请求并且不等待请求结果(异步操作)

AgentDbaMongo.gs

基于dba server的mongodb数据库服务的代理操作对象;本对象包含AgentDbaBase组件

一些写操作失败后,将把失败的操作备份到指定的sql3文件中再自动进行恢复

函数原型函数作用
bool ping(string path = nil)询问dba server,某个表所在的数据库是否可用
int count_content(string path, mixed condition = nil)统计指定表中满足条件的记录数量
int create_content(string path, string name, map content)创建一条记录
int delete_content(string path, string name, map extend_info = nil)删除一行记录
mixed get_catalog(string path, mixed condition, int start_pos = 0, int count = 1000)读取符合条件的数据的主键列表(目录)
mixed lock_content(string path, string name)通过修改runtime_holder字段值锁定一条记录
mixed unlock_content(string path, string name)通过修改runtime_holder字段值解锁一条记录
mixed query_runtime_holder(string path, string name)通过查询runtime_holder字段获得当前锁定记录的服务器信息
mixed read_content(string path, string name)读取一条记录
mixed read_group(string path, mixed cond = nil, map projection_fields = nil, int start_pos = 0, int count = 1000, map sort = nil)读取一组满足指定条件的记录
int write_content(string path, string name, map content, mixed cond = nil, map extend_info = nil)更新一条记录
int replace_content(string path, string name, map content, mixed cond = nil, map extend_info = nil)覆盖一条记录
mixed aggregate(string path, array pipeline, map opts = )在指定的数据集上执行聚合操作(具体参考mongdb的聚合操作)
mixed drop_all_tables()删除所有数据集合
mixed drop_table_data(string path)删除指定的数据集

AgentDbaSql.gs

基于dba server的msyql数据库服务的代理操作对象;本对象包含AgentDbaBase组件

一些写操作失败后,将把失败的操作备份到指定的sql3文件中再自动进行恢复

函数原型函数作用
bool ping(string path = nil)询问dba server,某个表所在的数据库是否可用
int count_content(string path, mixed condition = nil)统计指定表中满足条件的记录数量
int create_content(string path, string name, map content)创建一条记录
int delete_content(string path, string name, map extend_info = nil)删除一行记录
mixed get_catalog(string path, mixed condition, int start_pos = 0, int count = 1000)读取符合条件的数据的主键列表(目录)
mixed lock_content(string path, string name)通过修改runtime_holder字段值锁定一条记录
mixed unlock_content(string path, string name)通过修改runtime_holder字段值解锁一条记录
mixed query_runtime_holder(string path, string name)通过查询runtime_holder字段获得当前锁定记录的服务器信息
mixed read_content(string path, string name)读取一条记录
mixed read_group(string path, mixed condition = nil, string fields = nil)读取一组满足指定条件的记录
mixed update_field(string path, string name, map update, mixed cond)更新一条记录中的一组字段值
int write_content(string path, string name, map content, mixed cond = nil, map extend_info = nil)更新一条记录
int replace_content(string path, string name, map content, mixed cond = nil, map extend_info = nil)覆盖一条记录
mixed batch_execute(array operations)以事务的方式,批量执行操作

AgentLocalRedis.gs

基于redis数据库服务的代理操作对象;本对象包含AgentDbaBase组件

函数原型函数作用
bool exists(string key)判断指定的键是否存在
void set_int(string key, int val)将指定的键设置为int类型值
int get_int(string key)获取指定键的int类型值:
int add_int(string key, int val)为指定键(int类型值)增加指定的增量值
bool insert(string key, mixed value)当指定键不存在,插入一个指定的键
void set(string key, mixed val)将指定的键设置为指定值
void set_with_timeout(string key, mixed val, int timeout_seconds)将指定的键设置为指定值,并且设置这个键的过期时间
mixed get(string key)获取指定键的值
bool delete(string key)删除指定的键
bool map_insert(string name, string key, mixed value)在hash类型的键中插入一个字段
void map_set(string name, string key, mixed value)在hash类型的键中设置一个字段的值
mixed map_get(string name, string key)在hash类型的键中获取一个字段的值
bool map_delete(string name, mixed key_or_arr)在hash类型的键中删除一个或者一组字段
map map_get_all(string name)获取hash类型的键的所有字段值

AgentLocalSqlite.gs

基于mysql/sqlite数据库服务的代理操作对象;本对象包含AgentDbaBase组件

函数原型函数作用
int create_content(string path, string name, map data)创建一条记录
int write_content(string path, string name, map data, mixed cond = nil, map extend_info = nil)更新一条记录
mixed read_content(string path, string name)读取一条记录
int delete_content(string path, string name, mixed cond)删除一行记录
int replace_content(string path, string name, map data, mixed cond = nil)覆盖一条记录
mixed get_catalog(string path, mixed condition, int start_pos = 0, int count = 1000)读取符合条件的数据的主键列表(目录)
mixed update_field(string path, string name, map value, mixed cond)更新一条记录中的一组字段值
mixed read_group(string path, mixed condition = nil, string fields = nil)读取一组满足指定条件的记录

db_agent.gs

函数原型函数作用
void register_agent_factory_callback(string type, function func)注册回调函数:用于创建指定类型的数据库操作代理对象
void setup(map para)初始化本模块
object get_agent(string source)找到指定名称的数据库服务的代理操作对象
mixed op(string source, string op, ...)在指定名称的数据库服务操作对象上执行操作
object create_db_agent_sql(map para)创建一个基于dba server的mysql数据库服务的代理操作对象
object create_db_agent_mongo(map para)创建一个基于dba server的mongodb数据库服务的代理操作对象
object create_db_agent_local_redis(map para)创建一个基于redis数据库服务的代理操作对象
object create_db_agent_local_sqlite(map para)创建一个基于sqlite/mysql数据库服务的代理操作对象

枚举

DbaResult

DBA Server的操作结果值


OPERATE_OK = 0, // DBA返回操作成功
OPERATE_FAILED = -1, // DBA正常时,返回操作失败
DBA_FAILED = -2, // DBA不正常时,返回操作失败
BAD_DATA = -3, // DBA正常,数据没通过校验或不完整
DBA_PROCESSING = -4, // DBA正在执行操作

样例

// 这里只选取部分示例代码,具体可以看[源代码](https://m68gitlab.g-bits.com/pkgs/db_agent.git)中sample/pkg_sample中的示例

#pragma parallel

import gs.lang.*;
import gs.util.*;

import pkg.db_agent;
import pkg.hiredis;
import pkg.db_pool;
import pkg.architecture;
import pkg.json_config;
import pkg.redis_pool;

void create()
{
// 设置默认的编码输出
language.set_internal_encoding("UTF-8");

init_databases();

map para = {
"config_file" : "/test/db_agent.json",
"server_info" : {
"server_id" : 123,
},
};
db_agent.setup(para);
}

#define DB(x) db_agent.get_agent(x)

public void run()
{
mixed r;

// dba/mongo 1
r = DB("dba/mongodb#1").op("create_content", "mongo_account", "a1", {"a": 1});
printf("r: %O\n", r);

r = DB("dba/mongodb#1").op("read_content", "mongo_account", "a1");
printf("r: %O\n", r);

// dba/mongo 2
r = DB("dba/mongodb#2").op("create_content", "mongo_user", "u1", {"u": 1});
printf("r: %O\n", r);

r = DB("dba/mongodb#2").op("read_content", "mongo_user", "u1");
printf("r: %O\n", r);

// memory(local/sqlite)
r = DB("local/memdb#1").op("create_content", "mem_account", "a2", {"a":2});
printf("r: %O\n", r);

r = DB("local/memdb#1").op("read_content", "mem_account", "a2");
printf("r: %O\n", r);

// local/redis
r = DB("local/redis#1").op("map_set", "redis_account", "a3", {"a": 3});
printf("r: %O\n", r);

r = DB("local/redis#1").op("map_get", "redis_account", "a3");
printf("r: %O\n", r);

r = DB("local/redis#1").op("test_op", "1", 2, 3, 4, 5);

// dba/mysql 1
r = DB("dba/mysql#1").op("create_content", "account", "a4", {"a": 4});
printf("r: %O\n", r);

r = DB("dba/mysql#1").op("read_content", "account", "a4");
printf("r: %O\n", r);

// dba/mysql 2
r = DB("dba/mysql#2").op("create_content", "user", "u4", {"u": 4});
printf("r: %O\n", r);

r = DB("dba/mysql#2").op("read_content", "user", "u4");
printf("r: %O\n", r);
}

void init_databases()
{
map config = json_config.load("/test/db_config.json");

// for local redis
map redis_cfg = (map) config.get("redis_setup");
hiredis.setup_redis(redis_cfg);
for (string db, map db_config : (map) config.get("redis_dbs"))
redis_pool=>set_db_config(db, db_config);

// for local sqlite
// mysql & sqlite db
// 登记所有连接设置
for (string db, map db_config : (map) config.get("dbs"))
db_pool=>set_db_config(db, db_config);

// 加载配置文件
array db_xml_files = (array) config.get("db_xml_files");
architecture=>load_databases(db_xml_files);
}