feat(lsm): implement lsm entry add & remove
parent
115ee12f04
commit
fca8495de4
|
@ -59,6 +59,27 @@ bool lsm_entry_attr_present(lsm_entry *entry, lsm_attr_type type);
|
|||
lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry *entry,
|
||||
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 *entry, lsm_attr_type type,
|
||||
lsm_str *data);
|
||||
|
||||
/**
|
||||
* Remove an atribute from the given entry, if present.
|
||||
*
|
||||
* @param out pointer to store removed data pointer in. If NULL, data pointer
|
||||
* can get 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 *entry,
|
||||
lsm_attr_type type);
|
||||
|
||||
/**
|
||||
* A store consisting of LSM entries.
|
||||
*
|
||||
|
|
|
@ -3,10 +3,11 @@
|
|||
|
||||
#include "lsm/store.h"
|
||||
#include "lsm/str_internal.h"
|
||||
#include "lsm/trie.h"
|
||||
|
||||
typedef struct lsm_attr {
|
||||
lsm_attr_type type;
|
||||
lsm_str str;
|
||||
lsm_str *str;
|
||||
} lsm_attr;
|
||||
|
||||
struct lsm_entry {
|
||||
|
@ -19,4 +20,8 @@ struct lsm_entry {
|
|||
lsm_str data;
|
||||
};
|
||||
|
||||
struct lsm_store {
|
||||
lsm_trie *trie;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
#include "lsm/store.h"
|
||||
#include "lsm/store_internal.h"
|
||||
|
||||
/* lsm_error lsm_store_init(lsm_store **ptr) { */
|
||||
/* lsm_store *store = */
|
||||
/* } */
|
|
@ -1,4 +1,6 @@
|
|||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lsm.h"
|
||||
#include "lsm/store_internal.h"
|
||||
|
@ -27,7 +29,7 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry *entry,
|
|||
|
||||
for (uint64_t i = 0; i < entry->attrs.count; i++) {
|
||||
if (entry->attrs.items[i].type == type) {
|
||||
*out = &entry->attrs.items[i].str;
|
||||
*out = entry->attrs.items[i].str;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
@ -35,3 +37,72 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry *entry,
|
|||
|
||||
return lsm_error_not_found;
|
||||
}
|
||||
|
||||
lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry *entry,
|
||||
lsm_attr_type type) {
|
||||
if (!lsm_entry_attr_present(entry, type)) {
|
||||
return lsm_error_not_found;
|
||||
}
|
||||
|
||||
if (entry->attrs.count == 1) {
|
||||
*out = entry->attrs.items[0].str;
|
||||
|
||||
free(entry->attrs.items);
|
||||
entry->attrs.items = NULL;
|
||||
entry->attrs.count = 0;
|
||||
entry->attrs.bitmap = 0;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
uint64_t i = 0;
|
||||
|
||||
while (entry->attrs.items[i].type != type) {
|
||||
i++;
|
||||
}
|
||||
|
||||
lsm_attr *new_attrs = malloc((entry->attrs.count - 1) * sizeof(lsm_attr));
|
||||
|
||||
if (new_attrs == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
if (out != NULL) {
|
||||
*out = entry->attrs.items[i].str;
|
||||
}
|
||||
|
||||
memcpy(new_attrs, entry->attrs.items, i * sizeof(lsm_attr));
|
||||
memcpy(&new_attrs[i], &entry->attrs.items[i + 1],
|
||||
(entry->attrs.count - i - 1) * sizeof(lsm_attr));
|
||||
|
||||
free(entry->attrs.items);
|
||||
|
||||
entry->attrs.items = new_attrs;
|
||||
entry->attrs.count--;
|
||||
entry->attrs.bitmap &= ~type;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_entry_attr_insert(lsm_entry *entry, lsm_attr_type type,
|
||||
lsm_str *data) {
|
||||
if (lsm_entry_attr_present(entry, type)) {
|
||||
return lsm_error_already_present;
|
||||
}
|
||||
|
||||
lsm_attr *new_attrs =
|
||||
realloc(entry->attrs.items, (entry->attrs.count + 1) * sizeof(lsm_attr));
|
||||
|
||||
if (new_attrs == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
new_attrs[entry->attrs.count].type = type;
|
||||
new_attrs[entry->attrs.count].str = data;
|
||||
|
||||
entry->attrs.items = new_attrs;
|
||||
entry->attrs.count++;
|
||||
entry->attrs.bitmap |= type;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue