xref: /onnv-gate/usr/src/lib/libnisdb/db_scheme.cc (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	db_scheme.cc
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  *	Copyright (c) 1988-2000 Sun Microsystems, Inc.
26*0Sstevel@tonic-gate  *	All Rights Reserved.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include "db_headers.h"
33*0Sstevel@tonic-gate #include "db_scheme.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include "nisdb_mt.h"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  *  Constructor:  create new scheme by making copy of 'orig'.
39*0Sstevel@tonic-gate  * All items within old scheme are also copied (i.e. no shared pointers).
40*0Sstevel@tonic-gate */
db_scheme(db_scheme * orig)41*0Sstevel@tonic-gate db_scheme::db_scheme(db_scheme* orig)
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate 	int numkeys, i;
44*0Sstevel@tonic-gate 	keys.keys_len = 0;
45*0Sstevel@tonic-gate 	keys.keys_val = NULL;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 	if (orig == NULL) {
48*0Sstevel@tonic-gate 		WARNING("db_scheme::db_scheme: null original db_scheme");
49*0Sstevel@tonic-gate 		return;
50*0Sstevel@tonic-gate 	}
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	READLOCKV(orig, "r orig db_scheme::db_scheme");
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	numkeys = this->keys.keys_len = orig->keys.keys_len;
55*0Sstevel@tonic-gate 	db_key_desc * descols = this->keys.keys_val = new db_key_desc[numkeys];
56*0Sstevel@tonic-gate 	db_key_desc * srccols = orig->keys.keys_val;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	if (descols == NULL) {
59*0Sstevel@tonic-gate 		clear_columns(0);
60*0Sstevel@tonic-gate 		READUNLOCKV(orig, "ru orig db_scheme::db_scheme");
61*0Sstevel@tonic-gate 		FATAL("db_scheme::db_scheme: cannot allocate space for columns",
62*0Sstevel@tonic-gate 		DB_MEMORY_LIMIT);
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	for (i = 0; i < numkeys; i++) {
66*0Sstevel@tonic-gate 		if (srccols[i].key_name == NULL) {
67*0Sstevel@tonic-gate 			clear_columns(i);
68*0Sstevel@tonic-gate 			WARNING("db_scheme::db_scheme: null column name");
69*0Sstevel@tonic-gate 			READUNLOCKV(orig, "ru orig db_scheme::db_scheme");
70*0Sstevel@tonic-gate 			return;
71*0Sstevel@tonic-gate 		}
72*0Sstevel@tonic-gate 		descols[i].key_name = new item(srccols[i].key_name);
73*0Sstevel@tonic-gate 		if (descols[i].key_name == NULL) {
74*0Sstevel@tonic-gate 			clear_columns(i);
75*0Sstevel@tonic-gate 			READUNLOCKV(orig, "ru orig db_scheme::db_scheme");
76*0Sstevel@tonic-gate 			FATAL(
77*0Sstevel@tonic-gate 		"db_scheme::db_scheme: cannot allocate space for column names",
78*0Sstevel@tonic-gate 		DB_MEMORY_LIMIT);
79*0Sstevel@tonic-gate 		}
80*0Sstevel@tonic-gate 		descols[i].key_flags = srccols[i].key_flags;
81*0Sstevel@tonic-gate 		descols[i].where = srccols[i].where;
82*0Sstevel@tonic-gate 		descols[i].store_type = srccols[i].store_type;
83*0Sstevel@tonic-gate 		descols[i].column_number = srccols[i].column_number;
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 	this->max_columns = orig->max_columns;
86*0Sstevel@tonic-gate 	this->data = orig->data;
87*0Sstevel@tonic-gate 	READUNLOCKV(orig, "ru orig db_scheme::db_scheme");
88*0Sstevel@tonic-gate 	INITRW(scheme);
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /* Constructor:  create new sheme by using information in 'zdesc'. */
db_scheme(table_obj * zdesc)92*0Sstevel@tonic-gate db_scheme::db_scheme(table_obj *zdesc)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	keys.keys_len = 0;
95*0Sstevel@tonic-gate 	keys.keys_val = NULL;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	if (zdesc == NULL) {
98*0Sstevel@tonic-gate 		WARNING("db_scheme::db_scheme: null table obj");
99*0Sstevel@tonic-gate 		return;
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	max_columns = zdesc->ta_maxcol;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	/* find out how many searchable columns */
105*0Sstevel@tonic-gate 	int total_cols = zdesc->ta_cols.ta_cols_len;
106*0Sstevel@tonic-gate 	table_col * zcols = zdesc->ta_cols.ta_cols_val;
107*0Sstevel@tonic-gate 	int count = 0, i;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if (zcols == NULL) {
110*0Sstevel@tonic-gate 		WARNING("db_scheme::db_scheme: no columns in nis table obj");
111*0Sstevel@tonic-gate 		return;
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* find out number of indices  */
115*0Sstevel@tonic-gate 	for (i = 0; i < total_cols; i++) {
116*0Sstevel@tonic-gate 		if (zcols[i].tc_flags&TA_SEARCHABLE)
117*0Sstevel@tonic-gate 			++count;
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 	if (count == 0) {
120*0Sstevel@tonic-gate 		WARNING(
121*0Sstevel@tonic-gate 		"db_scheme::db_scheme: no searchable columns in nis table obj");
122*0Sstevel@tonic-gate 		return;
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	keys.keys_len = count;
126*0Sstevel@tonic-gate 	db_key_desc * scols = keys.keys_val = new db_key_desc[count];
127*0Sstevel@tonic-gate 	if (scols == NULL) {
128*0Sstevel@tonic-gate 		clear_columns(0);
129*0Sstevel@tonic-gate 		FATAL("db_scheme::db_scheme: cannot allocate space for keys",
130*0Sstevel@tonic-gate 			DB_MEMORY_LIMIT);
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	int keynum = 0;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	for (i = 0; i < total_cols; i++) {
135*0Sstevel@tonic-gate 		if (zcols[i].tc_flags&TA_SEARCHABLE) {
136*0Sstevel@tonic-gate 			if (zcols[i].tc_name == NULL) {
137*0Sstevel@tonic-gate 				clear_columns(keynum);
138*0Sstevel@tonic-gate 				WARNING(
139*0Sstevel@tonic-gate 	    "db_scheme::db_scheme: searchable column cannot have null name");
140*0Sstevel@tonic-gate 				return;
141*0Sstevel@tonic-gate 			}
142*0Sstevel@tonic-gate 			scols[keynum].key_name = new item(zcols[i].tc_name,
143*0Sstevel@tonic-gate 					strlen(zcols[i].tc_name));
144*0Sstevel@tonic-gate 			if (scols[keynum].key_name == NULL) {
145*0Sstevel@tonic-gate 				clear_columns(keynum);
146*0Sstevel@tonic-gate 				FATAL(
147*0Sstevel@tonic-gate 		    "db_scheme::db_scheme: cannot allocate space for key names",
148*0Sstevel@tonic-gate 		    DB_MEMORY_LIMIT);
149*0Sstevel@tonic-gate 			}
150*0Sstevel@tonic-gate 			scols[keynum].key_flags = zcols[i].tc_flags;
151*0Sstevel@tonic-gate 			scols[keynum].column_number = i;
152*0Sstevel@tonic-gate 			scols[keynum].where.max_len = NIS_MAXATTRVAL;
153*0Sstevel@tonic-gate 			scols[keynum].where.start_column = 0;
154*0Sstevel@tonic-gate 			/* don't care about position information for now */
155*0Sstevel@tonic-gate 			++keynum;	/* advance to next key number */
156*0Sstevel@tonic-gate 		}
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 	if (keynum != count) {		/* something is wrong */
159*0Sstevel@tonic-gate 		clear_columns(keynum);
160*0Sstevel@tonic-gate 		WARNING(
161*0Sstevel@tonic-gate 	    "db_scheme::db_scheme: incorrect number of  searchable columns");
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 	INITRW(scheme);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate void
clear_columns(int numkeys)167*0Sstevel@tonic-gate db_scheme::clear_columns(int numkeys)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 		int j;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 		WRITELOCKV(this, "w db_scheme::clear_columns");
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		db_key_desc * cols = keys.keys_val;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		if (cols) {
176*0Sstevel@tonic-gate 			for (j = 0; j < numkeys; j++) {
177*0Sstevel@tonic-gate 				if (cols[j].key_name)
178*0Sstevel@tonic-gate 					delete cols[j].key_name;
179*0Sstevel@tonic-gate 			}
180*0Sstevel@tonic-gate 			delete cols;
181*0Sstevel@tonic-gate 			keys.keys_val = NULL;
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 		keys.keys_len = 0;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 		WRITEUNLOCKV(this, "wu db_scheme::clear_columns");
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate /* Destructor:  delete all keys associated with scheme and scheme itself. */
~db_scheme()189*0Sstevel@tonic-gate db_scheme::~db_scheme()
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	WRITELOCKV(this, "w db_scheme::~db_scheme");
192*0Sstevel@tonic-gate 	clear_columns(keys.keys_len);
193*0Sstevel@tonic-gate 	DESTROYRW(scheme);
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate  * Predicate:  return whether given string is one of the index names
198*0Sstevel@tonic-gate  * this scheme.  If so, return in 'result' the index's number.
199*0Sstevel@tonic-gate */
200*0Sstevel@tonic-gate bool_t
find_index(char * purportedname,int * result)201*0Sstevel@tonic-gate db_scheme::find_index(char *purportedname, int *result)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	if (purportedname) {
204*0Sstevel@tonic-gate 		int i;
205*0Sstevel@tonic-gate 		int plen;
206*0Sstevel@tonic-gate 		plen = strlen(purportedname);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 		READLOCK(this, FALSE, "r db_scheme::find_index");
209*0Sstevel@tonic-gate 		for (i = 0; i < keys.keys_len; i++) {
210*0Sstevel@tonic-gate 			if (keys.keys_val[i].key_name->equal(purportedname,
211*0Sstevel@tonic-gate 								plen, TRUE)) {
212*0Sstevel@tonic-gate 				if (result) *result = i;
213*0Sstevel@tonic-gate 				READUNLOCK(this, TRUE,
214*0Sstevel@tonic-gate 					"ru db_scheme::find_index");
215*0Sstevel@tonic-gate 				return (TRUE);
216*0Sstevel@tonic-gate 			}
217*0Sstevel@tonic-gate 		}
218*0Sstevel@tonic-gate 		READUNLOCK(this, FALSE, "ru db_scheme::find_index");
219*0Sstevel@tonic-gate 	}
220*0Sstevel@tonic-gate 	return (FALSE);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate /* Print out description of table. */
224*0Sstevel@tonic-gate void
print()225*0Sstevel@tonic-gate db_scheme::print()
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	int i;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	READLOCKV(this, "r db_scheme::print");
230*0Sstevel@tonic-gate 	for (i = 0; i < keys.keys_len; i++) {
231*0Sstevel@tonic-gate 		keys.keys_val[i].key_name->print();
232*0Sstevel@tonic-gate 		printf(
233*0Sstevel@tonic-gate 	"\tcolumn=%d, flags=0x%x, key record position=%d, max length=%d\n",
234*0Sstevel@tonic-gate 			keys.keys_val[i].column_number,
235*0Sstevel@tonic-gate 			keys.keys_val[i].key_flags,
236*0Sstevel@tonic-gate 			keys.keys_val[i].where.start_column,
237*0Sstevel@tonic-gate 			keys.keys_val[i].where.max_len);
238*0Sstevel@tonic-gate 		printf("\tdata record position=%d, max length=%d\n",
239*0Sstevel@tonic-gate 			data.where.start_column, data.where.max_len);
240*0Sstevel@tonic-gate 	}
241*0Sstevel@tonic-gate 	printf("\tmaximum number of columns=%d\n", max_columns);
242*0Sstevel@tonic-gate 	READUNLOCKV(this, "ru db_scheme::print");
243*0Sstevel@tonic-gate }
244