-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.c
263 lines (216 loc) · 5.79 KB
/
engine.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "engine.h"
#include "def.h"
#include "table.h"
#include "relation.h"
#include "util.h"
/* FIXME research what this value should be */
#define MAX_CMD_LEN 20000
char cmd_buf[MAX_CMD_LEN];
/* 'fmt' can contain these specifiers:
*
* %i: step.tmpfile_prerequisites.tmpfile
* %o: step.tmpfile
* %%: %
*/
int
format_step(char *buf, char *fmt, step *stp) {
/* TODO: buffer offerflow problems? */
if (!buf) {
fprintf(stderr, "[ERROR] format_step(): buf was NULL\n");
return FAILURE;
}
if (!fmt) {
fprintf(stderr, "[ERROR] format_step(): fmt was NULL\n");
return FAILURE;
}
if (!stp) {
fprintf(stderr, "[ERROR] format_step(): stp was NULL\n");
return FAILURE;
}
char *bufp = buf;
char *fmtp = fmt;
step *prereq = stp->tmpfile_prerequisites;
int tmpfile_len;
if (stp->stdin_prerequisite) {
if (format_step(buf,
stp->stdin_prerequisite->stdout_cmd_fmt,
stp->stdin_prerequisite) == FAILURE) {
return FAILURE;
}
int buflen = strlen(buf);
strncpy(buf + buflen, " | ", 3);
bufp = buf + buflen + 3;
}
while (1) {
char *p = strchr(fmtp, '%');
if (p == NULL) {
if (prereq) {
fprintf(stderr, "[ERROR] format_step(): "
"more tmpfile_prerequisites than %%i specifiers\n");
return FAILURE;
}
else {
strcpy(bufp, fmtp);
return SUCCESS;
}
}
else {
int len = p - fmtp;
if (len > 0) {
strncpy(bufp, fmtp, len);
bufp += len;
}
switch (*(p + 1)) {
case '%':
*bufp = '%';
bufp++;
fmtp = p + 2;
break;
case 'i':
if (!prereq) {
fprintf(stderr, "[ERROR] format_step(): "
"more %%i specifiers than tmpfile_prerequisites\n");
return FAILURE;
}
tmpfile_len = strlen(prereq->tmpfile);
strncpy(bufp, prereq->tmpfile, tmpfile_len);
bufp += tmpfile_len;
prereq = prereq->next;
fmtp = p + 2;
break;
case 'o':
if (!stp->tmpfile) {
fprintf(stderr, "[ERROR] format_step(): "
"tmpfile for step was not set.\n");
return FAILURE;
}
tmpfile_len = strlen(stp->tmpfile);
strncpy(bufp, stp->tmpfile, tmpfile_len);
bufp += tmpfile_len;
fmtp = p + 2;
break;
default:
fprintf(stderr, "[ERROR] format_step(): "
"unrecognized format specifier: %%%c\n", *(p + 1));
return FAILURE;
}
}
}
}
char *
join_type_to_join_flag(join_type jt) {
/* returning a string literal is ok */
switch(jt) {
case INNER:
return "";
case LEFT:
return "-a 1";
case RIGHT:
return "-a 2";
case FULL:
return "-a 1 -a 2";
default:
fprintf(stderr, "[ERROR] unrecognized join_type %d", jt);
return NULL;
}
}
int
execute_tmpfile_step(step *stp) {
step *tmpfile_step;
char *tmpfile;
for (tmpfile_step = stp->tmpfile_prerequisites;
tmpfile_step;
tmpfile_step = tmpfile_step->next) {
execute_tmpfile_step(tmpfile_step);
}
stp->tmpfile = make_tmpfile();
if (format_step(cmd_buf, stp->tmpfile_cmd_fmt, stp) == SUCCESS) {
printf("[DEBUG]: about to execute: %s\n", cmd_buf);
int retval = system(cmd_buf);
return retval;
}
else {
/* TODO error msg */
return FAILURE;
}
}
int
execute_stdout_step(step *stp) {
step *tmpfile_step;
char *tmpfile;
for (tmpfile_step = stp->tmpfile_prerequisites;
tmpfile_step;
tmpfile_step = tmpfile_step->next) {
execute_tmpfile_step(tmpfile_step);
}
if (format_step(cmd_buf,
stp->stdout_cmd_fmt,
stp) == SUCCESS) {
printf("[DEBUG]: about to execute: %s\n", cmd_buf);
int retval = system(cmd_buf);
return retval;
}
// TODO error msg
return FAILURE;
}
/*
int
execute_join(query *qry, join *join) {
assert(qry);
assert(join);
// TODO: how do we set these?
//
int left_numeric = 0;
int right_numeric = 0;
table *left_relation = table_name_to_table(qry->tables,
join->left_relation_name);
table *right_relation = table_name_to_table(qry->tables,
join->right_relation_name);
int left_column_number = table_column__number(left_table,
join->left_column_name);
int right_column_number = table_column_number(right_table,
join->right_column_name);
// what about header?
snprintf(cmd_buf, MAX_CMD_LEN, "sort %s -t $'\\t' -k %d,%d %s > %s",
left_numeric ? "-n" : "",
left_column_number,
left_column_number,
left_path,
sort_left_path);
if (system(cmd_buf)) {
fprintf("[ERROR] external cmd failed: %s\n", cmd_buf);
return FAILURE;
}
// what about header?
snprintf(cmd_buf, MAX_CMD_LEN, "sort %s -t $'\\t' -k %d,%d %s > %s",
right_numeric ? "-n" : "",
right_column_number,
right_column_number,
right_path,
sort_right_path);
if (system(cmd_buf)) {
fprintf("[ERROR] external cmd failed: %s\n", cmd_buf);
return FAILURE;
}
char *join_flag = join_type_to_join_flag(join->join_type);
if (!join_flag) {
return FAILURE;
}
snprintf(cmd_buf, MAX_CMD_LEN, "join -1 %d -2 %d %s %s %s > %s",
left_column_number,
right_column_number,
join_flag,
sort_left_path,
sort_right_path,
join_path);
if (system(cmd_buf)) {
fprintf("[ERROR] external cmd failed: %s\n", cmd_buf);
return FAILURE;
}
return SUCCESS;
}
*/