#include <stdio.h>
#include "db_headers.h"
#include "db_index_entry.h"
#include "nisdb_mt.h"
db_index_entry::db_index_entry(char* name, int nlen, entryp ep)
{
if ((key = new item(name, nlen)) == NULL)
FATAL("db_index_entry::db_index_entry: cannot allocate space",
DB_MEMORY_LIMIT);
location = ep;
next_result = next = NULL;
}
db_index_entry::db_index_entry(unsigned long hval, item* k,
entryp ep, db_index_entry_p rest)
{
if ((key = new item(k)) == NULL)
FATAL(
"db_index_entry::db_index_entry: cannot allocate space (2)",
DB_MEMORY_LIMIT);
location = ep;
next = rest;
next_result = NULL;
hashval = hval;
}
db_index_entry_p
db_index_entry::join(long , long ,
db_index_entry_p list2, long * newsize)
{
db_index_entry_p mergedlist = NULL,
mergedtail = NULL,
current,
other,
otherprev,
otherstart = list2;
int count = 0;
for (current = this; (current != NULL) && (otherstart != NULL);
current = current->next_result) {
otherprev = NULL;
for (other = otherstart;
other != NULL;
other = other->next_result) {
if (current->location == other->location)
break;
else
otherprev = other;
}
if (other != NULL) {
if (otherprev == NULL) {
otherstart = otherstart->next_result;
} else {
otherprev->next_result = other->next_result;
}
if (mergedlist == NULL)
mergedlist = current;
else
mergedtail->next_result = current;
mergedtail = current;
++count;
}
}
if (mergedtail) mergedtail->next_result = NULL;
*newsize = count;
return (mergedlist);
}
void
db_index_entry::relocate(db_index_entry_p *new_tab, unsigned long hashsize)
{
db_index_entry_p np, next_np, *hp;
for (np = this; np != NULL; np = next_np) {
next_np = np->next;
hp = &new_tab[np->hashval % hashsize];
np->next = *hp;
*hp = np;
}
}
db_index_entry_p
db_index_entry::getnext(bool_t casein, unsigned long hval, item *i, entryp l)
{
db_index_entry_p np;
for (np = this; np != NULL; np = np->next) {
if ((np->hashval == hval) &&
(np->key->equal(i, casein)) && l == location) {
break;
}
}
if (np != NULL)
return (np->next);
else
return (NULL);
}
db_index_entry_p
db_index_entry::lookup(bool_t casein, unsigned long hval,
item *i, entryp recnum)
{
db_index_entry_p np;
for (np = this; np != NULL; np = np->next) {
if (np->hashval == hval && np->key->equal(i, casein) &&
np->location == recnum) {
break;
}
}
if (np) np->next_result = NULL;
return (np);
}
db_index_entry_p
db_index_entry::lookup(bool_t casein, unsigned long hval,
item *i, long * how_many)
{
db_index_entry_p fst, prev, curr;
long count = 0;
for (fst = this; fst != NULL; fst = fst->next) {
if ((fst->hashval == hval) && (fst->key->equal(i, casein))) {
++count;
break;
}
}
if (fst != NULL) {
prev = fst;
for (curr = fst->next; curr != NULL; curr = curr->next) {
if ((curr->hashval == hval) &&
(curr->key->equal(i, casein))) {
prev->addresult(curr);
prev = curr;
++count;
}
else
break;
}
prev->addresult(NULL);
}
*how_many = count;
return (fst);
}
bool_t
db_index_entry::remove(db_index_entry_p *head, bool_t casein,
unsigned long hval, item *i, entryp recnum)
{
db_index_entry_p np, dp;
for (dp = np = this; np != NULL; np = np->next) {
if (np->hashval == hval && np->key->equal(i, casein) &&
np->location == recnum) {
break;
} else {
dp = np;
}
}
if (np == NULL) return FALSE;
if (dp == np) {
*head = np->next;
} else {
dp->next = np->next;
}
delete np;
return (TRUE);
}
bool_t
db_index_entry::add(db_index_entry **head, bool_t casein,
unsigned long hval, item *i, entryp recnum)
{
db_index_entry_p curr, prev, rp, save;
for (prev = curr = this; curr != NULL; curr = curr->next) {
if (curr->hashval == hval && curr->key->equal(i, casein)) {
break;
} else {
prev = curr;
}
}
if (curr == NULL) {
save = *head;
*head = new db_index_entry(hval, i, recnum, * head);
if (*head == NULL) {
*head = save;
FATAL3(
"db_index_entry::add: cannot allocate space for head",
DB_MEMORY_LIMIT, FALSE);
}
} else {
save = prev->next;
prev->next = new db_index_entry(hval, i, recnum, prev->next);
if (prev->next == NULL) {
prev->next = save;
FATAL3(
"db_index_entry::add: cannot allocate space for entry",
DB_MEMORY_LIMIT, FALSE);
}
}
return (TRUE);
}
void
db_index_entry::print()
{
if (key != NULL) {
key->print();
printf("\t");
}
printf(": %d\n", location);
}
void
db_index_entry::print_all()
{
db_index_entry *np;
for (np = this; np != NULL; np = np->next) {
np->print();
}
}
void
db_index_entry::print_results()
{
db_index_entry *np;
for (np = this; np != NULL; np = np->next_result) {
np->print();
}
}