mirror of
https://github.com/fumiama/fumidb.git
synced 2026-06-11 05:30:26 +08:00
add types
This commit is contained in:
10
src/file.c
10
src/file.c
@@ -9,8 +9,16 @@
|
||||
uint8_t header[PAGESZ] = {'F', 'U', 'M', 'I', 'D', 'B', 1, 0};
|
||||
|
||||
int init_file_header_page(int fd) {
|
||||
uint8_t buf[8];
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
return write(fd, header, PAGESZ) != PAGESZ;
|
||||
if(write(fd, header, PAGESZ) != PAGESZ) return 1;
|
||||
// 将头的 HEADERSZ 字节之后的空间纳入空闲块
|
||||
if(lseek(fd, 8, SEEK_SET) < 0) return EOF;
|
||||
putle64(buf, HEADERSZ);
|
||||
if(write(fd, buf, 8) != 8) return 1;
|
||||
if(lseek(fd, HEADERSZ+8, SEEK_SET) < 0) return EOF;
|
||||
putle16(buf, PAGESZ - HEADERSZ);
|
||||
return write(fd, buf, 2) != 2;
|
||||
}
|
||||
|
||||
uint16_t get_db_version(int fd) {
|
||||
|
||||
30
src/page.c
30
src/page.c
@@ -10,7 +10,7 @@
|
||||
#include "../include/binary.h"
|
||||
#include "../include/page.h"
|
||||
|
||||
uint8_t nullpage[PAGESZ];
|
||||
static const uint8_t nullpage[PAGESZ];
|
||||
|
||||
void* alloc_page(int fd) {
|
||||
uint64_t ptr = 8, prev_ptr = 0, prev_prev_ptr = 0;
|
||||
@@ -62,6 +62,20 @@ void* alloc_page(int fd) {
|
||||
return page+8;
|
||||
}
|
||||
|
||||
void* get_page(int fd, uint64_t ptr) {
|
||||
if(ptr%PAGESZ) return NULL;
|
||||
if(lseek(fd, ptr, SEEK_SET) < 0) return NULL;
|
||||
void* page = malloc(PAGESZ+8);
|
||||
if(page == NULL) return NULL;
|
||||
putle64(page, ptr);
|
||||
page += 8;
|
||||
if(read(fd, page, PAGESZ) != PAGESZ) {
|
||||
free(page-8);
|
||||
return NULL;
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
int sync_page(int fd, void* page) {
|
||||
uint64_t ptr = le64(page-8);
|
||||
if(lseek(fd, ptr, SEEK_SET) < 0) return EOF;
|
||||
@@ -181,6 +195,20 @@ void* alloc_block(int fd, uint16_t size) {
|
||||
return blk+10;
|
||||
}
|
||||
|
||||
void* get_block(int fd, uint16_t size, uint64_t ptr) {
|
||||
if(lseek(fd, ptr, SEEK_SET) < 0) return NULL;
|
||||
void* blk = malloc(size+10);
|
||||
if(blk == NULL) return NULL;
|
||||
putle64(blk, ptr);
|
||||
putle16(blk+8, size);
|
||||
blk += 10;
|
||||
if(read(fd, blk, size) != size) {
|
||||
free(blk);
|
||||
return NULL;
|
||||
}
|
||||
return blk;
|
||||
}
|
||||
|
||||
int sync_block(int fd, void* blk) {
|
||||
uint64_t off = le64(blk-10);
|
||||
uint16_t size = le16(blk-2);
|
||||
|
||||
0
src/table.c
Normal file
0
src/table.c
Normal file
91
src/types.c
Normal file
91
src/types.c
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "../include/types.h"
|
||||
#include "../include/types/int8.h"
|
||||
|
||||
// ptr = init(fd)
|
||||
typedef uint64_t (*_type_init_t)(int);
|
||||
// ret = insert_item(fd, index, k, ptr)
|
||||
typedef int (*_insert_item_t)(int, uint64_t, key_t, uint64_t);
|
||||
// ptr = find_by_key(fd, index, k)
|
||||
typedef uint64_t (*_find_by_key_t)(int, uint64_t, key_t);
|
||||
// ret = remove_by_key(fd, index, k)
|
||||
typedef int (*_remove_by_key_t)(int, uint64_t, key_t);
|
||||
|
||||
// Function not implemented
|
||||
static uint64_t 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) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Function not implemented
|
||||
static uint64_t find_item_by_not_impl_key(int fd, uint64_t index, key_t k) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Function not implemented
|
||||
static int remove_item_by_not_impl_key(int fd, uint64_t index, key_t k) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static _type_init_t _types_init[] = {
|
||||
create_int8_index,
|
||||
create_not_impl_index,
|
||||
create_not_impl_index,
|
||||
create_not_impl_index,
|
||||
create_not_impl_index,
|
||||
create_not_impl_index,
|
||||
create_not_impl_index
|
||||
};
|
||||
|
||||
static _insert_item_t _insert_item[] = {
|
||||
insert_int8_item,
|
||||
insert_not_impl_item,
|
||||
insert_not_impl_item,
|
||||
insert_not_impl_item,
|
||||
insert_not_impl_item,
|
||||
insert_not_impl_item,
|
||||
insert_not_impl_item
|
||||
};
|
||||
|
||||
static _find_by_key_t _find_item_by_key[] = {
|
||||
find_item_by_int8_key,
|
||||
find_item_by_not_impl_key,
|
||||
find_item_by_not_impl_key,
|
||||
find_item_by_not_impl_key,
|
||||
find_item_by_not_impl_key,
|
||||
find_item_by_not_impl_key,
|
||||
find_item_by_not_impl_key
|
||||
};
|
||||
|
||||
static _remove_by_key_t _remove_item_by_key[] = {
|
||||
remove_item_by_int8_key,
|
||||
remove_item_by_not_impl_key,
|
||||
remove_item_by_not_impl_key,
|
||||
remove_item_by_not_impl_key,
|
||||
remove_item_by_not_impl_key,
|
||||
remove_item_by_not_impl_key,
|
||||
remove_item_by_not_impl_key
|
||||
};
|
||||
|
||||
uint64_t 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) {
|
||||
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) {
|
||||
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) {
|
||||
return _remove_item_by_key[t&7](fd, index, k);
|
||||
}
|
||||
29
src/types/int8.c
Normal file
29
src/types/int8.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "../../include/page.h"
|
||||
#include "../../include/types/int8.h"
|
||||
|
||||
uint64_t 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;
|
||||
}
|
||||
|
||||
int insert_int8_item(int fd, uint64_t index, key_t k, uint64_t ptr) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t find_item_by_int8_key(int fd, uint64_t index, key_t k) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int remove_item_by_int8_key(int fd, uint64_t index, key_t k) {
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user