/** * Initialize the database storage * @return true if the database was initialized successfully, false otherwise */ bool initialize_storage(void) { ib_err_t error; ib_id_t tid; checked(ib_init()); checked(ib_cfg_set_text("data_home_dir", "/tmp/memcached_light")); checked(ib_cfg_set_text("log_group_home_dir", "/tmp/memcached_light")); checked(ib_cfg_set_bool_on("file_per_table")); checked(ib_startup("barracuda")); /* check to see if the table exists or if we should create the schema */ error= ib_table_get_id(tablename, &tid); if (error == DB_TABLE_NOT_FOUND) { if (!create_schema()) { return false; } } else if (error != DB_SUCCESS) { fprintf(stderr, "Failed to get table id: %s\n", ib_strerror(error)); return false; } return true; error_exit: return false; }
int main(int argc, char *argv[]) { if (argc != 2) { exit(1); } char db_name[32] = "cstore"; char *table_name = argv[1]; char full_table_name[32]; if (!init_db()) { exit(1); } if (!create_db(db_name)) { std::cerr << "create_db failed" << std::endl; exit(1); } sprintf(full_table_name, "%s/%s", db_name, table_name); if (!create_schema(full_table_name)) { std::cerr << "create_schema failed" << std::endl; exit(1); } ib_crsr_t ib_crsr; std::string line; int i = 0; ib_trx_t ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); ib_cursor_open_table(full_table_name, ib_trx, &ib_crsr); while (getline(std::cin, line)) { std::vector<std::string> strs; boost::split(strs, line, boost::is_any_of(" ") ); ib_tpl_t tpl = ib_clust_read_tuple_create(ib_crsr); ib_tuple_write_u32(tpl, 0, atoi(strs[0].c_str())); ib_tuple_write_u32(tpl, 1, atoi(strs[1].c_str())); ib_tuple_write_u32(tpl, 2, atoi(strs[2].c_str())); ib_err_t err = ib_cursor_insert_row(ib_crsr, tpl); if (err != DB_SUCCESS) { std::cerr << "insert_row failed" << std::endl; } ib_tuple_delete(tpl); if (++i % 10000 == 0) { std::cout << i << std::endl; ib_cursor_close(ib_crsr); ib_trx_commit(ib_trx); ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); ib_cursor_open_table(full_table_name, ib_trx, &ib_crsr); } } ib_cursor_close(ib_crsr); ib_trx_commit(ib_trx); fin_db(); return 0; }
/** \details Open connection to indexing database for a given user \param mstore_ctx pointer to the mapistore context \param username name for which the indexing database has to be created \param connection_string mysql connection string \param ictxp returned value with the indexing context created \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error */ _PUBLIC_ enum mapistore_error mapistore_indexing_mysql_init(struct mapistore_context *mstore_ctx, const char *username, const char *connection_string, struct indexing_context **ictxp) { struct indexing_context *ictx; bool schema_created; char *schema_file; MYSQL *conn = NULL; /* Sanity checks */ MAPISTORE_RETVAL_IF(!mstore_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL); MAPISTORE_RETVAL_IF(!username, MAPISTORE_ERR_INVALID_PARAMETER, NULL); MAPISTORE_RETVAL_IF(!connection_string, MAPISTORE_ERR_INVALID_PARAMETER, NULL); MAPISTORE_RETVAL_IF(!ictxp, MAPISTORE_ERR_INVALID_PARAMETER, NULL); ictx = talloc_zero(mstore_ctx, struct indexing_context); MAPISTORE_RETVAL_IF(!ictx, MAPISTORE_ERR_NO_MEMORY, NULL); ictx->data = create_connection(connection_string, &conn); talloc_set_destructor(ictx, mapistore_indexing_mysql_destructor); MAPISTORE_RETVAL_IF(!ictx->data, MAPISTORE_ERR_NOT_INITIALIZED, ictx); if (!table_exists(conn, INDEXING_TABLE)) { DEBUG(3, ("Creating schema for indexing on mysql %s\n", connection_string)); schema_file = talloc_asprintf(ictx, "%s/%s", MAPISTORE_LDIF, INDEXING_SCHEMA_FILE); MAPISTORE_RETVAL_IF(!schema_file, MAPISTORE_ERR_NO_MEMORY, NULL); schema_created = create_schema(MYSQL(ictx), schema_file); talloc_free(schema_file); MAPISTORE_RETVAL_IF(!schema_created, MAPISTORE_ERR_NOT_INITIALIZED, ictx); } /* TODO: extract url from backend mapping, by the moment we use the username */ ictx->url = talloc_strdup(ictx, username); MAPISTORE_RETVAL_IF(!ictx->url, MAPISTORE_ERR_NO_MEMORY, NULL); /* Fill function pointers */ ictx->add_fmid = mysql_record_add; ictx->del_fmid = mysql_record_del; ictx->update_fmid = mysql_record_update; ictx->get_uri = mysql_record_get_uri; ictx->get_fmid = mysql_record_get_fmid; ictx->allocate_fmid = mysql_record_allocate_fmid; ictx->allocate_fmids = mysql_record_allocate_fmids; *ictxp = ictx; return MAPISTORE_SUCCESS; }
} END_TEST START_TEST (test_is_schema_created) { enum mapistore_error retval; ck_assert(is_schema_created(conn) == false); ck_assert(is_database_empty(conn) == true); retval = create_schema(conn, NAMEDPROPS_MYSQL_SCHEMA_PATH); ck_assert_int_eq(retval, MAPISTORE_SUCCESS); ck_assert(is_schema_created(conn) == true); ck_assert(is_database_empty(conn) == true); } END_TEST
int main() { struct _database_ db; char command[20], username[20], password[20], name[20]; printf("Done check 0\n"); scanf("%s",&command); if(!strcmp(command,"create")) { printf("Enter Username and Password\n"); printf("Username: "******"%s", &username); printf("Password: "******"%s", &password); } initialize_database(&db,username,password); printf("Done check 1\n"); create_schema(&db); printf("Done check 2\n"); printf("Enter Project Name: ") scanf("%s", name); int id=create_new_project(&db,name); printf("id = %d SUCCESS\n",id); add_single_file(&db,1,"C:\\Users\\Jatinder Dhawan\\Desktop\\git\\abc.png",3); printf("=======================\n"); find_in_database(&db,"C:\\Users\\Jatinder Dhawan\\Desktop\\git\\abc.png","C:\\Users\\Jatinder Dhawan\\Desktop\\git\\abc.png"); printf("=======================\n"); get_project_id(&db,"git_pro"); struct file_list* start=(struct file_list*)malloc(sizeof(struct file_list)); struct file_list*make=start; start->_version =5; start->number_of_files=3; start->name="sjvndfjv"; start->path="C:\\Users\\Jatinder Dhawan\\Desktop\\database\\credentials.txt"; start->next=(struct file_list*)malloc(sizeof(struct file_list)); start=start->next; start->_version =5; start->number_of_files=3; start->name="sjvndfjv"; start->path="C:\\Users\\Jatinder Dhawan\\Desktop\\database\\tempo.txt"; start->next=NULL; add_multiple_file(&db,make,1,3); update_project_version(&db,"git_pro"); return 0 ; }
// Main--------------------------------------------------------------------------------------------------------------- int main(int argc, char *argv[]) { int repeat, aux; char *input; // A lista schema é criada e lida da stdin SCHEMA *schema = create_schema(); get_schema(schema); do { // A cada repeticao le um comando da stdin repeat = 1; input = my_get_line(stdin, &aux); if(input == NULL) fprintf(stderr, "chamada errada\n"); // Analisa qual o comando desejado e chama a(s) funcao(oes) responsavel(is) por realiza-lo if(strcmp(input, "dump_schema") == 0) { dump_schema(schema); } else if(strcmp(input, "dump_data") == 0) { dump_data(schema); } else if(strcmp(input, "dump_index") == 0) { print_index(schema); } else if(strcmp(input, "update_index") == 0) { get_index(schema); sort_index(schema); } else if(strcmp(input, "insert") == 0) { insert_data(schema); } else if(strcmp(input, "select") == 0) { search_index_data(schema); } else if(strcmp(input, "exit") == 0) { // Caso seja digitado "exit", repeat recebe 0, saindo do loop repeat = 0; } // A cada repeticao input eh liberado caso tenha sido alocado adequadamente if(input != NULL) free(input); } while(repeat); // Libera a memoria alocada delete_schema(&schema); return 0; }
} END_TEST START_TEST (test_create_schema) { bool schema_created; int ret; const char *tmp_schema; char str[] = "invalid schema file"; char simple_schema[] = "CREATE TABLE IF NOT EXISTS `named_properties` (" "`id` int(10) unsigned NOT NULL AUTO_INCREMENT," "`type` TINYINT(1) NOT NULL," "`propType` INT(10) unsigned NOT NULL," "`oleguid` VARCHAR(255) NOT NULL," "`mappedId` INT(10) unsigned NOT NULL," "`propId` INT(10) unsigned DEFAULT NULL," "`propName` VARCHAR(255) DEFAULT NULL," "`oom` VARCHAR(255) DEFAULT NULL," "`canonical` VARCHAR(255) DEFAULT NULL," "PRIMARY KEY(`id`)" ") ENGINE=InnoDB CHARSET=utf8;"; FILE *fp; tmp_schema = MYSQL_TMP_SCHEMA; /* check sanity checks */ schema_created = create_schema(NULL, NULL); ck_assert(!schema_created); /* test non existent schema file */ unlink(tmp_schema); schema_created = create_schema(conn, tmp_schema); ck_assert(!schema_created); /* test empty schema file */ ret = unlink(tmp_schema); ck_assert_int_eq(ret, -1); fp = fopen(tmp_schema, "w+"); ck_assert(fp != NULL); fclose(fp); schema_created = create_schema(conn, tmp_schema); ck_assert(!schema_created); /* test invalid schema file */ fp = fopen(tmp_schema, "w+"); ck_assert(fp != NULL); fwrite(str, 1, sizeof(str), fp); fclose(fp); schema_created = create_schema(conn, tmp_schema); ck_assert(!schema_created); /* test simplified real schema file */ fp = fopen(tmp_schema, "w"); ck_assert(fp != NULL); fwrite(simple_schema, 1, sizeof(simple_schema), fp); fclose(fp); schema_created = create_schema(conn, tmp_schema); ck_assert(schema_created); ret = unlink(tmp_schema); ck_assert_int_eq(ret, 0); } END_TEST
/** \details Initialize the database and provision it \param conn pointer to the MySQL context \param schema_path pointer to the path holding schema files \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error */ static enum mapistore_error initialize_database(MYSQL *conn, const char *schema_path) { TALLOC_CTX *mem_ctx; enum mapistore_error retval = MAPISTORE_SUCCESS; struct ldb_context *ldb_ctx; struct ldb_ldif *ldif; struct ldb_message *msg; int ret; char *filename; FILE *f; bool inserted; bool schema_created; /* Sanity checks */ MAPISTORE_RETVAL_IF(!conn, MAPISTORE_ERR_DATABASE_INIT, NULL); mem_ctx = talloc_named(NULL, 0, "initialize_database"); MAPISTORE_RETVAL_IF(!mem_ctx, MAPISTORE_ERR_NO_MEMORY, NULL); filename = talloc_asprintf(mem_ctx, "%s/" NAMEDPROPS_MYSQL_SCHEMA, schema_path ? schema_path : mapistore_namedprops_get_ldif_path()); MAPISTORE_RETVAL_IF(!filename, MAPISTORE_ERR_NO_MEMORY, mem_ctx); schema_created = create_schema(conn, filename); if (!schema_created) { DEBUG(1, ("Failed named properties schema creation, " "last mysql error was: `%s`\n", mysql_error(conn))); MAPISTORE_RETVAL_ERR(MAPISTORE_ERR_DATABASE_INIT, mem_ctx); } ldb_ctx = ldb_init(mem_ctx, NULL); MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERR_BACKEND_INIT, mem_ctx); filename = talloc_asprintf(mem_ctx, "%s/mapistore_namedprops.ldif", schema_path ? schema_path : mapistore_namedprops_get_ldif_path()); MAPISTORE_RETVAL_IF(!filename, MAPISTORE_ERR_NO_MEMORY, mem_ctx); f = fopen(filename, "r"); talloc_free(filename); MAPISTORE_RETVAL_IF(!f, MAPISTORE_ERR_BACKEND_INIT, mem_ctx); while ((ldif = ldb_ldif_read_file(ldb_ctx, f))) { ret = ldb_msg_normalize(ldb_ctx, mem_ctx, ldif->msg, &msg); if (ret) { retval = MAPISTORE_ERR_DATABASE_INIT; mapistore_set_errno(MAPISTORE_ERR_DATABASE_INIT); goto end; } inserted = insert_ldif_msg(conn, msg); ldb_ldif_read_free(ldb_ctx, ldif); if (!inserted) { retval = MAPISTORE_ERR_DATABASE_OPS; mapistore_set_errno(MAPISTORE_ERR_DATABASE_OPS); goto end; } } end: talloc_free(mem_ctx); fclose(f); return retval; }