66#include <string.h>
77#include <strings.h>
88#include <time.h>
9+ #include <ctype.h>
910#include <omp.h>
10- #include "../include/executeEngine-serial.h"
11- #include "../include/connectEngine.h"
12- #include "../include/printHelper.h"
11+ #include "../include/executeEngine-omp.h"
12+
13+ // Fix for include conflict: printHelper.h includes executeEngine-serial.h which conflicts with executeEngine-omp.h
14+ // We will manually declare printTable and NOT include printHelper.h
15+ // #include "../include/printHelper.h"
16+ void printTable (FILE * output , struct resultSetS * result , int limit );
17+
18+ #include "../include/sql.h"
19+
20+ // Constants
21+ #define DATA_FILE "data-generation/commands_50k.csv"
22+ #define TABLE_NAME "commands"
23+ #define MAX_TOKENS 100
24+ #define ROW_LIMIT 20
25+
26+ // Optimal indexes constant
27+ const char * optimalIndexes [] = {
28+ "command_id" ,
29+ "user_id" ,
30+ "risk_level" ,
31+ "exit_code" ,
32+ "sudo_used"
33+ };
34+ const FieldType optimalIndexTypes [] = {
35+ FIELD_UINT64 ,
36+ FIELD_INT ,
37+ FIELD_INT ,
38+ FIELD_INT ,
39+ FIELD_BOOL
40+ };
41+ const int numOptimalIndexes = 5 ;
1342
1443// ANSI color codes for pretty printing
1544#define CYAN "\x1b[36m"
1645#define YELLOW "\x1b[33m"
1746#define BOLD "\x1b[1m"
1847#define RESET "\x1b[0m"
1948
49+ // Helper to trim whitespace from a string
50+ static inline char * trim (char * s ) {
51+ while (* s && isspace ((unsigned char )* s )) s ++ ;
52+ // Also trim trailing
53+ char * end = s + strlen (s ) - 1 ;
54+ while (end > s && isspace ((unsigned char )* end )) end -- ;
55+ * (end + 1 ) = 0 ;
56+ return s ;
57+ }
58+
2059// Helper to safely copy strings with truncation (local version)
2160static inline void safe_copy (char * dst , size_t n , const char * src ) {
2261 snprintf (dst , n , "%.*s" , (int )n - 1 , src );
2362}
2463
64+ // Helper to map Parser OperatorType to string
65+ const char * get_operator_string (OperatorType op ) {
66+ switch (op ) {
67+ case OP_EQ : return "=" ;
68+ case OP_NEQ : return "!=" ;
69+ case OP_GT : return ">" ;
70+ case OP_LT : return "<" ;
71+ case OP_GTE : return ">=" ;
72+ case OP_LTE : return "<=" ;
73+ default : return "=" ;
74+ }
75+ }
76+
77+ // Helper to map Parser LogicOperator to string
78+ const char * get_logic_op_string (LogicOperator op ) {
79+ switch (op ) {
80+ case LOGIC_AND : return "AND" ;
81+ case LOGIC_OR : return "OR" ;
82+ default : return "AND" ;
83+ }
84+ }
85+
86+ // Helper to convert ParsedSQL conditions to engine's whereClauseS linked list
87+ struct whereClauseS * convert_conditions (ParsedSQL * parsed ) {
88+ if (parsed -> num_conditions == 0 ) return NULL ;
89+
90+ struct whereClauseS * head = NULL ;
91+ struct whereClauseS * current = NULL ;
92+
93+ for (int i = 0 ; i < parsed -> num_conditions ; i ++ ) {
94+ struct whereClauseS * node = malloc (sizeof (struct whereClauseS ));
95+
96+ if (parsed -> conditions [i ].is_nested && parsed -> conditions [i ].nested_sql ) {
97+ node -> attribute = NULL ;
98+ node -> operator = NULL ;
99+ node -> value = NULL ;
100+ node -> value_type = 0 ;
101+ node -> sub = convert_conditions (parsed -> conditions [i ].nested_sql );
102+ } else {
103+ node -> attribute = parsed -> conditions [i ].column ;
104+ node -> operator = get_operator_string (parsed -> conditions [i ].op );
105+ node -> value = parsed -> conditions [i ].value ;
106+
107+ // Simple type inference for the test
108+ if (parsed -> conditions [i ].is_numeric ) {
109+ node -> value_type = 0 ; // Integer/Number
110+ } else {
111+ node -> value_type = 1 ; // String
112+ }
113+ node -> sub = NULL ;
114+ }
115+
116+ node -> next = NULL ;
117+
118+ if (i < parsed -> num_conditions - 1 ) {
119+ node -> logical_op = get_logic_op_string (parsed -> logic_ops [i ]);
120+ } else {
121+ node -> logical_op = NULL ;
122+ }
123+
124+ if (head == NULL ) {
125+ head = node ;
126+ current = node ;
127+ } else {
128+ current -> next = node ;
129+ current = node ;
130+ }
131+ }
132+ return head ;
133+ }
134+
135+ // Helper to free the manually constructed where clause
136+ void free_where_clause_list (struct whereClauseS * head ) {
137+ while (head ) {
138+ struct whereClauseS * temp = head ;
139+ head = head -> next ;
140+ free (temp );
141+ }
142+ }
143+
25144int main (int argc , char * argv []) {
145+ printf ("Starting main...\n" ); fflush (stdout );
26146
27147 // Pull out number of defined threads from CLI args (default to 8)
28148 int num_threads = 8 ;
@@ -34,14 +154,19 @@ int main(int argc, char *argv[]) {
34154 // Start a timer for total runtime statistics
35155 double totalStart = omp_get_wtime ();
36156
157+ printf ("Initializing Engine...\n" ); fflush (stdout );
37158 // Instantiate an engine object to handle the execution of the query
38- struct engineS * engine = initializeEngineSerial (
159+ struct engineS * engine = initializeEngineOMP (
39160 numOptimalIndexes , // Number of indexes
40161 optimalIndexes , // Indexes to build B+ trees for
41162 (const int * )optimalIndexTypes , // Index types
42163 DATA_FILE ,
43164 TABLE_NAME
44165 );
166+ printf ("Engine Initialized.\n" ); fflush (stdout );
167+
168+ // Wait for all threads to sync before proceeding
169+ #pragma omp barrier
45170
46171 // End timer for engine initialization
47172 double initTimeTaken = ((double )omp_get_wtime () - totalStart ) / CLOCKS_PER_SEC ;
@@ -51,7 +176,7 @@ int main(int argc, char *argv[]) {
51176 FILE * fp = fopen (query_file , "r" );
52177 if (!fp ) {
53178 perror ("Failed to open query file" );
54- destroyEngineSerial (engine );
179+ destroyEngineOMP (engine );
55180 return EXIT_FAILURE ;
56181 }
57182
@@ -63,15 +188,15 @@ int main(int argc, char *argv[]) {
63188 if (!buffer ) {
64189 perror ("Failed to allocate memory for query file" );
65190 fclose (fp );
66- destroyEngineSerial (engine );
191+ destroyEngineOMP (engine );
67192 return EXIT_FAILURE ;
68193 }
69194 size_t read_size = fread (buffer , 1 , fsize , fp );
70195 if (read_size != (size_t )fsize ) {
71196 perror ("Failed to read query file" );
72197 free (buffer );
73198 fclose (fp );
74- destroyEngineSerial (engine );
199+ destroyEngineOMP (engine );
75200 return EXIT_FAILURE ;
76201 }
77202 buffer [fsize ] = 0 ;
@@ -139,18 +264,18 @@ int main(int argc, char *argv[]) {
139264 safe_copy (r .host_name , sizeof (r .host_name ), parsed .insert_values [10 ]);
140265 r .risk_level = atoi (parsed .insert_values [11 ]);
141266
142- success = executeQueryInsertSerial (engine , parsed .table , & r );
267+ success = executeQueryInsertOMP (engine , parsed .table , & r );
143268 }
144269 }
145270 else if (parsed .command == CMD_DELETE ) {
146271 struct whereClauseS * whereClause = convert_conditions (& parsed );
147- result = executeQueryDeleteSerial (engine , parsed .table , whereClause );
272+ result = executeQueryDeleteOMP (engine , parsed .table , whereClause );
148273 if (result ) rowsAffected = result -> numRecords ;
149274 free_where_clause_list (whereClause );
150275 }
151276 else if (parsed .command == CMD_SELECT ) {
152277 struct whereClauseS * whereClause = convert_conditions (& parsed );
153- result = executeQuerySelectSerial (engine , selectItems , numSelectItems , parsed .table , whereClause );
278+ result = executeQuerySelectOMP (engine , selectItems , numSelectItems , parsed .table , whereClause );
154279 free_where_clause_list (whereClause );
155280 }
156281
@@ -197,7 +322,7 @@ int main(int argc, char *argv[]) {
197322 }
198323
199324 free (buffer );
200- destroyEngineSerial (engine );
325+ destroyEngineOMP (engine );
201326
202327 // Print total runtime statistics in pretty colors
203328 double totalTimeTaken = ((double )omp_get_wtime () - totalStart ) / CLOCKS_PER_SEC ;
@@ -209,4 +334,4 @@ int main(int argc, char *argv[]) {
209334 printf (CYAN "=================================" RESET "\n" );
210335
211336 return EXIT_SUCCESS ;
212- }
337+ }
0 commit comments