Skip to content

Commit

Permalink
Merge pull request #1 from codepr/refactor/make-it-a-lib
Browse files Browse the repository at this point in the history
Refactor/make it a lib
  • Loading branch information
codepr authored Mar 10, 2024
2 parents 0dd841f + 893d649 commit 047bb1b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 24 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/roach.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: roach build

on:
push:
branches: [ main ]
paths-ignore:
- '**.md'
pull_request:
branches: [ main ]
paths-ignore:
- '**.md'

jobs:
build:

runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]

steps:
- uses: actions/checkout@v2
- name: make
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.d
*.o
*.so
*.ko
*.obj
*.elf
Expand All @@ -19,3 +20,4 @@ logs
tags
build
roach
server
27 changes: 22 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
CFLAGS=-Wall -Wextra -Werror -Wunused -std=c11 -pedantic -ggdb -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -pg -D_DEFAULT_SOURCE=200809L
CC = gcc
CFLAGS = -Wall -Wextra -Werror -Wunused -std=c11 -pedantic -ggdb -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -pg -D_DEFAULT_SOURCE=200809L -Iinclude -Isrc
LDFLAGS = -L. -ltimeseries -fsanitize=address -fsanitize=undefined

all: roach
LIB_SOURCES = src/timeseries.c src/partition.c src/wal.c src/disk_io.c src/binary.c src/logging.c src/persistent_index.c src/commit_log.c
LIB_OBJECTS = $(LIB_SOURCES:.c=.o)
LIB_PERSISTENCE = logdata

roach: src/*.c
$(CC) $(CFLAGS) -o roach src/*.c
SERVER_SOURCES = src/main.c
SERVER_OBJECTS = $(SERVER_SOURCES:.c=.o)
SERVER_EXECUTABLE = server

all: libtimeseries.so $(SERVER_EXECUTABLE)

libtimeseries.so: $(LIB_OBJECTS)
$(CC) -shared -o $@ $(LIB_OBJECTS)

$(SERVER_EXECUTABLE): $(SERVER_OBJECTS) libtimeseries.so
$(CC) -o $@ $(SERVER_OBJECTS) $(LDFLAGS)

%.o: %.c
$(CC) $(CFLAGS) -fPIC -c $< -o $@

clean:
rm -rf roach logdata/*
rm -f $(LIB_OBJECTS) $(SERVER_OBJECTS) libtimeseries.so $(SERVER_EXECUTABLE)
rm -rf $(LIB_PERSISTENCE) 2> /dev/null
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ Still at the very early stages, the main concepts are

At the current stage, no server attached, just a tiny library with some crude APIs.

#### As a library

Build the `libtimeseries.so` first

```bash
make
```

In the target project, a generic hello world

```c
#include <stdio.h>
#include "timeseries.h"

int main(void) {
// Example usage of timeseries library functions
Timeseries *ts = ts_create("example_ts");
// Use timeseries functions...
ts_destroy(ts);
return 0;
}

```
Build it linking the library
```bash
gcc -o my_project main.c -I/path/to/timeseries/include -L/path/to/timeseries -ltimeseries
```

#### Quickstart

```c
#include "timeseries.h"

Expand All @@ -54,7 +86,7 @@ int main() {
Record r;
int result = ts_find_record(&ts, 1710033422047657984, &r);
if (result == 0) {
printf("Record found: timestamp=%" PRIu64 ", value=%.2f\n", r.timestamp, r.value);
printf("Record found: timestamp=%lu, value=%.2lf\n", r.timestamp, r.value);
} else {
printf("Record not found.\n");
}
Expand Down
4 changes: 4 additions & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-Iinclude
-Isrc
-L.
-ltimeseries
31 changes: 16 additions & 15 deletions src/timeseries.h → include/timeseries.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ typedef struct record {
int is_set;
} Record;

size_t ts_record_timestamp(const uint8_t *buf);
extern size_t ts_record_timestamp(const uint8_t *buf);

size_t ts_record_write(const Record *r, uint8_t *buf);
extern size_t ts_record_write(const Record *r, uint8_t *buf);

size_t ts_record_read(Record *r, const uint8_t *buf);
extern size_t ts_record_read(Record *r, const uint8_t *buf);

size_t ts_record_batch_write(const Record *r[], uint8_t *buf, size_t count);
extern size_t ts_record_batch_write(const Record *r[], uint8_t *buf,
size_t count);

typedef VEC(Record) Points;

Expand Down Expand Up @@ -70,29 +71,29 @@ typedef struct timeseries {
size_t partition_nr;
} Timeseries;

int ts_init(Timeseries *ts);
extern int ts_init(Timeseries *ts);

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

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

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

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

void ts_print(const Timeseries *ts);
extern void ts_print(const Timeseries *ts);

typedef struct timeseries_db {
char data_path[DATA_PATH_SIZE];
} Timeseries_DB;

Timeseries_DB *tsdb_init(const char *data_path);
extern Timeseries_DB *tsdb_init(const char *data_path);

void tsdb_close(Timeseries_DB *tsdb);
extern void tsdb_close(Timeseries_DB *tsdb);

Timeseries *ts_create(const Timeseries_DB *tsdb, const char *name,
int64_t retention);
extern Timeseries *ts_create(const Timeseries_DB *tsdb, const char *name,
int64_t retention);

Timeseries *ts_get(const Timeseries_DB *tsdb, const char *name);
extern Timeseries *ts_get(const Timeseries_DB *tsdb, const char *name);

#endif
4 changes: 1 addition & 3 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "binary.h"
#include "commit_log.h"
#include "disk_io.h"
#include "logging.h"
#include "partition.h"
#include "persistent_index.h"
#include "protocol.h"
#include "timeseries.h"
#include <stdio.h>
Expand Down
3 changes: 3 additions & 0 deletions src/timeseries.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Timeseries_DB *tsdb_init(const char *data_path) {
if (strlen(data_path) > DATA_PATH_SIZE)
return NULL;

if (make_dir(BASE_PATH) < 0)
return NULL;

Timeseries_DB *tsdb = malloc(sizeof(*tsdb));
if (!tsdb)
return NULL;
Expand Down

0 comments on commit 047bb1b

Please sign in to comment.