Skip to content

Commit

Permalink
Minor renaming of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
codepr committed Mar 10, 2024
1 parent 7af0760 commit 5766467
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 42 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,20 @@ int main() {
abort();

// Insert records into the timeseries
ts_set_record(&ts, 1710033421702081792, 25.5);
ts_set_record(&ts, 1710033422047657984, 26.0);
ts_insert(&ts, 1710033421702081792, 25.5);
ts_insert(&ts, 1710033422047657984, 26.0);

// Find a record by timestamp
Record r;
int result = ts_find_record(&ts, 1710033422047657984, &r);
int result = ts_find(&ts, 1710033422047657984, &r);
if (result == 0) {
printf("Record found: timestamp=%lu, value=%.2lf\n", r.timestamp, r.value);
} else {
printf("Record not found.\n");
}

// Release the timeseries
ts_destroy(&ts);
ts_close(&ts);

// Close the database
tsdb_close(db);
Expand Down
8 changes: 4 additions & 4 deletions include/timeseries.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define TS_MAX_PARTITIONS 16
#define DATA_PATH_SIZE 1 << 8

extern const size_t TS_DUMP_SIZE;
extern const size_t TS_FLUSH_SIZE;
extern const size_t TS_BATCH_OFFSET;

/*
Expand Down Expand Up @@ -73,11 +73,11 @@ typedef struct timeseries {

extern int ts_init(Timeseries *ts);

extern void ts_destroy(Timeseries *ts);
extern void ts_close(Timeseries *ts);

extern int ts_set_record(Timeseries *ts, uint64_t timestamp, double_t value);
extern int ts_insert(Timeseries *ts, uint64_t timestamp, double_t value);

extern int ts_find_record(const Timeseries *ts, uint64_t timestamp, Record *r);
extern int ts_find(const Timeseries *ts, uint64_t timestamp, Record *r);

extern int ts_range(const Timeseries *ts, uint64_t t0, uint64_t t1, Points *p);

Expand Down
9 changes: 9 additions & 0 deletions src/commit_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
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-%.20lu", path, base);

cl->fp = open_file(path_buf, "log", "w+");
if (!cl->fp)
return -1;

cl->base_timestamp = base;
cl->base_ns = 0;
cl->current_timestamp = base;
cl->size = 0;

return 0;
}

Expand All @@ -22,13 +27,17 @@ int c_log_from_disk(Commit_Log *cl, const char *path, uint64_t base) {
snprintf(path_buf, sizeof(path_buf), "%s/c-%.20lu", path, base);

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

cl->base_timestamp = base;

uint64_t record_size = 0;

Buffer buffer;
if (buf_read_file(cl->fp, &buffer) < 0)
return -1;

size_t size = buffer.size;
uint8_t *buf = buffer.buf;

Expand Down
38 changes: 19 additions & 19 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main(void) {
clock_gettime(CLOCK_REALTIME, &tv);
uint64_t timestamp = tv.tv_sec * 1e9 + tv.tv_nsec;
timestamps[i] = timestamp;
ts_set_record(ts, timestamp, (double_t)i);
ts_insert(ts, timestamp, (double_t)i);
usleep(115000);
}

Expand All @@ -75,7 +75,7 @@ int main(void) {

log_info("Find single record at %lu", timestamps[2]);
Record r;
(void)ts_find_record(ts, timestamps[2], &r);
(void)ts_find(ts, timestamps[2], &r);
log_info(" %lu {.sec: %lu, .nsec: %lu, .value: %.02f}", r.timestamp,
r.tv.tv_sec, r.tv.tv_nsec, r.value);

Expand All @@ -97,27 +97,27 @@ int main(void) {
/* p_index_print(&p.index); */

log_info("Find single record at %lu", timestamps[51]);
ts_find_record(ts, timestamps[51], &r);
ts_find(ts, timestamps[51], &r);
log_info("%lu %.20lf", r.timestamp, r.value);

log_info("Find single record at %lu", timestamps[0]);
ts_find_record(ts, timestamps[0], &r);
ts_find(ts, timestamps[0], &r);
log_info("%lu %.20lf", r.timestamp, r.value);

log_info("Find single record at %lu", timestamps[23]);
ts_find_record(ts, timestamps[23], &r);
ts_find(ts, timestamps[23], &r);
log_info("%lu %.20lf", r.timestamp, r.value);

log_info("Find single record at %lu", timestamps[89]);
ts_find_record(ts, timestamps[89], &r);
ts_find(ts, timestamps[89], &r);
log_info("%lu %.20lf", r.timestamp, r.value);

/* c_log_print(&p.clog); */

/* p_index_print(ts->partitions[0].index); */

log_info("Looking for record: %lu", timestamps[88]);
ts_find_record(ts, timestamps[88], &r);
ts_find(ts, timestamps[88], &r);
log_info(" %lu {.sec: %lu, .nsec: %lu, .value: %.02f}", r.timestamp,
r.tv.tv_sec, r.tv.tv_nsec, r.value);

Expand All @@ -143,23 +143,23 @@ int main(void) {
}

log_info("[3] Add an out of bounds timestamp");
ts_set_record(ts, timestamps[89] + 5e9, 181.1);
ts_insert(ts, timestamps[89] + 5e9, 181.1);
for (size_t i = 0; i <= ts->partition_nr; ++i)
c_log_print(&ts->partitions[i].clog);
(void)ts_find_record(ts, timestamps[89] + 5e9, &r);
(void)ts_find(ts, timestamps[89] + 5e9, &r);
log_info(" %lu {.sec: %lu, .nsec: %lu .value: %.02f}", r.timestamp,
r.tv.tv_sec, r.tv.tv_nsec, r.value);

log_info("[4] Add a prev range timestamp");
ts_set_record(ts, timestamps[89] + 5e7, 141.231);
ts_insert(ts, timestamps[89] + 5e7, 141.231);
for (size_t i = 0; i <= ts->partition_nr; ++i)
c_log_print(&ts->partitions[i].clog);
(void)ts_find_record(ts, timestamps[89] + 5e7, &r);
(void)ts_find(ts, timestamps[89] + 5e7, &r);
log_info(" %lu {.sec: %lu, .nsec: %lu .value: %.02f}", r.timestamp,
r.tv.tv_sec, r.tv.tv_nsec, r.value);

vec_destroy(coll);
ts_destroy(ts);
ts_close(ts);

/* Timeseries_DB *db = tsdb_init("testdb"); */
/* if (!db) { */
Expand All @@ -177,29 +177,29 @@ int main(void) {
/* struct timespec tv; */
/* clock_gettime(CLOCK_REALTIME, &tv); */
/* uint64_t timestamp1 = tv.tv_sec * 1e9 + tv.tv_nsec; */
/* ts_set_record(ts, timestamp1, 25.5); */
/* ts_insert(ts, timestamp1, 25.5); */
/* usleep(15000); */
/* clock_gettime(CLOCK_REALTIME, &tv); */
/* uint64_t timestamp2 = tv.tv_sec * 1e9 + tv.tv_nsec; */
/* ts_set_record(ts, timestamp2, 27.3); */
/* ts_insert(ts, timestamp2, 27.3); */

/* Record r = {0}; */
/* int result = ts_find_record(ts, timestamp1, &r); */
/* int result = ts_find(ts, timestamp1, &r); */
/* if (result == 0) { */
/* printf("Record found: timestamp=%lu value=%.2f\n", r.timestamp, */
/* r.value); */
/* } else { */
/* printf("Record not found.\n"); */
/* } */
/* result = ts_find_record(ts, timestamp2, &r); */
/* result = ts_find(ts, timestamp2, &r); */
/* if (result == 0) { */
/* printf("Record found: timestamp=%lu value=%.2f\n", r.timestamp, */
/* r.value); */
/* } else { */
/* printf("Record not found.\n"); */
/* } */

/* ts_destroy(ts); */
/* ts_close(ts); */
/* int run = 1; */
/* Timeseries_DB *db = tsdb_init("testdb"); */
/* if (!db) { */
Expand Down Expand Up @@ -227,7 +227,7 @@ int main(void) {
* strlen(cmd.metric)) */
/* ? ts */
/* : ts_get(db, cmd.metric); */
/* ts_set_record(ts, cmd.timestamp, cmd.value); */
/* ts_insert(ts, cmd.timestamp, cmd.value); */
/* printf("+Ok (%lu)\n", cmd.timestamp); */
/* break; */
/* case SELECT: */
Expand All @@ -240,7 +240,7 @@ int main(void) {
*/
/* strlen(cmd.metric), ts->name, strlen(ts->name), */
/* strncmp(cmd.metric, ts->name, strlen(cmd.metric))); */
/* ts_find_record(ts, cmd.timestamp, &r); */
/* ts_find(ts, cmd.timestamp, &r); */
/* printf("%lu %0.2lf\n", r.timestamp, r.value); */
/* break; */
/* case QUIT: */
Expand Down
2 changes: 1 addition & 1 deletion src/partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int partition_flush_chunk(Partition *p, const Timeseries_Chunk *tc) {

size_t total_records = 0;
size_t batch_size = 0;
uint8_t *buf = malloc(TS_DUMP_SIZE * 2);
uint8_t *buf = malloc(TS_FLUSH_SIZE * 2);
if (!buf) {
vec_destroy(records);
return -1;
Expand Down
6 changes: 6 additions & 0 deletions src/persistent_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ static const size_t INDEX_SIZE = 1 << 12;
int p_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-%.20lu", path, base);

pi->fp = open_file(path_buf, "index", "w+");
if (!pi->fp)
return -1;

pi->size = 0;
pi->base_timestamp = base;

return 0;
}

Expand All @@ -23,11 +26,14 @@ int p_index_close(Persistent_Index *pi) { return fclose(pi->fp); }
int p_index_from_disk(Persistent_Index *pi, const char *path, uint64_t base) {
char path_buf[MAX_PATH_SIZE];
snprintf(path_buf, sizeof(path_buf), "%s/i-%.20lu", path, base);

pi->fp = open_file(path_buf, "index", "r");
if (!pi->fp)
return -1;

pi->size = get_file_size(pi->fp, 0);
pi->base_timestamp = base;

return 0;
}

Expand Down
7 changes: 7 additions & 0 deletions src/persistent_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ typedef struct range {
int64_t end;
} Range;

// Initializes a Persistent_Index structure
int p_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);

// Loads a Persistent_Index structure from disk
int p_index_from_disk(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);

// 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);

// Prints information about a PersistentIndex structure
void p_index_print(const Persistent_Index *pi);

#endif
Loading

0 comments on commit 5766467

Please sign in to comment.