From 19e09bc63072008d394bf99f01d9bef4c448de37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 3 May 2022 17:36:09 +0800 Subject: [PATCH] add table api --- include/binary.h | 95 ++++++++++++++++++++++------------------- include/table.h | 79 ++++++++++++++++++++++++++++++++++ include/types.h | 24 +++++------ src/table.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++ src/types.c | 4 +- 5 files changed, 251 insertions(+), 58 deletions(-) diff --git a/include/binary.h b/include/binary.h index 74bf3ff..81790e6 100644 --- a/include/binary.h +++ b/include/binary.h @@ -46,61 +46,68 @@ #endif #endif -#ifdef _WIN64 - #ifdef WORDS_BIGENDIAN - #define putle16(buf, x) (*(uint16_t*)(buf) = _byteswap_ushort((uint16_t)(x))) - #define putle32(buf, x) (*(uint32_t*)(buf) = _byteswap_ulong((uint32_t)(x))) - #define putle64(buf, x) (*(uint64_t*)(buf) = _byteswap_uint64((uint64_t)(x))) +#ifdef WORDS_BIGENDIAN + #ifdef _WIN64 + # define le16toh(x) _byteswap_ushort(x) + # define le32toh(x) _byteswap_ulong(x) + # define le64toh(x) _byteswap_uint64(x) + # define htole16(x) _byteswap_ushort(x) + # define htole32(x) _byteswap_ulong(x) + # define htole64(x) _byteswap_uint64(x) - #define readle16(fd, num) ((~read((fd), &(num), 2))?((num)=_byteswap_ushort((uint16_t)(num))):EOF) - #define readle32(fd, num) ((~read((fd), &(num), 4))?((num)=_byteswap_ulong((uint32_t)(num))):EOF) - #define readle64(fd, num) ((~read((fd), &(num), 8))?((num)=_byteswap_uint64((uint64_t)(num))):EOF) + # define putle16(buf, x) (*(uint16_t*)(buf) = _byteswap_ushort((uint16_t)(x))) + # define putle32(buf, x) (*(uint32_t*)(buf) = _byteswap_ulong((uint32_t)(x))) + # define putle64(buf, x) (*(uint64_t*)(buf) = _byteswap_uint64((uint64_t)(x))) - #define le16(buf) _byteswap_ushort(*(uint16_t*)(buf)) - #define le32(buf) _byteswap_ulong(*(uint32_t*)(buf)) - #define le64(buf) _byteswap_uint64(*(uint64_t*)(buf)) + # define readle16(fd, num) (read((fd), &(num), 2),((num)=_byteswap_ushort((uint16_t)(num)))) + # define readle32(fd, num) (read((fd), &(num), 4),((num)=_byteswap_ulong((uint32_t)(num)))) + # define readle64(fd, num) (read((fd), &(num), 8),((num)=_byteswap_uint64((uint64_t)(num)))) + + # define le16(buf) _byteswap_ushort(*(uint16_t*)(buf)) + # define le32(buf) _byteswap_ulong(*(uint32_t*)(buf)) + # define le64(buf) _byteswap_uint64(*(uint64_t*)(buf)) #else - #define putle16(buf, x) (*(uint16_t*)(buf) = (uint16_t)(x)) - #define putle32(buf, x) (*(uint32_t*)(buf) = (uint32_t)(x)) - #define putle64(buf, x) (*(uint64_t*)(buf) = (uint64_t)(x)) + # define le16toh(x) __builtin_bswap16(x) + # define le32toh(x) __builtin_bswap32(x) + # define le64toh(x) __builtin_bswap64(x) + # define htole16(x) __builtin_bswap16(x) + # define htole32(x) __builtin_bswap32(x) + # define htole64(x) __builtin_bswap64(x) - #define readle16(fd, num) read((fd), &(num), 2) - #define readle32(fd, num) read((fd), &(num), 4) - #define readle64(fd, num) read((fd), &(num), 8) + # define putle16(buf, x) (*(uint16_t*)(buf) = __builtin_bswap16((uint16_t)(x))) + # define putle32(buf, x) (*(uint32_t*)(buf) = __builtin_bswap32((uint32_t)(x))) + # define putle64(buf, x) (*(uint64_t*)(buf) = __builtin_bswap64((uint64_t)(x))) - #define le16(buf) (*(uint16_t*)(buf)) - #define le32(buf) (*(uint32_t*)(buf)) - #define le64(buf) (*(uint64_t*)(buf)) + # define readle16(fd, num) (read((fd), &(num), 2),((num)=__builtin_bswap16((uint16_t)(num)))) + # define readle32(fd, num) (read((fd), &(num), 4),((num)=__builtin_bswap32((uint32_t)(num)))) + # define readle64(fd, num) (read((fd), &(num), 8),((num)=__builtin_bswap64((uint64_t)(num)))) + + # define le16(buf) __builtin_bswap16(*(uint16_t*)(buf)) + # define le32(buf) __builtin_bswap32(*(uint32_t*)(buf)) + # define le64(buf) __builtin_bswap64(*(uint64_t*)(buf)) #endif #else - #ifdef WORDS_BIGENDIAN - #define putle16(buf, x) (*(uint16_t*)(buf) = __builtin_bswap16((uint16_t)(x))) - #define putle32(buf, x) (*(uint32_t*)(buf) = __builtin_bswap32((uint32_t)(x))) - #define putle64(buf, x) (*(uint64_t*)(buf) = __builtin_bswap64((uint64_t)(x))) + # define le16toh(x) (x) + # define le32toh(x) (x) + # define le64toh(x) (x) + # define htole16(x) (x) + # define htole32(x) (x) + # define htole64(x) (x) - #define readle16(fd, num) ((~read((fd), &(num), 2))?((num)=__builtin_bswap16((uint16_t)(num))):EOF) - #define readle32(fd, num) ((~read((fd), &(num), 4))?((num)=__builtin_bswap32((uint32_t)(num))):EOF) - #define readle64(fd, num) ((~read((fd), &(num), 8))?((num)=__builtin_bswap64((uint64_t)(num))):EOF) + # define putle16(buf, x) (*(uint16_t*)(buf) = (uint16_t)(x)) + # define putle32(buf, x) (*(uint32_t*)(buf) = (uint32_t)(x)) + # define putle64(buf, x) (*(uint64_t*)(buf) = (uint64_t)(x)) - #define le16(buf) __builtin_bswap16(*(uint16_t*)(buf)) - #define le32(buf) __builtin_bswap32(*(uint32_t*)(buf)) - #define le64(buf) __builtin_bswap64(*(uint64_t*)(buf)) - #else - #define putle16(buf, x) (*(uint16_t*)(buf) = (uint16_t)(x)) - #define putle32(buf, x) (*(uint32_t*)(buf) = (uint32_t)(x)) - #define putle64(buf, x) (*(uint64_t*)(buf) = (uint64_t)(x)) + # define readle16(fd, num) read((fd), &(num), 2) + # define readle32(fd, num) read((fd), &(num), 4) + # define readle64(fd, num) read((fd), &(num), 8) - #define readle16(fd, num) read((fd), &(num), 2) - #define readle32(fd, num) read((fd), &(num), 4) - #define readle64(fd, num) read((fd), &(num), 8) - - #define le16(buf) (*(uint16_t*)(buf)) - #define le32(buf) (*(uint32_t*)(buf)) - #define le64(buf) (*(uint64_t*)(buf)) - #endif + # define le16(buf) (*(uint16_t*)(buf)) + # define le32(buf) (*(uint32_t*)(buf)) + # define le64(buf) (*(uint64_t*)(buf)) #endif -# define likely(x) __builtin_expect(!!(x), 1) -# define unlikely(x) __builtin_expect(!!(x), 0) +# define likely(x) __builtin_expect(!!(x), 1) +# define unlikely(x) __builtin_expect(!!(x), 0) #endif \ No newline at end of file diff --git a/include/table.h b/include/table.h index d419a77..dbca2ee 100644 --- a/include/table.h +++ b/include/table.h @@ -1,6 +1,85 @@ #ifndef _TABLE_H_ #define _TABLE_H_ +#include +#include "types.h" +// 创建表,可变参数为本表的一行的 types,详见 types.h +// 如果 types 为外键,需要紧跟一个 uint64_t ptr +// 指示外键链接到的表位置 +// 返回: +// NULL 失败,详见 errno +// table 指向表头的指针 +void* create_table(int fd, const char* name, ...); + +// 加载 ptr 位置的表 +// 返回: +// NULL 失败,详见 errno +// table 指向表头的指针 +void* load_table(int fd, uint64_t ptr); + +// 获得表名长度,包含结尾0 +uint16_t get_table_name_length(void* table); + +// 获得表名,写入buf,len(buf) >= len(name) +// 返回:buf +char* get_table_name(void* table, char* buf); + +// 为 pos 位置的列创建索引。不可用于 0 列,即 pk 列,因为 pk 必有索引 +// 返回: +// NULL 失败,详见 errno +// index 指向索引头的指针 +void* add_table_index(int fd, void* table, uint16_t pos); + +// 删除 pos 位置的列的索引。不可用于 0 列,即 pk 列,因为 pk 必有索引 +// 返回: +// 非 0 失败,详见 errno +// 0 成功 +int remove_table_index(int fd, void* table, uint16_t pos); + +// 插入一行,如果 pk 有值则替换 +// 如果当前项有 nullable 属性,需要在此项之前 +// 加一个 int isavailable,标记本项是否有值 +// 如果 isavailable==0,后面不再跟有本项数据 +// 如果 isavailable!=0,则在后面附加数据 +// 返回: +// 0 失败,详见 errno +// ptr 本行插入的位置 +uint64_t insert_row(int fd, void* table, ...); + +// 根据主键的匹配值查找行 +// 如果主键不为 string,k 直接装填其值 +// 否则,k 是指向 string 的指针 (const char*) +// 返回: +// 0 失败,详见 errno +// ptr 行所在位置 +uint64_t find_row_by_pk(int fd, void* table, key_t k); + +// 根据任意匹配值遍历查找行 +// 可变参数两两成对,uint16_t pos + key_t val +// 如果 val 不为 string,直接装填其值 +// 否则,值是指向 string 的指针 (const char*) +// f 为遍历函数,入参为本行 ptr,返回非 0 值中断遍历 +// 返回: +// 非 0 失败,详见 errno +// 0 成功 +int find_row_by(int fd, void* table, int (*f)(uint64_t), ...); + +// 根据主键的匹配值删除行 +// 如果主键不为 string,k 直接装填其值 +// 否则,k 是指向 string 的指针 (const char*) +// 返回: +// 非 0 失败,详见 errno +// 0 成功 +int remove_row_by_pk(int fd, void* table, key_t k); + +// 根据任意匹配值删除行 +// 可变参数两两成对,uint16_t pos + key_t val +// 如果 val 不为 string,直接装填其值 +// 否则,值是指向 string 的指针 (const char*) +// 返回: +// 非 0 失败,详见 errno +// 0 成功 +int remove_row_by(int fd, void* table, ...); #endif \ No newline at end of file diff --git a/include/types.h b/include/types.h index 0508458..2a95101 100644 --- a/include/types.h +++ b/include/types.h @@ -4,19 +4,19 @@ #include #include -#define TYPE_INT8 0 -#define TYPE_INT16 1 -#define TYPE_INT32 2 -#define TYPE_INT64 3 -#define TYPE_FLOAT 4 -#define TYPE_DOUBLE 5 -#define TYPE_STRING 6 -#define TYPE_BINARY 7 +#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 0x00 -#define EXTYPE_UNIQUE 0x40 -#define EXTYPE_NONNULL 0x80 -#define EXTYPE_FOREIGNKEY 0xc0 +#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; diff --git a/src/table.c b/src/table.c index e69de29..d65bf34 100644 --- a/src/table.c +++ b/src/table.c @@ -0,0 +1,107 @@ +#include +#include +#include "../include/binary.h" +#include "../include/table.h" + +// 创建表,可变参数为本表的一行的 types,详见 types.h +// 如果 types 为外键,需要紧跟一个 uint64_t ptr +// 指示外键链接到的表位置 +// 返回: +// NULL 失败,详见 errno +// table 指向表头的指针 +void* create_table(int fd, const char* name, ...) { + return NULL; +} + +// 加载 ptr 位置的表 +// 返回: +// NULL 失败,详见 errno +// table 指向表头的指针 +void* load_table(int fd, uint64_t ptr) { + return NULL; +} + +// 获得表名长度,包含结尾0 +uint16_t get_table_name_length(void* table) { + return le16(table+8)+1; +} + +// 获得表名,写入buf,len(buf) >= len(name) +// 返回:buf +char* get_table_name(void* table, char* buf) { + uint16_t len = le16(table+8); + memcpy(buf, table+10, len); + buf[len] = 0; + return buf; +} + +// 为 pos 位置的列创建索引。不可用于 0 列,即 pk 列,因为 pk 必有索引 +// 返回: +// NULL 失败,详见 errno +// index 指向索引头的指针 +void* add_table_index(int fd, void* table, uint16_t pos) { + return NULL; +} + +// 删除 pos 位置的列的索引。不可用于 0 列,即 pk 列,因为 pk 必有索引 +// 返回: +// 非 0 失败,详见 errno +// 0 成功 +int remove_table_index(int fd, void* table, uint16_t pos) { + return 0; +} + +// 插入一行,如果 pk 有值则替换 +// 如果当前项有 nullable 属性,需要在此项之前 +// 加一个 int isavailable,标记本项是否有值 +// 如果 isavailable==0,后面不再跟有本项数据 +// 如果 isavailable!=0,则在后面附加数据 +// 返回: +// 0 失败,详见 errno +// ptr 本行插入的位置 +uint64_t insert_row(int fd, void* table, ...) { + return 0; +} + +// 根据主键的匹配值查找行 +// 如果主键不为 string,k 直接装填其值 +// 否则,k 是指向 string 的指针 (const char*) +// 返回: +// 0 失败,详见 errno +// ptr 行所在位置 +uint64_t find_row_by_pk(int fd, void* table, key_t k) { + return 0; +} + +// 根据任意匹配值遍历查找行 +// 可变参数两两成对,uint16_t pos + key_t val +// 如果 val 不为 string,直接装填其值 +// 否则,值是指向 string 的指针 (const char*) +// f 为遍历函数,入参为本行 ptr,返回非 0 值中断遍历 +// 返回: +// 非 0 失败,详见 errno +// 0 成功 +int find_row_by(int fd, void* table, int (*f)(uint64_t), ...) { + return 1; +} + +// 根据主键的匹配值删除行 +// 如果主键不为 string,k 直接装填其值 +// 否则,k 是指向 string 的指针 (const char*) +// 返回: +// 非 0 失败,详见 errno +// 0 成功 +int remove_row_by_pk(int fd, void* table, key_t k) { + return 1; +} + +// 根据任意匹配值删除行 +// 可变参数两两成对,uint16_t pos + key_t val +// 如果 val 不为 string,直接装填其值 +// 否则,值是指向 string 的指针 (const char*) +// 返回: +// 非 0 失败,详见 errno +// 0 成功 +int remove_row_by(int fd, void* table, ...) { + return 1; +} diff --git a/src/types.c b/src/types.c index cf9ff5d..8adb386 100644 --- a/src/types.c +++ b/src/types.c @@ -32,7 +32,7 @@ static void* load_not_impl_index(int fd, uint64_t ptr, void* buf) { // Function not implemented static int remove_not_impl_index(int fd, void* index) { errno = ENOSYS; - return 0; + return -2; } // Function not implemented @@ -44,7 +44,7 @@ static uint64_t count_not_impl_items(int fd, void* index) { // Function not implemented static int insert_not_impl_item(int fd, void* index, key_t k, uint64_t ptr) { errno = ENOSYS; - return 0; + return -2; } // Function not implemented