summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnisdb/db_index_entry.cc
blob: 213358c65b26c61fdbef6bbe49876b609ac4c58b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 *	db_index_entry.cc
 *
 *	Copyright (c) 1988-2000 by Sun Microsystems, Inc.
 *	All Rights Reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>

#include "db_headers.h"
#include "db_index_entry.h"
#include "nisdb_mt.h"

/* Constructor:  create an entry using given string and location info. */
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;
	/* what about hashval ? */
}

/*
 * Constructor:  create an entry using the given info.
 * A copy of the item is made.  New entry is added to head of list of 'n'.
*/
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;
}

/*
 * Join two lists (entry as identified by its 'location' occurs on both list,
 * then it is included in the list returned).
 * Returns pointer to resulting list; size of list
 * returned in 'newsize'.  List is chained using the 'nextresult' pointer.
 */
db_index_entry_p
db_index_entry::join(long /* size1 */, long /* size2 */,
	db_index_entry_p list2, long * newsize)
{
	db_index_entry_p mergedlist = NULL, // records that occur on both lists
		mergedtail = NULL, 	// tail pointer of mergedlist
		current,		// current pointer of this list
		other,			// current pointer of updated list2
		otherprev,		// previous pointer of updated list2
		otherstart = list2;	// head of updated list2
	int count = 0;

	/*
	 * algorithm is straightforward:
	 * traverse this list,
	 * for each item, traverse list2,
	 * if item on list1 matches item on list2,
	 * add to merged list and delete it from list2.
	 */

	for (current = this; (current != NULL) && (otherstart != NULL);
					current = current->next_result) {
		/* find 'current' in 'other' list */
		otherprev = NULL;
		for (other = otherstart;
			other != NULL;
			other = other->next_result) {
			if (current->location == other->location)
				break;
			else
				otherprev = other;
		}
		if (other != NULL) { /* found */
			/* delete 'other' from future consideration */
			if (otherprev == NULL) {
				/* new head */
				otherstart = otherstart->next_result;
			} else {
				/* bypass 'other' */
				otherprev->next_result = other->next_result;
			}
			/* add 'current' to list of items found so far */
			if (mergedlist == NULL)
				mergedlist = current;	/* first one found */
			else
				mergedtail->next_result = current; /* append */
			mergedtail = current; /* point to last entry found */
			++count;
		}
	}
	if (mergedtail) mergedtail->next_result = NULL;  /* set end to null */
	*newsize = count;
	return (mergedlist);
}

/* Relocate bucket starting with this entry to new hashtable 'new_tab'. */
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;
	}
}

/* Return the next entry in the bucket starting with this entry
	    with the same hashvalue, key and location as this entry. */
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);
}

/*
 * Return pointer to index entry with same hash value, same key,
 * and same record number as those supplied.  Returns NULL if not found.
 */
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;  /* should only be 1 */
	return (np);
}

/*
 * Returns pointer to a list of index entries with the same hash value and
 * key as those given.  Returns in 'how_many' the number of entries in the
 * list returned.  The list is linked by the 'next_result' field of the
 * index entries.  These may be changed after the next call to 'lookup'
 * or 'join'.
 */
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;
		}
	}
	/*
	 * gather all the ones with the same key; assume that all entries
	 * with same key are located contiguously.
	 */
	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); /* terminate the list -CM */
	}
	*how_many = count;
	return (fst);
}

/*
 * Remove entry with the specified hashvalue, key, and record number.
 * Returns 'TRUE' if successful, FALSE otherwise.
 * If the entry being removed is at the head of the list, then
 * the head is updated to reflect the removal. The storage for the index
 * entry is freed. The record pointed to by 'recnum' must be removed
 * through another means.  All that is updated in this operation is the
 * index.
 */
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;

	/* Search for it in the bucket */
	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;	// cannot delete if it is not there

	if (dp == np) {
		*head = np->next;	// deleting head of bucket
	} else {
		dp->next = np->next;	// deleting interior link
		}
	delete np;

	return (TRUE);
}

/* Replace the 'location' field of the index entry with the given one. */
/*
void
db_index_entry::replace(entryp ep)
{
	location = ep;
}
*/

/*
 * Create and add an entry with the given hashvalue, key value, and record
 * location, to the bucket pointed to by 'hashvalue'.
 * If an entry with the same hashvalue and key value is found,
 * the entry is added after the first entry with this property.  Otherwise,
 * the entry is added to the head of the bucket.  This way, entries
 * with the same hashvalue and key are not scattered throughout the bucket
 * but they occur together. Copy is made of given key.
 */
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;

	/* Search for it in the bucket */
	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) {
		/* none with same hashvalue/key found. Add to head of list. */
		save = *head;
		*head = new db_index_entry(hval, i, recnum, * head);
		if (*head == NULL) {
			*head = save;	// restore previous state
			FATAL3(
			"db_index_entry::add: cannot allocate space for head",
			DB_MEMORY_LIMIT, FALSE);
		}
	} else {
		/* Found same hashvalue/key.  Add entry after that one. */
		save = prev->next;
		prev->next = new db_index_entry(hval, i, recnum, prev->next);
		if (prev->next == NULL) {
			prev->next = save; // restore previous state
			FATAL3(
			"db_index_entry::add: cannot allocate space for entry",
			DB_MEMORY_LIMIT, FALSE);
		}
	}

	return (TRUE);
}

/* Print this entry to stdout. */
void
db_index_entry::print()
{
	if (key != NULL) {
			key->print();
			printf("\t");
		}
	printf(": %d\n", location);
}

/* Print bucket starting with this entry. */
void
db_index_entry::print_all()
{
	db_index_entry *np;
	for (np = this; np != NULL; np = np->next) {
		np->print();
		}
}

/* Print result list starting with this entry. */
void
db_index_entry::print_results()
{
	db_index_entry *np;
	for (np = this; np != NULL; np = np->next_result) {
		np->print();
		}
}