/* 功能:按名次查找记录 参数: 姓名name,from 从第几个开始查找, 从0 到 count-1 返回值: int 型, 找到的记录在表中的位置, 没有找到则返回-1 打印: 无 */ int rank_findRecord(int rank, int from) { int i; int index = -1; int flag = 0; int count = countRecords(); rank_Refresh(); for(i=from; i<count; i++) { if( records[i].rank == rank) // 按指定的名次来查询 { flag = 1; index = i + 1; break; } } if(flag == 0) { return (index); } else { return (index); } }
void QgsSearchQueryBuilder::on_btnOk_clicked() { // if user hits OK and there is no query, skip the validation if ( txtSQL->text().trimmed().length() > 0 ) { accept(); return; } // test the query to see if it will result in a valid layer long numRecs = countRecords( txtSQL->text() ); if ( numRecs == -1 ) { // error shown in countRecords } else if ( numRecs == 0 ) { QMessageBox::warning( this, tr( "No Records" ), tr( "The query you specified results in zero records being returned." ) ); } else { accept(); } }
/* 功能:按学号查找记录 参数: 姓名name,from 从第几个开始查找, 从0 到 count-1 返回值: int 型, 找到的记录在表中的位置, 没有找到则返回-1 打印: 无 */ int num_findRecord(char *num, int from) { int count = 0; int index = -1; int i = 0; int flag = 0; //找到与否的标志 count = countRecords(); for(i=from; i<count; i++) { if( strcmp(records[i].num, num) == 0 ) // 按指定的学号来查询 { flag = 1; index = i + 1; break; } } if(flag == 0) { return (index); } else return (index); }
void QgsSearchQueryBuilder::on_btnTest_clicked() { long count = countRecords( txtSQL->text() ); // error? if ( count == -1 ) return; QMessageBox::information( this, tr( "Search results" ), tr( "Found %n matching feature(s).", "test result", count ) ); }
/* 功能:判断内存数组是否大小不够,不够的话自动增大INCR_SIZE 参数:无 返回:无 打印:使用条件编译,若定义了DEBUG则打印“数据库自动增大成功, 当前数据库大小为%d!”,便于调试 */ void autoIncr() { int count = countRecords(); if(count >= arrSize) { records = (StuInfo*) realloc( records, (arrSize+INCR_SIZE)*sizeof(StuInfo) ); // 当数据库大小不够时,每次自动增加INCR_SIZE if(records == NULL) { printf("memory failed!"); exit(-1); } arrSize = arrSize + INCR_SIZE; #ifdef DEBUG { printf("\n数据库自动增大成功, 当前数据库大小为%d! \n", arrSize); } #endif } }
/* 功能:判断新增加的学生的学号是否与内存中已有的学生学号冲突,冲突时给出提示 参数:要判断的学生 返回:当没有与其他学生重复时返回0,有重复时返回1 */ int dunum(StuInfo *stu) { int count = countRecords(); int i; int flag = 0; int du = 0; // 要判断的学生的学号与已有学生学号(包括自己)相比较,学号重复的次数,若du最后大于或等于2,则说明有重复 for(i=0; i<count; i++) { if( strcmp(stu->num, records[i].num) == 0) { ++du; } } if(du == 2 || du > 2) { printf("\n输入的学号与其他学生重复!\n"); flag = 1; } return flag; }
/* 功能:根据指定的序号显示出记录信息 参数:index,指定的序号 返回:void 指定的序号不存在时给出找不到的信息 */ void index_disPlay(int index) { int count = countRecords(); if( index > count ) // 输入不合法时 { printf("内存中没有该杀记录!\n"); } else if( index < 1 ) // 输入不合法时 printf("给定的序号不能为0或负数!\n"); else { if(records[index-1].savetag == 1) { showTable(); PR(index-1); // 宏定义 } else { printf("内存中没有该杀记录!\n"); // 找不到时相应处理 } } }
/* 功能:按姓名查找记录 参数: 姓名name,from 从第几个开始查找, 从0 到 count-1 返回值: int 型, 找到的记录在表中的位置, 没有找到则返回-1 打印: 无 */ int name_findRecord(char *name, int from) { int count = 0; //内存中学生记录数 int index = -1; // 位置标记 int i = 0; int flag = 0; //找到与否的标志,找到记录后为1 count = countRecords(); //调用记录统计函数,得出内存中当前的记录的条数 for(i=from; i<count; i++) { if( strcmp(records[i].name, name) == 0 ) //将内存中的记录逐条与要查找的记录比较 { flag = 1; // 如果找到则flag置为1 index = i + 1; // index 为该记录在内存中的位置,以1为起始 break; //退出循环 } } if(flag == 0) // flag为0则表示没有找到指定的记录 { return (index); } else return (index); }
// A function that performs a series of writes to a // Berkeley DB database. The information written // to the database is largely nonsensical, but the // mechanism of transactional commit/abort and // deadlock detection is illustrated here. void * writerThread(void *args) { int j, thread_num; int max_retries = 20; // Max retry on a deadlock const char *key_strings[] = {"key 1", "key 2", "key 3", "key 4", "key 5", "key 6", "key 7", "key 8", "key 9", "key 10"}; Db *dbp = (Db *)args; DbEnv *envp = dbp->get_env(); // Get the thread number (void)mutex_lock(&thread_num_lock); global_thread_num++; thread_num = global_thread_num; (void)mutex_unlock(&thread_num_lock); // Initialize the random number generator srand(thread_num); // Perform 50 transactions for (int i=0; i<50; i++) { DbTxn *txn; bool retry = true; int retry_count = 0; // while loop is used for deadlock retries while (retry) { // try block used for deadlock detection and // general db exception handling try { // Begin our transaction. We group multiple writes in // this thread under a single transaction so as to // (1) show that you can atomically perform multiple // writes at a time, and (2) to increase the chances // of a deadlock occurring so that we can observe our // deadlock detection at work. // Normally we would want to avoid the potential for // deadlocks, so for this workload the correct thing // would be to perform our puts with autocommit. But // that would excessively simplify our example, so we // do the "wrong" thing here instead. txn = NULL; envp->txn_begin(NULL, &txn, 0); // Perform the database write for this transaction. for (j = 0; j < 10; j++) { Dbt key, value; key.set_data((void *)key_strings[j]); key.set_size((u_int32_t)strlen(key_strings[j]) + 1); int payload = rand() + i; value.set_data(&payload); value.set_size(sizeof(int)); // Perform the database put dbp->put(txn, &key, &value, 0); } // countRecords runs a cursor over the entire database. // We do this to illustrate issues of deadlocking std::cout << thread_num << " : Found " << countRecords(dbp, txn) << " records in the database." << std::endl; std::cout << thread_num << " : committing txn : " << i << std::endl; // commit try { txn->commit(0); retry = false; txn = NULL; } catch (DbException &e) { std::cout << "Error on txn commit: " << e.what() << std::endl; } } catch (DbDeadlockException &) { // First thing that we MUST do is abort the transaction. if (txn != NULL) (void)txn->abort(); // Now we decide if we want to retry the operation. // If we have retried less than max_retries, // increment the retry count and goto retry. if (retry_count < max_retries) { std::cerr << "############### Writer " << thread_num << ": Got DB_LOCK_DEADLOCK.\n" << "Retrying write operation." << std::endl; retry_count++; retry = true; } else { // Otherwise, just give up. std::cerr << "Writer " << thread_num << ": Got DeadLockException and out of " << "retries. Giving up." << std::endl; retry = false; } } catch (DbException &e) { std::cerr << "db put failed" << std::endl; std::cerr << e.what() << std::endl; if (txn != NULL) txn->abort(); retry = false; } catch (std::exception &ee) { std::cerr << "Unknown exception: " << ee.what() << std::endl; return (0); } } } return (0); }