// Executes the benchmark to count the number of times an action occurred // using the QIP language. // // options - A list of options to use. void benchmark_count_with_qip(Options *options) { int rc; uint32_t path_count = 0; // Initialize the query. qip_ast_node *type_ref, *var_decl; uint32_t arg_count = 2; qip_ast_node *args[arg_count]; // Path arg. struct tagbstring path_str = bsStatic("path"); type_ref = qip_ast_type_ref_create_cstr("Path"); var_decl = qip_ast_var_decl_create(type_ref, &path_str, NULL); args[0] = qip_ast_farg_create(var_decl); // Map arg. struct tagbstring data_str = bsStatic("data"); type_ref = qip_ast_type_ref_create_cstr("Map"); qip_ast_type_ref_add_subtype(type_ref, qip_ast_type_ref_create_cstr("Int")); qip_ast_type_ref_add_subtype(type_ref, qip_ast_type_ref_create_cstr("Result")); var_decl = qip_ast_var_decl_create(type_ref, &data_str, NULL); args[1] = qip_ast_farg_create(var_decl); // Initialize query. struct tagbstring query = bsStatic( "[Hashable(\"id\")]\n" "class Result {\n" " public Int id;\n" " public Int count;\n" "}\n" "Cursor cursor = path.events();\n" "for each (Event event in cursor) {\n" " Result item = data.get(event.actionId);\n" " item.count = item.count + 1;\n" "}\n" "return;" ); struct tagbstring module_name = bsStatic("qip"); struct tagbstring core_class_path = bsStatic("lib/core"); struct tagbstring sky_class_path = bsStatic("lib/sky"); qip_compiler *compiler = qip_compiler_create(); qip_compiler_add_class_path(compiler, &core_class_path); qip_compiler_add_class_path(compiler, &sky_class_path); qip_module *module = qip_module_create(&module_name, compiler); rc = qip_compiler_compile(compiler, module, &query, args, arg_count); check(rc == 0, "Unable to compile"); if(module->error_count > 0) fprintf(stderr, "Parse error [line %d] %s\n", module->errors[0]->line_no, bdata(module->errors[0]->message)); check(module->error_count == 0, "Compile error"); qip_compiler_free(compiler); // Retrieve pointer to function. sky_qip_path_map_func process_path = NULL; rc = qip_module_get_main_function(module, (void*)(&process_path)); check(rc == 0, "Unable to retrieve main function"); // Initialize table. sky_table *table = sky_table_create(); check_mem(table); rc = sky_table_set_path(table, options->path); check(rc == 0, "Unable to set path on table"); // Open table rc = sky_table_open(table); check(rc == 0, "Unable to open table"); // Loop for desired number of iterations. int i; for(i=0; i<options->iterations; i++) { sky_path_iterator iterator; sky_path_iterator_init(&iterator); // Attach data file. rc = sky_path_iterator_set_data_file(&iterator, table->data_file); check(rc == 0, "Unable to initialze path iterator"); // Initialize QIP args. sky_qip_path *path = sky_qip_path_create(); qip_map *map = qip_map_create(); // Iterate over each path. while(!iterator.eof) { // Retrieve the path pointer. rc = sky_path_iterator_get_ptr(&iterator, &path->path_ptr); check(rc == 0, "Unable to retrieve the path iterator pointer"); // Execute query. process_path(path, map); // Move to next path. rc = sky_path_iterator_next(&iterator); check(rc == 0, "Unable to find next path"); path_count++; } // Clean up iteration. qip_map_free(map); } // Clean up rc = sky_table_close(table); check(rc == 0, "Unable to close table"); sky_table_free(table); // Show stats. printf("Total paths processed: %d\n", path_count); return; error: sky_table_free(table); }
int test_sky_path_iterator_data_file_next() { loadtmp("tests/fixtures/path_iterator/1"); int rc; void *ptr; sky_data_file *data_file = sky_data_file_create(); data_file->path = bfromcstr("tmp/data"); data_file->header_path = bfromcstr("tmp/header"); sky_data_file_load(data_file); sky_path_iterator *iterator = sky_path_iterator_create(); sky_path_iterator_set_data_file(iterator, data_file); mu_assert(iterator->block == NULL, ""); mu_assert(iterator->data_file == data_file, ""); mu_assert_int_equals(iterator->block_index, 0); // Path 1 rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-data_file->data, 0L); mu_assert_int_equals(iterator->current_object_id, 2); mu_assert_bool(!iterator->eof); // Path 2 rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 19); mu_assert_int_equals(iterator->current_object_id, 3); mu_assert_bool(!iterator->eof); rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-data_file->data, 19L); // Path 3 (Spanned) rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 0); mu_assert_int_equals(iterator->current_object_id, 4); mu_assert_bool(!iterator->eof); rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-data_file->data, 64L); // Path 4 rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 0); mu_assert_int_equals(iterator->current_object_id, 5); mu_assert_bool(!iterator->eof); rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-data_file->data, 128L); // EOF rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 0); mu_assert_bool(iterator->eof); sky_path_iterator_free(iterator); sky_data_file_free(data_file); return 0; }
// Executes the benchmark over the database to compute step counts in order to // generate a directed acyclic graph (DAG). // // options - A list of options to use. void benchmark_dag(Options *options) { int rc; sky_event *event = NULL; uint32_t event_count = 0; int32_t action_count = 100; // TODO: Retrieve action count from actions file. // Initialize table. sky_table *table = sky_table_create(); check_mem(table); rc = sky_table_set_path(table, options->path); check(rc == 0, "Unable to set path on table"); // Open table rc = sky_table_open(table); check(rc == 0, "Unable to open table"); // Loop for desired number of iterations. int i; for(i=0; i<options->iterations; i++) { sky_path_iterator iterator; sky_path_iterator_init(&iterator); // Attach data file. rc = sky_path_iterator_set_data_file(&iterator, table->data_file); check(rc == 0, "Unable to initialze path iterator"); // Create a square matrix of structs. Step *steps = calloc(action_count*action_count, sizeof(Step)); // Iterate over each path. while(!iterator.eof) { sky_action_id_t action_id, prev_action_id; // Retrieve the path pointer. void *path_ptr; rc = sky_path_iterator_get_ptr(&iterator, &path_ptr); check(rc == 0, "Unable to retrieve the path iterator pointer"); // Initialize the cursor. sky_cursor cursor; sky_cursor_init(&cursor); rc = sky_cursor_set_path(&cursor, path_ptr); check(rc == 0, "Unable to set cursor path"); // Increment total event count. event_count++; // Initialize the previous action. rc = sky_cursor_get_action_id(&cursor, &prev_action_id); check(rc == 0, "Unable to retrieve first action"); // Find next event. rc = sky_cursor_next(&cursor); check(rc == 0, "Unable to find next event"); // Loop over each event in the path. while(!cursor.eof) { // Increment total event count. event_count++; // Retrieve action. rc = sky_cursor_get_action_id(&cursor, &action_id); check(rc == 0, "Unable to retrieve first action"); // Aggregate step information. int32_t index = ((prev_action_id-1)*action_count) + (action_id-1); steps[index].count++; // Assign current action as previous action. prev_action_id = action_id; // Find next event. rc = sky_cursor_next(&cursor); check(rc == 0, "Unable to find next event"); } rc = sky_path_iterator_next(&iterator); check(rc == 0, "Unable to find next path"); } // Show DAG data. //int x; //int total=0; //for(x=0; x<action_count*action_count; x++) { // printf("%06d %d\n", x, steps[x].count); // total += steps[x].count; //} //printf("total: %d\n", total); } // Clean up rc = sky_table_close(table); check(rc == 0, "Unable to close table"); sky_table_free(table); // Show stats. printf("Total events processed: %d\n", event_count); return; error: sky_event_free(event); sky_table_free(table); }
// Runs a PEACH query against a table. // // message - The message. // table - The table to run the query against. // output - The output stream to write to. // // Returns 0 if successful, otherwise returns -1. int sky_peach_message_process(sky_peach_message *message, sky_table *table, FILE *output) { int rc; check(message != NULL, "Message required"); check(table != NULL, "Table required"); check(output != NULL, "Output stream required"); // Compile. sky_qip_module *module = sky_qip_module_create(); check_mem(module); module->table = table; rc = sky_qip_module_compile(module, message->query); check(rc == 0, "Unable to compile query"); sky_qip_path_map_func main_function = (sky_qip_path_map_func)module->main_function; // Initialize the path iterator. sky_path_iterator iterator; sky_path_iterator_init(&iterator); rc = sky_path_iterator_set_data_file(&iterator, table->data_file); check(rc == 0, "Unable to initialze path iterator"); // Initialize QIP args. sky_qip_path *path = sky_qip_path_create(); qip_map *map = qip_map_create(); // Iterate over each path. uint32_t path_count = 0; while(!iterator.eof) { // Retrieve the path pointer. rc = sky_path_iterator_get_ptr(&iterator, &path->path_ptr); check(rc == 0, "Unable to retrieve the path iterator pointer"); // Execute query. main_function(path, map); // Move to next path. rc = sky_path_iterator_next(&iterator); check(rc == 0, "Unable to find next path"); path_count++; } //debug("Paths processed: %d", path_count); // Retrieve Result serialization function. struct tagbstring result_str = bsStatic("Result"); struct tagbstring serialize_str = bsStatic("serialize"); sky_qip_result_serialize_func result_serialize = NULL; rc = qip_module_get_class_method(module->_qip_module, &result_str, &serialize_str, (void*)(&result_serialize)); check(rc == 0 && result_serialize != NULL, "Unable to find serialize() method on class 'Result'"); // Serialize. qip_serializer *serializer = qip_serializer_create(); qip_serializer_pack_map(module->_qip_module, serializer, map->count); int64_t i; for(i=0; i<map->count; i++) { result_serialize(map->elements[i], serializer); } // Send response to output stream. rc = fwrite(serializer->data, serializer->length, 1, output); check(rc == 1, "Unable to write serialized data to stream"); qip_map_free(map); sky_qip_module_free(module); return 0; error: sky_qip_module_free(module); return -1; }