diff options
Diffstat (limited to 'source/tb_db.c')
| -rw-r--r-- | source/tb_db.c | 77 |
1 files changed, 34 insertions, 43 deletions
diff --git a/source/tb_db.c b/source/tb_db.c index 8aba614..cfa26b1 100644 --- a/source/tb_db.c +++ b/source/tb_db.c | |||
| @@ -131,73 +131,64 @@ query_tokenizer(mem_arena *arena, string8 *buffer, query_token_list *list) | |||
| 131 | 131 | ||
| 132 | int main(int count, char **value) | 132 | int main(int count, char **value) |
| 133 | { | 133 | { |
| 134 | |||
| 135 | #if 1 | 134 | #if 1 |
| 136 | unused(nil_query_token_list); | 135 | unused(nil_query_token_list); |
| 137 | #endif | 136 | #endif |
| 138 | 137 | ||
| 139 | if(count < 2) value[1] = "./test/data.csv"; | 138 | if(count < 2) value[1] = "./test/data.csv"; |
| 140 | 139 | ||
| 141 | local_persist b32 running = 1; | 140 | local_persist b32 running = 1; |
| 142 | 141 | ||
| 143 | mem_arena *global_arena = arena_create(MiB(30)); | 142 | mem_arena *global_arena = arena_create(GiB(1)); |
| 144 | |||
| 145 | // NOTE(nasr): see note down below | ||
| 146 | // csv_table *global_table = PushStruct(global_arena, csv_table); | ||
| 147 | 143 | ||
| 148 | string8 buffer = load_file(global_arena, value[1]); | 144 | string8 buffer = load_file(global_arena, value[1]); |
| 149 | 145 | ||
| 150 | print("\nDatabase Engine\n"); | 146 | // NOTE(nasr): the use of tables is required for tracking headers etc. |
| 147 | // i think we can optimize this away in the future but for now its fine | ||
| 148 | csv_table *table = PushStructZero(global_arena, csv_table); | ||
| 149 | csv_token_list *token_list = PushStructZero(global_arena, csv_token_list); | ||
| 151 | 150 | ||
| 152 | for(;;) | 151 | token_list->start_token = &nil_csv_token; |
| 153 | { | 152 | token_list->end_token = &nil_csv_token; |
| 154 | if (running) | ||
| 155 | { | ||
| 156 | { | ||
| 157 | u8 *lbuf = PushArray(global_arena, u8, 256); | ||
| 158 | s32 err = os_read(STDIN_FD, lbuf, 256); | ||
| 159 | 153 | ||
| 160 | if(err < 0) | 154 | csv_token *tokens = tokenize_csv(buffer, global_arena, table, token_list); |
| 161 | { | 155 | assert_msg(tokens != NULL, "tokens are null"); |
| 162 | print("error reading from stdin"); | ||
| 163 | } | ||
| 164 | 156 | ||
| 157 | // NOTE(nasr): token_list is now populated — pass it directly, not a fresh empty list | ||
| 158 | btree *bt = parse_csv(global_arena, token_list, table); | ||
| 165 | 159 | ||
| 166 | // TODO(nasr): extract this later in the future and make a string copy function/macro | 160 | print("\nDatabase Engine\n"); |
| 167 | // @params (s32 lbuf_size , string8 lbuf_stringified) | ||
| 168 | s32 lbuf_size = sizeof(lbuf) - 1; | ||
| 169 | string8 lbuf_stringified = PushString(global_arena, lbuf_size); | ||
| 170 | { | ||
| 171 | memcpy(lbuf_stringified.data, lbuf, lbuf_size); | ||
| 172 | lbuf_stringified.size = sizeof(lbuf) - 1; | ||
| 173 | } | ||
| 174 | 161 | ||
| 175 | query_token_list *qtl = PushStruct(global_arena, query_token_list); | 162 | for(;;) |
| 163 | { | ||
| 164 | if(running) | ||
| 165 | { | ||
| 166 | u8 *lbuf = PushArray(global_arena, u8, 256); | ||
| 167 | s32 err = os_read(STDIN_FD, lbuf, 256); | ||
| 176 | 168 | ||
| 177 | query_tokenizer(global_arena, &lbuf_stringified, qtl); | 169 | if(err < 0) |
| 170 | { | ||
| 171 | print("error reading from stdin"); | ||
| 178 | } | 172 | } |
| 179 | 173 | ||
| 174 | // TODO(nasr): extract this later in the future and make a string copy function/macro | ||
| 175 | // @params (s32 lbuf_size, string8 lbuf_stringified) | ||
| 176 | // NOTE(nasr): use err (bytes read) not sizeof(lbuf*) — sizeof a pointer is always 8 | ||
| 177 | s32 lbuf_size = err; | ||
| 178 | string8 lbuf_stringified = PushString(global_arena, lbuf_size); | ||
| 180 | { | 179 | { |
| 180 | memcpy(lbuf_stringified.data, lbuf, lbuf_size); | ||
| 181 | lbuf_stringified.size = lbuf_size; | ||
| 182 | } | ||
| 181 | 183 | ||
| 182 | // NOTE(nasr): the use of tables is required for tracking headers etc. | 184 | query_token_list *qtl = PushStructZero(global_arena, query_token_list); |
| 183 | // i think we can optimize this away in the future but for now its fine | ||
| 184 | csv_table *table = PushStruct(global_arena, csv_table); | ||
| 185 | |||
| 186 | csv_token_list *token_list = PushStruct(global_arena, csv_token_list); | ||
| 187 | |||
| 188 | csv_token *tokens = tokenize_csv(buffer, global_arena, table, token_list); | ||
| 189 | 185 | ||
| 190 | assert_msg(tokens != NULL, "Tokens are NULL."); | ||
| 191 | 186 | ||
| 192 | csv_token_list *ctl = PushStruct(global_arena, csv_token_list); | 187 | query_tokenizer(global_arena, &lbuf_stringified, qtl); |
| 193 | btree *bt = parse_csv(global_arena, ctl, table); | ||
| 194 | 188 | ||
| 195 | btree_write(bt); | 189 | // TODO(nasr): dispatch qtl against bt here |
| 196 | } | ||
| 197 | 190 | ||
| 198 | // NOTE(nasr): not sure on how to approach the b-tree and the table format thing | 191 | unused(bt); |
| 199 | // we kind of want our table format i think? but i wouldnt be sure about the use case | ||
| 200 | // so we stick to the regular b_tree for now. commenting out the tables. | ||
| 201 | 192 | ||
| 202 | sleep(1); | 193 | sleep(1); |
| 203 | } | 194 | } |
