2023-10-17 11:25:51 +02:00
|
|
|
#ifndef LSM_STORE_INTERNAL
|
|
|
|
#define LSM_STORE_INTERNAL
|
|
|
|
|
2023-10-28 15:48:28 +02:00
|
|
|
#include <pthread.h>
|
2023-10-25 10:57:45 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-10-17 11:25:51 +02:00
|
|
|
#include "lsm/store.h"
|
|
|
|
#include "lsm/str_internal.h"
|
2023-10-20 10:41:53 +02:00
|
|
|
#include "lsm/trie.h"
|
2023-10-17 11:25:51 +02:00
|
|
|
|
2023-11-07 17:43:15 +01:00
|
|
|
#define LSM_DB_FILE_NAME "lsm.db"
|
|
|
|
#define LSM_IDX_FILE_NAME "lsm.idx"
|
|
|
|
|
2023-10-17 11:25:51 +02:00
|
|
|
typedef struct lsm_attr {
|
|
|
|
lsm_attr_type type;
|
2023-10-20 10:41:53 +02:00
|
|
|
lsm_str *str;
|
2023-10-17 11:25:51 +02:00
|
|
|
} lsm_attr;
|
|
|
|
|
2023-10-29 12:19:59 +01:00
|
|
|
/**
|
|
|
|
* 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 {
|
2023-10-25 10:57:45 +02:00
|
|
|
lsm_str *key;
|
2023-10-17 11:25:51 +02:00
|
|
|
struct {
|
|
|
|
uint64_t count;
|
|
|
|
uint64_t bitmap;
|
|
|
|
lsm_attr *items;
|
|
|
|
} attrs;
|
2023-10-25 10:57:45 +02:00
|
|
|
struct {
|
|
|
|
uint64_t len;
|
|
|
|
union {
|
|
|
|
FILE *file;
|
|
|
|
char *ptr;
|
|
|
|
} value;
|
|
|
|
bool on_disk;
|
|
|
|
} data;
|
2023-10-29 12:19:59 +01:00
|
|
|
} 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);
|
2023-10-17 11:25:51 +02:00
|
|
|
|
2023-10-28 15:48:28 +02:00
|
|
|
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);
|
2023-10-29 12:19:59 +01:00
|
|
|
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;
|
2023-10-29 12:19:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
lsm_error lsm_entry_handle_init(lsm_entry_handle **out);
|
2023-10-28 15:48:28 +02:00
|
|
|
|
2023-10-20 10:41:53 +02:00
|
|
|
struct lsm_store {
|
|
|
|
lsm_trie *trie;
|
2023-10-28 15:48:28 +02:00
|
|
|
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-10-20 10:41:53 +02:00
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
2023-10-17 11:25:51 +02:00
|
|
|
#endif
|