feat(lsm): introduce entry handles for concurrent access

This commit is contained in:
Jef Roosens 2023-10-29 12:19:59 +01:00
parent 0e4e18da6c
commit f44c512099
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 195 additions and 68 deletions

View file

@ -21,27 +21,10 @@ typedef enum lsm_attr_type : uint64_t {
} lsm_attr_type;
/**
* 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.
* A handle referencing an entry inside a store. Read/write operations from/to
* the entry go through this handle.
*/
typedef struct lsm_entry 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_handle lsm_entry_handle;
/**
* Checks whether the entry has an attribute with the specified type.
@ -49,7 +32,7 @@ void lsm_entry_free(lsm_entry *entry);
* @param entry entry to check
* @param type type of attribute to check for
*/
bool lsm_entry_attr_present(lsm_entry *entry, lsm_attr_type type);
bool lsm_entry_attr_present(lsm_entry_handle *handle, lsm_attr_type type);
/**
* Retrieve the contents of an attribute from an entry, if present
@ -58,7 +41,7 @@ bool lsm_entry_attr_present(lsm_entry *entry, lsm_attr_type type);
* @param entry entry to search for
* @param type type of attribute to return
*/
lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry *entry,
lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
lsm_attr_type type);
/**
@ -68,7 +51,7 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry *entry,
* @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 *entry, lsm_attr_type type,
lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, lsm_attr_type type,
lsm_str *data);
/**
@ -79,7 +62,7 @@ lsm_error lsm_entry_attr_insert(lsm_entry *entry, lsm_attr_type type,
* @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 *entry,
lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle,
lsm_attr_type type);
/**
@ -104,7 +87,7 @@ lsm_error lsm_store_init(lsm_store **ptr);
* @param db_path path to the database file
* @param data_path path to the data directory
*/
lsm_error lsm_store_open(lsm_store **ptr, lsm_str *db_path, lsm_str *data_path);
lsm_error lsm_store_load(lsm_store **ptr, lsm_str *db_path, lsm_str *data_path);
/**
* Dealocate an existing lsm_store object.
@ -114,43 +97,45 @@ lsm_error lsm_store_open(lsm_store **ptr, lsm_str *db_path, lsm_str *data_path);
void lsm_store_free(lsm_store *store);
/**
* Retrieve an entry from the store, preparing & locking it for the purpose of
* reading.
* Open a read handle to the given entry. This entry must be properly closed
* using `lsm_store_handle_close`.
*
* @param out pointer to store entry pointer
* @param out pointer to store handle pointer
* @param store store to retrieve entry from
* @param key key to search
*/
lsm_error lsm_store_get_read(lsm_entry **out, lsm_store *store, lsm_str *key);
lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
lsm_str *key);
/**
* Retrieve an entry from the store for the purposes of writing. This
* write-locks the entry.
* Open a write handle to the given entry. This entry must be properly closed
* using `lsm_store_handle_close`.
*
* @param out pointer to store entry pointer
* @param out pointer to store handle pointer
* @param store store to retrieve entry from
* @param key key to search
*/
lsm_error lsm_store_get_write(lsm_entry **out, lsm_store *store, lsm_str *key);
lsm_error lsm_store_open_write(lsm_entry_handle **out, lsm_store *store,
lsm_str *key);
/**
* Unlock a locked entry.
* Close an open entry handle.
*
* @param store store to unlock entry in
* @param entry entry to unlock
* @param store store the handle's entry is stored in
* @param handle handle to close
*/
lsm_error lsm_store_unlock(lsm_store *store, lsm_entry *entry);
void lsm_entry_close(lsm_entry_handle *handle);
/**
* Allocate a new entry in the store with the specified key. The entry returned
* will be write-locked, and should be unlocked after streaming the necessary
* data.
* 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 **out, lsm_store *store, lsm_str *key);
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.
@ -162,7 +147,7 @@ lsm_error lsm_store_insert(lsm_entry **out, lsm_store *store, lsm_str *key);
* @param entry entry to append data to
* @param data data to append
*/
lsm_error lsm_store_data_write(lsm_store *store, lsm_entry *entry,
lsm_str *data);
lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
lsm_str *data);
#endif