216 lines
6.3 KiB
C
216 lines
6.3 KiB
C
#ifndef LSM_STORE
|
|
#define LSM_STORE
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
#include "lsm.h"
|
|
#include "lsm/str.h"
|
|
|
|
#define LSM_STORE_DISK_THRESHOLD 1024
|
|
|
|
/**
|
|
* The type of an entry attribute.
|
|
*
|
|
* Each type is represented as a single bit of an
|
|
* integer, so they can be easily combined into a bitmap.
|
|
*/
|
|
typedef enum lsm_attr_type : uint64_t {
|
|
lsm_attr_type_entry_type = 1 << 0,
|
|
lsm_attr_type_content_type = 1 << 1,
|
|
lsm_attr_type_url = 1 << 2,
|
|
} lsm_attr_type;
|
|
|
|
/**
|
|
* A handle referencing an entry inside a store. Read/write operations from/to
|
|
* the entry go through this handle.
|
|
*/
|
|
typedef struct lsm_entry_handle lsm_entry_handle;
|
|
|
|
/**
|
|
* Checks whether the entry has an attribute with the specified type.
|
|
*
|
|
* @param entry entry to check
|
|
* @param type type of attribute to check for
|
|
*/
|
|
bool lsm_entry_attr_present(lsm_entry_handle *handle, lsm_attr_type type);
|
|
|
|
/**
|
|
* Retrieve the contents of an attribute from an entry, if present
|
|
*
|
|
* @param out where to store pointer to attribute data
|
|
* @param entry entry to search for
|
|
* @param type type of attribute to return
|
|
*/
|
|
lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
|
|
lsm_attr_type type);
|
|
|
|
/**
|
|
* Convenience wrapper around `lsm_entry_attr_get` that can be used if we know
|
|
* beforehand the attribute value is a 64-bit number.
|
|
*
|
|
* @param out where to store attribute data
|
|
* @param entry entry to search for
|
|
* @param type type of attribute to return
|
|
*/
|
|
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle,
|
|
lsm_attr_type type);
|
|
|
|
/**
|
|
* Add a new attribute to the entry.
|
|
*
|
|
* @param entry entry to modify
|
|
* @param type type of attribute to add
|
|
* @param data data of attribute; ownership of pointer is taken over
|
|
*/
|
|
lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, lsm_attr_type type,
|
|
lsm_str *data);
|
|
|
|
/**
|
|
* Convenience wrapper around `lsm_entry_attr_insert` that can be used if the
|
|
* data to be stored is a 64-bit number.
|
|
*
|
|
* @param entry entry to modify
|
|
* @param type type of attribute to add
|
|
* @param data data of attribute
|
|
*/
|
|
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle,
|
|
lsm_attr_type type, uint64_t data);
|
|
|
|
/**
|
|
* Remove an atribute from the given entry, if present.
|
|
*
|
|
* @param out pointer to store removed data pointer in. If NULL, data pointer
|
|
* will be leaked.
|
|
* @param entry entry to remove attribute from
|
|
* @param type type of attribute to remove
|
|
*/
|
|
lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle,
|
|
lsm_attr_type type);
|
|
|
|
/**
|
|
* A store consisting of LSM entries.
|
|
*
|
|
* A store manages both an in-memory data structure for quick lookup, and a
|
|
* database file for persistent storage of the contained entries.
|
|
*/
|
|
typedef struct lsm_store lsm_store;
|
|
|
|
/**
|
|
* Allocate and initialize a new lsm_store object.
|
|
*
|
|
* @param ptr where to store newly allocated pointer
|
|
*/
|
|
lsm_error lsm_store_init(lsm_store **ptr);
|
|
|
|
/**
|
|
* Open the given database file and load it into a new store object.
|
|
*
|
|
* @param ptr pointer to store newly allocated store
|
|
* @param data_path path to the data directory
|
|
*/
|
|
lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path);
|
|
|
|
/**
|
|
* Dealocate an existing lsm_store object.
|
|
*
|
|
* @param store object to deallocate
|
|
*/
|
|
void lsm_store_free(lsm_store *store);
|
|
|
|
/**
|
|
* Open a read handle to the given entry. This entry must be properly closed
|
|
* using `lsm_store_handle_close`.
|
|
*
|
|
* @param out pointer to store handle pointer
|
|
* @param store store to retrieve entry from
|
|
* @param key key to search
|
|
*/
|
|
lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
|
|
lsm_str *key);
|
|
|
|
/**
|
|
* Open a write handle to the given entry. This handle should only be used for
|
|
* writing; read operations on this handle are unsupported. This entry must be
|
|
* properly closed using `lsm_store_handle_close`.
|
|
*
|
|
* @param out pointer to store handle pointer
|
|
* @param store store to retrieve entry from
|
|
* @param key key to search
|
|
*/
|
|
lsm_error lsm_store_open_write(lsm_entry_handle **out, lsm_store *store,
|
|
lsm_str *key);
|
|
|
|
/**
|
|
* Close an open entry handle.
|
|
*
|
|
* @param store store the handle's entry is stored in
|
|
* @param handle handle to close
|
|
*/
|
|
void lsm_entry_close(lsm_entry_handle *handle);
|
|
|
|
/**
|
|
* Insert a new entry into the store, returning a write handle to the newly
|
|
* created entry.
|
|
*
|
|
* @param out pointer to store new entry pointer in
|
|
* @param store store to modify
|
|
* @param key key to add; ownership of key pointer is taken over
|
|
*/
|
|
lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
|
|
lsm_str *key);
|
|
|
|
/**
|
|
* Append new data to the given entry, which is expected to be in the store.
|
|
*
|
|
* This function will append either to disk or to memory, depending on the
|
|
* length of the entry's data.
|
|
*
|
|
* @param store store the entry is stored in
|
|
* @param entry entry to append data to
|
|
* @param data data to append
|
|
*/
|
|
lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
|
|
lsm_str *data);
|
|
|
|
/**
|
|
* Same as `lsm_entry_data_append`, except that it takes a direct char array.
|
|
*
|
|
* @param store store the entry is stored in
|
|
* @param entry entry to append data to
|
|
* @param data data to append
|
|
* @param len length of data array
|
|
*/
|
|
lsm_error lsm_entry_data_append_raw(lsm_store *store, lsm_entry_handle *handle,
|
|
char *data, uint64_t len);
|
|
|
|
/**
|
|
* Read a number of bytes from the entry's data field. The position from which
|
|
* data is read is dependent on previous read calls.
|
|
*
|
|
* @param out where to write how many bytes were read
|
|
* @param buf buffer to store read data in
|
|
* @param handle entry handle to read from
|
|
* @param len how many bytes to read at most
|
|
*/
|
|
lsm_error lsm_entry_data_read(uint64_t *out, char *buf,
|
|
lsm_entry_handle *handle, uint64_t len);
|
|
|
|
/**
|
|
* Persist the entry's data to disk.
|
|
*
|
|
* @param store store to persist entry in
|
|
* @param handle handle to entry to persist
|
|
*/
|
|
lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle);
|
|
|
|
/**
|
|
* Return the length of the entry's data.
|
|
*
|
|
* @param handle entry handle to return length for
|
|
* @return length of the data
|
|
*/
|
|
uint64_t lsm_entry_data_len(lsm_entry_handle *handle);
|
|
|
|
#endif
|