void basicSearch(char * input) { index_search_results_t * all_results; index_search_elem_t result; int num_results; char * file_name; int i, line_number; printf("%s\n", input); pthread_mutex_lock(&mutex_test); all_results = find_in_index(input); pthread_mutex_unlock(&mutex_test); if (all_results == NULL) { printf("Word not found.\n"); } else { num_results = all_results->num_results; for (i=0; i < num_results; i++) { result = all_results->results[i]; line_number = result.line_number; file_name = result.file_name; printf("FOUND: %s %d\n", file_name, line_number); } } }
static int find_i(T *thiz, mkey_t *key, mval_t *v, mkv_t *tkv) { int r, i; hdr_block_t *hdr; off_t off; fkv_t fkv; fkv.kv = tkv; RWLOCK_READ(LOCK); r = krange_cmp(thiz, key); if (r != 0) { r = RC_NOT_FOUND; goto _out; } hdr = thiz->hdr; off = (hdr->node_cnt - 1) * BTR_INDEX_BLK_SIZE; /* root off */ for (i = 0; i < hdr->tree_heigh; i++) { off = find_in_index(thiz, off, key, NULL); if (off <= 0) { r = RC_NOT_FOUND; goto _out; } off = off - (off_t)(hdr->leaf_off); } r = find_in_leaf(thiz, off, key, &fkv, NULL); if (r != 0) { r = RC_NOT_FOUND; goto _out; } if (v != NULL) { v->len = tkv->v.len; v->data = MY_Malloc(v->len); r = read_val(thiz->rfd, fkv.blkoff, fkv.voff, v); if (r != 0) { r = RC_ERR; MY_Free(v->data); goto _out; } tkv->v.data = v->data; check_fval(thiz->rfd, tkv); } r = RC_FOUND; DEBUG("hit %.*s in %s", (int)key->len, key->data, thiz->file); _out: RWUNLOCK(LOCK); return r; }
int main(int argc, char * argv[]) { index_search_results_t * results; init_index(); insert_into_index("hello", "test.c", 10); insert_into_index("hello", "test.c", 20); insert_into_index("hello", "foo.c", 30); insert_into_index("goodbye", "test.c", 10); results = find_in_index("hello"); if (results) { int i; for (i = 0; i < results->num_results; i++) { printf("%s: %d\n", results->results[i].file_name, results->results[i].line_number); } free(results); } find_in_index("goodbye"); return(0); }
/** Searches the index tree for the given string value. * * Compares the given value with the value in the given node, and returns the * value set for the current node if there is a match. Otherwise it performs * a recursive search of the left or right sub-tree depending on whether the * value is less than or greater than the value in the node. * * @param[in] root_node - a pointer to the root of the (sub-)tree. * @param[in] value_str_len - length of the string to be inserted. * @param[in] value_str - A pointer to string to be inserted. * * @return Pointer to the value set (list of locations) for the given value, or * null if not found */ IndexTreeValue *find_in_index( IndexTreeNode *root_node, size_t value_str_len, const char *value_str) { int cmp; if (!root_node) return (IndexTreeValue *)0; cmp = compare_with_tree_node(value_str_len, value_str, root_node); if (cmp < 0) { return find_in_index(root_node->left_node, value_str_len, value_str); } else if (cmp > 0) { return find_in_index(root_node->right_node, value_str_len, value_str); } else { return root_node->value; } }
void advSearch(char * input) { index_search_results_t * all_results; index_search_elem_t result; int num_results; char * file_name; file_name = malloc(512); char * file_name_result; file_name_result = malloc(512); char * word; word = malloc(512); int i, found=0, line_number; int adv_flag = FALSE; int file_flag = FALSE; word = strtok(input, " "); strcpy(file_name, word); word = strtok(NULL, " "); printf("FILE: %s WORD: %s\n", file_name, word); while(1){ adv_flag = FALSE; pthread_mutex_lock(&mutex_test); all_results = find_in_index(word); pthread_mutex_unlock(&mutex_test); if (all_results != NULL) { num_results = all_results->num_results; for (i=0; i < num_results; i++) { result = all_results->results[i]; line_number = result.line_number; file_name_result = result.file_name; int cmp = strncmp(file_name_result, file_name, 512); if (cmp != 0) { continue; } printf("FOUND: %s %d\n", file_name_result, line_number); found = 1; if(strcmp(file_name_result, input) == 0){ file_flag = TRUE; } } break; } pthread_mutex_lock(&mutex_advanced); while(!indexing_done){ printf("WAITING FOR INDEXING\n"); pthread_cond_wait(&indexed_cond, &mutex_advanced); adv_flag = TRUE; //printf("SHOULD BE INDEXED"); } //printf("ADVANCED: INDEXING IS DONE"); pthread_mutex_unlock(&mutex_advanced); if(!adv_flag) break; } if (!found){ printf("Word not found.\n"); } if (file_flag == FALSE){ printf("File not found.\n"); } }