Skip to content

Commit

Permalink
Small refactoring and naming simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
codepr committed Dec 11, 2024
1 parent ba919cb commit 711b970
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 44 deletions.
7 changes: 4 additions & 3 deletions src/commit_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include "disk_io.h"
#include "logging.h"
#include "timeseries.h"
#include <inttypes.h>

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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/commit_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"); */
Expand Down Expand Up @@ -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); */
Expand All @@ -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); */
Expand Down
1 change: 1 addition & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "parser.h"
#include <inttypes.h>
#include <string.h>

String_View string_view_from_parts(const char *src, size_t len)
Expand Down
18 changes: 9 additions & 9 deletions src/partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
17 changes: 9 additions & 8 deletions src/persistent_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
#include "binary.h"
#include "disk_io.h"
#include "logging.h"
#include <inttypes.h>

// 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)
Expand All @@ -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)
Expand All @@ -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);

Expand All @@ -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};
Expand Down Expand Up @@ -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];
Expand Down
12 changes: 6 additions & 6 deletions src/persistent_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions src/timeseries.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/wal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/wal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 711b970

Please sign in to comment.