implemented database functions

This commit is contained in:
2025-08-08 21:42:35 +03:00
parent eb8bad55ee
commit a19c5f7023
3 changed files with 335 additions and 2 deletions

50
src/db/idb.h Normal file
View File

@@ -0,0 +1,50 @@
#pragma once
#include "tlibc/errors.h"
#include "tlibc/string/str.h"
#include "tlibc/collections/Array.h"
#include "tlibc/collections/HashMap.h"
typedef struct IncrementalDB IncrementalDB;
typedef union Magic32 {
u32 n;
u8 bytes[4];
} Magic32;
typedef struct TableHeader {
Magic32 magic;
u16 version;
bool _dirty_bit;
u32 row_size;
} __attribute__((aligned(256))) TableHeader;
typedef struct Table {
TableHeader header;
IncrementalDB* db;
str name;
str table_file_path;
str changes_file_path;
FILE* table_file;
FILE* changes_file;
u64 row_count;
} Table;
typedef struct IncrementalDB {
str db_dir;
HashMap(Table**) tables_map;
} IncrementalDB;
Result(IncrementalDB*) idb_open(str db_dir);
void idb_close(IncrementalDB* db);
Result(Table*) idb_getOrCreateTable(IncrementalDB* db, str _table_name, u32 row_size);
Result(void) idb_getRows(Table* t, u64 id, void* dst, u64 count);
#define idb_getRow(T, ID, DST) idb_getRows(T, ID, DST, 1)
Result(u64) idb_pushRows(Table* t, const void* src, u64 count);
#define idb_pushRow(T, SRC) idb_pushRows(T, SRC, 1)
Result(void) idb_updateRows(Table* t, u64 id, const void* src, u64 count);
#define idb_updateRow(T, ID, SRC) idb_updateRows(T, ID, SRC, 1)