Esempio n. 1
0
void ReduceKeys(INode *node, IKeyControl *keyCont)
{

    keyCont->SortKeys();        // ensure the keys are sorted by time
    
    int to;         // the next key we're setting
    int from;       // the next key we're examining
    int origNumKeys = keyCont->GetNumKeys();
    int finalNumKeys = origNumKeys;
    
    for (to = 1, from = 1; from < origNumKeys - 1; to++, from++)
    {
        T prevKey, curKey, nextKey;

        keyCont->GetKey(from - 1, &prevKey);
        keyCont->GetKey(from, &curKey);
        keyCont->GetKey(from + 1, &nextKey);

        if (CompareKeys(curKey, prevKey) && CompareKeys(curKey, nextKey))
            finalNumKeys--; // skip it
        else
            keyCont->SetKey(to, &curKey); // copy current key
    }
    // copy the last one without peeking ahead
    T lastKey;
    keyCont->GetKey(from, &lastKey);
    keyCont->SetKey(to, &lastKey);

    keyCont->SetNumKeys(finalNumKeys);
    keyCont->SortKeys();
}
Esempio n. 2
0
int get_successors(char *key, int k, char *result[]) {
    if (k<1) {
		printf("k should be positive not -1");
	    return -1;
    }

	int i, j, len=0;
	PAGENO PageNo = treesearch_page(ROOT, key);

	assert(PageNo > 0);

	struct PageHdr *PagePtr = FetchPage(PageNo);
	struct KeyRecord *KeyRcPtr = PagePtr->KeyListPtr;

	if (result == NULL) {
		result = (char **)malloc(sizeof(char) * 10 * k);

		for (i=0; i<k; i++)
    		result[i] = (char *)malloc(sizeof(char) * 10);
	}

	while (strcmp(key, KeyRcPtr->StoredKey)>0) {
		KeyRcPtr = KeyRcPtr->Next;
	}
	if (strcmp(key, KeyRcPtr->StoredKey)==0)
		KeyRcPtr = KeyRcPtr->Next; 
	
	for (i=0; i<k && PagePtr!=NULL; i++) {
		if (KeyRcPtr == NULL) {
			PagePtr = FetchPage(PagePtr->PgNumOfNxtLfPg);
			if (PagePtr == NULL)
				break;
			KeyRcPtr = PagePtr->KeyListPtr;
		}
		result[i] = (char *)KeyRcPtr->StoredKey;
		KeyRcPtr = KeyRcPtr->Next;
		len++;
	}
	/* sort the result array */
	for (i=0; i<len; i++) {
		for (j = i+1; j<len; j++) {
			if (CompareKeys(result[i], result[j])==2) {
				char *tmp = result[i];
				result[i] = result[j];
				result[j] = tmp;
			}
		}
	}

	printf("found %d successors:\n", len);

	for (i=0; len>0; len--, i++) {
		printf("%s\n",result[i]);
	}

	free(result);

    return 0;
}
Esempio n. 3
0
static bool CompareKey(int key_id,
		       int key_len,
		       const char* type,
		       const char* key_seq,
		       struct key* actual) {
	struct key temp;

	temp.key_id = key_id;
	temp.key_len = key_len;
	strlcpy(temp.type, type, sizeof(temp.type));
	memcpy(temp.key_seq, key_seq, key_len);

	return CompareKeys(&temp, actual);
}
Esempio n. 4
0
int printLeaf(struct PageHdr *p, char *key, int k) {
    struct KeyRecord *keyptr = (struct KeyRecord *) malloc(sizeof(struct KeyRecord *));
    if (p != NULL) {
        keyptr = p->KeyListPtr;
    }
    int no_of_elements_removed = 0;
    while (k > 0 && keyptr != NULL) {
        if (CompareKeys(keyptr->StoredKey, key) == 2) {
            k--;
            no_of_elements_removed++;
            strcpy(global_arr_succ[global_succ_count], keyptr->StoredKey);
            global_succ_count++;
        }
        keyptr = keyptr->Next;
    }

    return no_of_elements_removed;
}
Esempio n. 5
0
 bool operator()(const CTempString& lhs, const CTempString& rhs) const
     { return CompareKeys(lhs, rhs) == 0; }
int iterate_stack(FILE *fpb, char *key, char *targetKey, int k) {
    char Ch;
    char **global_arr = create_string_array(k);
    int global_ptr = 0;
    POSTINGSPTR PostOffset;
    PAGENO PgNum, PtrToNxtLfPg, PtrToFinalRtgPg;
    NUMKEYS NumKeys;
    KEYLEN KeyLen;
    NUMBYTES NumBytes;
    int number_of_predecessors = 0;
    PAGENO cal[k];
    int calCounter = 0;
    while (!empty() && k > 0) {
        PAGENO curr = pop();
        cal[calCounter++] = curr;
        if (curr < 1) {
            printf("ERROR page numbers start from 1 and on\n");
            return -1;
        }
        if (fpb == NULL) {
            printf("ERROR fbp is NULL\n");
            return -1;
        }
        if (ffsize(fpb) <= (curr - 1) * PAGESIZE) { /* illegal page number */
            printf("ERROR page numbers start from 1 and not exceed the last one \n");
            return -1;
        }

        fseek(fpb, (long) (curr - 1) * PAGESIZE, 0);
        fread(&Ch, sizeof(Ch), 1, fpb);
        if (feof(fpbtree) != 0)
            exit(0);
        fread(&PgNum, sizeof(PgNum), 1, fpb);
        if (Ch == LeafSymbol) {
            fread(&PtrToNxtLfPg, sizeof(PtrToNxtLfPg), 1, fpb);
        }
        fread(&NumBytes, sizeof(NumBytes), 1, fpb);
        fread(&NumKeys, sizeof(NumKeys), 1, fpb);
        if (Ch == NonLeafSymbol) {
            fread(&PtrToFinalRtgPg, sizeof(PtrToFinalRtgPg), 1, fpb);
        }
        char values[NumKeys][MAXWORDSIZE];

        int j = 0, hits = 0;
        for (; j < NumKeys; j++) {
            if (Ch == NonLeafSymbol) {
                fread(&PgNum, sizeof(PgNum), 1, fpb);
                fread(&KeyLen, sizeof(KeyLen), 1, fpb);
                fread(key, sizeof(char), KeyLen, fpb);
                (*(key + KeyLen)) = '\0';
                int Result = CompareKeys(key, targetKey);
                if (Result == 1) {
                    push(PgNum);
                }
            }
            if (Ch == LeafSymbol) {
                fread(&KeyLen, sizeof(KeyLen), 1, fpb);
                fflush(stdout);
                fread(key, sizeof(char), KeyLen, fpb);
                (*(key + KeyLen)) = '\0';
                if (CompareKeys(key, targetKey) == 1) {
                    strcpy(values[j], key);
                    hits++;
                }
                fread(&PostOffset, sizeof(PostOffset), 1, fpb);
            }
        }
        if (Ch == NonLeafSymbol) {
            if (!checkProcessedBefore(PtrToFinalRtgPg, cal, calCounter)) {
                push(PtrToFinalRtgPg);
            }
        }
        int p = hits - 1;

        for (; p >= 0 && k > 0; p--) {
            number_of_predecessors++;
            strcpy(global_arr[global_ptr], values[p]);
            global_ptr++;
            --k;
        }

    }
    int g = global_ptr - 1;
    printf("found %d predecessors:\n", number_of_predecessors);
    for (; g >= 0; g--) {
        printf("%s\n", global_arr[g]);
    }
    fflush(stdout);
    return number_of_predecessors;
}
int dfs(char *key, char* result[],PAGENO pgnum,int need, int k){
	if(need<=0) return 0;

	struct PageHdr *PagePtr=FetchPage(pgnum);
	char *Word; /* Key stored in B-Tree */
	struct KeyRecord* KeyListTraverser=PagePtr->KeyListPtr;
	/* put keyrecord into stack*/

	int cmp_result;
	int NumKeys=PagePtr->NumKeys;
	int count=0;


	/*stack is only used for nonleaf pages*/
	Stack* stack;

	if (IsLeaf(PagePtr)){
		while (KeyListTraverser != NULL) {
			        if(CompareKeys(KeyListTraverser->StoredKey,key)==0){
			        	exist=1;
			        	break;
			        }
			        /*write this string into result. start from an empty place and, if get more string than needed, replace the oldest one*/
			        strcpy(result[count%need+k-need],KeyListTraverser->StoredKey);
			        count++;

			        KeyListTraverser = KeyListTraverser->Next;
		}

		FreePage(PagePtr);

        if( exist==0) return k;

		if(count>need) return need;
		else return count;

	}else{
		/*Use stack to do backtrack and keep searching predecessor leaf pages*/
		stack=init_stack(NumKeys+1);

		while( NumKeys>0){
			Word=KeyListTraverser->StoredKey;
			(*(Word + KeyListTraverser->KeyLen)) = '\0';

			cmp_result=CompareKeys(key, Word);
			NumKeys--;

			if(cmp_result<2){

				/*put PAGENO into stack*/
				push(stack,KeyListTraverser->PgNum);

				break;
			}

			/* put PAGENO into stack*/
			push(stack,KeyListTraverser->PgNum);

			KeyListTraverser = KeyListTraverser->Next;

		}

        /* Because PgNum means the left side(remember for B+ tree Child node is on left side of one key record) 
            child page no. This case means there are more beyond these left childs, which is the rightmost 
            child of the parent */
		if(cmp_result==2 && NumKeys==0){

			push(stack,PagePtr->PtrToFinalRtgPg);

		}

		while(stack->top>0){
			PAGENO tovisit= *(pop(stack));

			count+= dfs(key,result,tovisit,need-count,k);


			if(count>=need) return need;
		}
		free_stack(stack);
		FreePage(PagePtr);

		return count;
	}

}