77#include "../../include/buildEngine-omp.h"
88#include "../../include/executeEngine-omp.h"
99#include <omp.h>
10+ #include <stdlib.h>
11+ #include <stdio.h>
12+ #include <stdbool.h>
1013#define VERBOSE 0
1114
15+
1216// Function pointer type for WHERE condition evaluation
1317typedef bool (* where_condition_func )(void * record , void * value );
1418
@@ -634,57 +638,89 @@ bool executeQueryInsertSerial(
634638 return success ;
635639}
636640
637- /* Main functionality for DELETE logic
638- * Parameters:
639- * engine - constant engine object
640- * tableName - name of the table
641- * whereClause - WHERE clause (NULL for all rows)
642- * Returns:
643- * ResultSet containing number of deleted records
644- */
641+
642+ // assumes these types / functions are declared elsewhere:
643+ // struct engineS;
644+ // struct resultSetS;
645+ // struct whereClauseS;
646+ // typedef ... KEY_T;
647+ // typedef ... *ROW_PTR;
648+ // typedef struct recordS record;
649+ // bool evaluateWhereClause(record *rec, struct whereClauseS *whereClause);
650+ // KEY_T extract_key_from_record(record *rec, const char *attr);
651+ // void *delete(void *root, KEY_T key, ROW_PTR row_ptr);
652+
645653struct resultSetS * executeQueryDeleteSerial (
646- struct engineS * engine , // Constant engine object
647- const char * tableName , // Table to delete from
648- struct whereClauseS * whereClause // WHERE clause (NULL for all rows)
654+ struct engineS * engine , // Constant engine object
655+ const char * tableName , // Table to delete from (unused here)
656+ struct whereClauseS * whereClause // WHERE clause (NULL for all rows)
649657) {
650658 struct resultSetS * result = (struct resultSetS * )malloc (sizeof (struct resultSetS ));
651- result -> numRecords = 0 ;
652- result -> numColumns = 0 ;
653- result -> columnNames = NULL ;
654- result -> columnTypes = NULL ;
655- result -> data = NULL ;
656- result -> queryTime = 0.0 ;
657- result -> success = false;
658-
659- clock_t start = clock ();
659+ if (!result ) {
660+ return NULL;
661+ }
662+
663+ result -> numRecords = 0 ;
664+ result -> numColumns = 0 ;
665+ result -> columnNames = NULL ;
666+ result -> columnTypes = NULL ;
667+ result -> data = NULL ;
668+ result -> queryTime = 0.0 ;
669+ result -> success = false;
670+
671+ double start = omp_get_wtime ();
672+
673+ int num_records = engine -> num_records ;
660674 int deletedCount = 0 ;
661- int writeIndex = 0 ;
662675
663- // Iterate through all records in the engine
664- for (int i = 0 ; i < engine -> num_records ; i ++ ) {
665- record * currentRecord = engine -> all_records [i ];
666- bool shouldDelete = false;
676+ // 1) Parallel phase: compute delete flags
677+ int * deleteFlags = (int * )calloc (num_records , sizeof (int ));
678+ if (!deleteFlags ) {
679+ return result ; // result->success stays false
680+ }
667681
668- // Check if the record matches the WHERE clause
669- if (whereClause == NULL ) {
670- shouldDelete = true; // Delete all if no WHERE clause
671- } else {
672- shouldDelete = evaluateWhereClause (currentRecord , whereClause );
682+ #pragma omp parallel
683+ {
684+ int localDeleted = 0 ;
685+
686+ #pragma omp for nowait
687+ for (int i = 0 ; i < num_records ; i ++ ) {
688+ record * currentRecord = engine -> all_records [i ];
689+ bool shouldDelete ;
690+
691+ if (whereClause == NULL ) {
692+ shouldDelete = true; // Delete all if no WHERE clause
693+ } else {
694+ shouldDelete = evaluateWhereClause (currentRecord , whereClause );
695+ }
696+
697+ if (shouldDelete ) {
698+ deleteFlags [i ] = 1 ;
699+ localDeleted ++ ;
700+ }
673701 }
674702
675- if (shouldDelete ) {
676- // Delete the matched record
677-
678- // Remove from B+ Tree Indexes
703+ #pragma omp atomic
704+ deletedCount += localDeleted ;
705+ }
706+
707+ // 2) Serial phase: mutate engine, update B+ trees, free records, compact array
708+ int writeIndex = 0 ;
709+
710+ for (int i = 0 ; i < num_records ; i ++ ) {
711+ record * currentRecord = engine -> all_records [i ];
712+
713+ if (deleteFlags [i ]) {
714+ // Remove from B+ Tree indexes
679715 for (int j = 0 ; j < engine -> num_indexes ; j ++ ) {
680- const char * indexed_attr = engine -> indexed_attributes [j ];
681- KEY_T key = extract_key_from_record (currentRecord , indexed_attr );
682- engine -> bplus_tree_roots [j ] = delete (engine -> bplus_tree_roots [j ], key , (ROW_PTR )currentRecord );
716+ const char * indexed_attr = engine -> indexed_attributes [j ];
717+ KEY_T key = extract_key_from_record (currentRecord , indexed_attr );
718+ engine -> bplus_tree_roots [j ] =
719+ delete (engine -> bplus_tree_roots [j ], key , (ROW_PTR )currentRecord );
683720 }
684721
685722 // Free the record memory
686723 free (currentRecord );
687- deletedCount ++ ;
688724 } else {
689725 // Keep the record, move it to the current write position if needed
690726 if (writeIndex != i ) {
@@ -694,10 +730,12 @@ struct resultSetS *executeQueryDeleteSerial(
694730 }
695731 }
696732
733+ free (deleteFlags );
734+
697735 // Update the record count in the engine
698736 engine -> num_records = writeIndex ;
699737
700- // Rewrite the CSV file with the remaining records
738+ // Rewrite the CSV file with the remaining records (serial)
701739 FILE * file = fopen (engine -> datafile , "w" );
702740 if (file != NULL ) {
703741 for (int i = 0 ; i < engine -> num_records ; i ++ ) {
@@ -718,16 +756,18 @@ struct resultSetS *executeQueryDeleteSerial(
718756 }
719757 fclose (file );
720758 } else {
721- if (VERBOSE ) {
722- fprintf (stderr , "Failed to open data file for rewriting: %s\n" , engine -> datafile );
759+ if ( VERBOSE ) {
760+ fprintf (stderr ,
761+ "Failed to open data file for rewriting: %s\n" ,
762+ engine -> datafile );
723763 }
724764 }
725765
726- double time_taken = (( double ) clock () - start ) / CLOCKS_PER_SEC ;
727-
766+ double time_taken = omp_get_wtime () - start ;
767+
728768 result -> numRecords = deletedCount ;
729- result -> queryTime = time_taken ;
730- result -> success = true;
769+ result -> queryTime = time_taken ;
770+ result -> success = true;
731771
732772 return result ;
733773}
0 commit comments