#include #include #include #include "lsm.h" #include "lsm/store.h" #include "lsm/store_internal.h" #include "lsm/trie.h" lsm_error lsm_store_init(lsm_store **ptr) { lsm_store *store = calloc(1, sizeof(lsm_store)); if (store == NULL) { return lsm_error_failed_alloc; } lsm_error res = lsm_trie_init(&store->trie); if (res != lsm_error_ok) { free(store); return res; } *ptr = store; return lsm_error_ok; } lsm_error lsm_store_open(lsm_store **ptr, lsm_str *db_path, lsm_str *data_path) { lsm_store *store; LSM_RES(lsm_store_init(&store)); // TODO implement all of reading the db file store->db_path = db_path; store->data_path = data_path; *ptr = store; return lsm_error_ok; } lsm_error lsm_store_get_read(lsm_entry **out, lsm_store *store, lsm_str *key) { lsm_entry_wrapper *wrapper; LSM_RES(lsm_trie_search((void **)&wrapper, store->trie, key)); // We don't want to block the thread if (pthread_rwlock_tryrdlock(&wrapper->lock) != 0) { return lsm_error_lock_busy; } lsm_entry *entry = wrapper->entry; // While the trie's data field will never be NULL, the actual entry pointer // might be if (entry == NULL) { pthread_rwlock_unlock(&wrapper->lock); return lsm_error_not_found; } // Open a new file descriptor if needed if (entry->data.on_disk && (entry->data.value.file == NULL)) { char path[store->data_path->len + entry->key->len + 2]; sprintf(path, "%s/%s", lsm_str_ptr(store->data_path), lsm_str_ptr(entry->key)); FILE *f = fopen(path, "rb"); if (f == NULL) { return lsm_error_failed_io; } entry->data.value.file = f; } return lsm_error_ok; } lsm_error lsm_store_insert(lsm_entry **out, lsm_store *store, lsm_str *key) { lsm_entry_wrapper *wrapper; LSM_RES(lsm_entry_wrapper_init(&wrapper)); lsm_entry *entry; LSM_RES(lsm_entry_init(&entry)); entry->key = key; wrapper->entry = entry; pthread_rwlock_wrlock(&wrapper->lock); // TODO mem leak if already present LSM_RES(lsm_trie_insert(store->trie, key, wrapper)); *out = entry; return lsm_error_ok; } lsm_error lsm_store_data_write(lsm_store *store, lsm_entry *entry, lsm_str *data) { uint64_t new_len = entry->data.len + lsm_str_len(data); const char *data_s = lsm_str_ptr(data); // Data is in memory and still fits -> keep it in memory if ((new_len <= LSM_STORE_DISK_THRESHOLD) && (!entry->data.on_disk)) { char *buf = realloc(entry->data.value.ptr, new_len * sizeof(char)); if (buf == NULL) { return lsm_error_failed_alloc; } memcpy(&buf[entry->data.len], data_s, lsm_str_len(data)); entry->data.value.ptr = buf; entry->data.len = new_len; } // Data will end up on disk else { // Data is not yet on disk, so we create the file if (!entry->data.on_disk) { char path[store->data_path->len + entry->key->len + 2]; sprintf(path, "%s/%s", lsm_str_ptr(store->data_path), lsm_str_ptr(entry->key)); FILE *f = fopen(path, "w"); if (f == NULL) { return lsm_error_failed_io; } entry->data.value.file = f; entry->data.on_disk = true; // TODO free old buff, write original data to file } size_t written = 0; // TODO what happens when I/O fails? while (written < data->len) { written += fwrite(&data_s[written], sizeof(char), data->len - written, entry->data.value.file); } } return lsm_error_ok; }