1
0
mirror of https://github.com/fumiama/fumidb.git synced 2026-06-06 17:20:33 +08:00

add more api

This commit is contained in:
源文雨
2022-05-04 01:19:39 +08:00
parent 6022eb75fc
commit b33e5dbe73
4 changed files with 48 additions and 37 deletions

View File

@@ -11,7 +11,7 @@
// 返回:
// NULL 失败,详见 errno
// table 指向表头的指针
void* create_table(int fd, char* buf, const char* name, int row_len, ...);
void* create_table(int fd, char* buf, const char* name, int row_len, const void* list);
// 加载 ptr 位置的表
// len(buf) >= 4096+8+2=4106
@@ -54,7 +54,7 @@ int remove_table_index(int fd, void* table, uint16_t pos);
// 返回:
// 0 失败,详见 errno
// ptr 本行插入的位置
uint64_t insert_row(int fd, void* table, int row_len, ...);
uint64_t insert_row(int fd, void* table, int row_len, const void* list);
// 根据主键的匹配值查找行
// 如果主键不为 stringk 直接装填其值
@@ -72,7 +72,7 @@ uint64_t find_row_by_pk(int fd, void* table, key_t k);
// 返回:
// 非 0 失败,详见 errno
// 0 成功
int find_row_by(int fd, void* table, int (*f)(uint64_t), int row_len, ...);
int find_row_by(int fd, void* table, int (*f)(uint64_t), int row_len, const void* list);
// 根据主键的匹配值删除行
// 如果主键不为 stringk 直接装填其值
@@ -89,6 +89,6 @@ int remove_row_by_pk(int fd, void* table, key_t k);
// 返回:
// 非 0 失败,详见 errno
// 0 成功
int remove_row_by(int fd, void* table, int row_len, ...);
int remove_row_by(int fd, void* table, int row_len, const void* list);
#endif

View File

@@ -4,23 +4,23 @@
#include <stdint.h>
#include <errno.h>
#define TYPE_INT8 ((uint8_t)0)
#define TYPE_INT16 ((uint8_t)1)
#define TYPE_INT32 ((uint8_t)2)
#define TYPE_INT64 ((uint8_t)3)
#define TYPE_FLOAT ((uint8_t)4)
#define TYPE_DOUBLE ((uint8_t)5)
#define TYPE_STRING ((uint8_t)6)
#define TYPE_BINARY ((uint8_t)7)
#define EXTYPE_NULL ((uint8_t)0x00)
#define EXTYPE_UNIQUE ((uint8_t)0x40)
#define EXTYPE_NONNULL ((uint8_t)0x80)
#define EXTYPE_FOREIGNKEY ((uint8_t)0xc0)
typedef uint8_t type_t;
typedef uint64_t key_t;
#define TYPE_INT8 ((type_t)0)
#define TYPE_INT16 ((type_t)1)
#define TYPE_INT32 ((type_t)2)
#define TYPE_INT64 ((type_t)3)
#define TYPE_FLOAT ((type_t)4)
#define TYPE_DOUBLE ((type_t)5)
#define TYPE_STRING ((type_t)6)
#define TYPE_BINARY ((type_t)7)
#define EXTYPE_NULL ((type_t)0x00)
#define EXTYPE_UNIQUE ((type_t)0x10)
#define EXTYPE_NONNULL ((type_t)0x20)
#define EXTYPE_FOREIGNKEY ((type_t)0x40)
// 获得本类型 index 相对于 buffer 头的偏移
int type_offset(type_t t);

View File

@@ -82,7 +82,7 @@ static int _remove_index_type(int fd, type_t t, uint64_t ptr) {
// 返回:
// NULL 失败,详见 errno
// table 指向表头的指针
void* create_table(int fd, char* buf, const char* name, int row_len, ...) {
void* create_table(int fd, char* buf, const char* name, int row_len, const void* list) {
if(row_len <= 0 || row_len > 128) {
errno = EINVAL;
return NULL;
@@ -111,34 +111,44 @@ void* create_table(int fd, char* buf, const char* name, int row_len, ...) {
putle16(table+len, row_len);
len += 2;
int foreign_cnt = 0;
va_list list;
va_start(list, row_len);
int foreign_cnt = 0, ap = 0;
type_t t = va_arg(list, int); // 是主键,检查是否有 unique + nonnull 类型修饰符
type_t t = ((type_t*)list)[ap++]; // 是主键,检查是否有 unique + nonnull 类型修饰符
if(!(t&EXTYPE_NONNULL) || !(t&EXTYPE_UNIQUE)) {
errno = EINVAL;
return NULL;
}
((type_t*)table)[len] = t; // 填充 type of row No.0
#ifdef DEBUG
printf("fill[%d]: %d\n", ap, t);
#endif
if(t & EXTYPE_FOREIGNKEY) { // 是外键,还有一个参数
ptr = va_arg(list, uint64_t);
ptr = ((uint64_t*)list)[ap];
#ifdef DEBUG
printf("fill[%d]: %016llx\n", ap, ptr);
#endif
ap += 8;
putle64(table+len+(int)row_len*(8+1)+8*(foreign_cnt++), ptr);
}
// 为 pk 创建索引
if(_add_index_type(fd, table+len+(int)row_len, t)) return NULL;
for(int i = 1; i < (int)row_len; i++) {
t = va_arg(list, int);
t = ((type_t*)list)[ap++];
((type_t*)table)[len+i] = t; // 填充 type of row No.i
#ifdef DEBUG
printf("fill[%d]: %d\n", ap, t);
#endif
if(t & EXTYPE_FOREIGNKEY) { // 是外键,还有一个参数
ptr = va_arg(list, uint64_t);
ptr = ((uint64_t*)list)[ap];
#ifdef DEBUG
printf("fill[%d]: %016llx\n", ap, ptr);
#endif
ap += 8;
putle64(table+len+(int)row_len*(8+1)+8*(foreign_cnt++), ptr);
}
}
va_end(list);
len += (int)row_len*(8+1) + 8*foreign_cnt;
if(len > PAGESZ) {
errno = EFBIG;
@@ -274,7 +284,7 @@ int remove_table_index(int fd, void* table, uint16_t pos) {
// 返回:
// 0 失败,详见 errno
// ptr 本行插入的位置
uint64_t insert_row(int fd, void* table, int row_len, ...) {
uint64_t insert_row(int fd, void* table, int row_len, const void* list) {
return 0;
}
@@ -296,7 +306,7 @@ uint64_t find_row_by_pk(int fd, void* table, key_t k) {
// 返回:
// 非 0 失败,详见 errno
// 0 成功
int find_row_by(int fd, void* table, int (*f)(uint64_t), int row_len, ...) {
int find_row_by(int fd, void* table, int (*f)(uint64_t), int row_len, const void* list) {
return 1;
}
@@ -317,6 +327,6 @@ int remove_row_by_pk(int fd, void* table, key_t k) {
// 返回:
// 非 0 失败,详见 errno
// 0 成功
int remove_row_by(int fd, void* table, int row_len, ...) {
int remove_row_by(int fd, void* table, int row_len, const void* list) {
return 1;
}

View File

@@ -18,12 +18,13 @@ int main() {
}
if(init_file_header_page(fd)) return 2;
void* table = create_table(
fd, buf, "test_table", 5,
TYPE_INT16|EXTYPE_NONNULL|EXTYPE_UNIQUE,
TYPE_INT64,
TYPE_INT8,
TYPE_STRING,
TYPE_BINARY
fd, buf, "test_table", 5, (type_t[]){
TYPE_INT16|EXTYPE_NONNULL|EXTYPE_UNIQUE,
TYPE_INT64,
TYPE_INT8,
TYPE_STRING,
TYPE_BINARY
}
);
if(table == NULL) {
perror("create_table");