lander/lsm/src/_include/lsm/store_internal.h

94 lines
1.9 KiB
C
Raw Normal View History

#ifndef LSM_STORE_INTERNAL
#define LSM_STORE_INTERNAL
#include <pthread.h>
#include <stdio.h>
#include "lsm/store.h"
#include "lsm/str_internal.h"
#include "lsm/trie.h"
2023-11-07 17:43:15 +01:00
#define LSM_DB_FILE_NAME "lsm.db"
#define LSM_IDX_FILE_NAME "lsm.idx"
typedef struct lsm_attr {
lsm_attr_type type;
lsm_str *str;
} lsm_attr;
/**
* An entry inside an LSM store.
*
* Each entry consists of the key it's stored behind, zero or more attributes
* (metadata) and a data field. The data field can be stored on disk or
* in-memory, depending on the size.
*/
typedef struct lsm_entry {
lsm_str *key;
struct {
uint64_t count;
uint64_t bitmap;
lsm_attr *items;
} attrs;
struct {
uint64_t len;
union {
FILE *file;
char *ptr;
} value;
bool on_disk;
} data;
} lsm_entry;
/**
* Allocate and initialize a new lsm_entry object.
*
* @param ptr where to store newly allocated pointer
*/
lsm_error lsm_entry_init(lsm_entry **ptr);
/**
* Deallocate an existing lsm_entry object.
*
* @param entry object to deallocate
*/
void lsm_entry_free(lsm_entry *entry);
typedef struct lsm_entry_wrapper {
pthread_rwlock_t lock;
lsm_entry *entry;
} lsm_entry_wrapper;
lsm_error lsm_entry_wrapper_init(lsm_entry_wrapper **ptr);
void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper);
struct lsm_entry_handle {
lsm_entry_wrapper *wrapper;
FILE *f;
2023-10-29 14:41:40 +01:00
uint64_t pos;
};
lsm_error lsm_entry_handle_init(lsm_entry_handle **out);
struct lsm_store {
lsm_trie *trie;
lsm_str *data_path;
2023-11-07 17:43:15 +01:00
FILE *db_file;
uint64_t db_file_size;
pthread_mutex_t db_lock;
FILE *idx_file;
uint64_t idx_file_size;
pthread_mutex_t idx_lock;
};
2023-11-07 17:43:15 +01:00
/**
* Read in the database and construct the in-memory trie index. This function
* assumes the provided store is a newly initialized empty store with the
* database files opened.
*
* @param store store to read
*/
lsm_error lsm_store_load_db(lsm_store *store);
#endif