# Gudb **Repository Path**: coderlaogu/gudb ## Basic Information - **Project Name**: Gudb - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-07-26 - **Last Updated**: 2025-08-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # GuDB - 高性能数据库管理系统     > **GuDB** - 功能完整的关系型数据库管理系统,采用原生GuDB协议,提供ACID事务、WAL日志、Bw-Tree无锁索引等企业级功能。 ## 🚀 一分钟快速启动 ```bash # 1. 构建GuDB系统 make -f scripts/Makefile.gudb all # 2. 启动GuDB演示系统 ./scripts/start_gudb_native.sh # 3. 使用gu客户端连接 ./bin/gu -i ``` ## ⭐ 核心特性 ### 🏗️ 数据库引擎 - **ACID事务管理** - 完整的原子性、一致性、隔离性、持久性保证,支持4种隔离级别 - **WAL日志系统** - 写前日志确保数据持久性和崩溃恢复 - **多索引引擎** - Bw-Tree无锁索引、B+树、位图索引、R树、哈希索引全覆盖 - **完整SQL支持** - DDL、DML、DQL,支持子查询、连接、聚合、视图、触发器 - **智能查询优化** - 查询计划深拷贝、节点比较、表达式统计优化 - **高性能执行器** - 相关子查询优化、JOIN条件评估、死锁检测 ### 🌐 网络服务 - **GuDB原生协议** - 专为GuDB优化的高性能通信协议 - **多线程架构** - 连接池管理,支持高并发访问 - **专用端口7777** - 独立的服务端口 - **企业级可靠性** - 备份恢复、权限管理、错误处理、监控统计 - **完整索引支持** - Bw-Tree、B+树、位图索引、R树、哈希索引 - **高性能查询处理** - 查询计划优化、深拷贝、表达式统计 - **事务隔离级别** - READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ、SERIALIZABLE ### 💻 客户端工具 - **原生gu客户端** - 轻量级、高性能的专用客户端 - **交互式界面** - 友好的命令行交互体验 - **批量执行** - 支持单命令和脚本执行模式 - **完整协议支持** - 支持所有已实现的SQL语句和功能 ## 📋 系统要求 **支持平台**: Linux/macOS **编译器**: GCC 4.9+ 或 Clang 3.5+ **依赖**: zlib, pthread ```bash # Ubuntu/Debian sudo apt-get install build-essential zlib1g-dev # CentOS/RHEL sudo yum groupinstall "Development Tools" && sudo yum install zlib-devel # macOS xcode-select --install && brew install zlib ``` ## 💻 使用方式 ### 1. 嵌入式API ```c #include "gudb.h" gudb* db = gudb_open("/data/mydb", GU_OPEN_CREATE, "admin", "admin"); gudb_txn* txn = gudb_begin(db, GU_TXN_RC); gudb_exec(txn, "CREATE TABLE users (id INT, name VARCHAR(50))", NULL, NULL); gudb_exec(txn, "INSERT INTO users VALUES (1, 'Alice')", NULL, NULL); gudb_commit(txn); gudb_close(db); // 编译: gcc app.c -I include -L . -lgudb -lpthread -lz -o app ``` ### 2. 网络服务器模式 ```bash # 启动GuDB服务器 (端口7777) ./bin/gudb-server -v # 使用gu客户端连接 ./bin/gu -i # 交互模式 ./bin/gu "SELECT 1" # 单命令执行 ./bin/gu -h server -P 7777 -i # 连接远程服务器 # 客户端命令示例 gu[gudb]> help # 查看帮助 gu[gudb]> status # 连接状态 gu[gudb]> ping # 测试连接 gu[gudb]> quit # 退出客户端 ``` ### 3. 示例程序 ```bash ./examples/tutorial_basic # 基础操作教程 ./examples/simple_mm_test # 内存管理测试 ./scripts/start_gudb_native.sh # GuDB系统演示 ``` ## 🧪 测试验证 ```bash # 运行所有测试 ./tests/integration/test_simple # 基础功能 ./tests/integration/test_wal # WAL系统 ./tests/integration/test_transaction # 事务管理 ./tests/integration/test_server # 网络服务器 ./tests/integration/test_integration # 完整集成 # 功能测试 ./tests/test_index_functionality # 索引管理测试 ./tests/test_limit_functionality # LIMIT/OFFSET测试 ./tests/test_subquery_functionality # 子查询测试 ./tests/test_trigger_functionality # 触发器测试 ./tests/test_user_grant_functionality # 权限管理测试 ./tests/test_view_functionality # 视图测试 ``` ## 📊 SQL功能支持 ### ✅ **完整DDL语句支持** #### 🏗️ **数据库管理** ```sql -- 数据库操作 CREATE DATABASE company; -- 创建数据库 CREATE DATABASE IF NOT EXISTS app; -- 条件创建数据库 DROP DATABASE company; -- 删除数据库 DROP DATABASE IF EXISTS old_db; -- 条件删除数据库 USE company; -- 切换到指定数据库 SHOW DATABASES; -- 显示所有数据库 ``` #### 📋 **表结构管理** ```sql -- 基础建表语句 CREATE TABLE users ( id INT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, email VARCHAR(100) NOT NULL, age INT, salary DOUBLE DEFAULT 0.0, created_at TIMESTAMP, bio TEXT ); -- 支持的数据类型 CREATE TABLE data_types_demo ( int_col INT, -- 32位整数 bigint_col BIGINT, -- 64位整数 tinyint_col TINYINT, -- 8位整数 varchar_col VARCHAR(255), -- 可变长字符串 char_col CHAR(10), -- 固定长字符串 double_col DOUBLE, -- 双精度浮点数 float_col FLOAT, -- 单精度浮点数 text_col TEXT, -- 长文本 timestamp_col TIMESTAMP -- 时间戳 ); -- 表操作 DROP TABLE users; -- 删除表 DROP TABLE IF EXISTS old_table; -- 条件删除表 DESCRIBE users; -- 查看表结构 SHOW TABLES; -- 显示当前数据库所有表 ``` #### 🔧 **ALTER TABLE操作** ```sql -- 添加列 ALTER TABLE users ADD COLUMN phone VARCHAR(20); ALTER TABLE users ADD COLUMN status VARCHAR(10) DEFAULT 'active'; ALTER TABLE users ADD COLUMN score INT NOT NULL DEFAULT 0; -- 删除列 ALTER TABLE users DROP COLUMN phone; ALTER TABLE users DROP COLUMN bio; -- 修改列 ALTER TABLE users MODIFY COLUMN email VARCHAR(150); ALTER TABLE users MODIFY COLUMN age BIGINT; ALTER TABLE users MODIFY COLUMN salary DOUBLE DEFAULT 1000.0; ``` #### 🚀 **索引管理** ```sql -- 创建索引 CREATE INDEX idx_username ON users(username); -- 单列索引 CREATE INDEX idx_email ON users(email); -- 单列索引 CREATE UNIQUE INDEX idx_unique_email ON users(email); -- 唯一索引 CREATE INDEX idx_name_age ON users(username, age); -- 复合索引 CREATE INDEX idx_salary_desc ON users(salary DESC); -- 降序索引 CREATE INDEX IF NOT EXISTS idx_created ON users(created_at); -- 条件创建 -- 删除索引 DROP INDEX idx_username; -- 删除索引 DROP INDEX IF EXISTS idx_old; -- 条件删除 -- 索引查询命令 SHOW INDEX FROM users; -- 显示表的所有索引(计划支持) SHOW INDEXES IN users; -- 显示表索引(计划支持) EXPLAIN SELECT * FROM users WHERE username = 'john'; -- 查看查询执行计划(计划支持) ``` #### 👁️ **视图管理** ```sql -- 创建视图 CREATE VIEW active_users AS SELECT id, username, email FROM users WHERE status = 'active'; CREATE OR REPLACE VIEW user_summary AS SELECT username, age, salary FROM users WHERE salary > 5000; CREATE VIEW expensive_products AS SELECT * FROM products WHERE price > 1000; -- 删除视图 DROP VIEW active_users; DROP VIEW IF EXISTS old_view; ``` #### ⚡ **触发器系统** ```sql -- 创建触发器 CREATE TRIGGER audit_users_insert BEFORE INSERT ON users FOR EACH ROW BEGIN INSERT INTO audit_log VALUES (NEW.id, 'INSERT', NOW()); END; CREATE TRIGGER update_modified_time AFTER UPDATE ON users FOR EACH ROW BEGIN UPDATE users SET modified_at = NOW() WHERE id = NEW.id; END; CREATE TRIGGER validate_salary BEFORE UPDATE ON users FOR EACH ROW WHEN (NEW.salary < 0) BEGIN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary cannot be negative'; END; -- 删除触发器 DROP TRIGGER audit_users_insert; DROP TRIGGER IF EXISTS old_trigger; ``` #### 👤 **用户和权限管理** ```sql -- 创建用户 CREATE USER 'app_user' IDENTIFIED BY 'secure_password' READ_WRITE; CREATE USER 'admin_user' IDENTIFIED BY 'admin_pass' ADMIN; CREATE USER 'guest_user' READ_ONLY; CREATE USER 'api_user' IDENTIFIED BY 'api_key_123' READ_WRITE; -- 权限授予 GRANT SELECT ON users TO 'guest_user'; GRANT SELECT, INSERT ON products TO 'app_user'; GRANT SELECT, INSERT, UPDATE ON orders TO 'app_user'; GRANT SELECT, INSERT, UPDATE, DELETE ON inventory TO 'admin_user'; GRANT ALL ON *.* TO 'admin_user'; -- 所有权限 -- 权限管理示例 GRANT SELECT ON public_data TO 'guest_user'; -- 查询权限 GRANT INSERT, UPDATE ON user_posts TO 'app_user'; -- 增改权限 GRANT DELETE ON temp_data TO 'cleanup_user'; -- 删除权限 ``` ### ✅ **完整DML语句支持** #### 📝 **数据插入** ```sql -- 基础插入 INSERT INTO users VALUES (1, 'john_doe', 'john@email.com', 25, 50000.0); INSERT INTO users (id, username, email) VALUES (2, 'jane_smith', 'jane@email.com'); -- 批量插入 INSERT INTO products VALUES (1, 'Laptop', 1299.99, 'Electronics'), (2, 'Mouse', 29.99, 'Electronics'), (3, 'Keyboard', 79.99, 'Electronics'); -- 带默认值插入 INSERT INTO users (username, email) VALUES ('new_user', 'new@email.com'); ``` #### 🔄 **数据更新** ```sql -- 单条更新 UPDATE users SET salary = 60000.0 WHERE id = 1; UPDATE users SET age = 26, salary = 55000.0 WHERE username = 'john_doe'; -- 批量更新 UPDATE products SET price = price * 0.9 WHERE category = 'Electronics'; UPDATE users SET status = 'inactive' WHERE created_at < '2023-01-01'; -- 条件更新 UPDATE users SET salary = salary * 1.1 WHERE age > 30 AND status = 'active'; ``` #### 🗑️ **数据删除** ```sql -- 单条删除 DELETE FROM users WHERE id = 1; DELETE FROM products WHERE name = 'Old Product'; -- 批量删除 DELETE FROM users WHERE status = 'inactive'; DELETE FROM products WHERE price < 10.0; -- 条件删除 DELETE FROM orders WHERE created_at < '2023-01-01' AND status = 'cancelled'; ``` #### 🔍 **数据查询** ```sql -- 基础查询 SELECT * FROM users; SELECT id, username, email FROM users; SELECT username AS name, salary AS income FROM users; -- 条件查询 SELECT * FROM users WHERE age > 25; SELECT * FROM products WHERE price BETWEEN 50 AND 200; SELECT * FROM users WHERE username LIKE 'john%'; SELECT * FROM users WHERE email IS NOT NULL; -- 排序和分页 SELECT * FROM users ORDER BY salary DESC; SELECT * FROM users ORDER BY age ASC, salary DESC; SELECT * FROM users ORDER BY username LIMIT 10; SELECT * FROM users ORDER BY created_at DESC LIMIT 5 OFFSET 10; -- 连接查询 SELECT u.username, p.title FROM users u INNER JOIN posts p ON u.id = p.user_id; SELECT u.username, p.title FROM users u LEFT JOIN posts p ON u.id = p.user_id; SELECT u.username, o.total FROM users u RIGHT JOIN orders o ON u.id = o.customer_id; -- 子查询 SELECT * FROM users WHERE salary > (SELECT AVG(salary) FROM users); SELECT username FROM users WHERE id IN (SELECT user_id FROM premium_members); SELECT * FROM products WHERE EXISTS (SELECT 1 FROM orders WHERE product_id = products.id); SELECT customer_id, (SELECT name FROM customers WHERE id = customer_id) AS customer_name FROM orders; -- 聚合查询(部分支持) SELECT COUNT(*) FROM users; SELECT AVG(salary) FROM users; SELECT MIN(price), MAX(price) FROM products; SELECT SUM(total) FROM orders WHERE status = 'completed'; ``` ### ✅ **事务控制语句** ```sql -- 显式事务 START TRANSACTION; INSERT INTO users VALUES (10, 'test_user', 'test@email.com'); UPDATE users SET salary = 70000 WHERE id = 10; COMMIT; -- 回滚事务 START TRANSACTION; DELETE FROM users WHERE id = 10; ROLLBACK; -- 自动提交模式 SET AUTOCOMMIT = 1; -- 开启自动提交(计划支持) SET AUTOCOMMIT = 0; -- 关闭自动提交(计划支持) ``` ### 🔍 **索引查询和优化命令** ```sql -- 查看表索引信息(计划支持) SHOW INDEX FROM users; SHOW INDEXES IN products; SHOW KEYS FROM orders; -- 查询执行计划(计划支持) EXPLAIN SELECT * FROM users WHERE username = 'john'; EXPLAIN SELECT u.*, p.* FROM users u JOIN posts p ON u.id = p.user_id; -- 索引使用统计(计划支持) SHOW INDEX STATS FROM users; ANALYZE TABLE users; -- 强制使用索引(计划支持) SELECT * FROM users USE INDEX(idx_username) WHERE username = 'john'; SELECT * FROM users FORCE INDEX(idx_email) WHERE email = 'test@email.com'; ``` ### 🔧 **系统管理命令** ```sql -- 数据库信息 SHOW DATABASES; SHOW TABLES; SHOW TABLES FROM company; DESCRIBE users; SHOW CREATE TABLE users; -- 计划支持 -- 系统状态(计划支持) SHOW STATUS; SHOW VARIABLES; SHOW PROCESSLIST; -- 性能监控(计划支持) SHOW TABLE STATUS; SHOW ENGINE INNODB STATUS; ``` ### 🔧 **客户端专用命令** ```bash # 连接和状态管理 help # 显示所有可用命令 status # 显示连接状态 ping # 测试服务器连接 quit / exit # 退出客户端 # 内置数据库命令 show databases # 列出数据库 show tables # 列出当前数据库的表 describe