Go to file
程广 2841e31d84 ```
feat(database): 添加分页查询功能并完善适配器实现

- 在DatabaseAdapter接口中新增FindPage方法用于分页查询
- 实现PageResult结构体包含文档列表、是否有更多数据和总数
- 在BaseAdapter、DM8Adapter、PostgresAdapter和SQLiteAdapter中实现分页查询
- SQLite适配器现在正确检查集合是否存在和列出集合
- 调整CollectionExists方法返回nil而不是ErrNotImplemented

refactor(engine): 重构内存存储初始化策略

- 修改Initialize方法改为懒加载模式,不再一次性加载所有数据
- 添加Collection结构体的新字段:pageSize、loadedAll、totalCount
- 实现LoadCollectionPage方法支持按页加载数据
- 添加LoadEntireCollection和LazyLoadDocument方法
- 实现DocumentIterator用于文档遍历

feat(engine): 添加流式聚合执行功能

- 新增StreamAggregationOptions配置流式聚合参数
- 实现StreamExecute方法提供流式聚合能力
- 添加缓冲区大小、并发控制等选项

example: 添加流式聚合示例程序

- 创建stream_aggregate_example.go演示流式聚合用法
- 包含完整的测试数据创建和聚合管道执行流程
- 展示如何处理批量结果和错误通道

chore(config): 更新服务器TCP端口配置

- 将TCP监听地址从:27017更改为:28017
```
2026-03-18 15:36:58 +08:00
cmd/server feat(engine): 实现服务器重启后数据自动加载功能 2026-03-14 22:24:11 +08:00
doc docs(doc): 整理文档目录结构并创建项目文档索引 2026-03-14 19:22:45 +08:00
examples ``` 2026-03-18 15:36:58 +08:00
internal ``` 2026-03-18 15:36:58 +08:00
manual docs(manual): 添加 Gomog 完整 API 和操作符文档 2026-03-14 19:46:35 +08:00
pkg refactor(errors): 重构错误处理系统并实现结构化日志记录 2026-03-14 12:55:32 +08:00
.gitignore docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
Dockerfile docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
LICENSE chore(license): 添加 MIT 许可证文件 2026-03-12 22:23:22 +08:00
Makefile docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
README.md docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
RELOAD_FIX.md feat(engine): 实现服务器重启后数据自动加载功能 2026-03-14 22:24:11 +08:00
RELOAD_SUMMARY.md feat(engine): 实现服务器重启后数据自动加载功能 2026-03-14 22:24:11 +08:00
build.sh docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
check_tests.sh fix(tests): 修复测试代码中的编译错误和访问权限问题 2026-03-13 21:29:57 +08:00
config.yaml ``` 2026-03-18 15:36:58 +08:00
config.yaml.example docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
go.mod docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
go.sum fix(tests): 修复测试代码中的编译错误和访问权限问题 2026-03-13 21:29:57 +08:00
gomog docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
gomog.db-shm ``` 2026-03-18 15:36:58 +08:00
gomog.db-wal ``` 2026-03-18 15:36:58 +08:00
start.sh docs(guide): 添加项目文档和构建配置 2026-03-12 21:46:58 +08:00
test_batch2.sh feat(engine): 实现 MongoDB 高级查询和更新功能 2026-03-13 21:12:26 +08:00
test_persist.sh feat(engine): 实现数据库持久化功能并添加调试日志 2026-03-14 21:56:59 +08:00
test_quick.sh feat(engine): 实现服务器重启后数据自动加载功能 2026-03-14 22:24:11 +08:00
test_reload.sh feat(engine): 实现服务器重启后数据自动加载功能 2026-03-14 22:24:11 +08:00
test_reload_simple.sh feat(engine): 实现服务器重启后数据自动加载功能 2026-03-14 22:24:11 +08:00

README.md

Gomog - MongoDB 风格文档服务器

Gomog 是一个使用 Go 语言开发的中间层服务器,提供 MongoDB 风格的 CRUD 操作和聚合查询接口,底层使用关系型数据库的 JSON 字段存储文档数据。

特性

  • MongoDB 风格 API: 提供类似 MongoDB 的 find/insert/update/delete 操作和聚合管道
  • 多数据库支持: 通过适配器模式支持 SQLite、PostgreSQL、达梦 DM8 等多种关系型数据库
  • 内存查询引擎: 在内存中实现查询过滤和聚合逻辑,高性能处理
  • 双协议支持: 同时支持 HTTP/REST 和 TCP 两种通信协议
  • 灵活的 JSON 存储: 利用现代关系型数据库的 JSON/JSONB 类型

快速开始

安装依赖

make deps

配置

复制配置文件示例:

cp config.yaml.example config.yaml

编辑 config.yaml 配置数据库连接和其他参数。

运行

# 开发模式运行
make run

# 或构建后运行
make build
./bin/gomog -config config.yaml

使用 Docker

# 构建镜像
make docker-build

# 运行容器
make docker-run

API 使用示例

插入文档

curl -X POST http://localhost:8080/api/v1/testdb/users/insert \
  -H "Content-Type: application/json" \
  -d '{
    "documents": [
      {"name": "Alice", "age": 25, "email": "alice@example.com"},
      {"name": "Bob", "age": 30, "email": "bob@example.com"}
    ]
  }'

查询文档

curl -X POST http://localhost:8080/api/v1/testdb/users/find \
  -H "Content-Type: application/json" \
  -d '{
    "filter": {"age": {"$gte": 25}},
    "projection": {"name": 1, "email": 1},
    "sort": {"age": -1},
    "limit": 10
  }'

更新文档

curl -X POST http://localhost:8080/api/v1/testdb/users/update \
  -H "Content-Type: application/json" \
  -d '{
    "updates": [{
      "q": {"name": "Alice"},
      "u": {"$set": {"age": 26}}
    }]
  }'

删除文档

curl -X POST http://localhost:8080/api/v1/testdb/users/delete \
  -H "Content-Type: application/json" \
  -d '{
    "deletes": [{
      "q": {"name": "Bob"},
      "limit": 1
    }]
  }'

聚合查询

curl -X POST http://localhost:8080/api/v1/testdb/orders/aggregate \
  -H "Content-Type: application/json" \
  -d '{
    "pipeline": [
      {"$match": {"status": "completed"}},
      {"$group": {
        "_id": "$customer_id",
        "total": {"$sum": "$amount"},
        "count": {"$sum": 1}
      }},
      {"$sort": {"total": -1}},
      {"$limit": 10}
    ]
  }'

支持的查询操作符

比较操作符

  • $eq, $ne, $gt, $gte, $lt, $lte
  • $in, $nin
  • $regex

逻辑操作符

  • $and, $or, $nor, $not

元素操作符

  • $exists, $type

数组操作符

  • $all, $elemMatch, $size

支持的聚合阶段

  • $match - 过滤文档
  • $group - 分组聚合
  • $sort - 排序
  • $project - 字段投影
  • $limit - 限制结果数量
  • $skip - 跳过指定数量的文档
  • $unwind - 展开数组
  • $lookup - 左连接
  • $count - 计数

项目结构

gomog/
├── cmd/
│   └── server/           # 应用程序入口
├── internal/
│   ├── config/           # 配置管理
│   ├── protocol/         # 协议层HTTP/TCP
│   ├── parser/           # 请求解析
│   ├── engine/           # 查询和聚合引擎
│   ├── storage/          # 存储层
│   └── database/         # 数据库适配器
├── pkg/
│   ├── types/            # 类型定义
│   └── errors/           # 错误定义
├── Makefile
└── config.yaml.example

数据库配置

SQLite

database:
  type: "sqlite"
  dsn: "gomog.db"

PostgreSQL

database:
  type: "postgres"
  dsn: "postgres://user:password@localhost:5432/gomog?sslmode=disable"

达梦 DM8

database:
  type: "dm8"
  dsn: "user/password@localhost:5236"

开发

运行测试

make test

代码格式化

make fmt

构建所有平台

make build-linux
make build-darwin
make build-windows

技术栈

  • 语言: Go 1.21+
  • 数据库驱动:
    • SQLite: github.com/mattn/go-sqlite3
    • PostgreSQL: github.com/lib/pq
    • 达梦 DM8: 官方驱动或 ODBC
  • 配置: gopkg.in/yaml.v3
  • 日志: 标准库 log可升级到 zap

注意事项

  1. 数据一致性: 内存与数据库的数据同步采用写时异步持久化策略
  2. 内存管理: 大数据集建议定期重启服务或使用分页加载
  3. 事务支持: 当前版本简化处理,后续将完善事务支持
  4. 索引优化: 主要依赖内存查询,后续可添加热点字段索引

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request