mirror of
https://github.com/fumiama/fumidb.git
synced 2026-06-05 00:32:44 +08:00
finish type int8
This commit is contained in:
@@ -23,16 +23,20 @@ typedef uint64_t key_t;
|
||||
|
||||
// 为类型 type 创建索引
|
||||
// 返回:索引头节点的指针 index
|
||||
uint64_t create_index(int fd, type_t t);
|
||||
void* create_index(int fd, type_t t);
|
||||
|
||||
// 加载类型 type 的索引
|
||||
// 返回:索引头节点的指针 index
|
||||
void* load_index(int fd, type_t t, uint64_t ptr);
|
||||
|
||||
// 插入一条索引
|
||||
int insert_item(int fd, type_t t, uint64_t index, key_t k, uint64_t ptr);
|
||||
int insert_item(int fd, type_t t, void* index, key_t k, uint64_t ptr);
|
||||
|
||||
// 使用索引查找目标
|
||||
// 返回:ptr
|
||||
uint64_t find_item_by_key(int fd, type_t t, uint64_t index, key_t k);
|
||||
uint64_t find_item_by_key(int fd, type_t t, void* index, key_t k);
|
||||
|
||||
// 使用索引删除项
|
||||
int remove_item_by_key(int fd, type_t t, uint64_t index, key_t k);
|
||||
int remove_item_by_key(int fd, type_t t, void* index, key_t k);
|
||||
|
||||
#endif
|
||||
@@ -5,12 +5,14 @@
|
||||
|
||||
#define INT8_INDEX_SZ (256*8)
|
||||
|
||||
uint64_t create_int8_index(int fd);
|
||||
void* create_int8_index(int fd);
|
||||
|
||||
int insert_int8_item(int fd, uint64_t index, key_t k, uint64_t ptr);
|
||||
void* load_int8_index(int fd, uint64_t ptr);
|
||||
|
||||
uint64_t find_item_by_int8_key(int fd, uint64_t index, key_t k);
|
||||
int insert_int8_item(int fd, void* index, key_t k, uint64_t ptr);
|
||||
|
||||
int remove_item_by_int8_key(int fd, uint64_t index, key_t k);
|
||||
uint64_t find_item_by_int8_key(int fd, void* index, key_t k);
|
||||
|
||||
int remove_item_by_int8_key(int fd, void* index, key_t k);
|
||||
|
||||
#endif
|
||||
46
src/types.c
46
src/types.c
@@ -2,34 +2,42 @@
|
||||
#include "../include/types/int8.h"
|
||||
|
||||
// ptr = init(fd)
|
||||
typedef uint64_t (*_type_init_t)(int);
|
||||
typedef void* (*_type_init_t)(int);
|
||||
// ptr = load(fd, ptr)
|
||||
typedef void* (*_type_load_t)(int, uint64_t);
|
||||
// ret = insert_item(fd, index, k, ptr)
|
||||
typedef int (*_insert_item_t)(int, uint64_t, key_t, uint64_t);
|
||||
typedef int (*_insert_item_t)(int, void*, key_t, uint64_t);
|
||||
// ptr = find_by_key(fd, index, k)
|
||||
typedef uint64_t (*_find_by_key_t)(int, uint64_t, key_t);
|
||||
typedef uint64_t (*_find_by_key_t)(int, void*, key_t);
|
||||
// ret = remove_by_key(fd, index, k)
|
||||
typedef int (*_remove_by_key_t)(int, uint64_t, key_t);
|
||||
typedef int (*_remove_by_key_t)(int, void*, key_t);
|
||||
|
||||
// Function not implemented
|
||||
static uint64_t create_not_impl_index(int fd) {
|
||||
static void* create_not_impl_index(int fd) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Function not implemented
|
||||
static int insert_not_impl_item(int fd, uint64_t index, key_t k, uint64_t ptr) {
|
||||
static void* load_not_impl_index(int fd, uint64_t ptr) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Function not implemented
|
||||
static uint64_t find_item_by_not_impl_key(int fd, uint64_t index, key_t k) {
|
||||
static int insert_not_impl_item(int fd, void* index, key_t k, uint64_t ptr) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Function not implemented
|
||||
static int remove_item_by_not_impl_key(int fd, uint64_t index, key_t k) {
|
||||
static uint64_t find_item_by_not_impl_key(int fd, void* index, key_t k) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Function not implemented
|
||||
static int remove_item_by_not_impl_key(int fd, void* index, key_t k) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
@@ -44,6 +52,16 @@ static _type_init_t _types_init[] = {
|
||||
create_not_impl_index
|
||||
};
|
||||
|
||||
static _type_load_t _types_load[] = {
|
||||
load_int8_index,
|
||||
load_not_impl_index,
|
||||
load_not_impl_index,
|
||||
load_not_impl_index,
|
||||
load_not_impl_index,
|
||||
load_not_impl_index,
|
||||
load_not_impl_index
|
||||
};
|
||||
|
||||
static _insert_item_t _insert_item[] = {
|
||||
insert_int8_item,
|
||||
insert_not_impl_item,
|
||||
@@ -74,18 +92,22 @@ static _remove_by_key_t _remove_item_by_key[] = {
|
||||
remove_item_by_not_impl_key
|
||||
};
|
||||
|
||||
uint64_t create_index(int fd, type_t t) {
|
||||
void* create_index(int fd, type_t t) {
|
||||
return _types_init[t&7](fd);
|
||||
}
|
||||
|
||||
int insert_item(int fd, type_t t, uint64_t index, key_t k, uint64_t ptr) {
|
||||
void* load_index(int fd, type_t t, uint64_t ptr) {
|
||||
return _types_load[t&7](fd, ptr);
|
||||
}
|
||||
|
||||
int insert_item(int fd, type_t t, void* index, key_t k, uint64_t ptr) {
|
||||
return _insert_item[t&7](fd, index, k, ptr);
|
||||
}
|
||||
|
||||
uint64_t find_item_by_key(int fd, type_t t, uint64_t index, key_t k) {
|
||||
uint64_t find_item_by_key(int fd, type_t t, void* index, key_t k) {
|
||||
return _find_item_by_key[t&7](fd, index, k);
|
||||
}
|
||||
|
||||
int remove_item_by_key(int fd, type_t t, uint64_t index, key_t k) {
|
||||
int remove_item_by_key(int fd, type_t t, void* index, key_t k) {
|
||||
return _remove_item_by_key[t&7](fd, index, k);
|
||||
}
|
||||
|
||||
@@ -4,26 +4,31 @@
|
||||
#include "../../include/page.h"
|
||||
#include "../../include/types/int8.h"
|
||||
|
||||
uint64_t create_int8_index(int fd) {
|
||||
void* create_int8_index(int fd) {
|
||||
uint64_t* blk = alloc_block(fd, INT8_INDEX_SZ);
|
||||
if(blk == NULL) return 0;
|
||||
memset(blk, 0, INT8_INDEX_SZ);
|
||||
uint64_t index = blk[-1];
|
||||
unmount_block(fd, blk);
|
||||
return index;
|
||||
sync_block(fd, blk);
|
||||
return blk;
|
||||
}
|
||||
|
||||
int insert_int8_item(int fd, uint64_t index, key_t k, uint64_t ptr) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
void* load_int8_index(int fd, uint64_t ptr) {
|
||||
return get_block(fd, INT8_INDEX_SZ, ptr);
|
||||
}
|
||||
|
||||
uint64_t find_item_by_int8_key(int fd, uint64_t index, key_t k) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
int insert_int8_item(int fd, void* index, key_t k, uint64_t ptr) {
|
||||
uint8_t key = (uint8_t)k;
|
||||
((uint64_t*)index)[key] = ptr;
|
||||
return sync_block(fd, index);
|
||||
}
|
||||
|
||||
int remove_item_by_int8_key(int fd, uint64_t index, key_t k) {
|
||||
errno = ENOSYS;
|
||||
uint64_t find_item_by_int8_key(int fd, void* index, key_t k) {
|
||||
uint8_t key = (uint8_t)k;
|
||||
return ((uint64_t*)index)[key];
|
||||
}
|
||||
|
||||
int remove_item_by_int8_key(int fd, void* index, key_t k) {
|
||||
uint8_t key = (uint8_t)k;
|
||||
((uint64_t*)index)[key] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,20 +2,56 @@
|
||||
#include <fcntl.h>
|
||||
#include "../include/binary.h"
|
||||
#include "../include/file.h"
|
||||
#include "../include/page.h"
|
||||
#include "../include/types.h"
|
||||
#include "../include/types/int8.h"
|
||||
|
||||
int main() {
|
||||
/* test int8 */
|
||||
int fd = open("types_test_tmp.bin", O_RDWR | O_CREAT | O_TRUNC, 0644);
|
||||
if(fd < 0) {
|
||||
perror("create");
|
||||
return 1;
|
||||
}
|
||||
if(init_file_header_page(fd) < 0) return 2;
|
||||
uint64_t ptr = create_index(fd, TYPE_INT8);
|
||||
if(!ptr) {
|
||||
void* index = create_index(fd, TYPE_INT8);
|
||||
if(!index) {
|
||||
perror("create_index");
|
||||
return 3;
|
||||
}
|
||||
if(le64(index-10) != HEADERSZ) {
|
||||
printf("%016llx\n", le64(index-10));
|
||||
return 4;
|
||||
}
|
||||
if(le16(index-2) != INT8_INDEX_SZ) {
|
||||
printf("%04x\n", le16(index-2));
|
||||
return 5;
|
||||
}
|
||||
insert_item(fd, TYPE_INT8, index, 1, 3456432);
|
||||
insert_item(fd, TYPE_INT8, index, 3, 7654323456);
|
||||
insert_item(fd, TYPE_INT8, index, 45, 345743415);
|
||||
insert_item(fd, TYPE_INT8, index, 67, 56787145);
|
||||
insert_item(fd, TYPE_INT8, index, 123, 123567854424);
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 1) != 3456432) return 6;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 3) != 7654323456) return 7;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 45) != 345743415) return 8;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 67) != 56787145) return 9;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 123) != 123567854424) return 10;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 255) != 0) return 11;
|
||||
unmount_block(fd, index);
|
||||
index = NULL;
|
||||
close(fd);
|
||||
fd = open("types_test_tmp.bin", O_RDWR, 0644);
|
||||
index = load_index(fd, TYPE_INT8, HEADERSZ);
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 1) != 3456432) return 6;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 3) != 7654323456) return 7;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 45) != 345743415) return 8;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 67) != 56787145) return 9;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 123) != 123567854424) return 10;
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 255) != 0) return 11;
|
||||
remove_item_by_key(fd, TYPE_INT8, index, 123);
|
||||
if(find_item_by_key(fd, TYPE_INT8, index, 123) != 0) return 12;
|
||||
close(fd);
|
||||
/* end test int8 */
|
||||
// remove("types_test_tmp.bin");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user