From 5d3f3c240d23a449c49a569f277f4f53e8b1ce8a Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Mon, 15 Jan 2007 01:14:14 +0000 Subject: [PATCH] ITS#4554 slapindex takes a list of attributes to index --- servers/slapd/back-bdb/tools.c | 42 +++++++++++++++++++++++++++++++++- servers/slapd/slap.h | 2 +- servers/slapd/slapcommon.c | 10 ++++++-- servers/slapd/slapindex.c | 29 +++++++++++++++++++++-- 4 files changed, 77 insertions(+), 6 deletions(-) diff --git a/servers/slapd/back-bdb/tools.c b/servers/slapd/back-bdb/tools.c index 475f4acc09..d79bc18d6e 100644 --- a/servers/slapd/back-bdb/tools.c +++ b/servers/slapd/back-bdb/tools.c @@ -579,7 +579,8 @@ done: int bdb_tool_entry_reindex( BackendDB *be, - ID id ) + ID id, + AttributeDescription **adv ) { struct bdb_info *bi = (struct bdb_info *) be->be_private; int rc; @@ -599,6 +600,45 @@ int bdb_tool_entry_reindex( return 0; } + /* Check for explicit list of attrs to index */ + if ( adv ) { + int i, j, n; + + /* count */ + for ( n = 0; adv[n]; n++ ) ; + + /* insertion sort */ + for ( i = 0; i < n; i++ ) { + AttributeDescription *ad = adv[i]; + for ( j = i-1; j>=0; j--) { + if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break; + adv[j+1] = adv[j]; + } + adv[j+1] = ad; + } + + for ( i = 0; adv[i]; i++ ) { + if ( bi->bi_attrs[i]->ai_desc != adv[i] ) { + for ( j = i+1; j < bi->bi_nattrs; j++ ) { + if ( bi->bi_attrs[j]->ai_desc == adv[i] ) { + AttrInfo *ai = bi->bi_attrs[i]; + bi->bi_attrs[i] = bi->bi_attrs[j]; + bi->bi_attrs[j] = ai; + break; + } + } + if ( j == bi->bi_nattrs ) { + Debug( LDAP_DEBUG_ANY, + LDAP_XSTRING(bdb_tool_entry_reindex) + ": no index configured for %s\n", + adv[i]->ad_cname.bv_val, 0, 0 ); + return -1; + } + } + } + bi->bi_nattrs = i; + } + /* Get the first attribute to index */ if (bi->bi_linear_index && !index_nattrs) { index_nattrs = bi->bi_nattrs - 1; diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h index 930e9fd822..fa30ea09cb 100644 --- a/servers/slapd/slap.h +++ b/servers/slapd/slap.h @@ -2077,7 +2077,7 @@ typedef ID (BI_tool_entry_next) LDAP_P(( BackendDB *be )); typedef Entry* (BI_tool_entry_get) LDAP_P(( BackendDB *be, ID id )); typedef ID (BI_tool_entry_put) LDAP_P(( BackendDB *be, Entry *e, struct berval *text )); -typedef int (BI_tool_entry_reindex) LDAP_P(( BackendDB *be, ID id )); +typedef int (BI_tool_entry_reindex) LDAP_P(( BackendDB *be, ID id, AttributeDescription **adv )); typedef int (BI_tool_sync) LDAP_P(( BackendDB *be )); typedef ID (BI_tool_dn2id_get) LDAP_P(( BackendDB *be, struct berval *dn )); typedef int (BI_tool_id2entry_get) LDAP_P(( BackendDB *be, ID id, Entry **e )); diff --git a/servers/slapd/slapcommon.c b/servers/slapd/slapcommon.c index 37a91a1801..70843c731e 100644 --- a/servers/slapd/slapcommon.c +++ b/servers/slapd/slapcommon.c @@ -88,7 +88,7 @@ usage( int tool, const char *progname ) break; case SLAPINDEX: - options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [-q]\n"; + options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [attr ...] [-q]\n"; break; case SLAPTEST: @@ -441,13 +441,19 @@ slap_tool_init( switch ( tool ) { case SLAPADD: case SLAPCAT: - case SLAPINDEX: if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) { usage( tool, progname ); } break; + case SLAPINDEX: + if ( dbnum >= 0 && base.bv_val != NULL ) { + usage( tool, progname ); + } + + break; + case SLAPDN: if ( argc == optind ) { usage( tool, progname ); diff --git a/servers/slapd/slapindex.c b/servers/slapd/slapindex.c index 21734f1e11..e7c2d9e8e9 100644 --- a/servers/slapd/slapindex.c +++ b/servers/slapd/slapindex.c @@ -37,6 +37,7 @@ slapindex( int argc, char **argv ) ID id; int rc = EXIT_SUCCESS; const char *progname = "slapindex"; + AttributeDescription *ad, **adv = NULL; slap_tool_init( progname, SLAPINDEX, argc, argv ); @@ -51,12 +52,32 @@ slapindex( int argc, char **argv ) exit( EXIT_FAILURE ); } + argc -= optind; + if ( argc > 0 ) { + const char *text; + int i; + + argv = &argv[optind]; + adv = (AttributeDescription **)argv; + + for (i = 0; i < argc; i++ ) { + ad = NULL; + rc = slap_str2ad( argv[i], &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n", + argv[i], rc, ldap_err2string( rc )); + exit( EXIT_FAILURE ); + } + adv[i] = ad; + } + } + if( be->be_entry_open( be, 0 ) != 0 ) { fprintf( stderr, "%s: could not open database.\n", progname ); exit( EXIT_FAILURE ); } - + for ( id = be->be_entry_first( be ); id != NOID; id = be->be_entry_next( be ) ) @@ -67,7 +88,11 @@ slapindex( int argc, char **argv ) printf("indexing id=%08lx\n", (long) id ); } - rtn = be->be_entry_reindex( be, id ); + /* Backend will set its attr list on first call. Clear + * the list on all subsequent calls. + */ + rtn = be->be_entry_reindex( be, id, adv ); + adv = NULL; if( rtn != LDAP_SUCCESS ) { rc = EXIT_FAILURE; -- 2.39.5