|
| 1 | +/* OMP Execute Engine */ |
| 2 | + |
| 3 | +#ifndef EXECUTE_ENGINE_SERIAL_H |
| 4 | +#define EXECUTE_ENGINE_SERIAL_H |
| 5 | + |
| 6 | +#include <stdbool.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | +#include "logType.h" |
| 11 | +#include "recordSchema.h" |
| 12 | +#include <omp.h> |
| 13 | + |
| 14 | +/* Struct for the engine */ |
| 15 | +/* Holds the state of the database engine, including all data records and active indexes. */ |
| 16 | +struct engineS { |
| 17 | + char *tableName; // Name of the table represented by this engine |
| 18 | + node **bplus_tree_roots; // Array of roots for all B+ tree indexes |
| 19 | + int num_indexes; // Number of indexes |
| 20 | + char **indexed_attributes; // Names of indexed attributes |
| 21 | + FieldType *attribute_types; // Types of indexed attributes (from record schema) |
| 22 | + record **all_records; // Array of all records in the table (for full table scans on non-indexed queries and for assigning row pointers) |
| 23 | + int num_records; // Total number of records in the table |
| 24 | + char *datafile; // Path to the data file |
| 25 | +}; |
| 26 | + |
| 27 | +/* Result set - The results of any given query |
| 28 | + * Holds the output of a select query, containing the selected columns and rows in a 2D string matrix. |
| 29 | +*/ |
| 30 | +struct resultSetS { |
| 31 | + int numRecords; // Number of rows found or affected |
| 32 | + int numColumns; // Number of columns selected |
| 33 | + char **columnNames; // Array of column names (headers) |
| 34 | + FieldType *columnTypes; // Array of column types (corresponding to columnNames) |
| 35 | + char ***data; // 2D Matrix of result data as strings: data[row][col] |
| 36 | + double queryTime; // Time taken to execute the query |
| 37 | + bool success; // Whether the query was successful |
| 38 | +}; |
| 39 | + |
| 40 | +/* Frees the memory allocated for a result set */ |
| 41 | +void freeResultSet(struct resultSetS *result); |
| 42 | + |
| 43 | +/* WHERE clause struct to hold filtering conditions */ |
| 44 | +/* |
| 45 | + * Represents a single condition in a WHERE clause (e.g., "risk_level > 2"). |
| 46 | + * Can be chained together to form complex queries (e.g., "risk_level > 2 AND user_id = 101"). |
| 47 | + */ |
| 48 | +struct whereClauseS { |
| 49 | + const char *attribute; // Attribute name to filter on (e.g., "risk_level") |
| 50 | + const char *operator; // Comparison operator (=, !=, <, >, <=, >=) |
| 51 | + const char *value; // Value to compare against (as string, converted internally based on type) |
| 52 | + int value_type; // Type of value (0 = integer, 1 = string, 2 = boolean) |
| 53 | + struct whereClauseS *next; // Pointer to the next condition in the chain (or NULL) |
| 54 | + const char *logical_op; // Logical operator connecting to next condition ("AND", "OR") |
| 55 | + struct whereClauseS *sub; // Sub-expression for parentheses/nested conditions |
| 56 | +}; |
| 57 | + |
| 58 | +// Function pointers for non-numerical comparisons |
| 59 | +typedef bool (*compare_func_t)(const char *, const char *); // Comparing strings |
| 60 | +typedef bool (*compare_func_int_t)(const bool, const bool); // Comparing booleans |
| 61 | + |
| 62 | +// Select function - main entry point for SELECT queries. Returns a ResultSet |
| 63 | +/* |
| 64 | + * Executes a SELECT query. |
| 65 | + * - selectItems: Array of column names to retrieve. |
| 66 | + * - whereClause: Linked list of filtering conditions. |
| 67 | + * Returns a pointer to a ResultSet containing the selected columns and data. |
| 68 | + */ |
| 69 | +struct resultSetS *executeQuerySelectSerial( |
| 70 | + struct engineS *engine, // Engine object |
| 71 | + const char **selectItems, // Attributes to select (NULL for all) |
| 72 | + int numSelectItems, // Number of attributes to select |
| 73 | + const char *tableName, // Table to query from |
| 74 | + struct whereClauseS *whereClause // WHERE clause (NULL if no filtering) |
| 75 | +); |
| 76 | + |
| 77 | +// Insert function - main entry point for INSERT queries. Returns success/failure |
| 78 | +/* |
| 79 | + * Executes an INSERT query. |
| 80 | + * - insertItems: Array of [Attribute, Value] pairs to insert. |
| 81 | + * Updates both the main record storage and any relevant B+ tree indexes. |
| 82 | + */ |
| 83 | +bool executeQueryInsertSerial( |
| 84 | + struct engineS *engine, // Engine object |
| 85 | + const char *tableName, // Table to insert into |
| 86 | + const record *r // Record to insert |
| 87 | +); |
| 88 | + |
| 89 | +// Update function - main entry point for UPDATE queries. Returns a ResultSet |
| 90 | +/* |
| 91 | + * Executes an UPDATE query. |
| 92 | + * - setItems: Array of [Attribute, Value] pairs to update. |
| 93 | + * - whereClause: Filters which rows to update. |
| 94 | + * Returns a ResultSet containing the number of affected rows (numRecords). |
| 95 | + */ |
| 96 | +struct resultSetS *executeQueryUpdateSerial( |
| 97 | + struct engineS *engine, // Engine object |
| 98 | + const char *tableName, // Table to update |
| 99 | + const char *(*setItems)[2], // Array of attribute-value pairs to set |
| 100 | + int numSetItems, // Number of attribute-value pairs |
| 101 | + struct whereClauseS *whereClause // WHERE clause (NULL for all rows) |
| 102 | +); |
| 103 | + |
| 104 | +// Delete function - main entry point for DELETE queries. Returns number of rows deleted |
| 105 | +/* |
| 106 | + * Executes a DELETE query. |
| 107 | + * - whereClause: Filters which rows to delete. |
| 108 | + * Returns a ResultSet containing the number of deleted rows (numRecords). |
| 109 | + */ |
| 110 | +struct resultSetS *executeQueryDeleteSerial( |
| 111 | + struct engineS *engine, // Engine object |
| 112 | + const char *tableName, // Table to delete from |
| 113 | + struct whereClauseS *whereClause // WHERE clause (NULL for all rows) |
| 114 | +); |
| 115 | + |
| 116 | +// Initialize the engine, returning a pointer to the engine object |
| 117 | +struct engineS *initializeEngineSerial( |
| 118 | + int num_indexes, // Total number of indexes to start |
| 119 | + const char *indexed_attributes[], // Names of indexed attributes |
| 120 | + const int attribute_types[], // Types of indexed attributes (0 = integer, 1 = string, 2 = boolean) |
| 121 | + const char *datafile, // Path to the data file |
| 122 | + const char *tableName // Name of the table |
| 123 | +); |
| 124 | + |
| 125 | +// Destroy the engine and free resources |
| 126 | +void destroyEngineSerial(struct engineS *engine); |
| 127 | + |
| 128 | +// Declare an attribute to be indexed in the serial engine |
| 129 | +bool addAttributeIndexSerial( |
| 130 | + struct engineS *engine, // Constant engine object |
| 131 | + const char *tableName, // Table name |
| 132 | + const char *attributeName, // Name of the attribute to index |
| 133 | + int attributeType // Attribute type to index (0 = integer, 1 = string, 2 = boolean) |
| 134 | +); |
| 135 | + |
| 136 | +// Helper function that returns if a given attribute is already indexed, returning the index or -1 if not found |
| 137 | +int isAttributeIndexed( |
| 138 | + struct engineS *engine, // Constant engine object |
| 139 | + const char *attributeName // Name of the attribute to check |
| 140 | +); |
| 141 | + |
| 142 | +// Helper function that performs a linear search through a given array of records based on the WHERE clause |
| 143 | +record **linearSearchRecords( |
| 144 | + record **records, // Array of records to search |
| 145 | + int num_records, // Number of records in the array |
| 146 | + struct whereClauseS *whereClause, // WHERE clause |
| 147 | + int *matchingRecords // Output parameter for number of matching records |
| 148 | +); |
| 149 | + |
| 150 | +// Recursive evaluator for WHERE clause |
| 151 | +bool evaluateWhereClause(record *r, struct whereClauseS *wc); |
| 152 | + |
| 153 | +#endif // EXECUTE_ENGINE_SERIAL_H |
0 commit comments