int32_t storage_update(struct storage_manager * storage, struct low_data_struct * new_data, struct low_data_struct** old_data, uint32_t docid, MEM_POOL* mem_pool) { //如果更新的数据的长度为0,并且此列支持空值,则将标记位置为0 if(new_data->len == 0 ) { //获取老的数据 *old_data = storage_query(storage,docid,mem_pool); bitmark_clear(storage->null_bitmark,docid); return 0; } //先获取老的数据 *old_data = storage_query(storage,docid,mem_pool); //覆盖老的数据 memcpy(storage->mem_mmaped+docid*storage->value_size, new_data->data, storage->value_size); //如果支持空值,则需要把标记位设置为1 bitmark_set(storage->null_bitmark,docid); return 0; }
void config_set(storage_handle_t *handle, const char *name, const char *value) { char *sql = sqlite3_mprintf("INSERT OR REPLACE INTO config VALUES (%Q, %Q)", name, value); storage_stmt_t *stmt = storage_query(handle, sql); storage_step(stmt, NULL); storage_finalize(stmt); sqlite3_free(sql); }
bool nscapi::core_wrapper::storage_query(const std::string request, std::string &response) const { char *buffer = NULL; unsigned int buffer_size = 0; bool retC = NSCAPI::api_ok(storage_query(request.c_str(), static_cast<unsigned int>(request.size()), &buffer, &buffer_size)); if (buffer_size > 0 && buffer != NULL) { response = std::string(buffer, buffer_size); } DestroyBuffer(&buffer); return retC; }
END_TEST START_TEST (test_query_withmultiplespace) //Added 16 { // Query with spaced predicates. Expect no matches. int foundkeys = storage_query(INTTABLE, "col > 10", test_keys, MAX_RECORDS_PER_TABLE, test_conn); fail_unless(foundkeys == 0, "Query didn't find the correct number of keys."); // Make sure next key is not set to anything. fail_unless(strcmp(test_keys[0], "") == 0, "No extra keys should be modified.\n"); }
END_TEST START_TEST (test_query_int1) { // Do a query. Expect one match. int foundkeys = storage_query(INTTABLE, "col<-1", test_keys, MAX_RECORDS_PER_TABLE, test_conn); fail_unless(foundkeys == 1, "Query didn't find the correct number of keys."); // Check the matching keys. fail_unless( ( strcmp(test_keys[0], KEY1) == 0 ), "The returned keys don't match the query.\n"); // Make sure next key is not set to anything. fail_unless(strcmp(test_keys[1], "") == 0, "No extra keys should be modified.\n"); }
END_TEST /* * One server instance tests: * query from simple tables. */ START_TEST (test_query_bigint) // Added 13 { // Query a really large integer. Expect no matches. int foundkeys = storage_query(INTTABLE, "col>1000", test_keys, MAX_RECORDS_PER_TABLE, test_conn); fail_unless(foundkeys == 0, "Query didn't find the correct number of keys."); // Make sure next key is not set to anything. fail_unless(strcmp(test_keys[0], "") == 0, "No extra keys should be modified.\n"); }
hash_t *config_get_feeds(storage_handle_t *handle) { storage_stmt_t *stmt = storage_query(handle, "SELECT * FROM feeds"); hash_t *row, *ret = hash_init(); while (storage_step(stmt, &row) == SQLITE_ROW) { feed_t *feed = feed_create(hash_get(row, "url")); feed->f_name = hash_get_string(row, "name"); feed->f_description = hash_get_string(row, "description"); feed->f_last_update = hash_get_int(row, "updated"); hash_set(ret, xstrdup(hash_get(row, "name")), feed, TRUE); hash_free(row, FALSE, FALSE); } storage_finalize(stmt); return ret; }
hash_t *config_get_all(storage_handle_t *handle) { char *sql = "SELECT * FROM config"; hash_t *row, *ret = hash_init(); storage_stmt_t *stmt = storage_query(handle, sql); while (storage_step(stmt, &row) == SQLITE_ROW) { hash_set( ret, hash_get(row, "name"), hash_get(row, "value"), TRUE ); hash_free(row, FALSE, FALSE); } storage_finalize(stmt); return ret; }
char *config_get(storage_handle_t *handle, const char *name) { char *ret, *sql = sqlite3_mprintf("SELECT * FROM config WHERE name = %Q", name); storage_stmt_t *stmt = storage_query(handle, sql); hash_t *row = NULL; storage_step(stmt, &row); storage_finalize(stmt); sqlite3_free(sql); ret = row ? hash_get(row, "value") : xstrdup(""); if (!ret) ret = xstrdup("<brak>"); if (row) { free(hash_get(row, "name")); hash_free(row, FALSE, FALSE); } return ret; }
/** * @brief Attempts to set a value given table and key names and the new value in the server. * * Reports the error number set by the storage server. * Does not exit if an error occurs. * @return Returns 0 on success, -1 otherwise. */ int client_query() { char table[MAX_TABLE_LEN] = {0}, predicates[MAX_PREDICATE_LEN] = {0}; int max_keys = 0; bool read_success = false; while(read_success == false) { printf("Please enter the table name: "); char *l = fgets(input_buffer, sizeof input_buffer, stdin); if(l != input_buffer || (sscanf(input_buffer, "%[a-zA-Z0-9] %s", table, trash) != 1)) printf("Please enter a valid table name (only alphanumeric characters).\n"); else read_success = true; } printf("Please enter the predicates: "); fgets(input_buffer, sizeof input_buffer, stdin); sscanf(input_buffer, "%[^\n]", predicates); read_success = false; while(read_success == false) { printf("Please enter the maximum number of keys: "); char *l = fgets(input_buffer, sizeof input_buffer, stdin); if(l != input_buffer || (sscanf(input_buffer, "%d%s", &max_keys, trash) != 1) || max_keys < 0) printf("Please enter a valid max keys (positive integer).\n"); else read_success = true; } char* keys[max_keys]; int i; for (i = 0; i < max_keys; i++) { keys[i] = (char*)malloc(MAX_KEY_LEN); strcpy(keys[i], ""); } max_keys = -1; int num_matched_keys = storage_query(table, predicates, keys, max_keys, conn); if(num_matched_keys == -1) printf("storage_query failed. Error code: %d.\n", errno); else { printf("storage_query: %d key(s) matched with '%s' in table '%s'\n", num_matched_keys, predicates, table); if(num_matched_keys > 0) { printf("Matched Keys: '%s'", keys[0]); for(i = 1; i < num_matched_keys && i < max_keys; i++) printf(", '%s'", keys[i]); printf("\n"); } } for(i = 0; i < max_keys; i++) { free(keys[i]); keys[i] = NULL; } return (num_matched_keys != -1) ? 0 : -1; }
int main(){ void *conn = storage_connect(HOSTNAME , PORT); if(conn == NULL) { printf("Error starting the server, please make sure the socket is not being used.\n"); printf("Notice that the parameters inside the *.conf file should be as follows:\n"); printf("Hostname: %s\n",HOSTNAME); printf("Port: %d\n",PORT); return -1; } int status = storage_auth(ADMIN , DEC_PASSWORD , conn); if (status != 0) { printf("Error authenticating the server, make sure that the parameters inside the *.conf file are as follows:\n"); printf("Username: %s\n",ADMIN); printf("Decrypted Password: %s\n",DEC_PASSWORD); return -1; } char *keys[100]; struct storage_record record; //test case 1 for set strcpy(record.value,VALUE1); status = storage_set(TABLE1, KEY1, &record, conn); if (status != 0) { printf("The test failed to add a key."); return -1; } else { strcpy(record.value,VALUE2); status = storage_set(TABLE2, KEY2, &record, conn); if (status != 0) { printf("The test failed to add a key."); return -1; } } printf("\n\nTest 1: "); ///Test 1 status = storage_query(TABLE1 , "invalid query" , keys , 1 , conn ); if(status != -1) { printf("test failed, query should return an error."); } else { if (errno != 1){ printf("test failed, errno not being set correctly."); } else printf("test is ok."); } printf("\n\nTest 2: "); ///Test 2 status = storage_query(INV_TABLE , "col1 = string" , keys , 1 , conn ); if(status != -1) { printf("test failed, query should return an error."); } else { if (errno != 1){ printf("test failed, errno not being set correctly."); } else printf("test is ok."); } printf("\n\nTest 3: "); ///Test 3 status = storage_query(TABLE1 , "col1 > string" , keys , 1 , conn ); if(status != -1) { printf("test failed, query should return an error."); } else { if (errno != 1){ printf("test failed, errno not being set correctly."); } else printf("test is ok."); } printf("\n\nTest 4: "); ///Test 4 status = storage_query(TABLE2 , "col2 = string" , keys , 1 , conn ); if(status != -1) { printf("test failed, query should return an error."); } else { if (errno != 1){ printf("test failed, errno not being set correctly."); } else printf("test is ok."); } printf("\n\nTest 5: "); ///Test 5 status = storage_query(TABLE2 , "col2 > 0" , keys , 1 , conn ); if(status == -1) { printf("test failed, query failed."); } else { if (status != 1){ printf("test failed, number of keys returned not correct."); } else { if(strcmp(keys[1],"") == 0) { if(strcmp(keys[0],KEY2) == 0) printf("test is ok."); else printf("test failed, key retrieved isn't as expected."); } else printf("test failed, keys returned are different than expected."); } } printf("\n\n"); return 0; }
int main(int argc, char *argv[]) { // initial variables used throughout all of the commands int i; int SERVERPORT; char SERVERHOST[MAX_HOST_LEN], SERVERUSERNAME[MAX_USERNAME_LEN], SERVERPASSWORD[MAX_ENC_PASSWORD_LEN], SERVERPORTstr[MAX_PORT_LEN]; char TABLE[MAX_TABLE_LEN], KEY[MAX_KEY_LEN]; char **key = malloc(sizeof(char**)*800); char PREDICATES_1[500]; char PREDICATES[500]; void *conn = NULL; int status; struct storage_record r; int authenticated = 0; do{ printmenu(&i); // function to prompt the user for the menu and read in and update the variable i if(i == 0){// Connect to server if(conn != NULL) printf("You are already connected. Please choose another option.\n\n"); else { conn = storage_connect("localhost",2222); if(!conn) { printf("Cannot connect to server @ %s:%d. Error code: %d.\n", SERVERHOST, SERVERPORT, errno); //return -1; } else printf("\nUser conncected with the server\n"); status = storage_auth("admin", "dog4sale", conn); if(status != 0) { printf("storage_auth failed with username '%s' and password '%s'. " \ "Error code: %d, status: %d\n", SERVERUSERNAME, SERVERPASSWORD, errno, status); //storage_disconnect(conn); //return status; } else{ authenticated = 1; printf("\nUser authenticated with the server\n"); } } } if(i == 1) {// Connect to server if(conn != NULL) printf("You are already connected. Please choose another option.\n\n"); else { printf("Enter the Server Host\n"); fgets(SERVERHOST, MAX_HOST_LEN, stdin); sscanf(SERVERHOST, "%s", SERVERHOST); printf("Enter the Server Port\n"); fgets(SERVERPORTstr, MAX_PORT_LEN, stdin); sscanf(SERVERPORTstr, "%d", &SERVERPORT); conn = storage_connect(SERVERHOST, SERVERPORT); if(!conn) { printf("Cannot connect to server @ %s:%d. Error code: %d.\n", SERVERHOST, SERVERPORT, errno); //return -1; } else printf("\nUser connected with the server\n"); } } else if(i == 2) {// Authenticate the client. if(conn == NULL) printf("You are not connected yet. Please connect to the server first.\n\n"); //else if(authenticated == 1) //printf("You are already authenticated. Please choose another option.\n\n"); else { printf("Enter the UserName Host\n"); fgets(SERVERUSERNAME, MAX_USERNAME_LEN, stdin); sscanf(SERVERUSERNAME, "%s", SERVERUSERNAME); printf("Enter the Password Host\n"); fgets(SERVERPASSWORD, MAX_ENC_PASSWORD_LEN, stdin); sscanf(SERVERPASSWORD, "%s", SERVERPASSWORD); status = storage_auth(SERVERUSERNAME, SERVERPASSWORD, conn); if(status != 0) { printf("storage_auth failed with username '%s' and password '%s'. " \ "Error code: %d, status: %d\n", SERVERUSERNAME, SERVERPASSWORD, errno, status); } else{ authenticated = 1; printf("\nUser authenticated with the server\n"); } } } else if(i == 3) {// SET command if(conn == NULL) printf("You are not connected yet. Please connect to the server first.\n\n"); else { printf("Please enter the Table\n"); fgets(TABLE, MAX_TABLE_LEN, stdin); sscanf(TABLE, "%s", TABLE); printf("Please enter the Key\n"); fgets(KEY, MAX_KEY_LEN, stdin); sscanf(KEY, "%s", KEY); printf("Please Enter the record that you wish to store\n"); fgets(r.value, MAX_VALUE_LEN, stdin); r.value[strlen(r.value)-1] = '\0'; //strncpy(r.value, value, sizeof value); //This is for measuring the server processing time //remember when the process started clock_t t; t=clock(); //issue set call to DATABASE with TABLE, KEY and r.value if(!strcmp(r.value,"null")){ status = storage_set(TABLE,KEY,NULL,conn); } if(strcmp(r.value,"null")) status = storage_set(TABLE, KEY, &r, conn); //find how long it took to perform the GET operation //current time minus the time when the operation started t=clock()-t; if(status != 0) { printf("storage_set failed. Error code: %d.\n", errno); } else printf("storage_set: successful.\n"); } } else if(i == 4) {// GET command if(conn == NULL) printf("You are not connected yet. Please connect to the server first.\n\n"); //else if(authenticated == 0) //printf("You are not authenticated yet. Please authenticate first.\n\n"); else { printf("Please enter the Table\n"); fgets(TABLE, MAX_TABLE_LEN, stdin); sscanf(TABLE, "%s", TABLE); printf("Please enter the Key\n"); fgets(KEY, MAX_KEY_LEN, stdin); sscanf(KEY, "%s", KEY); status = storage_get(TABLE, KEY, &r, conn); if(status != 0) { printf("storage_get failed. Error code: %d.\n", errno); storage_disconnect(conn); return status; } else printf("storage_get: the value returned for key '%s' is '%s'.\n", KEY, r.value); } } else if(i == 5) {// QUERY command if(conn == NULL) printf("You are not connected yet. Please connect to the server first.\n\n"); else { printf("Please enter the Table\n"); fgets(TABLE, MAX_TABLE_LEN, stdin); sscanf(TABLE, "%s", TABLE); printf("Please enter the Predicates\n"); fgets(PREDICATES_1, 500, stdin); sscanf(PREDICATES_1, "%[^'\n]",PREDICATES); // (const char *table, const char *predicates, char **keys, const int max_keys, void *conn) status = storage_query(TABLE, PREDICATES, key, &r, conn); //int foundkeys = storage_query(INTTABLE, "col<-1", test_keys, MAX_RECORDS_PER_TABLE, test_conn); printf("status/number of keys: %d\n",status); if(status == -1) { printf("storage_query failed. Error code: %d.\n", errno); storage_disconnect(conn); return status; } else { int p = 0; int i = 0; while (key[i] != NULL){ printf("key: '%s'\n", key[i]); i++; } } } } else if(i==6) {// Disconnect from server status = storage_disconnect(conn); if(status != 0) { printf("storage_disconnect failed. Error code: %d.\n", errno); return status; } else { conn = NULL; authenticated = 0; printf("You are now disconnected\n"); } } else if (i==8){ // import the census into the data base if(conn == NULL) printf("You are not connected yet. Please connect to the server first.\n\n"); FILE* fp; fp=fopen("census", "r"); //open the file for reading only if(fp == NULL) printf("Error opening file"); else { int loop; //populate the table with all the data in the data file (city + its population) do { char name[100]; char value[MAX_VALUE_LEN]; char line[100]; char *l = fgets(line, sizeof line, fp); if (!feof(fp)){ sscanf(l, "%s %s", name, value); printf("name: %s , value: %s \n", name, value); strcpy(r.value,value); status = storage_set("census", name, &r, conn); if(status != 0) { printf("storage_set failed. Error code: %d.\n", errno); } } } while(!feof(fp)); //read the first line (city) } } } while (i!=7); // if i is 6, we want to exit from client // Exit return 0; }