Skip to content

Commit 711b970

Browse files
committed
Small refactoring and naming simplification
1 parent ba919cb commit 711b970

File tree

11 files changed

+47
-44
lines changed

11 files changed

+47
-44
lines changed

src/commit_log.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
#include "disk_io.h"
44
#include "logging.h"
55
#include "timeseries.h"
6+
#include <inttypes.h>
67

78
int c_log_init(Commit_Log *cl, const char *path, uint64_t base)
89
{
910
char path_buf[MAX_PATH_SIZE];
10-
snprintf(path_buf, sizeof(path_buf), "%s/c-%.20llu", path, base);
11+
snprintf(path_buf, sizeof(path_buf), "%s/c-%.20" PRIu64, path, base);
1112

1213
cl->fp = open_file(path_buf, "log", "w+");
1314
if (!cl->fp)
@@ -23,10 +24,10 @@ int c_log_init(Commit_Log *cl, const char *path, uint64_t base)
2324

2425
void c_log_set_base_ns(Commit_Log *cl, uint64_t ns) { cl->base_ns = ns; }
2526

26-
int c_log_from_disk(Commit_Log *cl, const char *path, uint64_t base)
27+
int c_log_load(Commit_Log *cl, const char *path, uint64_t base)
2728
{
2829
char path_buf[MAX_PATH_SIZE];
29-
snprintf(path_buf, sizeof(path_buf), "%s/c-%.20llu", path, base);
30+
snprintf(path_buf, sizeof(path_buf), "%s/c-%.20" PRIu64, path, base);
3031

3132
cl->fp = open_file(path_buf, "log", "r");
3233
if (!cl->fp)

src/commit_log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef struct commit_log {
1515

1616
int c_log_init(Commit_Log *cl, const char *path, uint64_t base);
1717

18-
int c_log_from_disk(Commit_Log *cl, const char *path, uint64_t base);
18+
int c_log_load(Commit_Log *cl, const char *path, uint64_t base);
1919

2020
void c_log_set_base_ns(Commit_Log *cl, uint64_t ns);
2121

src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int main(void)
6969
/* usleep(115000); */
7070
/* } */
7171

72-
/* p_index_print(&ts->partitions[0].index); */
72+
/* index_print(&ts->partitions[0].index); */
7373

7474
/* ts_print(ts); */
7575
/* log_info("Print log"); */
@@ -99,7 +99,7 @@ int main(void)
9999

100100
/* /\* log_info("Attempting a read from disk"); *\/ */
101101

102-
/* /\* p_index_print(&p.index); *\/ */
102+
/* /\* index_print(&p.index); *\/ */
103103

104104
/* log_info("Find single record at %lu", timestamps[51]); */
105105
/* ts_find(ts, timestamps[51], &r); */
@@ -119,7 +119,7 @@ int main(void)
119119

120120
/* /\* c_log_print(&p.clog); *\/ */
121121

122-
/* /\* p_index_print(ts->partitions[0].index); *\/ */
122+
/* /\* index_print(ts->partitions[0].index); *\/ */
123123

124124
/* log_info("Looking for record: %lu", timestamps[88]); */
125125
/* ts_find(ts, timestamps[88], &r); */

src/parser.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "parser.h"
2+
#include <inttypes.h>
23
#include <string.h>
34

45
String_View string_view_from_parts(const char *src, size_t len)

src/partition.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int partition_init(Partition *p, const char *path, uint64_t base)
1717
if (err < 0)
1818
return -1;
1919

20-
err = p_index_init(&p->index, path, base);
20+
err = index_init(&p->index, path, base);
2121
if (err < 0)
2222
return -1;
2323

@@ -27,13 +27,13 @@ int partition_init(Partition *p, const char *path, uint64_t base)
2727
return 0;
2828
}
2929

30-
int partition_from_disk(Partition *p, const char *path, uint64_t base)
30+
int partition_load(Partition *p, const char *path, uint64_t base)
3131
{
32-
int err = c_log_from_disk(&p->clog, path, base);
32+
int err = c_log_load(&p->clog, path, base);
3333
if (err < 0)
3434
return -1;
3535

36-
err = p_index_from_disk(&p->index, path, base);
36+
err = index_load(&p->index, path, base);
3737
if (err < 0)
3838
return -1;
3939

@@ -50,8 +50,8 @@ static int commit_records_to_log(Partition *p, const uint8_t *buf, size_t len)
5050
return -1;
5151

5252
size_t commit_log_size = p->clog.size;
53-
err = p_index_append_offset(&p->index, ts_record_timestamp(buf),
54-
commit_log_size - TS_BATCH_OFFSET);
53+
err = index_append_offset(&p->index, ts_record_timestamp(buf),
54+
commit_log_size - TS_BATCH_OFFSET);
5555
if (err < 0)
5656
return -1;
5757

@@ -141,7 +141,7 @@ static uint64_t end_offset(const Partition *p, const Range *r)
141141
int partition_find(const Partition *p, uint8_t *dst, uint64_t timestamp)
142142
{
143143
Range range;
144-
int err = p_index_find_offset(&p->index, timestamp, &range);
144+
int err = index_find_offset(&p->index, timestamp, &range);
145145
if (err < 0)
146146
return -1;
147147

@@ -175,11 +175,11 @@ int partition_find(const Partition *p, uint8_t *dst, uint64_t timestamp)
175175
int partition_range(const Partition *p, uint8_t *dst, uint64_t t0, uint64_t t1)
176176
{
177177
Range r0, r1;
178-
int err = p_index_find_offset(&p->index, t0, &r0);
178+
int err = index_find_offset(&p->index, t0, &r0);
179179
if (err < 0)
180180
return -1;
181181

182-
err = p_index_find_offset(&p->index, t1, &r1);
182+
err = index_find_offset(&p->index, t1, &r1);
183183
if (err < 0)
184184
return -1;
185185

src/partition.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef struct partition {
1515

1616
int partition_init(Partition *p, const char *path, uint64_t base);
1717

18-
int partition_from_disk(Partition *p, const char *path, uint64_t base);
18+
int partition_load(Partition *p, const char *path, uint64_t base);
1919

2020
int partition_flush_chunk(Partition *p, const Timeseries_Chunk *tc);
2121

src/persistent_index.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
#include "binary.h"
33
#include "disk_io.h"
44
#include "logging.h"
5+
#include <inttypes.h>
56

67
// relative timestamp -> main segment offset position in the file
78
static const size_t ENTRY_SIZE = sizeof(uint64_t) * 2;
89
static const size_t INDEX_SIZE = 1 << 12;
910

10-
int p_index_init(Persistent_Index *pi, const char *path, uint64_t base)
11+
int index_init(Persistent_Index *pi, const char *path, uint64_t base)
1112
{
1213
char path_buf[MAX_PATH_SIZE];
13-
snprintf(path_buf, sizeof(path_buf), "%s/i-%.20llu", path, base);
14+
snprintf(path_buf, sizeof(path_buf), "%s/i-%.20" PRIu64, path, base);
1415

1516
pi->fp = open_file(path_buf, "index", "w+");
1617
if (!pi->fp)
@@ -22,12 +23,12 @@ int p_index_init(Persistent_Index *pi, const char *path, uint64_t base)
2223
return 0;
2324
}
2425

25-
int p_index_close(Persistent_Index *pi) { return fclose(pi->fp); }
26+
int index_close(Persistent_Index *pi) { return fclose(pi->fp); }
2627

27-
int p_index_from_disk(Persistent_Index *pi, const char *path, uint64_t base)
28+
int index_load(Persistent_Index *pi, const char *path, uint64_t base)
2829
{
2930
char path_buf[MAX_PATH_SIZE];
30-
snprintf(path_buf, sizeof(path_buf), "%s/i-%.20llu", path, base);
31+
snprintf(path_buf, sizeof(path_buf), "%s/i-%.20" PRIu64, path, base);
3132

3233
pi->fp = open_file(path_buf, "index", "r");
3334
if (!pi->fp)
@@ -39,7 +40,7 @@ int p_index_from_disk(Persistent_Index *pi, const char *path, uint64_t base)
3940
return 0;
4041
}
4142

42-
int p_index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset)
43+
int index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset)
4344
{
4445
uint64_t relative_ts = ts - (pi->base_timestamp * 1e9);
4546

@@ -58,7 +59,7 @@ int p_index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset)
5859
return 0;
5960
}
6061

61-
int p_index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r)
62+
int index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r)
6263
{
6364
if (pi->size == 0) {
6465
*r = (Range){0, 0};
@@ -104,7 +105,7 @@ int p_index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r)
104105
return 0;
105106
}
106107

107-
void p_index_print(const Persistent_Index *pi)
108+
void index_print(const Persistent_Index *pi)
108109
{
109110
uint8_t buf[INDEX_SIZE];
110111
uint8_t *p = &buf[0];

src/persistent_index.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ typedef struct range {
2525
} Range;
2626

2727
// Initializes a Persistent_Index structure
28-
int p_index_init(Persistent_Index *pi, const char *path, uint64_t base);
28+
int index_init(Persistent_Index *pi, const char *path, uint64_t base);
2929

3030
// Closes the index file associated with a Persistent_Index structure
31-
int p_index_close(Persistent_Index *pi);
31+
int index_close(Persistent_Index *pi);
3232

3333
// Loads a Persistent_Index structure from disk
34-
int p_index_from_disk(Persistent_Index *pi, const char *path, uint64_t base);
34+
int index_load(Persistent_Index *pi, const char *path, uint64_t base);
3535

3636
// Appends an offset to the index file associated with a Persistent_Index
3737
// structure
38-
int p_index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset);
38+
int index_append_offset(Persistent_Index *pi, uint64_t ts, uint64_t offset);
3939

4040
// Finds the offset range for a given timestamp in the index file
41-
int p_index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r);
41+
int index_find_offset(const Persistent_Index *pi, uint64_t ts, Range *r);
4242

4343
// Prints information about a PersistentIndex structure
44-
void p_index_print(const Persistent_Index *pi);
44+
void index_print(const Persistent_Index *pi);
4545

4646
#endif

src/timeseries.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ static int ts_chunk_set_record(Timeseries_Chunk *tc, uint64_t sec,
236236
return 0;
237237
}
238238

239-
static int ts_chunk_from_disk(Timeseries_Chunk *tc, const char *pathbuf,
240-
uint64_t base_timestamp, int main)
239+
static int ts_chunk_load(Timeseries_Chunk *tc, const char *pathbuf,
240+
uint64_t base_timestamp, int main)
241241
{
242242

243-
int err = wal_from_disk(&tc->wal, pathbuf, base_timestamp, main);
243+
int err = wal_load(&tc->wal, pathbuf, base_timestamp, main);
244244
if (err < 0)
245245
return -1;
246246

@@ -298,16 +298,16 @@ int ts_init(Timeseries *ts)
298298
strncmp(dot, ".log", 4) == 0) {
299299
uint64_t base_timestamp = atoll(namelist[i]->d_name + 6);
300300
if (namelist[i]->d_name[4] == 'h') {
301-
err = ts_chunk_from_disk(&ts->head, pathbuf, base_timestamp, 1);
301+
err = ts_chunk_load(&ts->head, pathbuf, base_timestamp, 1);
302302
} else if (namelist[i]->d_name[4] == 't') {
303-
err = ts_chunk_from_disk(&ts->prev, pathbuf, base_timestamp, 0);
303+
err = ts_chunk_load(&ts->prev, pathbuf, base_timestamp, 0);
304304
}
305305
ok = err == 0;
306306
} else if (namelist[i]->d_name[0] == 'c') {
307307
// There is a log partition
308308
uint64_t base_timestamp = atoll(namelist[i]->d_name + 3);
309-
err = partition_from_disk(&ts->partitions[ts->partition_nr++],
310-
pathbuf, base_timestamp);
309+
err = partition_load(&ts->partitions[ts->partition_nr++], pathbuf,
310+
base_timestamp);
311311
}
312312

313313
free(namelist[i]);
@@ -395,7 +395,7 @@ int ts_insert(Timeseries *ts, uint64_t timestamp, double_t value)
395395
ts_chunk_init(&ts->prev, pathbuf, sec, 0);
396396

397397
// Persist to disk for disaster recovery
398-
wal_append_record(&ts->prev.wal, timestamp, value);
398+
wal_append(&ts->prev.wal, timestamp, value);
399399

400400
// If we successfully insert the record, we can return
401401
if (ts_chunk_record_fit(&ts->prev, sec) == 0)
@@ -406,7 +406,7 @@ int ts_insert(Timeseries *ts, uint64_t timestamp, double_t value)
406406
ts_chunk_init(&ts->head, pathbuf, sec, 1);
407407

408408
// Persist to disk for disaster recovery
409-
wal_append_record(&ts->head.wal, timestamp, value);
409+
wal_append(&ts->head.wal, timestamp, value);
410410
// Check if the timestamp is in range of the current chunk, otherwise
411411
// create a new in-memory segment
412412
if (ts_chunk_record_fit(&ts->head, sec) < 0) {

src/wal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int wal_delete(Wal *w)
4343
return remove(tmp);
4444
}
4545

46-
int wal_from_disk(Wal *w, const char *path, uint64_t base_timestamp, int main)
46+
int wal_load(Wal *w, const char *path, uint64_t base_timestamp, int main)
4747
{
4848
char path_buf[MAX_PATH_SIZE];
4949
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)
6161
return -1;
6262
}
6363

64-
int wal_append_record(Wal *wal, uint64_t ts, double_t value)
64+
int wal_append(Wal *wal, uint64_t ts, double_t value)
6565
{
6666
size_t len = sizeof(uint64_t) + sizeof(double_t);
6767
uint8_t buf[len];

0 commit comments

Comments
 (0)