wpi
ci/woodpecker/push/build Pipeline was successful
Details
ci/woodpecker/push/build Pipeline was successful
Details
parent
80281c702b
commit
f09405574b
|
@ -13,7 +13,11 @@ typedef struct lander_gctx {
|
|||
} lander_gctx;
|
||||
|
||||
typedef struct lander_ctx {
|
||||
lsm_entry_handle *entry;
|
||||
union {
|
||||
lsm_write_handle *write;
|
||||
lsm_read_handle *read;
|
||||
} entry;
|
||||
bool write;
|
||||
} lander_ctx;
|
||||
|
||||
typedef enum lander_attr_type : uint8_t {
|
||||
|
|
|
@ -19,12 +19,6 @@ typedef struct lsm_read_handle lsm_read_handle;
|
|||
*/
|
||||
typedef struct lsm_write_handle lsm_write_handle;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* A store consisting of LSM entries.
|
||||
*
|
||||
|
@ -86,23 +80,6 @@ lsm_error lsm_store_open_read(lsm_read_handle **out, lsm_store *store,
|
|||
lsm_error lsm_store_open_write(lsm_write_handle **out, lsm_store *store,
|
||||
const lsm_str *key);
|
||||
|
||||
/**
|
||||
* Commit any changes to the persistent storage. Any changes, insertions or
|
||||
* deletions that occured without a commit are reverted when the handle is
|
||||
* closed.
|
||||
*
|
||||
* @param handle handle to the entry
|
||||
*/
|
||||
lsm_error lsm_entry_commit(lsm_entry_handle *handle);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -111,57 +88,43 @@ void lsm_entry_close(lsm_entry_handle *handle);
|
|||
* @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);
|
||||
lsm_error lsm_store_open_new(lsm_write_handle **out, lsm_store *store,
|
||||
lsm_str *key);
|
||||
|
||||
/**
|
||||
* Mark the entry as removed.
|
||||
*
|
||||
* @param handle handle to entry to remove
|
||||
*/
|
||||
void lsm_entry_remove(lsm_entry_handle *handle);
|
||||
bool lsm_read_attr_present(lsm_read_handle *handle, uint8_t type);
|
||||
lsm_error lsm_read_attr_get(const lsm_str **out, const lsm_read_handle *handle,
|
||||
uint8_t type);
|
||||
lsm_error lsm_read_attr_get_uint64_t(uint64_t *out,
|
||||
const lsm_read_handle *handle,
|
||||
uint8_t type);
|
||||
lsm_error lsm_read_attr_get_uint8_t(uint8_t *out, const lsm_read_handle *handle,
|
||||
uint8_t type);
|
||||
uint64_t lsm_read_data_len(const lsm_read_handle *handle);
|
||||
lsm_error lsm_read_data_read(uint64_t *out, char *buf, lsm_read_handle *handle,
|
||||
uint64_t len);
|
||||
void lsm_read_close(lsm_read_handle *handle);
|
||||
|
||||
/**
|
||||
* 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_entry_handle *handle, const 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_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_read_handle *handle,
|
||||
uint64_t len);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
bool lsm_write_attr_present(const lsm_write_handle *handle, uint8_t type);
|
||||
lsm_error lsm_write_attr_get(const lsm_str **out,
|
||||
const lsm_write_handle *handle, uint8_t type);
|
||||
lsm_error lsm_write_attr_get_uint64_t(uint64_t *out,
|
||||
const lsm_write_handle *handle,
|
||||
uint8_t type);
|
||||
lsm_error lsm_write_attr_get_uint8_t(uint8_t *out,
|
||||
const lsm_write_handle *handle,
|
||||
uint8_t type);
|
||||
lsm_error lsm_write_attr_remove(lsm_str **out, lsm_write_handle *handle,
|
||||
uint8_t type);
|
||||
lsm_error lsm_write_attr_insert(lsm_write_handle *handle, uint8_t type,
|
||||
lsm_str *data);
|
||||
lsm_error lsm_write_attr_insert_uint64_t(lsm_write_handle *handle, uint8_t type,
|
||||
uint64_t data);
|
||||
lsm_error lsm_write_attr_insert_uint8_t(lsm_write_handle *handle, uint8_t type,
|
||||
uint8_t data);
|
||||
uint64_t lsm_write_data_len(const lsm_write_handle *handle);
|
||||
lsm_error lsm_write_data_append(lsm_write_handle *handle, const lsm_str *data);
|
||||
void lsm_write_remove(lsm_write_handle *handle);
|
||||
void lsm_write_close(lsm_write_handle *handle);
|
||||
lsm_error lsm_write_commit(lsm_write_handle *handle);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -74,20 +74,6 @@ typedef enum lsm_entry_handle_state : uint8_t {
|
|||
lsm_entry_handle_state_removed = 1 << 2,
|
||||
} lsm_entry_handle_state;
|
||||
|
||||
struct lsm_entry_handle {
|
||||
lsm_entry_wrapper *wrapper;
|
||||
lsm_store *store;
|
||||
|
||||
// Either read or append, depending on how it was opened
|
||||
FILE *f;
|
||||
// Current position in the file pointer
|
||||
uint64_t pos;
|
||||
// Required to determine in what way the database files need to be synced
|
||||
uint64_t states;
|
||||
};
|
||||
|
||||
lsm_error lsm_entry_handle_init(lsm_entry_handle **out);
|
||||
|
||||
struct lsm_store {
|
||||
lsm_trie *trie;
|
||||
lsm_str *data_path;
|
||||
|
@ -120,21 +106,21 @@ lsm_error lsm_store_load_db(lsm_store *store);
|
|||
*
|
||||
* @param handle handle to added entry
|
||||
*/
|
||||
lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle);
|
||||
lsm_error lsm_entry_disk_insert(lsm_store *store, lsm_entry *entry);
|
||||
|
||||
/**
|
||||
* Remove an entry from the database.
|
||||
*
|
||||
* @param handle handle to the removed entry
|
||||
*/
|
||||
lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle);
|
||||
lsm_error lsm_entry_disk_remove(lsm_store *store, lsm_entry *entry);
|
||||
|
||||
/**
|
||||
* Update an existing entry already in the store.
|
||||
*
|
||||
* @param handle to updated entry
|
||||
*/
|
||||
lsm_error lsm_entry_disk_update(lsm_entry_handle *handle);
|
||||
lsm_error lsm_entry_disk_update(lsm_store *store, lsm_entry *entry);
|
||||
|
||||
/**
|
||||
* Return the length of the path to this entry's data file
|
||||
|
@ -156,14 +142,6 @@ void lsm_entry_data_path(char *path, const lsm_store *store,
|
|||
*/
|
||||
lsm_error lsm_entry_data_open_read(lsm_read_handle *handle);
|
||||
|
||||
/**
|
||||
* Open the entry's data file for writing. The file and all subdirectories in
|
||||
* the data dir are created as needed.
|
||||
*
|
||||
* @param handle handle to the entry
|
||||
*/
|
||||
lsm_error lsm_entry_data_open_write(lsm_entry_handle *handle);
|
||||
|
||||
lsm_error lsm_entry_data_open(FILE **out, const lsm_store *store,
|
||||
const lsm_entry *entry, const char *mode);
|
||||
lsm_error lsm_entry_data_mkdirs(const lsm_store *store, const lsm_entry *entry);
|
||||
|
@ -191,7 +169,7 @@ bool lsm_entry_attr_present(const lsm_entry *entry, uint8_t type);
|
|||
* @param entry entry to search for
|
||||
* @param type type of attribute to return
|
||||
*/
|
||||
lsm_error lsm_entry_attr_get(lsm_str **out, const lsm_entry *entry,
|
||||
lsm_error lsm_entry_attr_get(const lsm_str **out, const lsm_entry *entry,
|
||||
uint8_t type);
|
||||
|
||||
/**
|
||||
|
@ -277,6 +255,7 @@ struct lsm_write_handle {
|
|||
lsm_store *store;
|
||||
|
||||
lsm_entry *dirty;
|
||||
bool removed;
|
||||
|
||||
struct {
|
||||
FILE *f;
|
||||
|
@ -294,4 +273,9 @@ lsm_error lsm_read_handle_init(lsm_read_handle **out);
|
|||
*/
|
||||
lsm_error lsm_write_handle_init(lsm_write_handle **out);
|
||||
|
||||
/**
|
||||
* Commit changes solely to the memory part of the store.
|
||||
*/
|
||||
void lsm_write_commit_mem(lsm_write_handle *handle);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -15,25 +15,29 @@ lsm_error lsm_read_handle_init(lsm_read_handle **out) {
|
|||
}
|
||||
|
||||
bool lsm_read_attr_present(lsm_read_handle *handle, uint8_t type) {
|
||||
return (handle->wrapper->entry->attrs.bitmap[type / 64] &
|
||||
(((uint64_t)1) << (type % 64))) != 0;
|
||||
return lsm_entry_attr_present(handle->wrapper->entry, type);
|
||||
}
|
||||
|
||||
lsm_error lsm_read_attr_get(lsm_str **out, lsm_read_handle *handle,
|
||||
lsm_error lsm_read_attr_get(const lsm_str **out, const lsm_read_handle *handle,
|
||||
uint8_t type) {
|
||||
return lsm_entry_attr_get(out, handle->wrapper->entry, type);
|
||||
}
|
||||
|
||||
lsm_error lsm_read_attr_get_uint64_t(uint64_t *out, lsm_read_handle *handle,
|
||||
lsm_error lsm_read_attr_get_uint64_t(uint64_t *out,
|
||||
const lsm_read_handle *handle,
|
||||
uint8_t type) {
|
||||
return lsm_entry_attr_get_uint64_t(out, handle->wrapper->entry, type);
|
||||
}
|
||||
|
||||
lsm_error lsm_read_attr_get_uint8_t(uint8_t *out, lsm_read_handle *handle,
|
||||
lsm_error lsm_read_attr_get_uint8_t(uint8_t *out, const lsm_read_handle *handle,
|
||||
uint8_t type) {
|
||||
return lsm_entry_attr_get_uint8_t(out, handle->wrapper->entry, type);
|
||||
}
|
||||
|
||||
uint64_t lsm_read_data_len(const lsm_read_handle *handle) {
|
||||
return handle->wrapper->entry->data_len;
|
||||
}
|
||||
|
||||
lsm_error lsm_read_data_read(uint64_t *out, char *buf, lsm_read_handle *handle,
|
||||
uint64_t len) {
|
||||
const lsm_entry *entry = handle->wrapper->entry;
|
||||
|
@ -54,6 +58,8 @@ lsm_error lsm_read_data_read(uint64_t *out, char *buf, lsm_read_handle *handle,
|
|||
|
||||
read = fread(buf, sizeof(char), len, handle->data.f);
|
||||
|
||||
printf("len = %lu, read = %lu\n", len, read);
|
||||
|
||||
if ((read == 0) && (ferror(handle->data.f) != 0)) {
|
||||
return lsm_error_failed_io;
|
||||
}
|
||||
|
|
|
@ -15,8 +15,7 @@ lsm_error lsm_write_handle_init(lsm_write_handle **out) {
|
|||
}
|
||||
|
||||
bool lsm_write_attr_present(const lsm_write_handle *handle, uint8_t type) {
|
||||
return (handle->wrapper->entry->attrs.bitmap[type / 64] &
|
||||
(((uint64_t)1) << (type % 64))) != 0;
|
||||
return lsm_entry_attr_present(handle->wrapper->entry, type);
|
||||
}
|
||||
|
||||
lsm_error lsm_write_attr_get(const lsm_str **out,
|
||||
|
@ -78,6 +77,13 @@ lsm_error lsm_write_attr_insert_uint8_t(lsm_write_handle *handle, uint8_t type,
|
|||
return lsm_entry_attr_insert_uint8_t(handle->dirty, type, data);
|
||||
}
|
||||
|
||||
uint64_t lsm_write_data_len(const lsm_write_handle *handle) {
|
||||
lsm_entry *entry =
|
||||
handle->dirty == NULL ? handle->wrapper->entry : handle->dirty;
|
||||
|
||||
return entry->data_len;
|
||||
}
|
||||
|
||||
lsm_error lsm_write_data_append(lsm_write_handle *handle, const lsm_str *data) {
|
||||
if (lsm_str_len(data) == 0) {
|
||||
return lsm_error_ok;
|
||||
|
@ -112,3 +118,66 @@ lsm_error lsm_write_data_append(lsm_write_handle *handle, const lsm_str *data) {
|
|||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_write_remove(lsm_write_handle *handle) { handle->removed = true; }
|
||||
|
||||
void lsm_write_close(lsm_write_handle *handle) {
|
||||
if (handle->data.f != NULL) {
|
||||
fclose(handle->data.f);
|
||||
handle->data.f = NULL;
|
||||
}
|
||||
|
||||
if (handle->dirty != NULL) {
|
||||
// Entry was never committed to store, so any created data file should be
|
||||
// removed
|
||||
if (handle->wrapper->entry == NULL) {
|
||||
lsm_entry_data_remove(handle->store, handle->dirty);
|
||||
}
|
||||
|
||||
lsm_entry_free(handle->dirty);
|
||||
}
|
||||
|
||||
pthread_rwlock_unlock(&handle->wrapper->lock);
|
||||
free(handle);
|
||||
}
|
||||
|
||||
lsm_error lsm_write_commit(lsm_write_handle *handle) {
|
||||
if (handle->removed && (handle->wrapper->entry != NULL)) {
|
||||
LSM_RES(lsm_entry_disk_remove(handle->store, handle->wrapper->entry));
|
||||
|
||||
lsm_entry_free(handle->wrapper->entry);
|
||||
handle->wrapper->entry = NULL;
|
||||
handle->removed = false;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
if (handle->dirty == NULL) {
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
if (handle->wrapper->entry == NULL) {
|
||||
LSM_RES(lsm_entry_disk_insert(handle->store, handle->dirty));
|
||||
} else {
|
||||
LSM_RES(lsm_entry_disk_update(handle->store, handle->dirty));
|
||||
lsm_entry_free(handle->wrapper->entry);
|
||||
}
|
||||
|
||||
handle->wrapper->entry = handle->dirty;
|
||||
handle->dirty = NULL;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_write_commit_mem(lsm_write_handle *handle) {
|
||||
if (handle->dirty == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (handle->wrapper->entry != NULL) {
|
||||
lsm_entry_free(handle->wrapper->entry);
|
||||
}
|
||||
|
||||
handle->wrapper->entry = handle->dirty;
|
||||
handle->dirty = NULL;
|
||||
}
|
||||
|
|
|
@ -88,8 +88,8 @@ lsm_error lsm_store_open_write(lsm_write_handle **out, lsm_store *store,
|
|||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
|
||||
lsm_str *key) {
|
||||
lsm_error lsm_store_open_new(lsm_write_handle **out, lsm_store *store,
|
||||
lsm_str *key) {
|
||||
// TODO what happens when two inserts to the same key happen at the same time?
|
||||
lsm_entry_wrapper *wrapper;
|
||||
|
||||
|
@ -117,24 +117,16 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
|
|||
LSM_RES2(lsm_entry_init(&entry), pthread_rwlock_unlock(&wrapper->lock));
|
||||
|
||||
entry->key = key;
|
||||
wrapper->entry = entry;
|
||||
|
||||
lsm_entry_handle *handle;
|
||||
LSM_RES2(lsm_entry_handle_init(&handle),
|
||||
lsm_write_handle *handle;
|
||||
LSM_RES2(lsm_write_handle_init(&handle),
|
||||
pthread_rwlock_unlock(&wrapper->lock));
|
||||
|
||||
// No need to set the handle's file, as the entry doesn't have any data yet
|
||||
handle->wrapper = wrapper;
|
||||
handle->store = store;
|
||||
|
||||
// Newly inserted entries are always dirty
|
||||
handle->states |= lsm_entry_handle_state_new;
|
||||
handle->dirty = entry;
|
||||
|
||||
*out = handle;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_entry_remove(lsm_entry_handle *handle) {
|
||||
handle->states |= lsm_entry_handle_state_removed;
|
||||
}
|
||||
|
|
|
@ -130,7 +130,7 @@ static lsm_error lsm_entry_read_str(lsm_str **out, uint64_t *sum, FILE *f) {
|
|||
return lsm_str_init(out, buf);
|
||||
}
|
||||
|
||||
static lsm_error lsm_entry_read_attrs(uint64_t *sum, lsm_entry_handle *handle,
|
||||
static lsm_error lsm_entry_read_attrs(uint64_t *sum, lsm_entry *entry,
|
||||
FILE *db_file) {
|
||||
uint8_t attr_count;
|
||||
LSM_RES(lsm_fread(&attr_count, sum, db_file, sizeof(uint8_t), 1));
|
||||
|
@ -142,11 +142,12 @@ static lsm_error lsm_entry_read_attrs(uint64_t *sum, lsm_entry_handle *handle,
|
|||
for (uint64_t i = 0; i < attr_count; i++) {
|
||||
LSM_RES(lsm_fread(&attr_type, sum, db_file, sizeof(uint8_t), 1));
|
||||
LSM_RES(lsm_entry_read_str(&val, sum, db_file));
|
||||
LSM_RES(lsm_entry_attr_insert(handle, attr_type, val));
|
||||
LSM_RES(lsm_entry_attr_insert(entry, attr_type, val));
|
||||
}
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
static lsm_error lsm_fseek(FILE *f, uint64_t pos) {
|
||||
if (fseek(f, pos, SEEK_SET) != 0) {
|
||||
return lsm_error_failed_io;
|
||||
|
@ -165,17 +166,17 @@ lsm_error lsm_store_insert_from_db(lsm_store *store, uint64_t pos,
|
|||
lsm_str *key;
|
||||
LSM_RES(lsm_entry_read_str(&key, NULL, store->db.f));
|
||||
|
||||
lsm_entry_handle *handle;
|
||||
LSM_RES(lsm_store_insert(&handle, store, key));
|
||||
lsm_write_handle *handle;
|
||||
LSM_RES(lsm_store_open_new(&handle, store, key));
|
||||
|
||||
LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db.f,
|
||||
LSM_RES(lsm_fread(&handle->dirty->data_len, NULL, store->db.f,
|
||||
sizeof(uint64_t), 1));
|
||||
LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db.f));
|
||||
LSM_RES(lsm_entry_read_attrs(NULL, handle->dirty, store->db.f));
|
||||
|
||||
handle->wrapper->entry->idx_file_offset = idx_file_offset;
|
||||
handle->dirty->idx_file_offset = idx_file_offset;
|
||||
|
||||
handle->states = 0;
|
||||
lsm_entry_close(handle);
|
||||
lsm_write_commit_mem(handle);
|
||||
lsm_write_close(handle);
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
|
|
@ -74,16 +74,14 @@ lsm_error lsm_write_idx_entry(uint64_t *size, FILE *idx_file, uint64_t offset,
|
|||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle) {
|
||||
lsm_store *store = handle->store;
|
||||
|
||||
lsm_error lsm_entry_disk_insert(lsm_store *store, lsm_entry *entry) {
|
||||
pthread_mutex_lock(&store->db.lock);
|
||||
|
||||
uint64_t db_entry_index = store->db.size;
|
||||
|
||||
uint64_t db_entry_size;
|
||||
lsm_error res = lsm_write_db_entry(&db_entry_size, store->db.f,
|
||||
handle->wrapper->entry, store->db.size);
|
||||
lsm_error res =
|
||||
lsm_write_db_entry(&db_entry_size, store->db.f, entry, store->db.size);
|
||||
fflush(store->db.f);
|
||||
|
||||
pthread_mutex_unlock(&store->db.lock);
|
||||
|
@ -117,7 +115,7 @@ lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle) {
|
|||
store->idx.block_count = new_block_count;
|
||||
store->db.size += db_entry_size;
|
||||
|
||||
handle->wrapper->entry->idx_file_offset = idx_entry_index;
|
||||
entry->idx_file_offset = idx_entry_index;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -154,24 +152,21 @@ static lsm_error lsm_idx_zero_block(lsm_store *store, uint64_t pos) {
|
|||
|
||||
// Marking an entry as removed in the idx file is simply setting the length of
|
||||
// its entry to zero
|
||||
lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle) {
|
||||
const lsm_entry *entry = handle->wrapper->entry;
|
||||
|
||||
LSM_RES(lsm_idx_zero_block(handle->store,
|
||||
entry->idx_file_offset + sizeof(uint64_t)));
|
||||
LSM_RES(lsm_entry_data_remove(handle));
|
||||
lsm_error lsm_entry_disk_remove(lsm_store *store, lsm_entry *entry) {
|
||||
LSM_RES(lsm_idx_zero_block(store, entry->idx_file_offset + sizeof(uint64_t)));
|
||||
LSM_RES(lsm_entry_data_remove(store, entry));
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_entry_disk_update(lsm_entry_handle *handle) {
|
||||
lsm_error lsm_entry_disk_update(lsm_store *store, lsm_entry *entry) {
|
||||
// An update is implemented by reinserting the entry at the end of the db file
|
||||
uint64_t old_idx_index = handle->wrapper->entry->idx_file_offset;
|
||||
uint64_t old_idx_index = entry->idx_file_offset;
|
||||
|
||||
// TODO is there any way we can make this atomic? If the zero write to the
|
||||
// index file fails, there are two entries in the db file for the same key.
|
||||
LSM_RES(lsm_entry_disk_insert(handle));
|
||||
LSM_RES(lsm_idx_zero_block(handle->store, old_idx_index + sizeof(uint64_t)));
|
||||
LSM_RES(lsm_entry_disk_insert(store, entry));
|
||||
LSM_RES(lsm_idx_zero_block(store, old_idx_index + sizeof(uint64_t)));
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,10 @@ lsm_error lsm_entry_init(lsm_entry **ptr) {
|
|||
|
||||
void lsm_entry_free(lsm_entry *entry) {
|
||||
if (entry->attrs.count > 0) {
|
||||
for (size_t i = 0; i < entry->attrs.count; i++) {
|
||||
lsm_str_free(entry->attrs.items[i].str);
|
||||
}
|
||||
|
||||
free(entry->attrs.items);
|
||||
}
|
||||
|
||||
|
@ -75,68 +79,6 @@ lsm_error lsm_entry_wrapper_init(lsm_entry_wrapper **ptr) {
|
|||
|
||||
void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper) { free(wrapper); }
|
||||
|
||||
lsm_error lsm_entry_handle_init(lsm_entry_handle **out) {
|
||||
lsm_entry_handle *handle = calloc(1, sizeof(lsm_entry_handle));
|
||||
|
||||
if (handle == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
*out = handle;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_entry_commit(lsm_entry_handle *handle) {
|
||||
uint8_t state_new = handle->states & lsm_entry_handle_state_new;
|
||||
uint8_t state_removed = handle->states & lsm_entry_handle_state_removed;
|
||||
uint8_t state_updated = handle->states & lsm_entry_handle_state_updated;
|
||||
|
||||
// Clean new entry
|
||||
if (state_new && !state_removed) {
|
||||
LSM_RES(lsm_entry_disk_insert(handle));
|
||||
}
|
||||
// Previously stored entry that needs to be removed; should be removed from db
|
||||
// file as well
|
||||
else if (state_removed && !state_new) {
|
||||
LSM_RES(lsm_entry_disk_remove(handle));
|
||||
|
||||
lsm_entry_free(handle->wrapper->entry);
|
||||
handle->wrapper->entry = NULL;
|
||||
} else if (state_updated && !(state_new || state_removed)) {
|
||||
LSM_RES(lsm_entry_disk_update(handle));
|
||||
}
|
||||
|
||||
// Reset states after committing current changes
|
||||
handle->states = 0;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_entry_close(lsm_entry_handle *handle) {
|
||||
if (handle->f != NULL) {
|
||||
fclose(handle->f);
|
||||
handle->f = NULL;
|
||||
}
|
||||
|
||||
uint8_t state_new = handle->states & lsm_entry_handle_state_new;
|
||||
/* bool state_updated = handle->states & lsm_entry_handle_state_updated; */
|
||||
|
||||
// New entries create a wrapper in the trie that should be removed if not
|
||||
// committed
|
||||
if (state_new) {
|
||||
lsm_entry_data_remove(handle);
|
||||
|
||||
lsm_entry_free(handle->wrapper->entry);
|
||||
handle->wrapper->entry = NULL;
|
||||
}
|
||||
|
||||
// TODO rollback uncomitted updates
|
||||
|
||||
pthread_rwlock_unlock(&handle->wrapper->lock);
|
||||
free(handle);
|
||||
}
|
||||
|
||||
bool lsm_entry_attr_present(const lsm_entry *entry, uint8_t type) {
|
||||
return (entry->attrs.bitmap[type / 64] & (((uint64_t)1) << (type % 64))) != 0;
|
||||
}
|
||||
|
@ -160,7 +102,7 @@ lsm_error lsm_entry_attr_get(const lsm_str **out, const lsm_entry *entry,
|
|||
|
||||
lsm_error lsm_entry_attr_get_uint64_t(uint64_t *out, const lsm_entry *entry,
|
||||
uint8_t type) {
|
||||
lsm_str *s;
|
||||
const lsm_str *s;
|
||||
|
||||
LSM_RES(lsm_entry_attr_get(&s, entry, type));
|
||||
|
||||
|
@ -177,7 +119,7 @@ lsm_error lsm_entry_attr_get_uint64_t(uint64_t *out, const lsm_entry *entry,
|
|||
|
||||
lsm_error lsm_entry_attr_get_uint8_t(uint8_t *out, const lsm_entry *entry,
|
||||
uint8_t type) {
|
||||
lsm_str *s;
|
||||
const lsm_str *s;
|
||||
|
||||
LSM_RES(lsm_entry_attr_get(&s, entry, type));
|
||||
|
||||
|
@ -273,10 +215,6 @@ lsm_error lsm_entry_attr_insert_uint8_t(lsm_entry *entry, uint8_t type,
|
|||
return lsm_entry_attr_insert(entry, type, s);
|
||||
}
|
||||
|
||||
uint64_t lsm_entry_data_len(lsm_entry_handle *handle) {
|
||||
return handle->wrapper->entry->data_len;
|
||||
}
|
||||
|
||||
uint64_t lsm_entry_data_path_len(const lsm_store *store,
|
||||
const lsm_entry *entry) {
|
||||
const lsm_str *data_path = store->data_path;
|
||||
|
|
|
@ -24,10 +24,15 @@ lnm_err lander_ctx_init(void **c_ctx, void *gctx) {
|
|||
}
|
||||
|
||||
void lander_ctx_reset(lander_ctx *ctx) {
|
||||
if (ctx->entry != NULL) {
|
||||
lsm_entry_close(ctx->entry);
|
||||
if (ctx->entry.read != NULL) {
|
||||
if (ctx->write) {
|
||||
lsm_write_close(ctx->entry.write);
|
||||
} else {
|
||||
lsm_read_close(ctx->entry.read);
|
||||
}
|
||||
|
||||
ctx->entry = NULL;
|
||||
ctx->entry.read = NULL;
|
||||
ctx->write = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,16 +50,23 @@ void lander_header_to_attr(lnm_http_loop_ctx *ctx, const char *header_name,
|
|||
lsm_str *value;
|
||||
lsm_str_init_copy_n(&value, (char *)header_value, header_value_len);
|
||||
|
||||
lsm_entry_attr_insert(c_ctx->entry, attr_type, value);
|
||||
lsm_write_attr_insert(c_ctx->entry.write, attr_type, value);
|
||||
}
|
||||
}
|
||||
|
||||
void lander_attr_to_header(lnm_http_loop_ctx *ctx, lander_attr_type attr_type,
|
||||
lnm_http_header header_type) {
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
lsm_str *value;
|
||||
const lsm_str *value;
|
||||
lsm_error res;
|
||||
|
||||
if (lsm_entry_attr_get(&value, c_ctx->entry, attr_type) == lsm_error_ok) {
|
||||
if (c_ctx->write) {
|
||||
res = lsm_write_attr_get(&value, c_ctx->entry.write, attr_type);
|
||||
} else {
|
||||
res = lsm_read_attr_get(&value, c_ctx->entry.read, attr_type);
|
||||
}
|
||||
|
||||
if (res == lsm_error_ok) {
|
||||
lnm_http_res_add_header_len(&ctx->res, header_type,
|
||||
(char *)lsm_str_ptr(value), lsm_str_len(value),
|
||||
false);
|
||||
|
|
|
@ -15,9 +15,9 @@ lnm_http_step_err lander_remove_entry(lnm_http_conn *conn) {
|
|||
lsm_str *key;
|
||||
lsm_str_init_copy_n(&key, (char *)key_s, key_len);
|
||||
|
||||
switch (lsm_store_open_write(&c_ctx->entry, c_gctx->store, key)) {
|
||||
switch (lsm_store_open_write(&c_ctx->entry.write, c_gctx->store, key)) {
|
||||
case lsm_error_ok:
|
||||
lsm_entry_remove(c_ctx->entry);
|
||||
lsm_write_remove(c_ctx->entry.write);
|
||||
break;
|
||||
case lsm_error_not_found:
|
||||
ctx->res.status = lnm_http_status_not_found;
|
||||
|
|
|
@ -36,17 +36,17 @@ lnm_http_step_err lander_get_redirect(lnm_http_conn *conn) {
|
|||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
// For redirects, the URL is stored as an in-memory attribute
|
||||
lsm_str *url_attr_val;
|
||||
const lsm_str *url_attr_val;
|
||||
|
||||
// This shouldn't be able to happen
|
||||
if (lsm_entry_attr_get(&url_attr_val, c_ctx->entry, lander_attr_type_url) !=
|
||||
lsm_error_ok) {
|
||||
if (lsm_read_attr_get(&url_attr_val, c_ctx->entry.read,
|
||||
lander_attr_type_url) != lsm_error_ok) {
|
||||
lnm_lerror("lander", "%s",
|
||||
"Entry of type redirect detected without URL attribute");
|
||||
|
||||
ctx->res.status = lnm_http_status_internal_server_error;
|
||||
lsm_entry_close(c_ctx->entry);
|
||||
c_ctx->entry = NULL;
|
||||
lsm_read_close(c_ctx->entry.read);
|
||||
c_ctx->entry.read = NULL;
|
||||
|
||||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ lnm_err lander_entry_data_streamer(uint64_t *written, char *buf,
|
|||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
lsm_entry_data_read(written, buf, c_ctx->entry, len);
|
||||
lsm_read_data_read(written, buf, c_ctx->entry.read, len);
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ lnm_http_step_err lander_get_paste(lnm_http_conn *conn) {
|
|||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer,
|
||||
lsm_entry_data_len(c_ctx->entry));
|
||||
lsm_read_data_len(c_ctx->entry.read));
|
||||
lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/plain",
|
||||
false);
|
||||
|
||||
|
@ -90,7 +90,7 @@ lnm_http_step_err lander_get_file(lnm_http_conn *conn) {
|
|||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer,
|
||||
lsm_entry_data_len(c_ctx->entry));
|
||||
lsm_read_data_len(c_ctx->entry.read));
|
||||
lander_attr_to_header(ctx, lander_attr_type_content_type,
|
||||
lnm_http_header_content_type);
|
||||
|
||||
|
@ -108,7 +108,8 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
|||
|
||||
lsm_str *key;
|
||||
lsm_str_init_copy_n(&key, (char *)key_s, key_len);
|
||||
lsm_error lsm_res = lsm_store_open_read(&c_ctx->entry, c_gctx->store, key);
|
||||
lsm_error lsm_res =
|
||||
lsm_store_open_read(&c_ctx->entry.read, c_gctx->store, key);
|
||||
lsm_str_free(key);
|
||||
|
||||
switch (lsm_res) {
|
||||
|
@ -123,8 +124,8 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
|||
}
|
||||
|
||||
lander_entry_type t;
|
||||
lsm_entry_attr_get_uint8_t((uint8_t *)&t, c_ctx->entry,
|
||||
lander_attr_type_entry_type);
|
||||
lsm_read_attr_get_uint8_t((uint8_t *)&t, c_ctx->entry.read,
|
||||
lander_attr_type_entry_type);
|
||||
|
||||
lnm_http_step_err res;
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ bool lander_insert_entry(lnm_http_loop_ctx *ctx, bool secure) {
|
|||
|
||||
// With placeholders, the entry will already be open so an entry should no
|
||||
// longer be created
|
||||
if (c_ctx->entry != NULL) {
|
||||
if (c_ctx->entry.write != NULL) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -50,11 +50,12 @@ bool lander_insert_entry(lnm_http_loop_ctx *ctx, bool secure) {
|
|||
}
|
||||
|
||||
// TODO free key on error
|
||||
switch (lsm_store_insert(&c_ctx->entry, c_gctx->store, key)) {
|
||||
switch (lsm_store_open_new(&c_ctx->entry.write, c_gctx->store, key)) {
|
||||
case lsm_error_already_present:
|
||||
ctx->res.status = lnm_http_status_conflict;
|
||||
return false;
|
||||
case lsm_error_ok:
|
||||
c_ctx->write = true;
|
||||
break;
|
||||
default:
|
||||
ctx->res.status = lnm_http_status_internal_server_error;
|
||||
|
@ -82,7 +83,7 @@ static lnm_http_step_err __lander_post_redirect(lnm_http_conn *conn,
|
|||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lsm_write_attr_insert_uint8_t(c_ctx->entry.write, lander_attr_type_entry_type,
|
||||
lander_entry_type_redirect);
|
||||
|
||||
return lnm_http_step_err_done;
|
||||
|
@ -102,7 +103,7 @@ lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn) {
|
|||
|
||||
lsm_str *attr_value;
|
||||
lsm_str_init_copy_n(&attr_value, ctx->req.body.buf, ctx->req.body.len);
|
||||
lsm_entry_attr_insert(c_ctx->entry, lander_attr_type_url, attr_value);
|
||||
lsm_write_attr_insert(c_ctx->entry.write, lander_attr_type_url, attr_value);
|
||||
|
||||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
@ -115,7 +116,7 @@ static lnm_http_step_err __lander_post_paste(lnm_http_conn *conn, bool secure) {
|
|||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lsm_write_attr_insert_uint8_t(c_ctx->entry.write, lander_attr_type_entry_type,
|
||||
lander_entry_type_paste);
|
||||
lander_header_to_attr(ctx, "X-Lander-Filename", lander_attr_type_file_name);
|
||||
|
||||
|
@ -138,7 +139,7 @@ static lnm_http_step_err __lander_post_file(lnm_http_conn *conn, bool secure) {
|
|||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lsm_write_attr_insert_uint8_t(c_ctx->entry.write, lander_attr_type_entry_type,
|
||||
lander_entry_type_file);
|
||||
lander_header_to_attr(ctx, "X-Lander-Content-Type",
|
||||
lander_attr_type_content_type);
|
||||
|
@ -163,7 +164,7 @@ lnm_http_step_err __lander_post_placeholder(lnm_http_conn *conn, bool secure) {
|
|||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lsm_write_attr_insert_uint8_t(c_ctx->entry.write, lander_attr_type_entry_type,
|
||||
lander_entry_type_placeholder);
|
||||
|
||||
return lnm_http_step_err_done;
|
||||
|
|
|
@ -11,19 +11,19 @@ lnm_http_step_err lander_stream_body_to_entry(lnm_http_conn *conn) {
|
|||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
uint64_t to_append =
|
||||
LNM_MIN(conn->r.size - conn->r.read,
|
||||
ctx->req.body.expected_len - lsm_entry_data_len(c_ctx->entry));
|
||||
uint64_t to_append = LNM_MIN(conn->r.size - conn->r.read,
|
||||
ctx->req.body.expected_len -
|
||||
lsm_write_data_len(c_ctx->entry.write));
|
||||
|
||||
lsm_str *data;
|
||||
lsm_str_init_copy_n(&data, (char *)&conn->r.buf[conn->r.read], to_append);
|
||||
lsm_entry_data_append(c_ctx->entry, data);
|
||||
printf("res = %i\n", lsm_write_data_append(c_ctx->entry.write, data));
|
||||
|
||||
conn->r.read += to_append;
|
||||
|
||||
lsm_str_free(data);
|
||||
|
||||
return lsm_entry_data_len(c_ctx->entry) == ctx->req.body.expected_len
|
||||
return lsm_write_data_len(c_ctx->entry.write) == ctx->req.body.expected_len
|
||||
? lnm_http_step_err_done
|
||||
: lnm_http_step_err_io_needed;
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ lnm_http_step_err lander_commit_entry(lnm_http_conn *conn) {
|
|||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
lsm_entry_commit(c_ctx->entry);
|
||||
lsm_write_commit(c_ctx->entry.write);
|
||||
|
||||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
@ -54,17 +54,20 @@ lnm_http_step_err lander_auth_or_placeholder(lnm_http_conn *conn) {
|
|||
lsm_str *key;
|
||||
lsm_str_init_copy_n(&key, key_s, key_len);
|
||||
|
||||
lsm_error res = lsm_store_open_write(&c_ctx->entry, c_gctx->store, key);
|
||||
lsm_error res = lsm_store_open_write(&c_ctx->entry.write, c_gctx->store, key);
|
||||
|
||||
lsm_str_free(key);
|
||||
|
||||
switch (res) {
|
||||
case lsm_error_ok: {
|
||||
c_ctx->write = true;
|
||||
|
||||
lander_entry_type t;
|
||||
|
||||
// If the entry is a placeholder, the request is always authenticated
|
||||
if ((lsm_entry_attr_get_uint8_t(
|
||||
&t, c_ctx->entry, lander_attr_type_entry_type) == lsm_error_ok) &&
|
||||
if ((lsm_write_attr_get_uint8_t(&t, c_ctx->entry.write,
|
||||
lander_attr_type_entry_type) ==
|
||||
lsm_error_ok) &&
|
||||
(t == lander_entry_type_placeholder)) {
|
||||
return lnm_http_step_err_done;
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue