int TestPartOneKeyNoData(CuTest *ct) { DB_ENV *dbenv; DB *dbp; /* Allocate the memory from stack. */ DBT keys[4]; u_int32_t i; dbenv = NULL; dbp = NULL; nparts = 5; /* Do not assign any data to the first DBT. */ memset(&keys[0], 0, sizeof(DBT)); for (i = 1 ; i < (nparts - 1); i++) { memset(&keys[i], 0, sizeof(DBT)); keys[i].data = &content[(i + 1) * (strlen(content) / nparts)]; keys[i].size = sizeof(char); } /* Do not set any database flags. */ CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0); /* * Verify that before the database is opened, DB->set_partition can * be called multiple times regardless of its return code. */ keys[0].flags = DB_DBT_MALLOC; CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) != 0); keys[0].flags = 0; CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); fclose(errfp); errfp = NULL; /* Set DB_DUPSORT flags. */ setup_envdir(TEST_ENV, 1); errfp = fopen("TESTDIR/errfile", "w"); CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUPSORT) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); fclose(errfp); errfp = NULL; /* Set DB_DUP flags. */ setup_envdir(TEST_ENV, 1); CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUP) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0); CuAssertTrue(ct, put_data(dbp) == 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); return (0); }
int TestPartDuplicatedKey(CuTest *ct) { DB_ENV *dbenv; DB *dbp; DBT *keys; u_int32_t i; dbenv = NULL; dbp = NULL; keys = NULL; nparts = 5; CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL); memset(keys, 0, (nparts - 1) * sizeof(DBT)); /* Assign the same data to the first 2 DBTs. */ for (i = 0 ; i < (nparts - 1); i++) { if (i < 2) keys[i].data = &content[strlen(content) / nparts]; else keys[i].data = &content[(i + 1) * (strlen(content) / nparts)]; keys[i].size = sizeof(char); } /* Do not set any database flags. */ CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); fclose(errfp); errfp = NULL; /* Set DB_DUPSORT flags. */ setup_envdir(TEST_ENV, 1); errfp = fopen("TESTDIR/errfile", "w"); CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUPSORT) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); fclose(errfp); errfp = NULL; /* Set DB_DUP flags. */ setup_envdir(TEST_ENV, 1); CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUP) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0); CuAssertTrue(ct, put_data(dbp) == 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); free(keys); return (0); }
/* * Loads the Customer Table */ void LoadCust(void) { long w_id; long d_id; char *name = CUSTOMER_INDEX_NAME; char *sec_name = CUSTOMER_SECONDARY_NAME; char *hist_name = HISTORY_INDEX_NAME; DB *dbp_sec; int err; if(create_db(db_env, &dbp_customer,DB_COMMON_PAGE_SIZE, 0) || open_db(dbp_customer, name, 0)) return; if(create_db(db_env, &dbp_sec, DB_COMMON_PAGE_SIZE, DB_DUP | DB_DUPSORT)) return; if((err = dbp_sec->set_bt_compare(dbp_sec, customer_secondary_comparison_func))) { db_error("DB->set_bt_compare", err); goto done; } if(open_db(dbp_sec, sec_name, 0)) return; if ((err = dbp_customer->associate(dbp_customer, 0, dbp_sec, get_customer_sec_key, 0)) != 0) { db_error("DB->associate failed: %s\n", err); goto done; } if(create_db(db_env, &dbp_history, DB_COMMON_PAGE_SIZE, 0) || open_db(dbp_history, hist_name, 0)) return; for (w_id=1L; w_id<=count_ware; w_id++) for (d_id=1L; d_id<=DIST_PER_WARE; d_id++) { if(Customer(d_id,w_id)) goto done; } done: if(dbp_customer) dbp_customer->close(dbp_customer, 0); if(dbp_sec) dbp_sec->close(dbp_sec, 0); if(dbp_history) dbp_history->close(dbp_history, 0); }
void create_database(char *db_name) { if (true == create_db(db_name)) { printf("create database \"%s\" \n", db_name); } else { printf("failed to create database \"%s\" \n", db_name); } }
void test_db(int n) { char *dir = Option.dir; struct db db; u64 key; struct lump val; u64 i; int rc; if (1) set_cleanup(cleanup); create_db(dir); db = open_db(dir); for (i = 0; i < n; i++) { key = randkey(); if (0) val = randlump(); if (1) val = key2lump(key); //printf("%lld %*s %zd\n", key, val.size, (char *)val.d, strlen(val.d)); insert_db(&db, key, val); } if (1) for (i = 0; i < n + 1; i++) { rc = next_log(db.r); if (rc == DONE) break; //printf("%lld %s\n", key_log(db.r), (char *)val_log(db.r).d); } if (1) merge_db(&db); close_db(&db); destroy_db(dir); }
int insert(char *name, char *image) { create_db(); char *sql = NULL; sqlite3 *db = NULL; int rc; char *zErrMsg = 0; /* Open database*/ rc = sqlite3_open("DataBase_facificator.db", &db); if ( rc ) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); } else { fprintf(stderr, "Opened database successfully\n"); } /* Create SQL statement */ sql = "INSERT INTO FACIFICATOR (NAME, PICTURE) VALUES ('"; char *result = malloc(strlen(sql) + strlen(name) + 1); strcpy(result, sql); strcat(result, name); sql = result; char *chaine_tmp = "', '"; result = malloc(strlen(sql) + strlen(chaine_tmp) + 1); strcpy(result, sql); strcat(result, chaine_tmp); sql = result; result = malloc(strlen(sql) + strlen(image) + 1); strcpy(result, sql); strcat(result, image); sql = result; chaine_tmp = "');"; result = malloc(strlen(sql) + strlen(chaine_tmp) + 1); strcpy(result, sql); strcat(result, chaine_tmp); sql = result; printf("%s\n", sql); /* Execute SQL statement */ rc = sqlite3_exec(db, sql, NULL, 0, &zErrMsg); if ( rc != SQLITE_OK ) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "Successfull insert\n\n"); } sqlite3_close(db); return 0; }
/* Sends syscheck db to the server. */ void send_sk_db() { /* Sending scan start message */ if(syscheck.dir[0]) { merror("%s: INFO: Starting syscheck scan (forwarding database).", ARGV0); send_rootcheck_msg("Starting syscheck scan."); } else { sleep(syscheck.tsleep +10); return; } create_db(1); /* Sending scan ending message */ sleep(syscheck.tsleep +10); if(syscheck.dir[0]) { merror("%s: INFO: Ending syscheck scan (forwarding database).", ARGV0); send_rootcheck_msg("Ending syscheck scan."); } }
int TestPartNumber(CuTest *ct) { DB_ENV *dbenv; DB *dbp; DBT *keys; u_int32_t i; dbenv = NULL; dbp = NULL; keys = NULL; nparts = 5; CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL); memset(keys, 0, (nparts - 1) * sizeof(DBT)); /* Assign data to the keys. */ for (i = 0 ; i < (nparts - 1); i++) { keys[i].data = &content[(i + 1) * (strlen(content) / nparts)]; keys[i].size = sizeof(char); } /* Partition number is less than 2. */ CuAssertTrue(ct, create_db(&dbenv, &dbp, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, 1, keys, NULL) != 0); /* Partition number is less than key array length plus 1. */ CuAssertTrue(ct, dbp->set_partition(dbp, nparts - 1, keys, NULL) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0); CuAssertTrue(ct, put_data(dbp) == 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); free(keys); return (0); }
int main(int argc, char* argv[]) { clock_t tstart, tend; sqlite3* phandle = NULL; create_db(&phandle); tstart = clock(); start_transation(phandle); insert_data(phandle); end_transation(phandle); tend = clock(); printf("transaction %lus\n", (tend-tstart)/CLOCKS_PER_SEC); tstart = clock(); // insert_data(phandle); tend = clock(); printf("insert %lus\n", (tend-tstart)/CLOCKS_PER_SEC); return EXIT_SUCCESS; }
int db_options(int& argc, char** argv) { if(argc == 1) { HELP: QString a0 = argv[0]; qDebug() << "número de argumentos invalido, use:\n\t" + a0 + " crear\n\t" + a0 + " mostrar"; } else { QString a1 = argv[1]; if(a1 == "crear") { create_db(); return 0; } else if(a1 == "mostrar") { mostar_usuarios(); return 0; } else goto HELP; } return 0; }
krb5_error_code krb5_db2_create(krb5_context context, char *conf_section, char **db_args) { krb5_error_code status = 0; krb5_db2_context *db_ctx; krb5_clear_error_message(context); if (k5db2_inited(context)) return 0; status = configure_context(context, conf_section, db_args); if (status != 0) return status; status = check_openable(context); if (status == 0) return EEXIST; db_ctx = context->dal_handle->db_context; status = create_db(context, db_ctx->db_name); if (status != 0) return status; return krb5_db2_init(context); }
int main(int argc, char** argv) { create_db(); create_table_pea_user(); create_table_pea_item(); create_table_pea_mail(); return 0; }
int TestPartUnsortedKey(CuTest *ct) { DB_ENV *dbenv; DB *dbp; DBT *keys; u_int32_t i, indx; dbenv = NULL; dbp = NULL; keys = NULL; nparts = 6; CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL); memset(keys, 0, (nparts - 1) * sizeof(DBT)); /* Assign unsorted keys to the array. */ for (i = 0, indx = 0; i < (nparts - 1); i++) { if (i == (nparts - 2) && i % 2 == 0) indx = i; else if (i % 2 != 0) indx = i - 1; else indx = i + 1; keys[i].data = &content[(indx + 1) * (strlen(content) / nparts)]; keys[i].size = sizeof(char); } CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts - 1, &keys[1], NULL) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0); CuAssertTrue(ct, put_data(dbp) == 0); CuAssertTrue(ct, dbp->close(dbp, 0) == 0); /* * Reconfig with a different partition number and * re-open the database. */ CuAssertTrue(ct, db_create(&dbp, dbenv, 0) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, 0, 0644) != 0); CuAssertTrue(ct, dbp->close(dbp, 0) == 0); /* * Reconfig with a different set of partition keys and * re-open the database. */ CuAssertTrue(ct, db_create(&dbp, dbenv, 0) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts - 1, keys, NULL) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, 0, 0644) != 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); free(keys); return (0); }
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; }
void db_put_dn(char *data_dn) { int ret; DBT key = {0}; DBT data = {0}; if(db_lock == NULL){ db_lock = PR_NewLock(); } PR_Lock(db_lock); create_db(); if ((ret = DB_OPEN(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { dbp->err(dbp, ret, "%s", DATABASE); } memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); key.data = (char *)malloc(key_buffer_size); /* make_key will set up the key and the data */ make_key(&key); data.data = slapi_ch_strdup(data_dn); data.size = strlen(data_dn); switch (ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) { case 0: slapi_log_error(SLAPI_LOG_PLUGIN, DB_PLUGIN_NAME, "db: %s: key stored.\n", (char *)key.data); break; case DB_KEYEXIST: slapi_log_error(SLAPI_LOG_PLUGIN, DB_PLUGIN_NAME, "db: %s: key previously stored.\n", (char *)key.data); break; default: dbp->err(dbp, ret, "DB->put"); goto err; } err: if(ret){ slapi_log_error(SLAPI_LOG_PLUGIN, DB_PLUGIN_NAME, "db: Error detected in db_put \n"); } free(key.data); if (dbp){ dbp->close(dbp,0); dbp=NULL; } PR_Unlock(db_lock); }
//Refer to apps_sfdl_gen/cql_rw_si_cons.h for constants to use when generating input. // this name stands for cql read/write select insert. void cql_rw_siVerifierInpGenHw::create_input(mpq_t* input_q, int num_inputs) { srand(time(NULL)); mpq_t* full_db_handle; int num_ints = sizeof(Student_handle_t) / sizeof(uint64_t); alloc_init_vec(&full_db_handle, num_ints); Student_handle_t handle; if (generate_states) { // SIZE should be a power of 2. int number_of_rows = SIZE - 1; // get the full handle of a DB. handle = create_db(number_of_rows, ("prover_1_" + shared_bstore_file_name).c_str()); uint64_t* input_ptr = (uint64_t*)&handle; for(int i = 0; i < num_ints; i++) { mpq_set_ui(full_db_handle[i], input_ptr[i], 1); } dump_vector(num_ints, full_db_handle, "db_handle", FOLDER_PERSIST_STATE); } else { // import the root hash from a place. load_vector(num_ints, full_db_handle, "db_handle", FOLDER_PERSIST_STATE); uint64_t* input_ptr = (uint64_t*)&handle; for(int i = 0; i < num_ints; i++) { input_ptr[i] = mpz_get_ui(mpq_numref(full_db_handle[i])); } } struct In input; Student_handle_t empty_handle; memset(&input, 0, sizeof(input)); // get a succinct handle of a DB using hashput. char db_file_path[BUFLEN]; snprintf(db_file_path, BUFLEN - 1, "%s/block_stores/prover_1_%s", FOLDER_STATE, shared_bstore_file_name.c_str()); HashBlockStore* bs = new ConfigurableBlockStore(db_file_path); hashput2(bs, &(input.db_handle), &handle); delete bs; // assign it to input_q uint64_t* input_ptr = (uint64_t*)&input.db_handle; int number_of_hash_elements = sizeof(hash_t) / sizeof(uint64_t); for(int i = 0; i < number_of_hash_elements; i++) { mpq_set_ui(input_q[i], input_ptr[i], 1); } for (int i = number_of_hash_elements; i < num_inputs; i++) { mpq_set_ui(input_q[i], rand(), 1); } clear_del_vec(full_db_handle, num_ints); }
int TestPartKeyCallNeitherSet(CuTest *ct) { DB_ENV *dbenv; DB *dbp; dbenv = NULL; dbp = NULL; CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, nparts, NULL, NULL) != 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); return (0); }
int main(int argc,char **argv) { anthy_context_t ac; FILE *fp; struct input cur_input; struct res_db *db; struct condition cond; cur_input.serial = 0; cur_input.str = 0; init_condition(&cond); parse_args(&cond, argc, argv); db = create_db(); read_db(db, expdata); printf("./test_anthy --help to print usage.\n"); print_run_env(); fp = fopen(testdata, "r"); if (!fp) { printf("failed to open %s.\n", testdata); return 0; } ac = init_lib(cond.use_utf8); /* ファイルを読んでいくループ */ while (!read_file(fp, &cur_input)) { if (check_cond(&cond, &cur_input)) { set_string(&cond, db, &cur_input, ac); } } anthy_release_context(ac); anthy_quit(); if (cond.ask) { /* ユーザに聞く */ ask_results(db); } show_stat(db); save_db(expdata, db); return 0; }
int main(int argc, char *argv[]) { do{ system("cls"); menu(); printf("Podaj numer opcji ");scanf("%i",&nr); switch (nr) { case 1:create_file();break; case 2:create_db();break; default:printf("NIE MA TAKIEJ OPCJI!!!\n");break; } printf("Zakonczyc program?(T/N) ");scanf("%s",&decyzja); }while(decyzja!='t'); return 0; }
/*main program*/ int main(int argc, char *argv[]) { if(argc > 1){ if(strcmp(argv[1],"-a")==0 || strcmp(argv[1],"add")==0) add_note(); else if(strcmp(argv[1],"-c")==0 || strcmp(argv[1],"configure")==0) create_db(); else if(strcmp(argv[1],"-h")==0 || strcmp(argv[1],"help")==0) u_help(); else if(strcmp(argv[1],"-t")==0 || strcmp(argv[1],"truncate")==0) truncate(); else show_queried_note(argv[1]); } else show_notes(); return 0; }
int TestPartNumber(CuTest *ct) { DB_ENV *dbenv; DB *dbp; DBT *keys; u_int32_t i; dbenv = NULL; dbp = NULL; keys = NULL; nparts = 1000000; CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL); memset(keys, 0, (nparts - 1) * sizeof(DBT)); /* Assign data to the keys. */ for (i = 0 ; i < (nparts - 1); i++) { CuAssertTrue(ct, (keys[i].data = malloc(sizeof(u_int32_t))) != NULL); memcpy(keys[i].data, &i, sizeof(u_int32_t)); keys[i].size = sizeof(u_int32_t); } /* Partition number is less than 2. */ CuAssertTrue(ct, create_db(&dbenv, &dbp, 1, ct) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, 1, keys, NULL) != 0); /* Partition number is bigger than the limit 1000000. */ CuAssertTrue(ct, dbp->set_partition(dbp, nparts + 1, keys, NULL) == EINVAL); /* Partition number is equal to the limit 1000000. */ CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0); /* Partition keys do not fix into a single database page. */ CuAssertTrue(ct, dbp->set_pagesize(dbp, 512) == 0); CuAssertTrue(ct, dbp->set_partition(dbp, 800, keys, NULL) == 0); CuAssertTrue(ct, dbp->open(dbp, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0); CuAssertTrue(ct, put_data(dbp) == 0); CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0); for (i = 0 ; i < (nparts - 1); i++) free(keys[i].data); free(keys); return (0); }
dbfile_t *open_dbfile(char *dbname){ FILE *fp; dbfile_t *tree; struct stat dbstat; strcat(dbname, "_.vdb"); if(stat(dbname, &dbstat) < 0){ fp = fopen(dbname, "w+b"); tree = create_db(fp); printf("Create database\n"); }else if(dbstat.st_size){ fp = fopen(dbname, "r+b"); tree = open_db(fp); printf("Open database\n"); }else{ printf("Cannot create or open database\n"); } return tree; }
/* Загрузить БД из файла */ BookDB* load_db(char* filename) { FILE* fin = fopen(filename, "r"); if (!fin) return 0; int n = 0; fscanf(fin, "%d", &n); getc(fin); BookDB* db = create_db(n); db->count = n; if (n > 0) { Book *book, *end; for (book = db->books, end = db->books + n; book < end; book++) { fscanf(fin, "%d", &(book->id)); getc(fin); fgets(book->title, MAX_STRING_LENGTH, fin); delns(book->title); fgets(book->author, MAX_STRING_LENGTH, fin); delns(book->author); fgets(book->ganre, MAX_STRING_LENGTH, fin); delns(book->ganre); } } db->saved = 1; fclose(fin); return db; }
int main(int argc, char**argv) { if (argc < 2) panic("Too few arguments!\n"); if (!strcmp(argv[1], "create") && argc == 3) create_db(argv[2]); if (!strcmp(argv[1], "get") && argc == 4) get_key_val(argv[2], argv[3]); if (!strcmp(argv[1], "add") && argc == 5) add_key_val(argv[2], argv[3], argv[4], strlen(argv[4])); printf("Usage i.e.:\n"); printf(" tikdb create todo.db\n"); printf(" Add key-value:\n"); printf(" tikdb add todo.db \"25.02.2012\" \"Watch new Dr Who episodes\"\n"); printf(" Update key-value:\n"); printf(" tikdb add todo.db \"25.02.2012\" \"Watch old Dr Who episodes\"\n"); printf(" Retrival:\n"); printf(" tikdb get todo.db \"25.02.2012\"\n"); printf("Note: max key size is 31 ASCII chars\n"); return 0; }
int see_all_db() { create_db(); char *zErrMsg = 0; char *sql; sqlite3 *db = NULL; int rc; /* Open database*/ rc = sqlite3_open("DataBase_facificator.db", &db); if ( rc ) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); } else { fprintf(stderr, "Opened database successfully\n"); } /* Create SQL statement */ sql = "SELECT * from FACIFICATOR"; /* Execute SQL statement */ rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); if ( rc != SQLITE_OK ) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "Operation done successfully\n"); } sqlite3_close(db); return 0; }
int main(int argc, char** argv) { const char *root = getenv("TDB_TMP_DIR"); tdb *dbs[] = {create_db(root, 1, 10000), create_db(root, 2, 1000), create_db(root, 3, 100), create_db(root, 4, 0), create_db(root, 5, 50), create_db(root, 6, 500)}; const uint32_t NUM_DBS = sizeof(dbs) / sizeof(dbs[0]); const uint64_t BUFFER_SIZES[] = {1, 10, 1001, 1000000}; uint64_t i, j; for (i = 0; i < sizeof(BUFFER_SIZES) / sizeof(BUFFER_SIZES[0]); i++){ tdb_opt_value val = {.value = BUFFER_SIZES[i]}; for (j = 0; j < NUM_DBS; j++) assert(tdb_set_opt(dbs[j], TDB_OPT_CURSOR_EVENT_BUFFER_SIZE, val) == 0); test_multicursor(dbs, NUM_DBS); } return 0; }
/* init_db -- * Prepare the database. Register the compress/uncompress functions and the * stopword tokenizer. * db_flag specifies the mode in which to open the database. 3 options are * available: * 1. DB_READONLY: Open in READONLY mode. An error if db does not exist. * 2. DB_READWRITE: Open in read-write mode. An error if db does not exist. * 3. DB_CREATE: Open in read-write mode. It will try to create the db if * it does not exist already. * RETURN VALUES: * The function will return NULL in case the db does not exist and DB_CREATE * was not specified. And in case DB_CREATE was specified and yet NULL is * returned, then there was some other error. * In normal cases the function should return a handle to the db. */ sqlite3 * init_db(int db_flag, const char *manconf) { sqlite3 *db = NULL; sqlite3_stmt *stmt; struct stat sb; int rc; int create_db_flag = 0; char *dbpath = get_dbpath(manconf); if (dbpath == NULL) errx(EXIT_FAILURE, "_mandb entry not found in man.conf"); /* Check if the database exists or not */ if (!(stat(dbpath, &sb) == 0 && S_ISREG(sb.st_mode))) { /* Database does not exist, check if DB_CREATE was specified, and set * flag to create the database schema */ if (db_flag != (MANDB_CREATE)) { warnx("Missing apropos database. " "Please run makemandb to create it."); return NULL; } create_db_flag = 1; } /* Now initialize the database connection */ sqlite3_initialize(); rc = sqlite3_open_v2(dbpath, &db, db_flag, NULL); if (rc != SQLITE_OK) { warnx("%s", sqlite3_errmsg(db)); sqlite3_shutdown(); return NULL; } if (create_db_flag && create_db(db) < 0) { warnx("%s", "Unable to create database schema"); goto error; } rc = sqlite3_prepare_v2(db, "PRAGMA user_version", -1, &stmt, NULL); if (rc != SQLITE_OK) { warnx("Unable to query schema version: %s", sqlite3_errmsg(db)); goto error; } if (sqlite3_step(stmt) != SQLITE_ROW) { sqlite3_finalize(stmt); warnx("Unable to query schema version: %s", sqlite3_errmsg(db)); goto error; } if (sqlite3_column_int(stmt, 0) != APROPOS_SCHEMA_VERSION) { sqlite3_finalize(stmt); warnx("Incorrect schema version found. " "Please run makemandb -f."); goto error; } sqlite3_finalize(stmt); sqlite3_extended_result_codes(db, 1); /* Register the zip and unzip functions for FTS compression */ rc = sqlite3_create_function(db, "zip", 1, SQLITE_ANY, NULL, zip, NULL, NULL); if (rc != SQLITE_OK) { warnx("Unable to register function: compress: %s", sqlite3_errmsg(db)); goto error; } rc = sqlite3_create_function(db, "unzip", 1, SQLITE_ANY, NULL, unzip, NULL, NULL); if (rc != SQLITE_OK) { warnx("Unable to register function: uncompress: %s", sqlite3_errmsg(db)); goto error; } return db; error: sqlite3_close(db); sqlite3_shutdown(); return NULL; }
/* * "Atomically" rename the database in a way that locks out read * access in the middle of the rename. * * Not perfect; if we crash in the middle of an update, we don't * necessarily know to complete the transaction the rename, but... * * Since the rename operation happens outside the init/fini bracket, we * have to go through the same stuff that we went through up in db_destroy. */ krb5_error_code krb5_db2_rename(krb5_context context, char *from, char *to, int merge_nra) { char *fromok; krb5_error_code retval; krb5_db2_context *s_context, *db_ctx; kdb5_dal_handle *dal_handle = context->dal_handle; s_context = dal_handle->db_context; dal_handle->db_context = NULL; if ((retval = k5db2_init_context(context))) return retval; db_ctx = (krb5_db2_context *) dal_handle->db_context; /* * Create the database if it does not already exist; the * files must exist because krb5_db2_lock, called below, * will fail otherwise. */ retval = create_db(context, to); if (retval != 0 && retval != EEXIST) goto errout; /* * Set the database to the target, so that other processes sharing * the target will stop their activity, and notice the new database. */ db_ctx->db_name = strdup(to); if (db_ctx->db_name == NULL) { retval = ENOMEM; goto errout; } retval = check_openable(context); if (retval) goto errout; retval = krb5_db2_init(context); if (retval) goto errout; db_ctx->db_lf_name = gen_dbsuffix(db_ctx->db_name, KDB2_LOCK_EXT); if (db_ctx->db_lf_name == NULL) { retval = ENOMEM; goto errout; } db_ctx->db_lf_file = open(db_ctx->db_lf_name, O_RDWR|O_CREAT, 0600); if (db_ctx->db_lf_file < 0) { retval = errno; goto errout; } set_cloexec_fd(db_ctx->db_lf_file); db_ctx->db_inited = 1; retval = krb5_db2_get_age(context, NULL, &db_ctx->db_lf_time); if (retval) goto errout; fromok = gen_dbsuffix(from, KDB2_LOCK_EXT); if (fromok == NULL) { retval = ENOMEM; goto errout; } if ((retval = krb5_db2_lock(context, KRB5_LOCKMODE_EXCLUSIVE))) goto errfromok; if ((retval = krb5_db2_start_update(context))) goto errfromok; if (merge_nra) { if ((retval = krb5_db2_begin_nra_merge(context, s_context, db_ctx))) goto errfromok; } if (rename(from, to)) { retval = errno; goto errfromok; } if (unlink(fromok)) { retval = errno; goto errfromok; } if (merge_nra) { krb5_db2_end_nra_merge(context, s_context, db_ctx); } retval = krb5_db2_end_update(context); if (retval) goto errfromok; { /* XXX moved so that NRA merge works */ /* Ugly brute force hack. Should be going through nice friendly helper routines for this, but it's a mess of jumbled so-called interfaces right now. */ char policy[2048], new_policy[2048]; assert (strlen(db_ctx->db_name) < 2000); snprintf(policy, sizeof(policy), "%s.kadm5", db_ctx->db_name); snprintf(new_policy, sizeof(new_policy), "%s~.kadm5", db_ctx->db_name); if (0 != rename(new_policy, policy)) { retval = errno; goto errfromok; } strlcat(new_policy, ".lock",sizeof(new_policy)); (void) unlink(new_policy); } errfromok: free_dbsuffix(fromok); errout: if (dal_handle->db_context) { if (db_ctx->db_lf_file >= 0) { krb5_db2_unlock(context); close(db_ctx->db_lf_file); } k5db2_clear_context((krb5_db2_context *) dal_handle->db_context); free(dal_handle->db_context); } dal_handle->db_context = s_context; (void) krb5_db2_unlock(context); /* unlock saved context db */ return retval; }
int mdhim_mysql_open(void **dbh, void **dbs, char *path, int flags,int key_type, struct mdhim_options_t *db_opts) { struct MDI *Input_DB; struct MDI *Stats_DB; Input_DB = malloc(sizeof(struct MDI)); Stats_DB = malloc(sizeof(struct MDI)); MYSQL *db = mysql_init(NULL); MYSQL *sdb = mysql_init(NULL); if (db == NULL){ fprintf(stderr, "%s\n", mysql_error(db)); return MDHIM_DB_ERROR; } if (sdb == NULL){ fprintf(stderr, "%s\n", mysql_error(db)); return MDHIM_DB_ERROR; } /*char *path_s = malloc(sizeof(path)); path_s = strcpy(path_s, path); */ Input_DB ->host = db_opts->db_host; Stats_DB->host = db_opts->dbs_host; Input_DB ->user = db_opts->db_user; Stats_DB ->user = db_opts->dbs_user; Input_DB ->pswd = db_opts->db_upswd; Stats_DB->pswd = db_opts->dbs_upswd; //Abstracting the host, usernames, and password Input_DB->database= "maindb"; //mstore_opts -> db_ptr4; //Abstracting Database Input_DB->table = "mdhim"; Stats_DB ->database = "statsdb";//mstore_opts -> db_ptr4; //Abstracting Statsics Database Stats_DB->table = "mdhim"; Input_DB->msqkt = db_opts->db_key_type; Stats_DB->msqkt = db_opts->db_key_type; //connect to the Database if (mysql_real_connect(db, Input_DB->host, Input_DB->user, Input_DB->pswd, NULL, 0, NULL, 0) == NULL){ fprintf(stderr, "%s\n", mysql_error(db)); mysql_close(db); return MDHIM_DB_ERROR; } if (mysql_real_connect(sdb, Stats_DB->host, Stats_DB->user, Stats_DB->pswd, NULL, 0, NULL, 0) == NULL){ fprintf(stderr, "%s\n", mysql_error(db)); mysql_close(sdb); return MDHIM_DB_ERROR; } if (mysql_library_init(0, NULL, NULL)) { fprintf(stderr, "could not initialize MySQL library\n"); return MDHIM_DB_ERROR; } create_db(Input_DB->database, db); create_table(Input_DB->database, db,Input_DB->table, Input_DB->msqkt); create_db(Stats_DB->database, sdb); create_table(Stats_DB->database, sdb,Stats_DB->table, Stats_DB->msqkt); //Abstracting the host, usernames, and password Input_DB->msqdb = db; Input_DB->msqht = MYSQLDB_HANDLE; Stats_DB->msqdb = sdb; Stats_DB->msqht = MYSQLDB_STAT_HANDLE; *dbh = Input_DB; *dbs = Stats_DB; return MDHIM_SUCCESS; }
void *memory_set(void *info) { DEBUG_PRINT("notice: memory_set\n"); int val_len = 0; int val_differs = 1; // if the values are the same store_entry *entry = NULL; store_db *db = NULL; // extract information from our info variable char *key = ((struct entry_inf *) info)->key; char *value = ((struct entry_inf *) info)->value; char *db_name = ((struct entry_inf *) info)->db_name; store_db **dbs = ((struct entry_inf *) info)->dbs; int *error = &(((struct entry_inf *) info)->error); // we shouldn't write while reading/writing write_lock(); // locate our db and find our entry db = locate_db(db_name, *dbs); // create our db if it doesn't exist if(db == NULL) { DEBUG_PRINT("notice: db \"%s\" %p not found, creating...\n", db_name, dbs); db = create_db(db_name, dbs); } // error allocating data while creating the db? if(db == NULL) { perror("create_db"); *error = ERR_ALLOC; } // are we unsetting an entry? else if(value[0] == '\0') { // we don't wan't to give any error if entry was not found delete_entry(key, db); } else { entry = locate_entry(key, db); // no value size limit? if(MAX_VAL_SIZE <= 0) { val_len = strlen(value) + 1; } // if we have set a value limit, apply it else { val_len = (size_t) min((int) strlen(value) + 1, MAX_VAL_SIZE); } // did we find an entry? if(entry != NULL) { if(0 == strncmp(value, entry->val, val_len)) { DEBUG_PRINT("notice: %s: values are equal\n", db_name); val_differs = 0; } else { // if the value has changed, free the previous one free(entry->val); entry->val = NULL; } } // if we didn't, create a new one else { entry = create_entry(key, db); if(entry == NULL) { print_perror("create_entry"); *error = ERR_ALLOC; } } if(*error == ERR_NONE) { // reserve space for our value as long as it was not the same if(val_differs) { entry->val = (char *) malloc(val_len * sizeof(char)); if(entry->val == NULL) { print_perror("malloc"); *error = ERR_ALLOC; } else { strncpy(entry->val, value, val_len); entry->val[val_len - 1] = '\0'; } } DEBUG_PRINT("notice: \"%s\": setting \"%s\"=\"%s\" (\"%d\"B) DONE\n", db_name, entry->key, entry->val, val_len); // save the entry to our info variable (as output) ((struct entry_inf *) info)->entry = entry; #ifdef __DEBUG__ print_store_tree(dbs); #endif } } // done! write_unlock(); DEBUG_PRINT("notice: memory_set returning thread error %d\n", *error); pthread_exit(NULL); }