-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.c
89 lines (84 loc) · 2.55 KB
/
schema.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "schema.h"
#include "vector.h"
#include "datatypes.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
struct schema * init_schema(int colcount, struct vector * datatypes){
struct schema * o = calloc(1, sizeof(struct schema));
o->colcount = colcount;
o->rowbytesize = NULL;
if(datatypes->size != colcount){
printf("ERROR: init_schema called with a datatypes vector of size %d, expected colcount size %d, exiting!\n", datatypes->size, colcount);
return 0;
}
o->datatypes = datatypes;
recompute_rowbytesize(o);
return o;
}
// colcount -> expected column count, text -> space separated string of datatypes, if error, returns NULL
struct schema * init_schema_from_text(int colcount, char * text){
char *tc = calloc(strlen(text) + 1, sizeof(char));
char *tcog = tc;
strcpy(tc, text);
struct vector * datatypes = init_vector();
char * tok = tc;
while((tok = strsep(&tc, " "))){
//printf("DEBUG TOK = %s\n", tok);
struct datatype * dt = parse_string_to_datatype(tok);
if(!dt){
printf("ERROR: In init_schema_from_text, parse_string_to_datatype failed to parse datatype string %s\n", tok);
free(datatypes->values);
free(datatypes);
free(tcog);
return NULL;
}
add_vector(datatypes, dt);
}
if(datatypes->size != colcount){
printf("ERROR: init_schema_from_text called with a text string with %d datatypes, expected colcount size %d\n", datatypes->size, colcount);
for(int i = 0; i < datatypes->size; ++i){
free(datatypes->values[i]);
}
free(tcog);
free(datatypes->values);
free(datatypes);
return NULL;
}
free(tcog);
return init_schema(colcount, datatypes);
}
void recompute_rowbytesize(struct schema * schm){
struct vector * dts = schm->datatypes;
int sum = 0;
int * newrowbytesize = calloc(schm->colcount + 1, sizeof(int));
for(int i = 0; i < schm->colcount; ++i){
newrowbytesize[i] = sum;
struct datatype * dt = dts->values[i];
sum += get_datatype_size(dt);
}
newrowbytesize[schm->colcount] = sum;
free(schm->rowbytesize);
schm->rowbytesize = newrowbytesize;
return;
}
void print_schema(struct schema * schm){
printf("SCHEMA:\n");
printf("colcount: %d\n", schm->colcount);
printf("rowbytesize: [");
for(int i = 0; i <= schm->colcount; ++i){
if(i != 0){
printf(", ");
}
printf("%d", schm->rowbytesize[i]);
}
printf("]\ndatatypes: [");
for(int i = 0; i < schm->colcount; ++i){
if(i != 0){
printf(", ");
}
print_datatype(schm->datatypes->values[i]);
}
printf("]\n");
}