compile commands, working on storing the database data and refactoring the code to read the data
This commit is contained in:
117
src/main.c
117
src/main.c
@@ -1,9 +1,51 @@
|
||||
#include <duckdb.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lapinfo.h"
|
||||
|
||||
void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo *info) {
|
||||
duckdb_result result;
|
||||
int32_t iterations = 0;
|
||||
int32_t number_of_data_points = 0;
|
||||
|
||||
duckdb_query(*conn, query, &result);
|
||||
|
||||
while (true) {
|
||||
iterations++;
|
||||
|
||||
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result);
|
||||
if (!data_chunk) {
|
||||
break;
|
||||
}
|
||||
|
||||
idx_t row_count = duckdb_data_chunk_get_size(data_chunk); // number of rows from the data chunk
|
||||
number_of_data_points += row_count;
|
||||
|
||||
// set column
|
||||
duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0);
|
||||
float *col1_data = (float *)duckdb_vector_get_data(col1);
|
||||
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
||||
|
||||
for (idx_t row = 0; row < row_count; row++) {
|
||||
if (duckdb_validity_row_is_valid(col1_validity, row)) {
|
||||
// put a switch here based on the info from the struct
|
||||
((float *)info->data)[info->data_last_index] = col1_data[row];
|
||||
info->data_last_index++;
|
||||
// TODO: set info->data_size as capacity at struct creation
|
||||
printf("%f, ", col1_data[row]);
|
||||
} else {
|
||||
printf("NULL");
|
||||
}
|
||||
}
|
||||
|
||||
duckdb_destroy_data_chunk(&data_chunk);
|
||||
}
|
||||
duckdb_destroy_result(&result);
|
||||
printf("\nNumber of iterations: %d, number of data points: %d\n", iterations, number_of_data_points);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
printf("Hello from HardCompound!\n");
|
||||
|
||||
@@ -137,55 +179,38 @@ int main(int argc, char **argv) {
|
||||
|
||||
const int event_table_count = sizeof(event_tables) / sizeof(event_tables[0]);
|
||||
|
||||
// Test query
|
||||
duckdb_result result;
|
||||
int32_t iterations = 0;
|
||||
int32_t number_of_data_points = 0;
|
||||
LapInfo info = {.lap_number = 0,
|
||||
.start_time = 0,
|
||||
.throttle_pos = {.data_last_index = 0,
|
||||
.data_size = 75000,
|
||||
.frequency = 50,
|
||||
.type = FLOAT,
|
||||
.validity = NULL,
|
||||
.data = malloc(sizeof(float) * 75000)},
|
||||
.brake_pos = {.data_last_index = 0,
|
||||
.data_size = 75000,
|
||||
.frequency = 50,
|
||||
.type = FLOAT,
|
||||
.validity = NULL,
|
||||
.data = malloc(sizeof(float) * 75000)},
|
||||
.steering_pos = {.data_last_index = 0,
|
||||
.data_size = 75000,
|
||||
.frequency = 50,
|
||||
.type = FLOAT,
|
||||
.validity = NULL,
|
||||
.data = malloc(sizeof(float) * 75000)},
|
||||
.speed = {.data_last_index = 0,
|
||||
.data_size = 75000,
|
||||
.frequency = 50,
|
||||
.type = FLOAT,
|
||||
.validity = NULL,
|
||||
.data = malloc(sizeof(float) * 75000)}};
|
||||
|
||||
for (int i = 0; i < 1 /*channel_table_count*/; i++) {
|
||||
char queryBuffer[256]; // buffer to hold the formatted query
|
||||
|
||||
// snprintf(queryBuffer, sizeof(queryBuffer), "SELECT * FROM \"%s\"\n", channel_tables[i]);
|
||||
|
||||
// duckdb_query(conn, queryBuffer, &result);
|
||||
duckdb_query(conn, "SELECT CAST(value AS FLOAT) from \"Lap Time\"\n", &result);
|
||||
|
||||
while (true) {
|
||||
iterations++;
|
||||
|
||||
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result);
|
||||
|
||||
if (!data_chunk) {
|
||||
break;
|
||||
}
|
||||
idx_t row_count = duckdb_data_chunk_get_size(data_chunk); // number of rows from the data chunk
|
||||
number_of_data_points += row_count;
|
||||
|
||||
// set column
|
||||
duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0);
|
||||
float_t *col1_data = (float_t *)duckdb_vector_get_data(col1);
|
||||
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
||||
|
||||
for (idx_t row = 0; row < row_count; row++) {
|
||||
if (duckdb_validity_row_is_valid(col1_validity, row)) {
|
||||
printf("%f, ", col1_data[row]);
|
||||
} else {
|
||||
printf("NULL");
|
||||
}
|
||||
}
|
||||
|
||||
duckdb_destroy_data_chunk(&data_chunk);
|
||||
}
|
||||
duckdb_destroy_result(&result);
|
||||
}
|
||||
|
||||
printf("\nNumber of iterations: %d, number of data points: %d\n", iterations, number_of_data_points);
|
||||
get_data_from_db("Select * from \"Throttle Pos\"\n", &conn, &info.throttle_pos);
|
||||
destroyLapinfo(&info);
|
||||
|
||||
printf("Shutting down HardCompound!\n");
|
||||
|
||||
printf("Destroying result!\n");
|
||||
duckdb_destroy_result(&result);
|
||||
|
||||
printf("Closing database and any connection.\n");
|
||||
duckdb_disconnect(&conn);
|
||||
duckdb_close(&db);
|
||||
|
||||
Reference in New Issue
Block a user