示例#1
0
int test_sky_table_open() {
    struct tagbstring lock_file_path = bsStatic("tmp/.skylock");
    struct tagbstring data_file_path = bsStatic("tmp/0/data");
    struct tagbstring header_file_path = bsStatic("tmp/0/header");
    cleantmp();
    
    int rc;
    sky_table *table = sky_table_create();
    table->path = bfromcstr("tmp");
    
    // Open table.
    rc = sky_table_open(table);
    mu_assert_int_equals(rc, 0);
    mu_assert(sky_file_exists(&lock_file_path), "");
    mu_assert(sky_file_exists(&data_file_path), "");
    mu_assert(sky_file_exists(&header_file_path), "");
    
    // Close table.
    rc = sky_table_close(table);
    mu_assert_int_equals(rc, 0);
    mu_assert(!sky_file_exists(&lock_file_path), "");
    mu_assert(sky_file_exists(&data_file_path), "");
    mu_assert(sky_file_exists(&header_file_path), "");

    sky_table_free(table);
    return 0;
}
示例#2
0
文件: importer.c 项目: dasfaha/sky
int sky_importer_process_table(sky_importer *importer, bstring source,
                               jsmntok_t *tokens, uint32_t *index)
{
    int rc;
    check(importer != NULL, "Importer required");
    check(source != NULL, "Source required");
    check(tokens != NULL, "Tokens required");
    check(index != NULL, "Token index required");

    jsmntok_t *table_token = &tokens[*index];
    (*index)++;
    
    // Initialize import table.
    importer->table = sky_table_create(); check_mem(importer->table);
    importer->table->path = bstrcpy(importer->path);
    
    // Process over child tokens.
    int32_t i;
    for(i=0; i<(table_token->size/2); i++) {
        jsmntok_t *token = &tokens[*index];
        (*index)++;
        
        if(sky_importer_tokstr_equal(source, token, "blockSize")) {
            importer->table->default_block_size = (uint32_t)sky_importer_token_parse_int(source, &tokens[(*index)++]);
        }
        else if(sky_importer_tokstr_equal(source, token, "actions")) {
            rc = sky_importer_process_actions(importer, source, tokens, index);
            check(rc == 0, "Unable to process actions import");
        }
        else if(sky_importer_tokstr_equal(source, token, "properties")) {
            rc = sky_importer_process_properties(importer, source, tokens, index);
            check(rc == 0, "Unable to process properties import");
        }
        else if(sky_importer_tokstr_equal(source, token, "events")) {
            rc = sky_importer_process_events(importer, source, tokens, index);
            check(rc == 0, "Unable to process events import");
        }
        else {
            sentinel("Invalid token at char %d", tokens[*index].start);
        }
    }
    
    sky_table_close(importer->table);

    return 0;

error:
    return -1;
}
示例#3
0
文件: table.c 项目: hongfei1988/sky
// Opens the table for reading and writing events.
//
// table - The table to open.
//
// Returns 0 if successful, otherwise returns -1.
int sky_table_open(sky_table *table)
{
    int rc;
    check(table != NULL, "Table required");
    check(table->path != NULL, "Table path is required");
    check(!table->opened, "Table is already open");

    // Create directory if it doesn't exist.
    if(!sky_file_exists(table->path)) {
        rc = mkdir(bdata(table->path), S_IRWXU);
        check(rc == 0, "Unable to create table directory: %s", bdata(table->path));
    }

    // Obtain a lock.
    rc = sky_table_lock(table);
    check(rc == 0, "Unable to obtain lock");

    // Load data file.
    rc = sky_table_load_data_file(table);
    check(rc == 0, "Unable to load data file");

    // Load action file.
    rc = sky_table_load_action_file(table);
    check(rc == 0, "Unable to load action file");

    // Load property file.
    rc = sky_table_load_property_file(table);
    check(rc == 0, "Unable to load property file");

    // Flag the table as open.
    table->opened = true;

    return 0;

error:
    sky_table_close(table);
    return -1;
}
示例#4
0
文件: sky_bench.c 项目: dasfaha/sky
// 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);
}
示例#5
0
文件: sky_bench.c 项目: dasfaha/sky
// 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);
}