xref: /illumos-gate/usr/src/lib/libnisdb/db_index.cc (revision 439b932b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	db_index.cc
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  *  Copyright 1988-2002 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  *  Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <malloc.h>
317c478bd9Sstevel@tonic-gate #include "db_headers.h"
327c478bd9Sstevel@tonic-gate #include "db_index.h"
337c478bd9Sstevel@tonic-gate #include "db_pickle.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
367c478bd9Sstevel@tonic-gate #include "nisdb_rw.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static int hashsizes[] = {		/* hashtable sizes */
397c478bd9Sstevel@tonic-gate 	11,
407c478bd9Sstevel@tonic-gate 	113,
417c478bd9Sstevel@tonic-gate 	337,
427c478bd9Sstevel@tonic-gate 	977,
437c478bd9Sstevel@tonic-gate 	2053,
447c478bd9Sstevel@tonic-gate 	4073,
457c478bd9Sstevel@tonic-gate 	8011,
467c478bd9Sstevel@tonic-gate 	16001,
477c478bd9Sstevel@tonic-gate 	0
487c478bd9Sstevel@tonic-gate };
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate // prevents wrap around numbers from being passed
517c478bd9Sstevel@tonic-gate #define	CALLOC_LIMIT 536870911
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* Constructor: creates empty index. */
db_index()547c478bd9Sstevel@tonic-gate db_index::db_index()
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	tab = NULL;
577c478bd9Sstevel@tonic-gate 	table_size = 0;
587c478bd9Sstevel@tonic-gate 	count = 0;
597c478bd9Sstevel@tonic-gate 	case_insens = FALSE;
607c478bd9Sstevel@tonic-gate 	INITRW(index);
617c478bd9Sstevel@tonic-gate /*  grow(); */
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* Destructor: deletes index, including all associated db_index_entry. */
~db_index()667c478bd9Sstevel@tonic-gate db_index::~db_index()
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_index::~db_index");
697c478bd9Sstevel@tonic-gate 	reset();
707c478bd9Sstevel@tonic-gate 	DESTROYRW(index);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* Get rid of table and all associated entries, and reset counters */
747c478bd9Sstevel@tonic-gate void
reset()757c478bd9Sstevel@tonic-gate db_index::reset()
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	db_index_entry * curr, *n;
787c478bd9Sstevel@tonic-gate 	int i;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_index::reset");
817c478bd9Sstevel@tonic-gate 	/* Add sanity test in case table was corrupted */
827c478bd9Sstevel@tonic-gate 	if (tab != NULL) {
837c478bd9Sstevel@tonic-gate 		for (i = 0; i < table_size; i++) {	// go through table
847c478bd9Sstevel@tonic-gate 			curr = tab[i];
857c478bd9Sstevel@tonic-gate 			while (curr != NULL) {		// go through bucket
867c478bd9Sstevel@tonic-gate 				n = curr->getnextentry();
877c478bd9Sstevel@tonic-gate 				delete curr;
887c478bd9Sstevel@tonic-gate 				curr = n;
897c478bd9Sstevel@tonic-gate 			}
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	delete tab;				// get rid of table itself
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	tab = NULL;
967c478bd9Sstevel@tonic-gate 	table_size = count = 0;
977c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "wu db_index::reset");
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Initialize index according to the specification of the key descriptor
1037c478bd9Sstevel@tonic-gate  * Currently, only affects case_insens flag of index.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate void
init(db_key_desc * k)1067c478bd9Sstevel@tonic-gate db_index::init(db_key_desc * k)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_index::init");
1097c478bd9Sstevel@tonic-gate 	if ((k->key_flags)&DB_KEY_CASE)
1107c478bd9Sstevel@tonic-gate 		case_insens = TRUE;
1117c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "wu db_index::init");
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* Returns the next size to use for the hash table */
1157c478bd9Sstevel@tonic-gate static long unsigned
get_next_hashsize(long unsigned oldsize)1167c478bd9Sstevel@tonic-gate get_next_hashsize(long unsigned oldsize)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	long unsigned newsize = 0, n;
1197c478bd9Sstevel@tonic-gate 	if (oldsize == 0)
1207c478bd9Sstevel@tonic-gate 		newsize = hashsizes[0];
1217c478bd9Sstevel@tonic-gate 	else {
1227c478bd9Sstevel@tonic-gate 		for (n = 0; newsize = hashsizes[n++]; )
1237c478bd9Sstevel@tonic-gate 			if (oldsize == newsize) {
1247c478bd9Sstevel@tonic-gate 				newsize = hashsizes[n];	/* get next size */
1257c478bd9Sstevel@tonic-gate 				break;
1267c478bd9Sstevel@tonic-gate 			}
1277c478bd9Sstevel@tonic-gate 		if (newsize == 0)
1287c478bd9Sstevel@tonic-gate 			newsize = oldsize * 2 + 1;	/* just double */
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	return (newsize);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Grow the current hashtable upto the next size.
1357c478bd9Sstevel@tonic-gate  *    The contents of the existing hashtable is copied to the new one and
1367c478bd9Sstevel@tonic-gate  *    relocated according to its hashvalue relative to the new size.
1377c478bd9Sstevel@tonic-gate  *    Old table is deleted after the relocation.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate void
grow()1407c478bd9Sstevel@tonic-gate db_index::grow()
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	long unsigned oldsize = table_size, i;
1437c478bd9Sstevel@tonic-gate 	db_index_entry_p * oldtab = tab;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_index::grow");
1467c478bd9Sstevel@tonic-gate 	table_size = get_next_hashsize(table_size);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #ifdef DEBUG
1497c478bd9Sstevel@tonic-gate 	if (debug > 3)
1507c478bd9Sstevel@tonic-gate 		fprintf(ddt, "savehash GROWING to %d\n", table_size);
1517c478bd9Sstevel@tonic-gate #endif
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (table_size > CALLOC_LIMIT) {
1547c478bd9Sstevel@tonic-gate 		table_size = oldsize;
1557c478bd9Sstevel@tonic-gate 		WRITEUNLOCKV(this,
1567c478bd9Sstevel@tonic-gate 			"wu db_index::grow: table size exceeds calloc limit");
1577c478bd9Sstevel@tonic-gate 		FATAL("db_index::grow: table size exceeds calloc limit",
1587c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if ((tab = (db_index_entry_p*)
1627c478bd9Sstevel@tonic-gate 		calloc((unsigned int) table_size,
1637c478bd9Sstevel@tonic-gate 			sizeof (db_index_entry_p))) == NULL) {
1647c478bd9Sstevel@tonic-gate 		tab = oldtab;		// restore previous table info
1657c478bd9Sstevel@tonic-gate 		table_size = oldsize;
1667c478bd9Sstevel@tonic-gate 		WRITEUNLOCKV(this,
1677c478bd9Sstevel@tonic-gate 			"wu db_index::grow: cannot allocate space");
1687c478bd9Sstevel@tonic-gate 		FATAL("db_index::grow: cannot allocate space", DB_MEMORY_LIMIT);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (oldtab != NULL) {		// must transfer contents of old to new
1727c478bd9Sstevel@tonic-gate 		for (i = 0; i < oldsize; i++) {
1737c478bd9Sstevel@tonic-gate 			oldtab[i]->relocate(tab, table_size);
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 		delete oldtab;		// delete old hashtable
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "wu db_index::grow");
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * Look up given index value in hashtable.
1827c478bd9Sstevel@tonic-gate  * Return pointer to db_index_entries that match the given value, linked
1837c478bd9Sstevel@tonic-gate  * via the 'next_result' pointer.  Return in 'how_many_found' the size
1847c478bd9Sstevel@tonic-gate  * of this list. Return NULL if not found.
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate db_index_entry *
lookup(item * index_value,long * how_many_found,db_table * table,bool_t checkTTL)1877c478bd9Sstevel@tonic-gate db_index::lookup(item *index_value, long *how_many_found,
1887c478bd9Sstevel@tonic-gate 		db_table *table, bool_t checkTTL)
1897c478bd9Sstevel@tonic-gate {
190*439b932bSToomas Soome 	unsigned long hval;
1917c478bd9Sstevel@tonic-gate 	unsigned long bucket;
1927c478bd9Sstevel@tonic-gate 	db_index_entry	*ret;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r db_index::lookup");
1957c478bd9Sstevel@tonic-gate 	if (index_value == NULL || table_size == 0 || tab == NULL) {
1967c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL, "ru db_index::lookup");
1977c478bd9Sstevel@tonic-gate 		return (NULL);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	hval = index_value->get_hashval(case_insens);
2007c478bd9Sstevel@tonic-gate 	bucket = hval % table_size;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	db_index_entry_p fst = tab[bucket ];
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (fst != NULL)
2057c478bd9Sstevel@tonic-gate 		ret = fst->lookup(case_insens, hval,
2067c478bd9Sstevel@tonic-gate 					index_value, how_many_found);
2077c478bd9Sstevel@tonic-gate 	else
2087c478bd9Sstevel@tonic-gate 		ret = NULL;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (ret != NULL && checkTTL && table != NULL) {
2117c478bd9Sstevel@tonic-gate 		if (!table->cacheValid(ret->getlocation()))
2127c478bd9Sstevel@tonic-gate 			ret = NULL;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "ru db_index::lookup");
2167c478bd9Sstevel@tonic-gate 	return (ret);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * Remove the entry with the given index value and location 'recnum'.
2217c478bd9Sstevel@tonic-gate  * If successful, return DB_SUCCESS; otherwise DB_NOTUNIQUE if index_value
2227c478bd9Sstevel@tonic-gate  * is null; DB_NOTFOUND if entry is not found.
2237c478bd9Sstevel@tonic-gate  * If successful, decrement count of number of entries in hash table.
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate db_status
remove(item * index_value,entryp recnum)2267c478bd9Sstevel@tonic-gate db_index::remove(item* index_value, entryp recnum)
2277c478bd9Sstevel@tonic-gate {
228*439b932bSToomas Soome 	unsigned long hval;
2297c478bd9Sstevel@tonic-gate 	unsigned long bucket;
230*439b932bSToomas Soome 	db_index_entry *fst;
2317c478bd9Sstevel@tonic-gate 	db_status	ret;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	if (index_value == NULL)
2347c478bd9Sstevel@tonic-gate 		return (DB_NOTUNIQUE);
2357c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_index::remove");
2367c478bd9Sstevel@tonic-gate 	if (table_size == 0 || tab == NULL) {
2377c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_NOTFOUND, "wu db_index::remove");
2387c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	hval = index_value->get_hashval(case_insens);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	bucket = hval % table_size;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	fst = tab[bucket];
2457c478bd9Sstevel@tonic-gate 	if (fst == NULL)
2467c478bd9Sstevel@tonic-gate 		ret = DB_NOTFOUND;
2477c478bd9Sstevel@tonic-gate 	else if (fst->remove(&tab[bucket], case_insens, hval, index_value,
2487c478bd9Sstevel@tonic-gate 			recnum)) {
2497c478bd9Sstevel@tonic-gate 		--count;
2507c478bd9Sstevel@tonic-gate 		ret = DB_SUCCESS;
2517c478bd9Sstevel@tonic-gate 	} else
2527c478bd9Sstevel@tonic-gate 		ret = DB_NOTFOUND;
2537c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, ret, "wu db_index::remove");
2547c478bd9Sstevel@tonic-gate 	return (ret);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * Add a new index entry with the given index value and location 'recnum'.
2597c478bd9Sstevel@tonic-gate  * Return DB_NOTUNIQUE, if entry with identical index_value and recnum
2607c478bd9Sstevel@tonic-gate  * already exists.  If entry is added, return DB_SUCCESS.
2617c478bd9Sstevel@tonic-gate  * Increment count of number of entries in index table and grow table
2627c478bd9Sstevel@tonic-gate  * if number of entries equals size of table.
2637c478bd9Sstevel@tonic-gate  * Note that a copy of index_value is made for new entry.
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate db_status
add(item * index_value,entryp recnum)2667c478bd9Sstevel@tonic-gate db_index::add(item* index_value, entryp recnum)
2677c478bd9Sstevel@tonic-gate {
268*439b932bSToomas Soome 	unsigned long hval;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (index_value == NULL)
2717c478bd9Sstevel@tonic-gate 		return (DB_NOTUNIQUE);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	hval = index_value->get_hashval(case_insens);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_index::add");
2767c478bd9Sstevel@tonic-gate 	if (tab == NULL) grow();
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	db_index_entry_p fst, newbucket;
2797c478bd9Sstevel@tonic-gate 	unsigned long bucket;
2807c478bd9Sstevel@tonic-gate 	bucket = hval %table_size;
2817c478bd9Sstevel@tonic-gate 	fst = tab[bucket];
2827c478bd9Sstevel@tonic-gate 	if (fst == NULL)  { /* Empty bucket */
2837c478bd9Sstevel@tonic-gate 		if ((newbucket = new db_index_entry(hval, index_value,
2847c478bd9Sstevel@tonic-gate 				recnum, tab[bucket])) == NULL) {
2857c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, DB_MEMORY_LIMIT,
2867c478bd9Sstevel@tonic-gate 				"wu db_index::add");
2877c478bd9Sstevel@tonic-gate 			FATAL3("db_index::add: cannot allocate space",
2887c478bd9Sstevel@tonic-gate 				DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 		tab[bucket] = newbucket;
2917c478bd9Sstevel@tonic-gate 	} else if (fst->add(&tab[bucket], case_insens,
2927c478bd9Sstevel@tonic-gate 				hval, index_value, recnum)) {
2937c478bd9Sstevel@tonic-gate 		/* do nothing */
2947c478bd9Sstevel@tonic-gate 	} else {
2957c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_NOTUNIQUE, "wu db_index::add");
2967c478bd9Sstevel@tonic-gate 		return (DB_NOTUNIQUE);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/* increase hash table size if number of entries equals table size */
3007c478bd9Sstevel@tonic-gate 	if (++count > table_size)
3017c478bd9Sstevel@tonic-gate 		grow();
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_SUCCESS, "wu db_index::add");
3047c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /* ************************* pickle_index ********************* */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /* Does the actual writing to/from file specific for db_index structure. */
3107c478bd9Sstevel@tonic-gate static bool_t
transfer_aux(XDR * x,pptr ip)3117c478bd9Sstevel@tonic-gate transfer_aux(XDR* x, pptr ip)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	return (xdr_db_index(x, (db_index*) ip));
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate class pickle_index: public pickle_file {
3177c478bd9Sstevel@tonic-gate     public:
pickle_index(char * f,pickle_mode m)3187c478bd9Sstevel@tonic-gate 	pickle_index(char *f, pickle_mode m) : pickle_file(f, m) {}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* Transfers db_index structure pointed to by dp to/from file. */
transfer(db_index * dp)3217c478bd9Sstevel@tonic-gate 	int transfer(db_index* dp)
3227c478bd9Sstevel@tonic-gate 		{ return (pickle_file::transfer((pptr) dp, &transfer_aux)); }
3237c478bd9Sstevel@tonic-gate };
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /* Dumps this index to named file. */
3267c478bd9Sstevel@tonic-gate int
dump(char * file)3277c478bd9Sstevel@tonic-gate db_index::dump(char *file)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	int	ret;
3307c478bd9Sstevel@tonic-gate 	pickle_index f(file, PICKLE_WRITE);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	WRITELOCK(this, -1, "w db_index::dump");
3337c478bd9Sstevel@tonic-gate 	int status =  f.transfer(this);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (status == 1)
3367c478bd9Sstevel@tonic-gate 		ret = -1; /* cannot open for write */
3377c478bd9Sstevel@tonic-gate 	else
3387c478bd9Sstevel@tonic-gate 		ret = status;
3397c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, ret, "wu db_index::dump");
340b3bec642SToomas Soome 	return (ret);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * Constructor: creates index by loading it from the specified file.
3457c478bd9Sstevel@tonic-gate  * If loading fails, creates empty index.
3467c478bd9Sstevel@tonic-gate  */
db_index(char * file)3477c478bd9Sstevel@tonic-gate db_index::db_index(char *file)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	pickle_index f(file, PICKLE_READ);
3507c478bd9Sstevel@tonic-gate 	tab = NULL;
3517c478bd9Sstevel@tonic-gate 	table_size = count = 0;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/* load new hashbuf */
3547c478bd9Sstevel@tonic-gate 	if (f.transfer(this) < 0) {
3557c478bd9Sstevel@tonic-gate 		/* Load failed; reset. */
3567c478bd9Sstevel@tonic-gate 		tab = NULL;
3577c478bd9Sstevel@tonic-gate 		table_size = count = 0;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	INITRW(index);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * Return in 'tsize' the table_size, and 'tcount' the number of entries
3667c478bd9Sstevel@tonic-gate  * in the table.
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate void
stats(long * tsize,long * tcount)3697c478bd9Sstevel@tonic-gate db_index::stats(long *tsize, long *tcount)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	READLOCKV(this, "r db_index::stats");
3727c478bd9Sstevel@tonic-gate 	*tsize = table_size;
3737c478bd9Sstevel@tonic-gate 	*tcount = count;
3747c478bd9Sstevel@tonic-gate 	READUNLOCKV(this, "ru db_index::stats");
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /* Print all entries in the table. */
3787c478bd9Sstevel@tonic-gate void
print()3797c478bd9Sstevel@tonic-gate db_index::print()
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	long i;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	READLOCKV(this, "r db_index::print");
3847c478bd9Sstevel@tonic-gate 	/* Add sanity check in case table corrupted */
3857c478bd9Sstevel@tonic-gate 	if (tab != NULL) {
3867c478bd9Sstevel@tonic-gate 		for (i = 0; i < table_size; i++) {
3877c478bd9Sstevel@tonic-gate 			if (tab[i] != NULL)
3887c478bd9Sstevel@tonic-gate 				tab[i]->print_all();
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 	READUNLOCKV(this, "ru db_index::print");
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate  * Moves an index from an xdr index. Upon completion, original index's tab
3967c478bd9Sstevel@tonic-gate  * will be NULL.
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate db_status
move_xdr_db_index(db_index * orig)4007c478bd9Sstevel@tonic-gate db_index::move_xdr_db_index(db_index *orig)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	table_size = orig->table_size;
4037c478bd9Sstevel@tonic-gate 	orig->table_size = 0;
4047c478bd9Sstevel@tonic-gate 	count = orig->count;
4057c478bd9Sstevel@tonic-gate 	orig->count = 0;
4067c478bd9Sstevel@tonic-gate 	case_insens = orig->case_insens;
4077c478bd9Sstevel@tonic-gate 	tab = orig->tab;
4087c478bd9Sstevel@tonic-gate 	orig->tab = NULL;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
4117c478bd9Sstevel@tonic-gate }
412