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
536e852a1SRaja Andra * Common Development and Distribution License (the "License").
636e852a1SRaja Andra * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate * db_mindex.cc
237c478bd9Sstevel@tonic-gate *
2436e852a1SRaja Andra * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <malloc.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include "db_headers.h"
357c478bd9Sstevel@tonic-gate #include "db.h"
367c478bd9Sstevel@tonic-gate #include "db_mindex.h"
377c478bd9Sstevel@tonic-gate #include "db_pickle.h"
387c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
397c478bd9Sstevel@tonic-gate #include "nisdb_ldap.h"
407c478bd9Sstevel@tonic-gate #include "ldap_nisdbquery.h"
417c478bd9Sstevel@tonic-gate #include "ldap_map.h"
427c478bd9Sstevel@tonic-gate #include "ldap_ruleval.h"
437c478bd9Sstevel@tonic-gate #include "ldap_scheme.h"
447c478bd9Sstevel@tonic-gate #include "ldap_parse.h"
457c478bd9Sstevel@tonic-gate #include "nis_hashitem.h"
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * Constructor: Create new table using scheme defintion supplied.
497c478bd9Sstevel@tonic-gate * (Make copy of scheme and keep it with table.)
507c478bd9Sstevel@tonic-gate */
db_mindex(db_scheme * how,char * tablePath)517c478bd9Sstevel@tonic-gate db_mindex::db_mindex(db_scheme *how, char *tablePath) : rversion()
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate noWriteThrough.flag = 0;
547c478bd9Sstevel@tonic-gate noLDAPquery.flag = 0;
557c478bd9Sstevel@tonic-gate initialLoad.flag = 0;
567c478bd9Sstevel@tonic-gate objPath.ptr = NULL;
577c478bd9Sstevel@tonic-gate init(how);
587c478bd9Sstevel@tonic-gate if (tablePath != NULL)
597c478bd9Sstevel@tonic-gate configure(tablePath);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate /* Constructor: Create empty table (no scheme, no table or indices). */
db_mindex()637c478bd9Sstevel@tonic-gate db_mindex::db_mindex() : rversion()
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate scheme = NULL;
667c478bd9Sstevel@tonic-gate table = NULL;
677c478bd9Sstevel@tonic-gate indices.indices_len = 0;
687c478bd9Sstevel@tonic-gate indices.indices_val = NULL;
697c478bd9Sstevel@tonic-gate noWriteThrough.flag = 0;
707c478bd9Sstevel@tonic-gate noLDAPquery.flag = 0;
717c478bd9Sstevel@tonic-gate initialLoad.flag = 0;
727c478bd9Sstevel@tonic-gate objPath.ptr = NULL;
737c478bd9Sstevel@tonic-gate INITRW(mindex);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
~db_mindex()767c478bd9Sstevel@tonic-gate db_mindex::~db_mindex()
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate reset(); /* get rid of data structures first */
797c478bd9Sstevel@tonic-gate DESTROYRW(mindex);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * Initialize table using information given in scheme 'how'.
847c478bd9Sstevel@tonic-gate * Record the scheme for later use (make copy of it);
857c478bd9Sstevel@tonic-gate * create the required number of indices; and create table for storing
867c478bd9Sstevel@tonic-gate * entries.
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate void
init(db_scheme * how)897c478bd9Sstevel@tonic-gate db_mindex::init(db_scheme * how)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate scheme = new db_scheme(how); // make copy
927c478bd9Sstevel@tonic-gate if (scheme == NULL)
937c478bd9Sstevel@tonic-gate FATAL("db_mindex::init: could not allocate space for scheme",
947c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT);
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if (scheme->numkeys() == 0) {
977c478bd9Sstevel@tonic-gate WARNING("db_mindex::init: empty scheme encountered");
987c478bd9Sstevel@tonic-gate /* what action should we take here? */
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate indices.indices_len = how->numkeys();
1027c478bd9Sstevel@tonic-gate db_key_desc * keys = how->keyloc();
1037c478bd9Sstevel@tonic-gate int i;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /* homogeneous indices for now */
1067c478bd9Sstevel@tonic-gate indices.indices_val = new db_index[indices.indices_len];
1077c478bd9Sstevel@tonic-gate if (indices.indices_val == NULL) {
1087c478bd9Sstevel@tonic-gate delete scheme;
1097c478bd9Sstevel@tonic-gate indices.indices_len = 0;
1107c478bd9Sstevel@tonic-gate scheme = NULL;
1117c478bd9Sstevel@tonic-gate FATAL("db_mindex::init: could not allocate space for indices",
1127c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate for (i = 0; i < indices.indices_len; i++) {
1157c478bd9Sstevel@tonic-gate indices.indices_val[i].init(&(keys[i]));
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate table = new db_table();
1187c478bd9Sstevel@tonic-gate if (table == NULL) {
1197c478bd9Sstevel@tonic-gate delete scheme;
1207c478bd9Sstevel@tonic-gate scheme = NULL;
1217c478bd9Sstevel@tonic-gate delete indices.indices_val;
1227c478bd9Sstevel@tonic-gate indices.indices_val = NULL;
1237c478bd9Sstevel@tonic-gate indices.indices_len = 0;
1247c478bd9Sstevel@tonic-gate FATAL("db_mindex::init: could not allocate space for table",
1257c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate rversion.zero();
1287c478bd9Sstevel@tonic-gate INITRW(mindex);
1297c478bd9Sstevel@tonic-gate objPath.ptr = NULL;
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /* empty associated tables associated */
1337c478bd9Sstevel@tonic-gate void
reset_tables()1347c478bd9Sstevel@tonic-gate db_mindex::reset_tables()
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate int i;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate WRITELOCKV(this, "w db_mindex::reset_tables");
1397c478bd9Sstevel@tonic-gate /* Add sanity check in case of table corruption */
1407c478bd9Sstevel@tonic-gate if (indices.indices_val != NULL) {
1417c478bd9Sstevel@tonic-gate for (i = 0; i < indices.indices_len; i++) {
1427c478bd9Sstevel@tonic-gate indices.indices_val[i].reset();
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate if (table) table->reset();
1467c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_mindex::reset_tables");
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * Return a list of index_entries that satsify the given query 'q'.
1527c478bd9Sstevel@tonic-gate * Return the size of the list in 'count'. Return NULL if list is empty.
1537c478bd9Sstevel@tonic-gate * Return in 'valid' FALSE if query is not well formed.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate db_index_entry_p
satisfy_query(db_query * q,long * count,bool_t * valid)1567c478bd9Sstevel@tonic-gate db_mindex::satisfy_query(db_query *q, long *count, bool_t *valid) {
1577c478bd9Sstevel@tonic-gate return (satisfy_query(q, count, valid, FALSE));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate db_index_entry_p
satisfy_query(db_query * q,long * count,bool_t * valid,bool_t fromLDAP)1617c478bd9Sstevel@tonic-gate db_mindex::satisfy_query(db_query *q, long *count, bool_t *valid,
1627c478bd9Sstevel@tonic-gate bool_t fromLDAP) {
1637c478bd9Sstevel@tonic-gate db_index_entry_p ret;
1647c478bd9Sstevel@tonic-gate bool_t validRequest;
1657c478bd9Sstevel@tonic-gate int queryRes;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /* Make sure we have somewhere to store the "request valid" status */
1687c478bd9Sstevel@tonic-gate if (valid == NULL)
1697c478bd9Sstevel@tonic-gate valid = &validRequest;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /* Prepare for a failed lock */
1727c478bd9Sstevel@tonic-gate *count = 0;
1737c478bd9Sstevel@tonic-gate *valid = FALSE;
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate READLOCK(this, NULL, "r db_mindex::satisfy_query");
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate * Only get data from LDAP if the caller requested it,
1797c478bd9Sstevel@tonic-gate * and if we're mapping for this table.
1807c478bd9Sstevel@tonic-gate */
1817c478bd9Sstevel@tonic-gate fromLDAP = (fromLDAP && !noLDAPquery.flag &&
1827c478bd9Sstevel@tonic-gate (table->mapping.fromLDAP ||
1837c478bd9Sstevel@tonic-gate table->mapping.objType != NIS_TABLE_OBJ));
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate * If we always fetch data from LDAP for query's, then do so now,
1877c478bd9Sstevel@tonic-gate * before invoking the "real" satisfy_query().
1887c478bd9Sstevel@tonic-gate */
1897c478bd9Sstevel@tonic-gate if (fromLDAP && table->mapping.matchFetch == mat_always) {
1907c478bd9Sstevel@tonic-gate int lockcode = 0;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate READLOCKNR(table, lockcode,
1937c478bd9Sstevel@tonic-gate "r db_mindex::satisfy_query table");
1947c478bd9Sstevel@tonic-gate if (lockcode != 0) {
1957c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
1967c478bd9Sstevel@tonic-gate return (NULL);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate queryRes = queryLDAP(q, 0, 1);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate READUNLOCKNR(table, lockcode,
2027c478bd9Sstevel@tonic-gate "ru db_mindex::satisfy_query table");
2037c478bd9Sstevel@tonic-gate if (lockcode != 0) {
2047c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
2057c478bd9Sstevel@tonic-gate return (NULL);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate if (queryRes != LDAP_SUCCESS) {
2087c478bd9Sstevel@tonic-gate /* queryLDAP() sets error codes etc. */
2097c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
2107c478bd9Sstevel@tonic-gate return (NULL);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate ret = satisfy_query_dbonly(q, count, fromLDAP ? TRUE : FALSE, valid);
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /* If we found it, or if we're not mapping, return */
2187c478bd9Sstevel@tonic-gate if (ret != NULL || !fromLDAP) {
2197c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
2207c478bd9Sstevel@tonic-gate return (ret);
2217c478bd9Sstevel@tonic-gate } else if (ret == NULL && !(*valid)) {
2227c478bd9Sstevel@tonic-gate /* No result, and the request wasn't valid */
2237c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
2247c478bd9Sstevel@tonic-gate return (NULL);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /* Get data from LDAP */
2287c478bd9Sstevel@tonic-gate if (table->mapping.matchFetch != mat_never) {
2297c478bd9Sstevel@tonic-gate queryRes = queryLDAP(q, 0, 1);
2307c478bd9Sstevel@tonic-gate } else {
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate * We'll now go on to check for an un-expired entry again,
2337c478bd9Sstevel@tonic-gate * even though we're pretty sure that won't work (already
2347c478bd9Sstevel@tonic-gate * did that, and nothing's changed). However, we accept that
2357c478bd9Sstevel@tonic-gate * slight inefficiency in the interest of keeping the code
2367c478bd9Sstevel@tonic-gate * simple; we expect 'mat_never' to be used very rarely.
2377c478bd9Sstevel@tonic-gate */
2387c478bd9Sstevel@tonic-gate queryRes = LDAP_SUCCESS;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate if (queryRes == LDAP_SUCCESS) {
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate * Check if we've got a match now. If not, try one
2447c478bd9Sstevel@tonic-gate * last time for an expired match.
2457c478bd9Sstevel@tonic-gate */
2467c478bd9Sstevel@tonic-gate ret = satisfy_query_dbonly(q, count, TRUE, valid);
2477c478bd9Sstevel@tonic-gate if (ret == NULL) {
2487c478bd9Sstevel@tonic-gate ret = satisfy_query_dbonly(q, count, FALSE, valid);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate } else {
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * Check if we have an expired entry; if so, return
2537c478bd9Sstevel@tonic-gate * it with an appropriate status.
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate ret = satisfy_query_dbonly(q, count, FALSE, valid);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate return (ret);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate db_index_entry_p
satisfy_query_dbonly(db_query * q,long * count,bool_t checkExpire,bool_t * valid)2647c478bd9Sstevel@tonic-gate db_mindex::satisfy_query_dbonly(db_query *q, long *count,
2657c478bd9Sstevel@tonic-gate bool_t checkExpire, bool_t *valid)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate db_index_entry_p oldres = NULL, newres;
2687c478bd9Sstevel@tonic-gate int i, curr_ind;
2697c478bd9Sstevel@tonic-gate long num_new, num_old = 0;
2707c478bd9Sstevel@tonic-gate int limit = q->size();
2717c478bd9Sstevel@tonic-gate db_qcomp * comps = q->queryloc();
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate if (valid) *valid = TRUE; /* True to begin with. */
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate /* Add sanity check in case table corrupted */
2767c478bd9Sstevel@tonic-gate if (indices.indices_len != 0 && indices.indices_val == NULL) {
2777c478bd9Sstevel@tonic-gate WARNING("db_mindex::satisfy_query: table has no indices");
2787c478bd9Sstevel@tonic-gate if (valid) *valid = FALSE;
2797c478bd9Sstevel@tonic-gate *count = 0;
2807c478bd9Sstevel@tonic-gate return (NULL);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate for (i = 0; i < limit; i++) {
2847c478bd9Sstevel@tonic-gate if ((curr_ind = comps[i].which_index) < indices.indices_len) {
2857c478bd9Sstevel@tonic-gate newres = indices.indices_val[curr_ind].lookup(
2867c478bd9Sstevel@tonic-gate comps[i].index_value, &num_new,
2877c478bd9Sstevel@tonic-gate table, checkExpire);
2887c478bd9Sstevel@tonic-gate if (newres == NULL) {
2897c478bd9Sstevel@tonic-gate *count = 0;
2907c478bd9Sstevel@tonic-gate return (NULL);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate if (oldres == NULL) {
2937c478bd9Sstevel@tonic-gate oldres = newres;
2947c478bd9Sstevel@tonic-gate num_old = num_new;
2957c478bd9Sstevel@tonic-gate } else {
2967c478bd9Sstevel@tonic-gate oldres = newres->join(num_new, num_old,
2977c478bd9Sstevel@tonic-gate oldres, &num_old);
2987c478bd9Sstevel@tonic-gate if (oldres == NULL) {
2997c478bd9Sstevel@tonic-gate *count = 0;
3007c478bd9Sstevel@tonic-gate return (NULL);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate } else {
3047c478bd9Sstevel@tonic-gate WARNING("db_mindex::satisfy_query: index out of range");
3057c478bd9Sstevel@tonic-gate if (valid) *valid = FALSE;
3067c478bd9Sstevel@tonic-gate *count = 0;
3077c478bd9Sstevel@tonic-gate return (NULL);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate *count = num_old;
3117c478bd9Sstevel@tonic-gate return (oldres);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate * Returns an array of size 'count' of 'entry_object_p's, pointing to
3167c478bd9Sstevel@tonic-gate * copies of entry_objects named by the result list of db_index_entries 'res'.
3177c478bd9Sstevel@tonic-gate * Sets db_status 'statp' if error encountered; otherwise, leaves it unchanged.
3187c478bd9Sstevel@tonic-gate */
3197c478bd9Sstevel@tonic-gate entry_object_p *
prepare_results(int count,db_index_entry_p res,db_status * statp)3207c478bd9Sstevel@tonic-gate db_mindex::prepare_results(int count, db_index_entry_p res, db_status *statp)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate READLOCK(this, NULL, "r db_mindex::prepare_results");
3237c478bd9Sstevel@tonic-gate READLOCK2(table, NULL, "r table db_mindex::prepare_results", this);
3247c478bd9Sstevel@tonic-gate entry_object_p * entries = new entry_object_p[count];
3257c478bd9Sstevel@tonic-gate int i;
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate if (entries == NULL) {
3287c478bd9Sstevel@tonic-gate READUNLOCK2(this, table, NULL, NULL,
3297c478bd9Sstevel@tonic-gate "ru db_mindex::prepare_results: could not allocate space",
3307c478bd9Sstevel@tonic-gate "ru table db_mindex::prepare_results: could not allocate space");
3317c478bd9Sstevel@tonic-gate FATAL3("db_mindex::prepare_results: could not allocate space",
3327c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, NULL);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) {
3367c478bd9Sstevel@tonic-gate if (res == NULL) {
3377c478bd9Sstevel@tonic-gate int j;
3387c478bd9Sstevel@tonic-gate for (j = 0; j < i; j++) // cleanup
3397c478bd9Sstevel@tonic-gate free_entry(entries[j]);
3407c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
3417c478bd9Sstevel@tonic-gate "db_mindex::prepare_results: incorrect count");
3427c478bd9Sstevel@tonic-gate *statp = DB_INTERNAL_ERROR;
3437c478bd9Sstevel@tonic-gate } else {
3447c478bd9Sstevel@tonic-gate entries[i] =
3457c478bd9Sstevel@tonic-gate new_entry(table->get_entry(res->getlocation()));
3467c478bd9Sstevel@tonic-gate res = res->getnextresult();
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate READUNLOCK2(this, table, entries, entries,
3507c478bd9Sstevel@tonic-gate "ru db_mindex::prepare_results",
3517c478bd9Sstevel@tonic-gate "ru db_mindex::prepare_results");
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate return (entries);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate * Returns a newly created db_query structure containing the index values
3587c478bd9Sstevel@tonic-gate * as obtained from the record named by 'recnum'. The record itself, along
3597c478bd9Sstevel@tonic-gate * with information on the schema definition of this table, will determine
3607c478bd9Sstevel@tonic-gate * which values are extracted from the record and placed into the result.
3617c478bd9Sstevel@tonic-gate * Returns NULL if recnum is not a valid entry.
3627c478bd9Sstevel@tonic-gate * Note that space is allocated for the query and the index values
3637c478bd9Sstevel@tonic-gate * (i.e. do not share pointers with strings in 'obj'.)
3647c478bd9Sstevel@tonic-gate */
3657c478bd9Sstevel@tonic-gate db_query *
extract_index_values_from_record(entryp recnum)3667c478bd9Sstevel@tonic-gate db_mindex::extract_index_values_from_record(entryp recnum)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate db_query *ret;
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate ret = extract_index_values_from_object(table->get_entry(recnum));
3717c478bd9Sstevel@tonic-gate return (ret);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate * Returns a newly created db_query containing the index values as
3767c478bd9Sstevel@tonic-gate * obtained from the given object. The object itself,
3777c478bd9Sstevel@tonic-gate * along with information on the scheme given, will determine
3787c478bd9Sstevel@tonic-gate * which values are extracted from the object and placed into the query.
3797c478bd9Sstevel@tonic-gate * Returns an empty query if 'obj' is not a valid entry.
3807c478bd9Sstevel@tonic-gate * Note that space is allocated for the query and the index values
3817c478bd9Sstevel@tonic-gate * (i.e. do not share pointers with strings in 'obj'.)
3827c478bd9Sstevel@tonic-gate */
3837c478bd9Sstevel@tonic-gate db_query *
extract_index_values_from_object(entry_object_p obj)3847c478bd9Sstevel@tonic-gate db_mindex::extract_index_values_from_object(entry_object_p obj)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate READLOCK(this, NULL, "r db_mindex::extract_index_values_from_object");
3877c478bd9Sstevel@tonic-gate if (scheme->numkeys() != indices.indices_len) { // probably built wrong
3887c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
3897c478bd9Sstevel@tonic-gate "number of keys (%d) does not equal number of indices (%d)",
3907c478bd9Sstevel@tonic-gate scheme->numkeys(), indices.indices_len);
3917c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL,
3927c478bd9Sstevel@tonic-gate "ru db_mindex::extract_index_values_from_object");
3937c478bd9Sstevel@tonic-gate return (new db_query()); // null query
3947c478bd9Sstevel@tonic-gate } else if (obj == NULL) {
3957c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL,
3967c478bd9Sstevel@tonic-gate "ru db_mindex::extract_index_values_from_object");
3977c478bd9Sstevel@tonic-gate return (NULL);
3987c478bd9Sstevel@tonic-gate } else {
3997c478bd9Sstevel@tonic-gate db_query* answer = new db_query(scheme, obj);
4007c478bd9Sstevel@tonic-gate if (answer) {
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate * XXX If the unlock fails, and we return NULL,
4037c478bd9Sstevel@tonic-gate * we leak 'answer'. On the other hand, if we
4047c478bd9Sstevel@tonic-gate * return 'answer', the object may remain locked,
4057c478bd9Sstevel@tonic-gate * but the caller doesn't know that anything
4067c478bd9Sstevel@tonic-gate * went wrong.
4077c478bd9Sstevel@tonic-gate */
4087c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL,
4097c478bd9Sstevel@tonic-gate "ru db_mindex::extract_index_values_from_object");
4107c478bd9Sstevel@tonic-gate return (answer);
4117c478bd9Sstevel@tonic-gate } else {
4127c478bd9Sstevel@tonic-gate FATAL3("db_mindex::extract: could not allocate space",
4137c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, NULL);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL,
4177c478bd9Sstevel@tonic-gate "ru db_mindex::extract_index_values_from_object");
4187c478bd9Sstevel@tonic-gate return (NULL);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate * Returns the first entry found in the table by setting 'answer' to
4237c478bd9Sstevel@tonic-gate * point to the a copy of entry_object. Returns DB_SUCCESS if found;
4247c478bd9Sstevel@tonic-gate * DB_NOTFOUND otherwise.
4257c478bd9Sstevel@tonic-gate */
4267c478bd9Sstevel@tonic-gate db_status
first(entryp * where,entry_object ** answer)4277c478bd9Sstevel@tonic-gate db_mindex::first(entryp *where, entry_object ** answer)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate db_status ret = DB_SUCCESS;
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate * table->first_entry() returns a pointer into the table, so
4337c478bd9Sstevel@tonic-gate * we must keep the table read locked until we've copied the
4347c478bd9Sstevel@tonic-gate * entry_object. In order to maintain lock integrity, we must
4357c478bd9Sstevel@tonic-gate * lock the db_mindex (this) before the db_table (table).
4367c478bd9Sstevel@tonic-gate */
4377c478bd9Sstevel@tonic-gate READLOCK(this, DB_LOCK_ERROR, "r db_mindex::first");
4387c478bd9Sstevel@tonic-gate READLOCK2(table, DB_LOCK_ERROR, "r table db_mindex::first", this);
4397c478bd9Sstevel@tonic-gate if (table->mapping.fromLDAP) {
4407c478bd9Sstevel@tonic-gate struct timeval now;
4417c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL);
4427c478bd9Sstevel@tonic-gate if (now.tv_sec >= table->mapping.enumExpire) {
4437c478bd9Sstevel@tonic-gate int queryRes = queryLDAP(0, 0, 1);
4447c478bd9Sstevel@tonic-gate if (queryRes == LDAP_SUCCESS)
4457c478bd9Sstevel@tonic-gate table->mapping.enumExpire = now.tv_sec +
4467c478bd9Sstevel@tonic-gate table->mapping.ttl;
4477c478bd9Sstevel@tonic-gate else {
4487c478bd9Sstevel@tonic-gate READUNLOCK2(this, table,
4497c478bd9Sstevel@tonic-gate DB_LOCK_ERROR, DB_LOCK_ERROR,
4507c478bd9Sstevel@tonic-gate "ru db_mindex::first LDAP",
4517c478bd9Sstevel@tonic-gate "ru table db_mindex::first LDAP");
4527c478bd9Sstevel@tonic-gate return (DB_INTERNAL_ERROR);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate entry_object_p ptr = table->first_entry(where);
4577c478bd9Sstevel@tonic-gate if (ptr == NULL)
4587c478bd9Sstevel@tonic-gate ret = DB_NOTFOUND;
4597c478bd9Sstevel@tonic-gate else
4607c478bd9Sstevel@tonic-gate *answer = new_entry(ptr);
4617c478bd9Sstevel@tonic-gate READUNLOCK2(this, table, ret, ret,
4627c478bd9Sstevel@tonic-gate "ru db_mindex::first", "ru table db_mindex::first");
4637c478bd9Sstevel@tonic-gate return (ret);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate * Returns the next entry in the table after 'previous' by setting 'answer' to
4687c478bd9Sstevel@tonic-gate * point to copy of the entry_object. Returns DB_SUCCESS if 'previous' is
4697c478bd9Sstevel@tonic-gate * valid and next entry is found; DB_NOTFOUND otherwise. Sets 'where' to
4707c478bd9Sstevel@tonic-gate * location of where entry is found for input as subsequent 'next' operation.
4717c478bd9Sstevel@tonic-gate */
4727c478bd9Sstevel@tonic-gate db_status
next(entryp previous,entryp * where,entry_object ** answer)4737c478bd9Sstevel@tonic-gate db_mindex::next(entryp previous, entryp *where, entry_object **answer)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate db_status ret = DB_SUCCESS;
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate READLOCK(this, DB_LOCK_ERROR, "r db_mindex::next");
4787c478bd9Sstevel@tonic-gate READLOCK2(table, DB_LOCK_ERROR, "r db_mindex::next", this);
4797c478bd9Sstevel@tonic-gate if (!(table->entry_exists_p(previous)))
4807c478bd9Sstevel@tonic-gate ret = DB_NOTFOUND;
4817c478bd9Sstevel@tonic-gate else {
4827c478bd9Sstevel@tonic-gate entry_object * ptr = table->next_entry(previous, where);
4837c478bd9Sstevel@tonic-gate if (ptr == NULL)
4847c478bd9Sstevel@tonic-gate ret = DB_NOTFOUND;
4857c478bd9Sstevel@tonic-gate else
4867c478bd9Sstevel@tonic-gate *answer = new_entry(ptr);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate READUNLOCK2(this, table, ret, ret,
4897c478bd9Sstevel@tonic-gate "ru db_mindex::next", "ru table db_mindex::next");
4907c478bd9Sstevel@tonic-gate return (ret);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate static void
delete_result_list(db_next_index_desc * orig)4947c478bd9Sstevel@tonic-gate delete_result_list(db_next_index_desc* orig)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate db_next_index_desc* curr, *save_next;
4977c478bd9Sstevel@tonic-gate for (curr = orig; curr != NULL; 0) {
4987c478bd9Sstevel@tonic-gate save_next = curr->next;
4997c478bd9Sstevel@tonic-gate delete curr;
5007c478bd9Sstevel@tonic-gate curr = save_next;
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate static db_next_index_desc *
copy_result_list(db_index_entry * orig)5067c478bd9Sstevel@tonic-gate copy_result_list(db_index_entry* orig)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate db_next_index_desc *head = NULL, *curr;
5097c478bd9Sstevel@tonic-gate db_index_entry *current;
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate for (current = orig; current != NULL;
5127c478bd9Sstevel@tonic-gate current = current->getnextresult()) {
5137c478bd9Sstevel@tonic-gate curr = new db_next_index_desc(current->getlocation(), head);
5147c478bd9Sstevel@tonic-gate if (curr == NULL) {
5157c478bd9Sstevel@tonic-gate FATAL3(
5167c478bd9Sstevel@tonic-gate "db_mindex::copy_result_list: could not allocate space",
5177c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, NULL);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate head = curr; // list is actually reversed
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate return (head);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate * Delete the given list of results; used when no longer interested in
5267c478bd9Sstevel@tonic-gate * the results of the first/next query that returned this list.
5277c478bd9Sstevel@tonic-gate */
5287c478bd9Sstevel@tonic-gate db_status
reset_next(db_next_index_desc * orig)5297c478bd9Sstevel@tonic-gate db_mindex::reset_next(db_next_index_desc *orig)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate if (orig == NULL)
5327c478bd9Sstevel@tonic-gate return (DB_NOTFOUND);
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate delete_result_list(orig);
5357c478bd9Sstevel@tonic-gate return (DB_SUCCESS);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate * Finds entry that satisfy the query 'q'. Returns the first answer by
5407c478bd9Sstevel@tonic-gate * setting the pointer 'answer' to point to a copy of it. 'where' is set
5417c478bd9Sstevel@tonic-gate * so that the other answers could be gotten by passing 'where' to 'next'
5427c478bd9Sstevel@tonic-gate * successively. Note that the answer is a pointer to a copy of the entry.
5437c478bd9Sstevel@tonic-gate * Returns DB_SUCCESS if search was successful; DB_NOTFOUND otherwise.
5447c478bd9Sstevel@tonic-gate */
5457c478bd9Sstevel@tonic-gate db_status
first(db_query * q,db_next_index_desc ** where,entry_object ** answer)5467c478bd9Sstevel@tonic-gate db_mindex::first(db_query *q,
5477c478bd9Sstevel@tonic-gate db_next_index_desc **where, entry_object ** answer)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate READLOCK(this, DB_LOCK_ERROR, "r db_mindex::first");
5507c478bd9Sstevel@tonic-gate READLOCK2(table, DB_LOCK_ERROR, "r table db_mindex::first", this);
5517c478bd9Sstevel@tonic-gate long count;
5527c478bd9Sstevel@tonic-gate bool_t valid_query;
5537c478bd9Sstevel@tonic-gate db_status ret = DB_SUCCESS;
5547c478bd9Sstevel@tonic-gate db_index_entry * rp = satisfy_query(q, &count, &valid_query, TRUE);
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate if (valid_query != TRUE)
5577c478bd9Sstevel@tonic-gate ret = DB_BADQUERY;
5587c478bd9Sstevel@tonic-gate else if (rp == NULL) {
5597c478bd9Sstevel@tonic-gate *answer = NULL;
5607c478bd9Sstevel@tonic-gate ret = DB_NOTFOUND;
5617c478bd9Sstevel@tonic-gate } else {
5627c478bd9Sstevel@tonic-gate *where = copy_result_list(rp);
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate entry_object_p ptr = table->get_entry((*where)->location);
5657c478bd9Sstevel@tonic-gate if (ptr == NULL)
5667c478bd9Sstevel@tonic-gate ret = DB_NOTFOUND;
5677c478bd9Sstevel@tonic-gate else
5687c478bd9Sstevel@tonic-gate *answer = new_entry(ptr);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate READUNLOCK2(this, table, ret, ret,
5717c478bd9Sstevel@tonic-gate "ru db_mindex::first", "ru table db_mindex::first");
5727c478bd9Sstevel@tonic-gate return (ret);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate * Returns the next entry in the table after 'previous' by setting 'answer' to
5777c478bd9Sstevel@tonic-gate * point to copy of the entry_object. Next is next in chain of answers found
5787c478bd9Sstevel@tonic-gate * in previous first search with query. Returns DB_SUCCESS if 'previous' is
5797c478bd9Sstevel@tonic-gate * valid and next entry is found; DB_NOTFOUND otherwise. Sets 'where' to
5807c478bd9Sstevel@tonic-gate * location of where entry is found for input as subsequent 'next' operation.
5817c478bd9Sstevel@tonic-gate */
5827c478bd9Sstevel@tonic-gate db_status
next(db_next_index_desc * previous,db_next_index_desc ** where,entry_object ** answer)5837c478bd9Sstevel@tonic-gate db_mindex::next(db_next_index_desc *previous, db_next_index_desc **where,
5847c478bd9Sstevel@tonic-gate entry_object **answer)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate READLOCK(this, DB_LOCK_ERROR, "r db_mindex::next");
5877c478bd9Sstevel@tonic-gate READLOCK2(table, DB_LOCK_ERROR, "r table db_mindex::next", this);
5887c478bd9Sstevel@tonic-gate db_status ret = DB_SUCCESS;
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate if (previous == NULL)
5917c478bd9Sstevel@tonic-gate ret = DB_NOTFOUND;
5927c478bd9Sstevel@tonic-gate else {
5937c478bd9Sstevel@tonic-gate // should further check validity of 'previous' pointer
5947c478bd9Sstevel@tonic-gate *where = previous->next;
5957c478bd9Sstevel@tonic-gate delete previous; // delete previous entry
5967c478bd9Sstevel@tonic-gate if (*where == NULL)
5977c478bd9Sstevel@tonic-gate ret = DB_NOTFOUND;
5987c478bd9Sstevel@tonic-gate else {
5997c478bd9Sstevel@tonic-gate entry_object * ptr =
6007c478bd9Sstevel@tonic-gate table->get_entry((*where)->location);
6017c478bd9Sstevel@tonic-gate if (ptr == NULL)
6027c478bd9Sstevel@tonic-gate ret = DB_NOTFOUND;
6037c478bd9Sstevel@tonic-gate else {
6047c478bd9Sstevel@tonic-gate *answer = new_entry(ptr);
6057c478bd9Sstevel@tonic-gate ret = DB_SUCCESS;
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate READUNLOCK2(this, table, ret, ret,
6107c478bd9Sstevel@tonic-gate "ru db_mindex::next", "ru table db_mindex::next");
6117c478bd9Sstevel@tonic-gate return (ret);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate * Finds entry that satisfy the query 'q'. Returns the answer by
6167c478bd9Sstevel@tonic-gate * setting the pointer 'rp' to point to the list of answers.
6177c478bd9Sstevel@tonic-gate * Note that the answers are pointers to the COPIES of entries.
6187c478bd9Sstevel@tonic-gate * Returns the number of answers find in 'count'.
6197c478bd9Sstevel@tonic-gate * Returns DB_SUCCESS if search found at least one answer;
6207c478bd9Sstevel@tonic-gate * returns DB_NOTFOUND if none is found.
6217c478bd9Sstevel@tonic-gate */
6227c478bd9Sstevel@tonic-gate db_status
lookup(db_query * q,long * count,entry_object_p ** result)6237c478bd9Sstevel@tonic-gate db_mindex::lookup(db_query *q, long *count, entry_object_p **result)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate bool_t valid_query;
6267c478bd9Sstevel@tonic-gate db_index_entry * rp = satisfy_query(q, count, &valid_query, TRUE);
6277c478bd9Sstevel@tonic-gate db_status stat = DB_SUCCESS;
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate if (valid_query != TRUE)
6307c478bd9Sstevel@tonic-gate return (DB_BADQUERY);
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate if (rp == NULL) {
6337c478bd9Sstevel@tonic-gate *result = NULL;
6347c478bd9Sstevel@tonic-gate return (DB_NOTFOUND);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate *result = prepare_results((int)*count, rp, &stat);
6387c478bd9Sstevel@tonic-gate
6397c478bd9Sstevel@tonic-gate return (stat);
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate * Return all entries within table. Returns the answer by
6447c478bd9Sstevel@tonic-gate * setting the pointer 'rp' to point to the list of answers.
6457c478bd9Sstevel@tonic-gate * Note that the answers are pointers to copies of the entries.
6467c478bd9Sstevel@tonic-gate * Returns the number of answers find in 'count'.
6477c478bd9Sstevel@tonic-gate * Returns DB_SUCCESS if search found at least one answer;
6487c478bd9Sstevel@tonic-gate * returns DB_NOTFOUND if none is found.
6497c478bd9Sstevel@tonic-gate */
6507c478bd9Sstevel@tonic-gate db_status
all(long * count,entry_object_p ** result)6517c478bd9Sstevel@tonic-gate db_mindex::all(long *count, entry_object_p **result)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate entry_object *ptr;
6547c478bd9Sstevel@tonic-gate entryp where;
6557c478bd9Sstevel@tonic-gate long how_many, i;
6567c478bd9Sstevel@tonic-gate int lret = 0;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate if (table == NULL) {
6597c478bd9Sstevel@tonic-gate *result = NULL;
6607c478bd9Sstevel@tonic-gate return (DB_NOTFOUND);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate READLOCK(this, DB_LOCK_ERROR, "r db_mindex::all");
6647c478bd9Sstevel@tonic-gate /* Read lock 'table' while we're traversing it */
6657c478bd9Sstevel@tonic-gate READLOCKNR(table, lret, "r table db_mindex::all");
6667c478bd9Sstevel@tonic-gate if (lret != 0) {
6677c478bd9Sstevel@tonic-gate READUNLOCK(this, DB_LOCK_ERROR, "ru db_mindex::all");
6687c478bd9Sstevel@tonic-gate return (DB_LOCK_ERROR);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate if (table->mapping.fromLDAP) {
6727c478bd9Sstevel@tonic-gate struct timeval now;
6737c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL);
6747c478bd9Sstevel@tonic-gate if (now.tv_sec >= table->mapping.enumExpire) {
6757c478bd9Sstevel@tonic-gate int queryRes = queryLDAP(0, 0, 1);
6767c478bd9Sstevel@tonic-gate if (queryRes != LDAP_SUCCESS) {
6777c478bd9Sstevel@tonic-gate READUNLOCKNR(table, lret,
6787c478bd9Sstevel@tonic-gate "ru table db_mindex::all LDAP");
6797c478bd9Sstevel@tonic-gate READUNLOCK(this, DB_LOCK_ERROR,
6807c478bd9Sstevel@tonic-gate "ru db_mindex::all LDAP");
6817c478bd9Sstevel@tonic-gate return (DB_INTERNAL_ERROR);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate if ((how_many = table->fullness()) <= 0) {
6877c478bd9Sstevel@tonic-gate /*
6887c478bd9Sstevel@tonic-gate * Set '*count' so that the caller avoids putting garbage
6897c478bd9Sstevel@tonic-gate * in an 'objects_len' field.
6907c478bd9Sstevel@tonic-gate */
6917c478bd9Sstevel@tonic-gate *count = 0;
6927c478bd9Sstevel@tonic-gate *result = NULL;
6937c478bd9Sstevel@tonic-gate READUNLOCKNR(table, lret, "ru table db_mindex::all");
6947c478bd9Sstevel@tonic-gate READUNLOCK(this, DB_NOTFOUND, "ru db_mindex::all");
6957c478bd9Sstevel@tonic-gate return (DB_NOTFOUND);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate
6987c478bd9Sstevel@tonic-gate entry_object_p * answer = new entry_object_p[how_many];
6997c478bd9Sstevel@tonic-gate if (answer == NULL) {
7007c478bd9Sstevel@tonic-gate READUNLOCKNR(table, lret, "ru table db_mindex::all");
7017c478bd9Sstevel@tonic-gate READUNLOCK(this, DB_MEMORY_LIMIT, "ru db_mindex::all");
7027c478bd9Sstevel@tonic-gate FATAL3("db_mindex::all: could not allocate space",
7037c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate
7067c478bd9Sstevel@tonic-gate *count = how_many;
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate ptr = table->first_entry(&where);
7097c478bd9Sstevel@tonic-gate if (ptr != NULL)
7107c478bd9Sstevel@tonic-gate answer[0] = new_entry(ptr);
7117c478bd9Sstevel@tonic-gate else {
7127c478bd9Sstevel@tonic-gate WARNING("db_mindex::all: null first entry found in all");
7137c478bd9Sstevel@tonic-gate answer[0] = NULL;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate for (i = 1; i < how_many; i++) {
7167c478bd9Sstevel@tonic-gate ptr = table->next_entry(where, &where);
7177c478bd9Sstevel@tonic-gate if (ptr != NULL)
7187c478bd9Sstevel@tonic-gate answer[i] = new_entry(ptr);
7197c478bd9Sstevel@tonic-gate else {
7207c478bd9Sstevel@tonic-gate WARNING(
7217c478bd9Sstevel@tonic-gate "db_mindex::all: null internal entry found in all");
7227c478bd9Sstevel@tonic-gate answer[i] = NULL; /* Answer gets null too. -CM */
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate READUNLOCKNR(table, lret, "ru table db_mindex::all");
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate *result = answer;
7297c478bd9Sstevel@tonic-gate READUNLOCK(this, DB_SUCCESS, "ru db_mindex::all");
7307c478bd9Sstevel@tonic-gate return (DB_SUCCESS);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate * Remove the entry identified by 'recloc' from:
7357c478bd9Sstevel@tonic-gate * 1. all indices, as obtained by extracting the index values from the entry
7367c478bd9Sstevel@tonic-gate * 2. table where entry is stored.
7377c478bd9Sstevel@tonic-gate */
7387c478bd9Sstevel@tonic-gate db_status
remove_aux(entryp recloc)7397c478bd9Sstevel@tonic-gate db_mindex::remove_aux(entryp recloc)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate int i, curr_ind;
7427c478bd9Sstevel@tonic-gate db_status res = DB_SUCCESS;
7437c478bd9Sstevel@tonic-gate
7447c478bd9Sstevel@tonic-gate WRITELOCK(this, DB_LOCK_ERROR, "w db_mindex::remove_aux");
7457c478bd9Sstevel@tonic-gate /* get index values of this record */
7467c478bd9Sstevel@tonic-gate db_query * cq = extract_index_values_from_record(recloc);
7477c478bd9Sstevel@tonic-gate if (cq == NULL) {
7487c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, DB_MEMORY_LIMIT, "wu db_mindex::remove_aux");
7497c478bd9Sstevel@tonic-gate FATAL3("db_mindex::remove_aux: could not allocate space",
7507c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate if (cq->size() != indices.indices_len) { /* something is wrong */
7537c478bd9Sstevel@tonic-gate delete cq; // clean up
7547c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
7557c478bd9Sstevel@tonic-gate "db_mindex::remove_aux: record contains wrong number of indices");
7567c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, DB_INTERNAL_ERROR,
7577c478bd9Sstevel@tonic-gate "wu db_mindex::remove_aux");
7587c478bd9Sstevel@tonic-gate return (DB_INTERNAL_ERROR);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate
7617c478bd9Sstevel@tonic-gate if (!noWriteThrough.flag) {
7627c478bd9Sstevel@tonic-gate nis_object *o = 0;
7637c478bd9Sstevel@tonic-gate entry_object *e = table->get_entry(recloc);
7647c478bd9Sstevel@tonic-gate int queryRes, doingModify;
7657c478bd9Sstevel@tonic-gate
7667c478bd9Sstevel@tonic-gate /*
7677c478bd9Sstevel@tonic-gate * If the removal is part of a modify operation, we
7687c478bd9Sstevel@tonic-gate * defer the LDAP update until the modified NIS+ object
7697c478bd9Sstevel@tonic-gate * is added back.
7707c478bd9Sstevel@tonic-gate */
7717c478bd9Sstevel@tonic-gate if (saveOldObjForModify((entry_obj *)e, &doingModify) == 0)
7727c478bd9Sstevel@tonic-gate res = DB_INTERNAL_ERROR;
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate if (res == DB_SUCCESS && !doingModify) {
7757c478bd9Sstevel@tonic-gate /*
7767c478bd9Sstevel@tonic-gate * If we're removing a directory entry, and the
7777c478bd9Sstevel@tonic-gate * entry is LDAP-mapped, but the directory isn't,
7787c478bd9Sstevel@tonic-gate * we need a copy of the entry object in order
7797c478bd9Sstevel@tonic-gate * to remove if from LDAP.
7807c478bd9Sstevel@tonic-gate */
7817c478bd9Sstevel@tonic-gate if (e != 0 && e->en_type != 0 &&
7827c478bd9Sstevel@tonic-gate strcmp(e->en_type, "IN_DIRECTORY") == 0)
7837c478bd9Sstevel@tonic-gate o = unmakePseudoEntryObj(e, 0);
7847c478bd9Sstevel@tonic-gate queryRes = removeLDAP(cq, o);
7857c478bd9Sstevel@tonic-gate if (queryRes != LDAP_SUCCESS) {
7867c478bd9Sstevel@tonic-gate if (table->mapping.storeErrorDisp == abandon)
7877c478bd9Sstevel@tonic-gate res = DB_INTERNAL_ERROR;
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate if (o != 0)
7907c478bd9Sstevel@tonic-gate nis_destroy_object(o);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate if (res == DB_SUCCESS) {
7957c478bd9Sstevel@tonic-gate db_qcomp * comps = cq->queryloc();
7967c478bd9Sstevel@tonic-gate
7977c478bd9Sstevel@tonic-gate /* Add sanity check in case of corrupted table */
7987c478bd9Sstevel@tonic-gate if (indices.indices_val != NULL) {
7997c478bd9Sstevel@tonic-gate /* update indices */
8007c478bd9Sstevel@tonic-gate for (i = 0; i < indices.indices_len; i++) {
8017c478bd9Sstevel@tonic-gate /* unnec. if sorted */
8027c478bd9Sstevel@tonic-gate curr_ind = comps[i].which_index;
8037c478bd9Sstevel@tonic-gate indices.indices_val[curr_ind].remove(
8047c478bd9Sstevel@tonic-gate comps[i].index_value, recloc);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate
8087c478bd9Sstevel@tonic-gate /* update table where record is stored */
8097c478bd9Sstevel@tonic-gate table->delete_entry(recloc);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate /* delete query */
8137c478bd9Sstevel@tonic-gate delete cq;
8147c478bd9Sstevel@tonic-gate
8157c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, DB_SUCCESS, "wu db_mindex::remove_aux");
8167c478bd9Sstevel@tonic-gate
8177c478bd9Sstevel@tonic-gate return (res);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate /*
8217c478bd9Sstevel@tonic-gate * Removes the entry in the table named by given query 'q'.
8227c478bd9Sstevel@tonic-gate * If a NULL query is supplied, all entries in table are removed.
8237c478bd9Sstevel@tonic-gate * Returns DB_NOTFOUND if no entry is found.
8247c478bd9Sstevel@tonic-gate * Returns DB_SUCCESS if one entry is found; this entry is removed from
8257c478bd9Sstevel@tonic-gate * its record storage, and it is also removed from all the indices of the
8267c478bd9Sstevel@tonic-gate * table. If more than one entry satisfying 'q' is found, all are removed.
8277c478bd9Sstevel@tonic-gate */
8287c478bd9Sstevel@tonic-gate db_status
remove(db_query * q)8297c478bd9Sstevel@tonic-gate db_mindex::remove(db_query *q)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate long count = 0;
8327c478bd9Sstevel@tonic-gate db_index_entry *rp;
8337c478bd9Sstevel@tonic-gate db_status rstat;
8347c478bd9Sstevel@tonic-gate bool_t valid_query;
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate WRITELOCK(this, DB_LOCK_ERROR, "w db_mindex::remove");
8377c478bd9Sstevel@tonic-gate WRITELOCK2(table, DB_LOCK_ERROR, "w table db_mindex::remove", this);
8387c478bd9Sstevel@tonic-gate if (q == NULL) { /* remove all entries in table */
8397c478bd9Sstevel@tonic-gate if (table->mapping.toLDAP && !noWriteThrough.flag) {
8407c478bd9Sstevel@tonic-gate int queryRes = removeLDAP(q, 0);
8417c478bd9Sstevel@tonic-gate #ifdef NISDB_LDAP_DEBUG
8427c478bd9Sstevel@tonic-gate if (queryRes != LDAP_SUCCESS)
8437c478bd9Sstevel@tonic-gate abort();
8447c478bd9Sstevel@tonic-gate #endif /* NISDB_LDAP_DEBUG */
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate if (table != NULL && table->getsize() > 0) {
8477c478bd9Sstevel@tonic-gate reset_tables();
8487c478bd9Sstevel@tonic-gate WRITEUNLOCK2(table, this, DB_SUCCESS, DB_SUCCESS,
8497c478bd9Sstevel@tonic-gate "wu table db_mindex::remove",
8507c478bd9Sstevel@tonic-gate "wu db_mindex::remove");
8517c478bd9Sstevel@tonic-gate return (DB_SUCCESS);
8527c478bd9Sstevel@tonic-gate } else {
8537c478bd9Sstevel@tonic-gate WRITEUNLOCK2(table, this, DB_NOTFOUND, DB_NOTFOUND,
8547c478bd9Sstevel@tonic-gate "wu table db_mindex::remove",
8557c478bd9Sstevel@tonic-gate "wu db_mindex::remove");
8567c478bd9Sstevel@tonic-gate return (DB_NOTFOUND);
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate
8607c478bd9Sstevel@tonic-gate rp = satisfy_query(q, &count, &valid_query, FALSE);
8617c478bd9Sstevel@tonic-gate
8627c478bd9Sstevel@tonic-gate if (valid_query != TRUE) {
8637c478bd9Sstevel@tonic-gate WRITEUNLOCK2(table, this, DB_BADQUERY, DB_BADQUERY,
8647c478bd9Sstevel@tonic-gate "wu table db_mindex::remove", "wu db_mindex::remove");
8657c478bd9Sstevel@tonic-gate return (DB_BADQUERY);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate
8687c478bd9Sstevel@tonic-gate if (count == 0) { /* not found */
8697c478bd9Sstevel@tonic-gate WRITEUNLOCK2(table, this, DB_NOTFOUND, DB_NOTFOUND,
8707c478bd9Sstevel@tonic-gate "wu table db_mindex::remove", "wu db_mindex::remove");
8717c478bd9Sstevel@tonic-gate return (DB_NOTFOUND);
8727c478bd9Sstevel@tonic-gate } else if (count == 1) { /* found, update indices */
8737c478bd9Sstevel@tonic-gate db_status s;
8747c478bd9Sstevel@tonic-gate
8757c478bd9Sstevel@tonic-gate s = remove_aux(rp->getlocation());
8767c478bd9Sstevel@tonic-gate
8777c478bd9Sstevel@tonic-gate WRITEUNLOCK2(table, this, s, s,
8787c478bd9Sstevel@tonic-gate "wu table db_mindex::remove", "wu db_mindex::remove");
8797c478bd9Sstevel@tonic-gate return (s);
8807c478bd9Sstevel@tonic-gate } else { /* ambiguous, remove all entries */
8817c478bd9Sstevel@tonic-gate int i;
8827c478bd9Sstevel@tonic-gate db_index_entry *next_entry;
8837c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) {
8847c478bd9Sstevel@tonic-gate if (rp == NULL) {
8857c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
8867c478bd9Sstevel@tonic-gate "db_mindex::remove: incorrect number of indices");
8877c478bd9Sstevel@tonic-gate WRITEUNLOCK2(table, this, DB_INTERNAL_ERROR,
8887c478bd9Sstevel@tonic-gate DB_INTERNAL_ERROR,
8897c478bd9Sstevel@tonic-gate "wu table db_mindex::remove",
8907c478bd9Sstevel@tonic-gate "wu db_mindex::remove");
8917c478bd9Sstevel@tonic-gate return (DB_INTERNAL_ERROR);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate
8947c478bd9Sstevel@tonic-gate next_entry = rp->getnextresult(); // save before removal
8957c478bd9Sstevel@tonic-gate rstat = remove_aux(rp->getlocation());
8967c478bd9Sstevel@tonic-gate if (rstat != DB_SUCCESS) {
8977c478bd9Sstevel@tonic-gate WRITEUNLOCK2(table, this, rstat, rstat,
8987c478bd9Sstevel@tonic-gate "wu table db_mindex::remove",
8997c478bd9Sstevel@tonic-gate "wu db_mindex::remove");
9007c478bd9Sstevel@tonic-gate return (rstat);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate rp = next_entry; // go on to next
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate WRITEUNLOCK2(table, this, DB_SUCCESS, DB_SUCCESS,
9057c478bd9Sstevel@tonic-gate "wu table db_mindex::remove", "wu db_mindex::remove");
9067c478bd9Sstevel@tonic-gate return (DB_SUCCESS);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate * Add copy of given entry to table. Entry is identified by query 'q'.
9127c478bd9Sstevel@tonic-gate * The entry (if any) satisfying the query is first deleted, then
9137c478bd9Sstevel@tonic-gate * added to the indices (using index values extracted form the given entry)
9147c478bd9Sstevel@tonic-gate * and the table.
9157c478bd9Sstevel@tonic-gate * Returns DB_NOTUNIQUE if more than one entry satisfies the query.
9167c478bd9Sstevel@tonic-gate * Returns DB_NOTFOUND if query is not well-formed.
9177c478bd9Sstevel@tonic-gate * Returns DB_SUCCESS if entry can be added.
9187c478bd9Sstevel@tonic-gate */
9197c478bd9Sstevel@tonic-gate db_status
add(db_query * q,entry_object * obj)9207c478bd9Sstevel@tonic-gate db_mindex::add(db_query *q, entry_object * obj)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate long count = 0;
9237c478bd9Sstevel@tonic-gate int i, curr_ind;
9247c478bd9Sstevel@tonic-gate bool_t valid;
9257c478bd9Sstevel@tonic-gate db_index_entry *rp = NULL;
9267c478bd9Sstevel@tonic-gate db_status rstat;
927*8d0852b7SRichard Lowe const char *myself = "db_mindex::add";
9287c478bd9Sstevel@tonic-gate
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate * The argument q is only NULL when we know that there are
9317c478bd9Sstevel@tonic-gate * no objects in the database that match the object.
9327c478bd9Sstevel@tonic-gate */
9337c478bd9Sstevel@tonic-gate WRITELOCK(this, DB_LOCK_ERROR, "w db_mindex::add");
9347c478bd9Sstevel@tonic-gate WRITELOCK2(table, DB_LOCK_ERROR, "w table db_mindex::add", this);
9357c478bd9Sstevel@tonic-gate if (q) {
9367c478bd9Sstevel@tonic-gate rp = satisfy_query(q, &count, &valid, FALSE);
9377c478bd9Sstevel@tonic-gate if (!valid) {
9387c478bd9Sstevel@tonic-gate WRITEUNLOCK2(this, table, DB_LOCK_ERROR, DB_LOCK_ERROR,
9397c478bd9Sstevel@tonic-gate "wu db_mindex::add",
9407c478bd9Sstevel@tonic-gate "wu table db_mindex::add");
9417c478bd9Sstevel@tonic-gate return (DB_BADQUERY);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate if (count == 1) { /* found, first delete */
9457c478bd9Sstevel@tonic-gate rstat = remove_aux(rp->getlocation());
9467c478bd9Sstevel@tonic-gate if (rstat != DB_SUCCESS) {
9477c478bd9Sstevel@tonic-gate WRITEUNLOCK2(this, table, rstat, rstat,
9487c478bd9Sstevel@tonic-gate "wu db_mindex::add",
9497c478bd9Sstevel@tonic-gate "wu table db_mindex::add");
9507c478bd9Sstevel@tonic-gate return (rstat);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate count = 0; /* fall through to add */
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate
9557c478bd9Sstevel@tonic-gate if (count == 0) { /* not found, insert */
9567c478bd9Sstevel@tonic-gate /* add object to table */
9577c478bd9Sstevel@tonic-gate entryp recloc = table->add_entry(obj, initialLoad.flag);
9587c478bd9Sstevel@tonic-gate /* get index values of this object, might be same as 'q' */
9597c478bd9Sstevel@tonic-gate db_query *cq = extract_index_values_from_object(obj);
9607c478bd9Sstevel@tonic-gate if (cq == NULL) {
9617c478bd9Sstevel@tonic-gate table->delete_entry(recloc);
9627c478bd9Sstevel@tonic-gate WRITEUNLOCK2(this, table,
9637c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, DB_MEMORY_LIMIT,
9647c478bd9Sstevel@tonic-gate "wu db_mindex::add DB_MEMORY_LIMIT",
9657c478bd9Sstevel@tonic-gate "wu table db_mindex::add DB_MEMORY_LIMIT");
9667c478bd9Sstevel@tonic-gate FATAL3("db_mindex::add: could not allocate space for",
9677c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate if (cq ->size() != indices.indices_len) { /* something wrong */
9707c478bd9Sstevel@tonic-gate table->delete_entry(recloc);
9717c478bd9Sstevel@tonic-gate delete cq; // clean up
9727c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
9737c478bd9Sstevel@tonic-gate "db_mindex::add: record contains wrong number of indices");
9747c478bd9Sstevel@tonic-gate WRITEUNLOCK2(this, table,
9757c478bd9Sstevel@tonic-gate DB_INTERNAL_ERROR, DB_INTERNAL_ERROR,
9767c478bd9Sstevel@tonic-gate "wu db_mindex::add DB_INTERNAL_ERROR",
9777c478bd9Sstevel@tonic-gate "wu table db_mindex::add DB_INTERNAL_ERROR");
9787c478bd9Sstevel@tonic-gate return (DB_INTERNAL_ERROR);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate db_qcomp * comps = cq->queryloc();
9817c478bd9Sstevel@tonic-gate
9827c478bd9Sstevel@tonic-gate /* update indices */
9837c478bd9Sstevel@tonic-gate if (indices.indices_val != NULL) {
9847c478bd9Sstevel@tonic-gate for (i = 0; i < indices.indices_len; i++) {
9857c478bd9Sstevel@tonic-gate curr_ind = comps[i].which_index;
9867c478bd9Sstevel@tonic-gate indices.indices_val[curr_ind].add(
9877c478bd9Sstevel@tonic-gate comps[i].index_value, recloc);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate delete cq; // clean up
9917c478bd9Sstevel@tonic-gate if (!noWriteThrough.flag) {
9927c478bd9Sstevel@tonic-gate int queryRes;
9937c478bd9Sstevel@tonic-gate entry_object *e = 0;
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate if (retrieveOldObjForModify((entry_obj **)&e) == 0) {
9967c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
9977c478bd9Sstevel@tonic-gate "%s: Error retrieving old object for LDAP update",
9987c478bd9Sstevel@tonic-gate myself);
9997c478bd9Sstevel@tonic-gate return (DB_INTERNAL_ERROR);
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate
10027c478bd9Sstevel@tonic-gate queryRes = storeLDAP(q, obj, 0, e, 0);
10037c478bd9Sstevel@tonic-gate if (queryRes != LDAP_SUCCESS) {
10047c478bd9Sstevel@tonic-gate if (table->mapping.storeErrorDisp == abandon) {
10057c478bd9Sstevel@tonic-gate WRITEUNLOCK2(this, table,
10067c478bd9Sstevel@tonic-gate DB_INTERNAL_ERROR,
10077c478bd9Sstevel@tonic-gate DB_INTERNAL_ERROR,
10087c478bd9Sstevel@tonic-gate "wu db_mindex::add LDAP",
10097c478bd9Sstevel@tonic-gate "wu table db_mindex::add LDAP");
10107c478bd9Sstevel@tonic-gate return (DB_INTERNAL_ERROR);
10117c478bd9Sstevel@tonic-gate } else {
10127c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
10137c478bd9Sstevel@tonic-gate "%s: LDAP store failed: %s",
10147c478bd9Sstevel@tonic-gate myself,
10157c478bd9Sstevel@tonic-gate ldap_err2string(queryRes));
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate rstat = DB_SUCCESS;
10207c478bd9Sstevel@tonic-gate } else /* ambiguous */
10217c478bd9Sstevel@tonic-gate rstat = DB_NOTUNIQUE;
10227c478bd9Sstevel@tonic-gate
10237c478bd9Sstevel@tonic-gate WRITEUNLOCK2(this, table, rstat, rstat,
10247c478bd9Sstevel@tonic-gate "wu db_mindex::add",
10257c478bd9Sstevel@tonic-gate "wu table db_mindex::add");
10267c478bd9Sstevel@tonic-gate return (rstat);
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate
10297c478bd9Sstevel@tonic-gate /* ************************* pickle_mindex ********************* */
10307c478bd9Sstevel@tonic-gate /* Does the actual writing to/from file specific for db_mindex structure. */
10317c478bd9Sstevel@tonic-gate static bool_t
transfer_aux(XDR * x,pptr rp)10327c478bd9Sstevel@tonic-gate transfer_aux(XDR* x, pptr rp)
10337c478bd9Sstevel@tonic-gate {
10347c478bd9Sstevel@tonic-gate return (xdr_db_mindex(x, (db_mindex*) rp));
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate
10377c478bd9Sstevel@tonic-gate class pickle_mindex: public pickle_file {
10387c478bd9Sstevel@tonic-gate public:
pickle_mindex(char * f,pickle_mode m)10397c478bd9Sstevel@tonic-gate pickle_mindex(char *f, pickle_mode m) : pickle_file(f, m) {}
10407c478bd9Sstevel@tonic-gate
10417c478bd9Sstevel@tonic-gate /* Transfers db_mindex structure pointed to by dp to/from file. */
transfer(db_mindex * dp)10427c478bd9Sstevel@tonic-gate int transfer(db_mindex* dp)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate int ret;
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate WRITELOCK(dp, -1, "w pickle_mindex::transfer");
10477c478bd9Sstevel@tonic-gate ret = pickle_file::transfer((pptr) dp, &transfer_aux);
10487c478bd9Sstevel@tonic-gate WRITEUNLOCK(dp, ret, "wu pickle_mindex::transfer");
10497c478bd9Sstevel@tonic-gate return (ret);
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate };
10527c478bd9Sstevel@tonic-gate
10537c478bd9Sstevel@tonic-gate /* Write this structure (table, indices, scheme) into the specified file. */
10547c478bd9Sstevel@tonic-gate int
dump(char * file)10557c478bd9Sstevel@tonic-gate db_mindex::dump(char *file)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate pickle_mindex f(file, PICKLE_WRITE);
10587c478bd9Sstevel@tonic-gate int status = f.transfer(this);
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate if (status == 1)
10617c478bd9Sstevel@tonic-gate return (-1); /* could not open for write */
10627c478bd9Sstevel@tonic-gate else
10637c478bd9Sstevel@tonic-gate return (status);
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate
10667c478bd9Sstevel@tonic-gate /*
10677c478bd9Sstevel@tonic-gate * Reset the table by: deleting all the indices, table of entries, and its
10687c478bd9Sstevel@tonic-gate * scheme.
10697c478bd9Sstevel@tonic-gate */
10707c478bd9Sstevel@tonic-gate void
reset()10717c478bd9Sstevel@tonic-gate db_mindex::reset()
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate WRITELOCKV(this, "w db_mindex::reset");
10747c478bd9Sstevel@tonic-gate reset_tables(); /* clear table contents first */
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate if (indices.indices_val) {
10777c478bd9Sstevel@tonic-gate delete [] indices.indices_val;
10787c478bd9Sstevel@tonic-gate indices.indices_val = NULL;
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate if (table) { delete table; table = NULL; }
10817c478bd9Sstevel@tonic-gate if (scheme) { delete scheme; scheme = NULL; }
10827c478bd9Sstevel@tonic-gate indices.indices_len = 0;
10837c478bd9Sstevel@tonic-gate rversion.zero();
10847c478bd9Sstevel@tonic-gate if (objPath.ptr != 0) {
10857c478bd9Sstevel@tonic-gate free(objPath.ptr);
10867c478bd9Sstevel@tonic-gate objPath.ptr = 0;
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_mindex::reset");
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate
10917c478bd9Sstevel@tonic-gate /*
10927c478bd9Sstevel@tonic-gate * Initialize table using information from specified file.
10937c478bd9Sstevel@tonic-gate * The table is first 'reset', then the attempt to load from the file
10947c478bd9Sstevel@tonic-gate * is made. If the load failed, the table is again reset.
10957c478bd9Sstevel@tonic-gate * Therefore, the table will be modified regardless of the success of the
10967c478bd9Sstevel@tonic-gate * load. Returns 0 if successful, 1 if DB disk file couldn't be opened,
10977c478bd9Sstevel@tonic-gate * -1 for various other failures.
10987c478bd9Sstevel@tonic-gate */
10997c478bd9Sstevel@tonic-gate int
load(char * file)11007c478bd9Sstevel@tonic-gate db_mindex::load(char *file)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate pickle_mindex f(file, PICKLE_READ);
11037c478bd9Sstevel@tonic-gate int status;
11047c478bd9Sstevel@tonic-gate int init_table = (this->table == NULL);
11057c478bd9Sstevel@tonic-gate int init_scheme = (this->scheme == NULL);
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate WRITELOCK(this, -1, "w db_mindex::load");
11087c478bd9Sstevel@tonic-gate reset();
11097c478bd9Sstevel@tonic-gate
11107c478bd9Sstevel@tonic-gate /* load new mindex */
11117c478bd9Sstevel@tonic-gate if ((status = f.transfer(this)) != 0) {
11127c478bd9Sstevel@tonic-gate /* load failed. Reset. */
11137c478bd9Sstevel@tonic-gate reset();
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate /* Initialize the 'scheme' locking */
11177c478bd9Sstevel@tonic-gate if (status == 0 && this->scheme != 0 && init_scheme) {
11187c478bd9Sstevel@tonic-gate /*
11197c478bd9Sstevel@tonic-gate * Since we've added fields to the db_scheme that aren't
11207c478bd9Sstevel@tonic-gate * read from disk, we need to re-allocate so that the
11217c478bd9Sstevel@tonic-gate * db_scheme instance is large enough.
11227c478bd9Sstevel@tonic-gate */
11237c478bd9Sstevel@tonic-gate db_scheme *tmpscheme = new db_scheme();
11247c478bd9Sstevel@tonic-gate if (tmpscheme != 0) {
11257c478bd9Sstevel@tonic-gate (void) memcpy(tmpscheme, this->scheme,
11267c478bd9Sstevel@tonic-gate this->scheme->oldstructsize());
11277c478bd9Sstevel@tonic-gate free(this->scheme);
11287c478bd9Sstevel@tonic-gate this->scheme = tmpscheme;
11297c478bd9Sstevel@tonic-gate } else {
11307c478bd9Sstevel@tonic-gate status = -1;
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate * If the 'table' field was NULL before the load, but not now,
11357c478bd9Sstevel@tonic-gate * initialize the table locking and mapping.
11367c478bd9Sstevel@tonic-gate */
11377c478bd9Sstevel@tonic-gate if (status == 0 && this->table != 0 && init_table) {
11387c478bd9Sstevel@tonic-gate /*
11397c478bd9Sstevel@tonic-gate * As for the db_scheme, make sure the db_table is large
11407c478bd9Sstevel@tonic-gate * enough.
11417c478bd9Sstevel@tonic-gate */
11427c478bd9Sstevel@tonic-gate db_table *tmptable = new db_table();
11437c478bd9Sstevel@tonic-gate if (tmptable != 0) {
11447c478bd9Sstevel@tonic-gate (void) memcpy(tmptable, this->table,
11457c478bd9Sstevel@tonic-gate this->table->oldstructsize());
11467c478bd9Sstevel@tonic-gate free(this->table);
11477c478bd9Sstevel@tonic-gate this->table = tmptable;
11487c478bd9Sstevel@tonic-gate (void) this->configure(file);
11497c478bd9Sstevel@tonic-gate } else {
11507c478bd9Sstevel@tonic-gate status = -1;
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate
11547c478bd9Sstevel@tonic-gate if (status == 0 && this->indices.indices_val != NULL) {
11557c478bd9Sstevel@tonic-gate /*
11567c478bd9Sstevel@tonic-gate * Recreate the db_index instance so that it is
11577c478bd9Sstevel@tonic-gate * correctly initialized.
11587c478bd9Sstevel@tonic-gate */
11597c478bd9Sstevel@tonic-gate db_index *tmp_indices;
11607c478bd9Sstevel@tonic-gate int n_index = this->indices.indices_len;
11617c478bd9Sstevel@tonic-gate
11627c478bd9Sstevel@tonic-gate tmp_indices = new db_index[n_index];
11637c478bd9Sstevel@tonic-gate if (tmp_indices != NULL) {
11647c478bd9Sstevel@tonic-gate for (int i = 0; i < n_index; i++) {
11657c478bd9Sstevel@tonic-gate if (tmp_indices[i].move_xdr_db_index
11667c478bd9Sstevel@tonic-gate (&this->indices.indices_val[i]) != DB_SUCCESS) {
11677c478bd9Sstevel@tonic-gate status = -1;
11687c478bd9Sstevel@tonic-gate break;
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate free(this->indices.indices_val);
11727c478bd9Sstevel@tonic-gate this->indices.indices_val = tmp_indices;
11737c478bd9Sstevel@tonic-gate this->indices.indices_len = n_index;
11747c478bd9Sstevel@tonic-gate } else {
11757c478bd9Sstevel@tonic-gate status = -1;
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate
11797c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, status, "wu db_mindex::load");
11807c478bd9Sstevel@tonic-gate return (status);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate
11837c478bd9Sstevel@tonic-gate /*
11847c478bd9Sstevel@tonic-gate * Prints statistics of the table. This includes the size of the table,
11857c478bd9Sstevel@tonic-gate * the number of entries, and the index sizes.
11867c478bd9Sstevel@tonic-gate */
11877c478bd9Sstevel@tonic-gate void
print_stats()11887c478bd9Sstevel@tonic-gate db_mindex::print_stats()
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate long size, count, i;
11917c478bd9Sstevel@tonic-gate long *stats = table->stats(TRUE);
11927c478bd9Sstevel@tonic-gate
11937c478bd9Sstevel@tonic-gate printf("table_size = %d\n", stats[0]);
11947c478bd9Sstevel@tonic-gate printf("last_used = %d\n", stats[1]);
11957c478bd9Sstevel@tonic-gate printf("count = %d\n", stats[2]);
11967c478bd9Sstevel@tonic-gate printf("free list size = %d\n", stats[3]);
11977c478bd9Sstevel@tonic-gate printf("free list count = %d\n", stats[4]);
11987c478bd9Sstevel@tonic-gate
11997c478bd9Sstevel@tonic-gate for (i = 5; i < 5+stats[4]; i++) {
12007c478bd9Sstevel@tonic-gate printf("%d, ", stats[i]);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate printf("\n");
12037c478bd9Sstevel@tonic-gate free((char *)stats);
12047c478bd9Sstevel@tonic-gate
12057c478bd9Sstevel@tonic-gate /* Add sanity check in case of corrupted table */
12067c478bd9Sstevel@tonic-gate if (indices.indices_val == NULL) {
12077c478bd9Sstevel@tonic-gate printf("No indices to print\n");
12087c478bd9Sstevel@tonic-gate return;
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate for (i = 0; i < indices.indices_len; i++) {
12117c478bd9Sstevel@tonic-gate printf("***** INDEX %d ******\n", i);
12127c478bd9Sstevel@tonic-gate indices.indices_val[i].stats(&size, &count);
12137c478bd9Sstevel@tonic-gate printf("index table size = %d\ncount = %d\n", size, count);
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate
12177c478bd9Sstevel@tonic-gate /* Prints statistics about all indices of table. */
12187c478bd9Sstevel@tonic-gate void
print_all_indices()12197c478bd9Sstevel@tonic-gate db_mindex::print_all_indices()
12207c478bd9Sstevel@tonic-gate {
12217c478bd9Sstevel@tonic-gate int i;
12227c478bd9Sstevel@tonic-gate
12237c478bd9Sstevel@tonic-gate READLOCKV(this, "r db_mindex::print_all_indices");
12247c478bd9Sstevel@tonic-gate /* Add sanity check in case of corrupted table */
12257c478bd9Sstevel@tonic-gate if (indices.indices_val == NULL) {
12267c478bd9Sstevel@tonic-gate printf("No indices to print\n");
12277c478bd9Sstevel@tonic-gate READUNLOCKV(this, "ru db_mindex::print_all_indices");
12287c478bd9Sstevel@tonic-gate return;
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate for (i = 0; i < indices.indices_len; i++) {
12317c478bd9Sstevel@tonic-gate printf("***** INDEX %d ******\n", i);
12327c478bd9Sstevel@tonic-gate indices.indices_val[i].print();
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate READUNLOCKV(this, "ru db_mindex::print_all_indices");
12357c478bd9Sstevel@tonic-gate }
12367c478bd9Sstevel@tonic-gate
12377c478bd9Sstevel@tonic-gate /* Prints statistics about indices identified by 'n'. */
12387c478bd9Sstevel@tonic-gate void
print_index(int n)12397c478bd9Sstevel@tonic-gate db_mindex::print_index(int n)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate READLOCKV(this, "r db_mindex::print_index");
12427c478bd9Sstevel@tonic-gate if (n >= 0 && n < indices.indices_len)
12437c478bd9Sstevel@tonic-gate indices.indices_val[n].print();
12447c478bd9Sstevel@tonic-gate READUNLOCKV(this, "ru db_mindex::print_index");
12457c478bd9Sstevel@tonic-gate }
1246