buddy_mgr(std::string dbname = ":memory:") { m_sql.open(soci::sqlite3, dbname); db_initialize(); m_sql.close(); m_sql.open(soci::sqlite3, dbname); }
void test_with_index_dump(MEM_POOL* mem_pool) { system("rm -rf /tmp/binlog"); system("rm -rf /tmp/log"); system("rm -rf /tmp/storage"); db_initialize(mem_pool); //构造数据 uint16 sid = 0; uint32 docid = 0; int32 ret; uint16 field_count = 5; enum field_types types[5] = {HI_TYPE_STRING,HI_TYPE_LONG,HI_TYPE_TINY,HI_TYPE_LONGLONG,HI_TYPE_DOUBLE}; //将第一个段填满 while(sid == 0) { ret = db_insert("hello", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); } sid = 0; //建hash索引,然后unload load 验证 ret = db_ensure_index("hello","HI_TYPE_STRING",HI_KEY_ALG_HASH,HI_TYPE_STRING, mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); struct low_data_struct* data = (struct low_data_struct*)mem_pool_malloc(mem_pool,sizeof(struct low_data_struct)); get_low_data(data,HI_TYPE_STRING,mem_pool); struct list_head* index_equal_ret = db_index_equal_query("hello",get_time_hint(mem_pool),data,mem_pool); struct segment_query_rowids* node; list_for_each_entry(node,index_equal_ret,rowids_list) { if(node->sid == 0) ASSERT_EQ(node->rowids->rowid_num,10); else if(node->sid == 1) ASSERT_EQ(node->rowids->rowid_num,1); else FAIL(); } ret = db_unload_segment("hello",sid,mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); index_equal_ret = db_index_equal_query("hello",get_time_hint(mem_pool),data,mem_pool); list_for_each_entry(node,index_equal_ret,rowids_list) { ASSERT_EQ(node->rowids->rowid_num,1); ASSERT_EQ(node->sid,1); }
//简单测试 void test_single_dump(MEM_POOL* mem_pool) { system("rm -rf /tmp/binlog"); system("rm -rf /tmp/log"); system("rm -rf /tmp/storage"); db_initialize(mem_pool); //构造数据 uint16 sid = 0; uint32 docid = 0; int32 ret; uint16 field_count = 5; enum field_types types[5] = {HI_TYPE_STRING,HI_TYPE_LONG,HI_TYPE_TINY,HI_TYPE_LONGLONG,HI_TYPE_DOUBLE}; //将第一个段填满 while(sid == 0) { ret = db_insert("hello", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); } sid = 0; docid = 10; //验证是否可查询 uint32 i; for(i=0; i<docid; i++) ASSERT_EQ(1,verify_row_data(db_data_query_row("hello",sid,i,mem_pool),field_count,types)); ret = db_unload_segment("hello",sid,mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); //验证是否可查询 for(i=0; i<docid; i++) ASSERT_EQ(0,verify_row_data(db_data_query_row("hello",sid,i,mem_pool),field_count,types)); char segment_file_name[FILENAME_MAX_LENGTH]; memset(segment_file_name,0,sizeof(segment_file_name)); sprintf(segment_file_name,"%s/hello/hello_segment_000000_dump",get_db()->work_space); ret = db_load_segment("hello",sid,segment_file_name,mem_pool); //验证是否可查询 for(i=0; i<docid; i++) ASSERT_EQ(1,verify_row_data(db_data_query_row("hello",sid,i,mem_pool),field_count,types)); db_release(); }
void index_create_test(MEM_POOL* mem_pool) { system("rm -rf /tmp/binlog"); system("rm -rf /tmp/log"); system("rm -rf /tmp/storage"); int32 ret; db_initialize(mem_pool); //先创建表,再建索引,再插数据 ret = db_ensure_index("hello","HI_TYPE_STRING",HI_KEY_ALG_HASH,HI_TYPE_STRING,mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); uint16 field_count = 3; enum field_types types[5] = {HI_TYPE_STRING,HI_TYPE_LONG,HI_TYPE_TINY,HI_TYPE_LONGLONG,HI_TYPE_DOUBLE}; //构造数据 uint16 sid = 0; uint32 docid = 0; struct low_data_struct* new_data = (struct low_data_struct*)mem_pool_malloc(mem_pool,sizeof(struct low_data_struct)); get_low_data(new_data,HI_TYPE_STRING,mem_pool); ret = db_insert("hello", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); struct list_head* index_equal_ret = NULL; index_equal_ret = db_index_equal_query("hello",get_time_hint(mem_pool),new_data,mem_pool); struct segment_query_rowids* node; list_for_each_entry(node,index_equal_ret,rowids_list){ ASSERT_EQ(node->sid,0); ASSERT_EQ(node->rowids->rowid_num,1); ASSERT_EQ(node->rowids->head->rowid_array[0],0); }
void InitializeDatabases() { db_initialize(); }
/* * <0: error * ==0: success * ==1: new db opened, caller must initialize tables */ int osd_db_open(const char *path, struct osd_device *osd) { int ret; struct stat sb; char SQL[MAXSQLEN]; char *err = NULL; int is_new_db = 0; ret = stat(path, &sb); if (ret == 0) { if (!S_ISREG(sb.st_mode)) { osd_error("%s: path %s not a regular file %d", __func__, path, sb.st_mode); ret = 1; goto out; } } else { if (errno != ENOENT) { osd_error("%s: stat path %s", __func__, path); goto out; } /* sqlite3 will create it for us */ is_new_db = 1; } osd->handle->dbc = Calloc(1, sizeof(*osd->handle->dbc)); if (!osd->handle->dbc) { ret = -ENOMEM; goto out; } ret = sqlite3_open(path, &(osd->handle->dbc->db)); if (ret != SQLITE_OK) { osd_error("%s: open db %s", __func__, path); ret = OSD_ERROR; goto out_free_dbc; } if (is_new_db) { /* build tables from schema file */ ret = sqlite3_exec(osd->handle->dbc->db, osd_schema, NULL, NULL, &err); if (ret != SQLITE_OK) { sqlite3_free(err); ret = OSD_ERROR; goto out_close_db; } } else { /* existing db, check for tables */ ret = db_check_tables(osd->handle->dbc); if (ret != OSD_OK) goto out_close_db; } /* initialize dbc fields */ ret = db_initialize(osd->handle->dbc); if (ret != OSD_OK) { ret = OSD_ERROR; goto out_close_db; } if (is_new_db) ret = 1; goto out; out_close_db: sqlite3_close(osd->handle->dbc->db); out_free_dbc: free(osd->handle->dbc); osd->handle->dbc = NULL; out: return ret; }
void data_insert_test(MEM_POOL* mem_pool) { system("rm -rf /tmp/binlog"); system("rm -rf /tmp/log"); system("rm -rf /tmp/storage"); db_initialize(mem_pool); //构造数据 uint16 sid = 0; uint32 docid = 0; int32 ret; uint16 field_count = 3; enum field_types types[5] = {HI_TYPE_STRING,HI_TYPE_LONG,HI_TYPE_TINY,HI_TYPE_LONGLONG,HI_TYPE_DOUBLE}; ret = db_insert("hello", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); struct row_data* query_row_ret = NULL; struct low_data_struct* query_col_ret = NULL; query_row_ret = db_data_query_row("hello",sid,docid,mem_pool); ASSERT_EQ(1,verify_row_data(query_row_ret,field_count,types)); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_STRING",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_STRING)); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONG",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_LONG)); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_TINY",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_TINY)); //不同的列数量 field_count = 5; ret = db_insert("hello", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); query_row_ret = db_data_query_row("hello",sid,docid,mem_pool); ASSERT_EQ(1,verify_row_data(query_row_ret,field_count,types)); //表名不对 query_row_ret = db_data_query_row("world",sid,docid,mem_pool); ASSERT_EQ(0,verify_row_data(query_row_ret,field_count,types)); //sid不对 query_row_ret = db_data_query_row("hello",sid+1,docid,mem_pool); ASSERT_EQ(0,verify_row_data(query_row_ret,field_count,types)); //docid不对 query_row_ret = db_data_query_row("hello",sid,docid+1,mem_pool); ASSERT_EQ(0,verify_row_data(query_row_ret,field_count,types)); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_STRING",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_STRING)); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONG",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_LONG)); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONGLONG",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_LONGLONG)); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_TINY",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_TINY)); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_DOUBLE",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_DOUBLE)); //表名不对 query_col_ret = db_data_query_col("world",sid,"HI_TYPE_DOUBLE",docid,mem_pool); ASSERT_EQ(0,verify_low_data(query_col_ret,HI_TYPE_DOUBLE)); //sid不对 query_col_ret = db_data_query_col("hello",sid+1,"HI_TYPE_LONGLONG",docid,mem_pool); ASSERT_EQ(0,verify_low_data(query_col_ret,HI_TYPE_LONGLONG)); //docid不对 query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONGLONG",docid+1,mem_pool); ASSERT_EQ(0,verify_low_data(query_col_ret,HI_TYPE_LONGLONG)); //列名不对 query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LON",docid,mem_pool); ASSERT_EQ(0,verify_low_data(query_col_ret,HI_TYPE_LONG)); //列名不对 query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONG",docid,mem_pool); ASSERT_EQ(0,verify_low_data(query_col_ret,HI_TYPE_LONGLONG)); //换一个表名 field_count = 3; ret = db_insert("world", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); ASSERT_EQ(sid,0); ASSERT_EQ(docid,0); query_row_ret = db_data_query_row("world",sid,docid,mem_pool); ASSERT_EQ(1,verify_row_data(query_row_ret,field_count,types)); query_col_ret = db_data_query_col("world",sid,"HI_TYPE_STRING",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_STRING)); query_col_ret = db_data_query_col("world",sid,"HI_TYPE_LONG",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_LONG)); query_col_ret = db_data_query_col("world",sid,"HI_TYPE_TINY",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_TINY)); db_release(); }
void data_update_test(MEM_POOL* mem_pool) { system("rm -rf /tmp/binlog"); system("rm -rf /tmp/log"); system("rm -rf /tmp/storage"); db_initialize(mem_pool); uint16 field_count = 3; enum field_types types[5] = {HI_TYPE_STRING,HI_TYPE_LONG,HI_TYPE_TINY,HI_TYPE_LONGLONG,HI_TYPE_DOUBLE}; //构造数据 uint16 sid = 0; uint32 docid = 0; int32 ret; ret = db_insert("hello", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); struct low_data_struct* new_data = (struct low_data_struct*)mem_pool_malloc(mem_pool,sizeof(struct low_data_struct)); get_low_data(new_data,HI_TYPE_STRING,mem_pool); struct low_data_struct* old_data = NULL; memset(new_data->field_name,0,20); strcpy(new_data->field_name,"HI_TYPE_LONG"); //更新已有的列 ret = db_update("hello",sid,docid,new_data,&old_data,mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); ASSERT_EQ(1,verify_low_data(old_data,HI_TYPE_LONG)); //查询一列 struct low_data_struct* query_col_ret = NULL; query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONG",docid,mem_pool); ASSERT_STREQ(query_col_ret->field_name,"HI_TYPE_LONG"); query_col_ret->field_name = (char*)mem_pool_malloc(mem_pool,20); strcpy(query_col_ret->field_name,"HI_TYPE_STRING"); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_STRING)); //更新不存在的列 query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONGLONG",docid,mem_pool); ASSERT_EQ(0,verify_low_data(query_col_ret,HI_TYPE_LONGLONG)); get_low_data(new_data,HI_TYPE_LONGLONG,mem_pool); ret = db_update("hello",sid,docid,new_data,&old_data,mem_pool); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONGLONG",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_LONGLONG)); //更新不存在的段 ret = db_update("hello",sid+1,docid,new_data,&old_data,mem_pool); ASSERT_EQ(ERROR_SEGMENT_NOT_INIT,ret); //更新不存在的docid ret = db_update("hello",sid,docid+1,new_data,&old_data,mem_pool); ASSERT_EQ(ERROR_EXCEED_CURRENT,ret); ASSERT_EQ(0,verify_low_data(old_data,HI_TYPE_STRING)); //更新不存在的表名 ret = db_update("world",sid,docid,new_data,&old_data,mem_pool); ASSERT_EQ(ret,ERROR_TABLE_NOT_EXIT); db_release(); }