diff --git a/src/commit_log.c b/src/commit_log.c index 9c5f31b..a27567d 100644 --- a/src/commit_log.c +++ b/src/commit_log.c @@ -3,11 +3,12 @@ #include "disk_io.h" #include "logging.h" #include "timeseries.h" +#include int c_log_init(Commit_Log *cl, const char *path, uint64_t base) { char path_buf[MAX_PATH_SIZE]; - snprintf(path_buf, sizeof(path_buf), "%s/c-%.20llu", path, base); + snprintf(path_buf, sizeof(path_buf), "%s/c-%.20" PRIu64, path, base); cl->fp = open_file(path_buf, "log", "w+"); if (!cl->fp) @@ -23,10 +24,10 @@ int c_log_init(Commit_Log *cl, const char *path, uint64_t base) void c_log_set_base_ns(Commit_Log *cl, uint64_t ns) { cl->base_ns = ns; } -int c_log_from_disk(Commit_Log *cl, const char *path, uint64_t base) +int c_log_load(Commit_Log *cl, const char *path, uint64_t base) { char path_buf[MAX_PATH_SIZE]; - snprintf(path_buf, sizeof(path_buf), "%s/c-%.20llu", path, base); + snprintf(path_buf, sizeof(path_buf), "%s/c-%.20" PRIu64, path, base); cl->fp = open_file(path_buf, "log", "r"); if (!cl->fp) diff --git a/src/commit_log.h b/src/commit_log.h index e396385..3f8710e 100644 --- a/src/commit_log.h +++ b/src/commit_log.h @@ -15,7 +15,7 @@ typedef struct commit_log { int c_log_init(Commit_Log *cl, const char *path, uint64_t base); -int c_log_from_disk(Commit_Log *cl, const char *path, uint64_t base); +int c_log_load(Commit_Log *cl, const char *path, uint64_t base); void c_log_set_base_ns(Commit_Log *cl, uint64_t ns); diff --git a/src/main.c b/src/main.c index 58c36ad..dde28c8 100644 --- a/src/main.c +++ b/src/main.c @@ -69,7 +69,7 @@ int main(void) /* usleep(115000); */ /* } */ - /* p_index_print(&ts->partitions[0].index); */ + /* index_print(&ts->partitions[0].index); */ /* ts_print(ts); */ /* log_info("Print log"); */ @@ -99,7 +99,7 @@ int main(void) /* /\* log_info("Attempting a read from disk"); *\/ */ - /* /\* p_index_print(&p.index); *\/ */ + /* /\* index_print(&p.index); *\/ */ /* log_info("Find single record at %lu", timestamps[51]); */ /* ts_find(ts, timestamps[51], &r); */ @@ -119,7 +119,7 @@ int main(void) /* /\* c_log_print(&p.clog); *\/ */ - /* /\* p_index_print(ts->partitions[0].index); *\/ */ + /* /\* index_print(ts->partitions[0].index); *\/ */ /* log_info("Looking for record: %lu", timestamps[88]); */ /* ts_find(ts, timestamps[88], &r); */ diff --git a/src/parser.c b/src/parser.c index e580a89..484934d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,4 +1,5 @@ #include "parser.h" +#include #include String_View string_view_from_parts(const char *src, size_t len) diff --git a/src/partition.c b/src/partition.c index 0536c47..717e37b 100644 --- a/src/partition.c +++ b/src/partition.c @@ -17,7 +17,7 @@ int partition_init(Partition *p, const char *path, uint64_t base) if (err < 0) return -1; - err = p_index_init(&p->index, path, base); + err = index_init(&p->index, path, base); if (err < 0) return -1; @@ -27,13 +27,13 @@ int partition_init(Partition *p, const char *path, uint64_t base) return 0; } -int partition_from_disk(Partition *p, const char *path, uint64_t base) +int partition_load(Partition *p, const char *path, uint64_t base) { - int err = c_log_from_disk(&p->clog, path, base); + int err = c_log_load(&p->clog, path, base); if (err < 0) return -1; - err = p_index_from_disk(&p->index, path, base); + err = index_load(&p->index, path, base); if (err < 0) return -1; @@ -50,8 +50,8 @@ static int commit_records_to_log(Partition *p, const uint8_t *buf, size_t len) return -1; size_t commit_log_size = p->clog.size; - err = p_index_append_offset(&p->index, ts_record_timestamp(buf), - commit_log_size - TS_BATCH_OFFSET); + err = index_append_offset(&p->index, ts_record_timestamp(buf), + commit_log_size - TS_BATCH_OFFSET); if (err < 0) return -1; @@ -141,7 +141,7 @@ static uint64_t end_offset(const Partition *p, const Range *r) int partition_find(const Partition *p, uint8_t *dst, uint64_t timestamp) { Range range; - int err = p_index_find_offset(&p->index, timestamp, &range); + int err = index_find_offset(&p->index, timestamp, &range); if (err < 0) return -1; @@ -175,11 +175,11 @@ int partition_find(const Partition *p, uint8_t *dst, uint64_t timestamp) int partition_range(const Partition *p, uint8_t *dst, uint64_t t0, uint64_t t1) { Range r0, r1; - int err = p_index_find_offset(&p->index, t0, &r0); + int err = index_find_offset(&p->index, t0, &r0); if (err < 0) return -1; - err = p_index_find_offset(&p->index, t1, &r1); + err = index_find_offset(&p->index, t1, &r1); if (err < 0) return -1; diff --git a/src/partition.h b/src/partition.h index ec0c4d8..c6f9f41 100644 --- a/src/partition.h +++ b/src/partition.h @@ -15,7 +15,7 @@ typedef struct partition { int partition_init(Partition *p, const char *path, uint64_t base); -int partition_from_disk(Partition *p, const char *path, uint64_t base); +int partition_load(Partition *p, const char *path, uint64_t base); int partition_flush_chunk(Partition *p, const Timeseries_Chunk *tc); diff --git a/src/persistent_index.c b/src/persistent_index.c index 21ae41f..3223599 100644 --- a/src/persistent_index.c +++ b/src/persistent_index.c @@ -2,15 +2,16 @@ #include "binary.h" #include "disk_io.h" #include "logging.h" +#include // relative timestamp -> main segment offset position in the file static const size_t ENTRY_SIZE = sizeof(uint64_t) * 2; static const size_t INDEX_SIZE = 1 << 12; -int p_index_init(Persistent_Index *pi, const char *path, uint64_t base) +int index_init(Persistent_Index *pi, const char *path, uint64_t base) { char path_buf[MAX_PATH_SIZE]; - snprintf(path_buf, sizeof(path_buf), "%s/i-%.20llu", path, base); + snprintf(path_buf, sizeof(path_buf), "%s/i-%.20" PRIu64, path, base); pi->fp = open_file(path_buf, "index", "w+"); if (!pi->fp) @@ -22,12 +23,12 @@ int p_index_init(Persistent_Index *pi, const char *path, uint64_t base) return 0; } -int p_index_close(Persistent_Index *pi) { return fclose(pi->fp); } +int index_close(Persistent_Index *pi) { return fclose(pi->fp); } -int p_index_from_disk(Persistent_Index *pi, const char *path, uint64_t base) +int index_load(Persistent_Index *pi, const char *path, uint64_t base) { char path_buf[MAX_PATH_SIZE]; - snprintf(path_buf, sizeof(path_buf), "%s/i-%.20llu", path, base); + snprintf(path_buf, sizeof(path_buf), "%s/i-%.20" PRIu64, path, base); pi->fp = open_file(path_buf, "index", "r"); if (!pi->fp) @@ -39,7 +40,7 @@ int p_index_from_disk(Persistent_Index *pi, const char *path, uint64_t base) return 0; } -int p_index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset) +int index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset) { uint64_t relative_ts = ts - (pi->base_timestamp * 1e9); @@ -58,7 +59,7 @@ int p_index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset) return 0; } -int p_index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r) +int index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r) { if (pi->size == 0) { *r = (Range){0, 0}; @@ -104,7 +105,7 @@ int p_index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r) return 0; } -void p_index_print(const Persistent_Index *pi) +void index_print(const Persistent_Index *pi) { uint8_t buf[INDEX_SIZE]; uint8_t *p = &buf[0]; diff --git a/src/persistent_index.h b/src/persistent_index.h index 51a0790..4d12227 100644 --- a/src/persistent_index.h +++ b/src/persistent_index.h @@ -25,22 +25,22 @@ typedef struct range { } Range; // Initializes a Persistent_Index structure -int p_index_init(Persistent_Index *pi, const char *path, uint64_t base); +int index_init(Persistent_Index *pi, const char *path, uint64_t base); // Closes the index file associated with a Persistent_Index structure -int p_index_close(Persistent_Index *pi); +int index_close(Persistent_Index *pi); // Loads a Persistent_Index structure from disk -int p_index_from_disk(Persistent_Index *pi, const char *path, uint64_t base); +int index_load(Persistent_Index *pi, const char *path, uint64_t base); // Appends an offset to the index file associated with a Persistent_Index // structure -int p_index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset); +int index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset); // Finds the offset range for a given timestamp in the index file -int p_index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r); +int index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r); // Prints information about a PersistentIndex structure -void p_index_print(const Persistent_Index *pi); +void index_print(const Persistent_Index *pi); #endif diff --git a/src/timeseries.c b/src/timeseries.c index 909f6d4..c827f3f 100644 --- a/src/timeseries.c +++ b/src/timeseries.c @@ -236,11 +236,11 @@ static int ts_chunk_set_record(Timeseries_Chunk *tc, uint64_t sec, return 0; } -static int ts_chunk_from_disk(Timeseries_Chunk *tc, const char *pathbuf, - uint64_t base_timestamp, int main) +static int ts_chunk_load(Timeseries_Chunk *tc, const char *pathbuf, + uint64_t base_timestamp, int main) { - int err = wal_from_disk(&tc->wal, pathbuf, base_timestamp, main); + int err = wal_load(&tc->wal, pathbuf, base_timestamp, main); if (err < 0) return -1; @@ -298,16 +298,16 @@ int ts_init(Timeseries *ts) strncmp(dot, ".log", 4) == 0) { uint64_t base_timestamp = atoll(namelist[i]->d_name + 6); if (namelist[i]->d_name[4] == 'h') { - err = ts_chunk_from_disk(&ts->head, pathbuf, base_timestamp, 1); + err = ts_chunk_load(&ts->head, pathbuf, base_timestamp, 1); } else if (namelist[i]->d_name[4] == 't') { - err = ts_chunk_from_disk(&ts->prev, pathbuf, base_timestamp, 0); + err = ts_chunk_load(&ts->prev, pathbuf, base_timestamp, 0); } ok = err == 0; } else if (namelist[i]->d_name[0] == 'c') { // There is a log partition uint64_t base_timestamp = atoll(namelist[i]->d_name + 3); - err = partition_from_disk(&ts->partitions[ts->partition_nr++], - pathbuf, base_timestamp); + err = partition_load(&ts->partitions[ts->partition_nr++], pathbuf, + base_timestamp); } free(namelist[i]); @@ -395,7 +395,7 @@ int ts_insert(Timeseries *ts, uint64_t timestamp, double_t value) ts_chunk_init(&ts->prev, pathbuf, sec, 0); // Persist to disk for disaster recovery - wal_append_record(&ts->prev.wal, timestamp, value); + wal_append(&ts->prev.wal, timestamp, value); // If we successfully insert the record, we can return if (ts_chunk_record_fit(&ts->prev, sec) == 0) @@ -406,7 +406,7 @@ int ts_insert(Timeseries *ts, uint64_t timestamp, double_t value) ts_chunk_init(&ts->head, pathbuf, sec, 1); // Persist to disk for disaster recovery - wal_append_record(&ts->head.wal, timestamp, value); + wal_append(&ts->head.wal, timestamp, value); // Check if the timestamp is in range of the current chunk, otherwise // create a new in-memory segment if (ts_chunk_record_fit(&ts->head, sec) < 0) { diff --git a/src/wal.c b/src/wal.c index a4b6535..ca9aad1 100644 --- a/src/wal.c +++ b/src/wal.c @@ -43,7 +43,7 @@ int wal_delete(Wal *w) return remove(tmp); } -int wal_from_disk(Wal *w, const char *path, uint64_t base_timestamp, int main) +int wal_load(Wal *w, const char *path, uint64_t base_timestamp, int main) { char path_buf[MAX_PATH_SIZE]; snprintf(path_buf, sizeof(path_buf), "%s/wal-%c-%.20" PRIu64, path, t[main], @@ -61,7 +61,7 @@ int wal_from_disk(Wal *w, const char *path, uint64_t base_timestamp, int main) return -1; } -int wal_append_record(Wal *wal, uint64_t ts, double_t value) +int wal_append(Wal *wal, uint64_t ts, double_t value) { size_t len = sizeof(uint64_t) + sizeof(double_t); uint8_t buf[len]; diff --git a/src/wal.h b/src/wal.h index 9ca7673..13c4863 100644 --- a/src/wal.h +++ b/src/wal.h @@ -16,11 +16,11 @@ typedef struct wal { int wal_init(Wal *w, const char *path, uint64_t base_timestamp, int main); -int wal_from_disk(Wal *w, const char *path, uint64_t base_timestamp, int main); +int wal_load(Wal *w, const char *path, uint64_t base_timestamp, int main); int wal_delete(Wal *w); -int wal_append_record(Wal *wal, uint64_t ts, double_t value); +int wal_append(Wal *wal, uint64_t ts, double_t value); size_t wal_size(const Wal *wal);