Compare commits
No commits in common. "fc4187e6ce8327c81404a52d14d3bdb6350199b2" and "1c421c1e678b06f73892b873011888c49eeea9d2" have entirely different histories.
fc4187e6ce
...
1c421c1e67
24
lsm/Makefile
24
lsm/Makefile
|
|
@ -9,17 +9,13 @@ SRCS != find '$(SRC_DIR)' -iname '*.c'
|
||||||
SRCS_H != find $(INC_DIRS) -iname '*.h'
|
SRCS_H != find $(INC_DIRS) -iname '*.h'
|
||||||
SRCS_H_INTERNAL != find $(SRC_DIR) -iname '*.h'
|
SRCS_H_INTERNAL != find $(SRC_DIR) -iname '*.h'
|
||||||
SRCS_TEST != find '$(TEST_DIR)' -iname '*.c'
|
SRCS_TEST != find '$(TEST_DIR)' -iname '*.c'
|
||||||
SRCS_EXAMPLE != find '$(EXAMPLE_DIR)' -iname '*.c'
|
|
||||||
|
|
||||||
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
|
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
|
||||||
OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o)
|
OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o)
|
||||||
OBJS_EXAMPLE := $(SRCS_EXAMPLE:%=$(BUILD_DIR)/%.o)
|
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d)
|
||||||
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d) $(SRCS_EXAMPLE:%=$(BUILD_DIR)/%.d)
|
|
||||||
|
|
||||||
BINS_TEST := $(OBJS_TEST:%.c.o=%)
|
BINS_TEST := $(OBJS_TEST:%.c.o=%)
|
||||||
BINS_EXAMPLE := $(OBJS_EXAMPLE:%.c.o=%)
|
|
||||||
TARGETS_TEST := $(BINS_TEST:%=test-%)
|
TARGETS_TEST := $(BINS_TEST:%=test-%)
|
||||||
TARGETS_EXAMPLE := $(BINS_EXAMPLE:%=test-%)
|
|
||||||
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
|
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
|
||||||
|
|
||||||
_CFLAGS := $(addprefix -I,$(INC_DIRS)) $(CFLAGS) -Wall -Wextra
|
_CFLAGS := $(addprefix -I,$(INC_DIRS)) $(CFLAGS) -Wall -Wextra
|
||||||
|
|
@ -75,27 +71,14 @@ $(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
|
||||||
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \
|
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \
|
||||||
-c $< -o $@
|
-c $< -o $@
|
||||||
|
|
||||||
# =====EXAMPLES=====
|
|
||||||
.PHONY: build-example
|
|
||||||
build-example: $(BINS_EXAMPLE)
|
|
||||||
|
|
||||||
$(BINS_EXAMPLE): %: %.c.o $(LIB)
|
|
||||||
$(CC) \
|
|
||||||
$^ -o $@
|
|
||||||
|
|
||||||
# Example binaries link the resulting library
|
|
||||||
$(BUILD_DIR)/$(EXAMPLE_DIR)/%.c.o: $(EXAMPLE_DIR)/%.c
|
|
||||||
mkdir -p $(dir $@)
|
|
||||||
$(CC) $(_CFLAGS) -I$(PUB_INC_DIR) -c $< -o $@
|
|
||||||
|
|
||||||
# =====MAINTENANCE=====
|
# =====MAINTENANCE=====
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
lint:
|
lint:
|
||||||
clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) $(SRCS_EXAMPLE)
|
clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
|
||||||
|
|
||||||
.PHONY: fmt
|
.PHONY: fmt
|
||||||
fmt:
|
fmt:
|
||||||
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) $(SRCS_EXAMPLE)
|
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
|
|
@ -106,7 +89,6 @@ clean:
|
||||||
bear: clean
|
bear: clean
|
||||||
bear -- make
|
bear -- make
|
||||||
bear --append -- make build-test
|
bear --append -- make build-test
|
||||||
bear --append -- make build-example
|
|
||||||
|
|
||||||
|
|
||||||
# Make make aware of the .d files
|
# Make make aware of the .d files
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,7 @@ LIB_FILENAME = liblsm.a
|
||||||
BUILD_DIR = build
|
BUILD_DIR = build
|
||||||
SRC_DIR = src
|
SRC_DIR = src
|
||||||
TEST_DIR = test
|
TEST_DIR = test
|
||||||
EXAMPLE_DIR = example
|
INC_DIRS = include src/_include
|
||||||
PUB_INC_DIR = include
|
|
||||||
INC_DIRS = $(PUB_INC_DIR) src/_include
|
|
||||||
|
|
||||||
# -MMD: generate a .d file for every source file. This file can be imported by
|
# -MMD: generate a .d file for every source file. This file can be imported by
|
||||||
# make and makes make aware that a header file has been changed, ensuring an
|
# make and makes make aware that a header file has been changed, ensuring an
|
||||||
|
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "lsm.h"
|
|
||||||
#include "lsm/store.h"
|
|
||||||
#include "lsm/str.h"
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
lsm_str *db_path, *data_dir;
|
|
||||||
lsm_str_init_copy(&db_path, "data/data.db");
|
|
||||||
lsm_str_init_copy(&data_dir, "data");
|
|
||||||
|
|
||||||
lsm_store *store;
|
|
||||||
lsm_store_load(&store, db_path, data_dir);
|
|
||||||
|
|
||||||
lsm_str *key;
|
|
||||||
lsm_str_init_copy(&key, "key");
|
|
||||||
|
|
||||||
lsm_entry_handle *handle;
|
|
||||||
assert(lsm_store_insert(&handle, store, key) == lsm_error_ok);
|
|
||||||
|
|
||||||
lsm_str *data;
|
|
||||||
lsm_str_init_copy(&data, "hello");
|
|
||||||
|
|
||||||
for (int i = 0; i < 50; i++) {
|
|
||||||
lsm_entry_data_append(store, handle, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
lsm_entry_close(handle);
|
|
||||||
|
|
||||||
assert(lsm_store_open_read(&handle, store, key) == lsm_error_ok);
|
|
||||||
|
|
||||||
char buf[24];
|
|
||||||
uint64_t read;
|
|
||||||
uint64_t total = 0;
|
|
||||||
|
|
||||||
lsm_entry_data_read(&read, buf, handle, 24);
|
|
||||||
total += read;
|
|
||||||
|
|
||||||
while (read > 0) {
|
|
||||||
printf("%.*s", read, buf);
|
|
||||||
lsm_entry_data_read(&read, buf, handle, 24);
|
|
||||||
total += read;
|
|
||||||
}
|
|
||||||
printf("\n%lu", total);
|
|
||||||
}
|
|
||||||
|
|
@ -151,16 +151,4 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
|
||||||
lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
|
lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
|
||||||
lsm_str *data);
|
lsm_str *data);
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,6 @@ void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper);
|
||||||
struct lsm_entry_handle {
|
struct lsm_entry_handle {
|
||||||
lsm_entry_wrapper *wrapper;
|
lsm_entry_wrapper *wrapper;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
uint64_t pos;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
lsm_error lsm_entry_handle_init(lsm_entry_handle **out);
|
lsm_error lsm_entry_handle_init(lsm_entry_handle **out);
|
||||||
|
|
|
||||||
|
|
@ -259,34 +259,3 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
|
||||||
|
|
||||||
return lsm_error_ok;
|
return lsm_error_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
lsm_error lsm_entry_data_read(uint64_t *out, char *buf,
|
|
||||||
lsm_entry_handle *handle, uint64_t len) {
|
|
||||||
lsm_entry *entry = handle->wrapper->entry;
|
|
||||||
|
|
||||||
if (entry->data.len == 0) {
|
|
||||||
*out = 0;
|
|
||||||
|
|
||||||
return lsm_error_ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t read;
|
|
||||||
|
|
||||||
if (entry->data.on_disk) {
|
|
||||||
read = fread(buf, sizeof(char), len, handle->f);
|
|
||||||
|
|
||||||
if ((read == 0) && (ferror(handle->f) != 0)) {
|
|
||||||
return lsm_error_failed_io;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
read = (entry->data.len - handle->pos) < len
|
|
||||||
? (entry->data.len - handle->pos)
|
|
||||||
: len;
|
|
||||||
memcpy(buf, &entry->data.value.ptr[handle->pos], read * sizeof(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
handle->pos += read;
|
|
||||||
*out = read;
|
|
||||||
|
|
||||||
return lsm_error_ok;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue