]> git.sur5r.net Git - openldap/commitdiff
add monitor support for unindexed attributes (need to manually #define BDB_MONITOR_IDX)
authorPierangelo Masarati <ando@openldap.org>
Tue, 7 Aug 2007 22:36:04 +0000 (22:36 +0000)
committerPierangelo Masarati <ando@openldap.org>
Tue, 7 Aug 2007 22:36:04 +0000 (22:36 +0000)
servers/slapd/back-bdb/back-bdb.h
servers/slapd/back-bdb/index.c
servers/slapd/back-bdb/init.c
servers/slapd/back-bdb/monitor.c
servers/slapd/back-bdb/proto-bdb.h

index 85361505793d6c80d410ba996cfbf11e03cf182b..721096fa9b5de8529e63d7422d52eb315fd7ab11 100644 (file)
@@ -208,6 +208,12 @@ struct bdb_info {
        char            *bi_db_config_path;
        BerVarray       bi_db_config;
        bdb_monitor_t   bi_monitor;
+
+#ifdef BDB_MONITOR_IDX
+       ldap_pvt_thread_mutex_t bi_idx_mutex;
+       Avlnode         *bi_idx;
+#endif /* BDB_MONITOR_IDX */
+
        int             bi_flags;
 #define        BDB_IS_OPEN             0x01
 #define        BDB_HAS_CONFIG  0x02
index 3bf4fd136eebad75ca8fbfb7208517e88772a0c5..e193dae30d90e5ab122931421e12b28f2dbe0ac5 100644 (file)
@@ -97,12 +97,32 @@ int bdb_index_param(
 {
        AttrInfo *ai;
        int rc;
-       slap_mask_t mask;
+       slap_mask_t mask, type = 0;
        DB *db;
 
        ai = index_mask( be, desc, prefixp );
 
-       if( !ai ) {
+       if ( !ai ) {
+#ifdef BDB_MONITOR_IDX
+               switch ( ftype ) {
+               case LDAP_FILTER_PRESENT:
+                       type = SLAP_INDEX_PRESENT;
+                       break;
+               case LDAP_FILTER_APPROX:
+                       type = SLAP_INDEX_APPROX;
+                       break;
+               case LDAP_FILTER_EQUALITY:
+                       type = SLAP_INDEX_EQUALITY;
+                       break;
+               case LDAP_FILTER_SUBSTRINGS:
+                       type = SLAP_INDEX_SUBSTR;
+                       break;
+               default:
+                       return LDAP_INAPPROPRIATE_MATCHING;
+               }
+               bdb_monitor_idx_add( be->be_private, desc, type );
+#endif /* BDB_MONITOR_IDX */
+
                return LDAP_INAPPROPRIATE_MATCHING;
        }
        mask = ai->ai_indexmask;
@@ -115,6 +135,7 @@ int bdb_index_param(
 
        switch( ftype ) {
        case LDAP_FILTER_PRESENT:
+               type = SLAP_INDEX_PRESENT;
                if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
                        *prefixp = presence_key;
                        goto done;
@@ -122,6 +143,7 @@ int bdb_index_param(
                break;
 
        case LDAP_FILTER_APPROX:
+               type = SLAP_INDEX_APPROX;
                if ( desc->ad_type->sat_approx ) {
                        if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
                                goto done;
@@ -133,12 +155,14 @@ int bdb_index_param(
                /* fall thru */
 
        case LDAP_FILTER_EQUALITY:
+               type = SLAP_INDEX_EQUALITY;
                if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
                        goto done;
                }
                break;
 
        case LDAP_FILTER_SUBSTRINGS:
+               type = SLAP_INDEX_SUBSTR;
                if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
                        goto done;
                }
@@ -148,6 +172,10 @@ int bdb_index_param(
                return LDAP_OTHER;
        }
 
+#ifdef BDB_MONITOR_IDX
+       bdb_monitor_idx_add( be->be_private, desc, type );
+#endif /* BDB_MONITOR_IDX */
+
        return LDAP_INAPPROPRIATE_MATCHING;
 
 done:
index 650813ab0a8b98068d8d8b2697aada7fb480e7b2..a1699bb57015cb9ac6ebead92010893e95d49da8 100644 (file)
@@ -84,7 +84,6 @@ bdb_db_init( BackendDB *be, ConfigReply *cr )
        ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
        ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
 
-
        be->be_private = bdb;
        be->be_cf_ocs = be->bd_info->bi_cf_ocs;
 
index 9c16fab2a81a00f9ca089e2bddce91245e7dad86..9764d88f950c808efdf6cada04d4954b2e5ce594 100644 (file)
@@ -35,6 +35,15 @@ static AttributeDescription  *ad_olmBDBEntryCache,
        *ad_olmBDBEntryInfo, *ad_olmBDBIDLCache,
        *ad_olmDbDirectory;
 
+#ifdef BDB_MONITOR_IDX
+static int
+bdb_monitor_idx_entry_add(
+       struct bdb_info *bdb,
+       Entry           *e );
+
+static AttributeDescription    *ad_olmBDBNotIndexed;
+#endif /* BDB_MONITOR_IDX */
+
 /*
  * NOTE: there's some confusion in monitor OID arc;
  * by now, let's consider:
@@ -95,6 +104,16 @@ static struct {
                "USAGE dSAOperation )",
                &ad_olmDbDirectory },
 
+#ifdef BDB_MONITOR_IDX
+       { "( olmBDBAttributes:5 "
+               "NAME ( 'olmBDBNotIndexed' ) "
+               "DESC 'Missing indexes resulting from candidate selection' "
+               "SUP monitoredInfo "
+               "NO-USER-MODIFICATION "
+               "USAGE dSAOperation )",
+               &ad_olmBDBNotIndexed },
+#endif /* BDB_MONITOR_IDX */
+
        { NULL }
 };
 
@@ -112,6 +131,9 @@ static struct {
                        "$ olmBDBEntryInfo "
                        "$ olmBDBIDLCache "
                        "$ olmDbDirectory "
+#ifdef BDB_MONITOR_IDX
+                       "$ olmBDBNotIndexed "
+#endif /* BDB_MONITOR_IDX */
                        ") )",
                &oc_olmBDBDatabase },
 
@@ -149,6 +171,10 @@ bdb_monitor_update(
        bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
        ber_bvreplace( &a->a_vals[ 0 ], &bv );
        
+#ifdef BDB_MONITOR_IDX
+       bdb_monitor_idx_entry_add( bdb, e );
+#endif /* BDB_MONITOR_IDX */
+
        return SLAP_CB_CONTINUE;
 }
 
@@ -284,6 +310,11 @@ bdb_monitor_db_init( BackendDB *be )
                SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
        }
 
+#ifdef BDB_MONITOR_IDX
+       bdb->bi_idx = NULL;
+       ldap_pvt_thread_mutex_init( &bdb->bi_idx_mutex );
+#endif /* BDB_MONITOR_IDX */
+
        bdb->bi_monitor.bdm_scope = -1;
 
        return 0;
@@ -545,5 +576,161 @@ bdb_monitor_db_destroy( BackendDB *be )
                return 0;
        }
 
+#ifdef BDB_MONITOR_IDX
+       {
+               struct bdb_info         *bdb = (struct bdb_info *) be->be_private;
+
+               /* TODO: free tree */
+               ldap_pvt_thread_mutex_destroy( &bdb->bi_idx_mutex );
+               avl_free( bdb->bi_idx, ch_free );
+       }
+#endif /* BDB_MONITOR_IDX */
+
+       return 0;
+}
+
+#ifdef BDB_MONITOR_IDX
+typedef struct monitor_idx_t monitor_idx_t;
+
+struct monitor_idx_t {
+       AttributeDescription    *idx_ad;
+       slap_mask_t             idx_type;
+       unsigned long           idx_count;
+};
+
+static int
+monitor_idx_cmp( const void *p1, const void *p2 )
+{
+       const monitor_idx_t     *idx1 = (const monitor_idx_t *)p1;
+       const monitor_idx_t     *idx2 = (const monitor_idx_t *)p2;
+
+       return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad );
+}
+
+static int
+monitor_idx_dup( void *p1, void *p2 )
+{
+       monitor_idx_t   *idx1 = (monitor_idx_t *)p1;
+       monitor_idx_t   *idx2 = (monitor_idx_t *)p2;
+
+       return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0;
+}
+
+int
+bdb_monitor_idx_add(
+       struct bdb_info         *bdb,
+       AttributeDescription    *desc,
+       slap_mask_t             type )
+{
+       monitor_idx_t           idx_dummy = { 0 },
+                               *idx;
+       int                     rc = 0;
+
+       idx_dummy.idx_ad = desc;
+       if ( type == SLAP_INDEX_SUBSTR ) {
+               type = SLAP_INDEX_SUBSTR_DEFAULT;
+       }
+
+       ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
+
+       idx = (monitor_idx_t *)avl_find( bdb->bi_idx,
+               (caddr_t)&idx_dummy, monitor_idx_cmp );
+       if ( idx == NULL ) {
+               idx = (monitor_idx_t *)ch_malloc( sizeof( monitor_idx_t ) );
+               idx->idx_ad = desc;
+               idx->idx_type = type;
+               idx->idx_count = 1;
+
+               switch ( avl_insert( &bdb->bi_idx, (caddr_t)idx, 
+                       monitor_idx_cmp, monitor_idx_dup ) )
+               {
+               case 0:
+                       break;
+
+               default:
+                       ch_free( idx );
+                       rc = -1;
+               }
+
+       } else {
+               idx->idx_type |= type;
+               idx->idx_count++;
+       }
+
+       ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
+
+       return rc;
+}
+
+static int
+bdb_monitor_idx_apply( void *v_idx, void *v_valp )
+{
+       monitor_idx_t   *idx = (monitor_idx_t *)v_idx;
+       BerVarray       *valp = (BerVarray *)v_valp;
+
+       struct berval   bv, index;
+       char            *ptr;
+       char            count_buf[ SLAP_TEXT_BUFLEN ];
+       ber_len_t       count_len;
+
+       count_len = snprintf( count_buf, sizeof( count_buf ), "%lu", idx->idx_count );
+       slap_index2bvlen( idx->idx_type, &index );
+
+       bv.bv_len = idx->idx_ad->ad_cname.bv_len
+               + STRLENOF( "##" )
+               + count_len
+               + index.bv_len;
+       ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
+       ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val );
+       ptr[ 0 ] = '#';
+       ++ptr;
+       ptr = lutil_strcopy( ptr, count_buf );
+       ptr[ 0 ] = '#';
+       index.bv_val = ++ptr;
+       assert( index.bv_val + index.bv_len == bv.bv_val + bv.bv_len );
+       slap_index2bv( idx->idx_type, &index );
+
+       ber_bvarray_add( valp, &bv );
+
        return 0;
 }
+
+static int
+bdb_monitor_idx_entry_add(
+       struct bdb_info *bdb,
+       Entry           *e )
+{
+       BerVarray       vals = NULL;
+       Attribute       *a;
+
+       a = attr_find( e->e_attrs, ad_olmBDBNotIndexed );
+
+       ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
+
+       avl_apply( bdb->bi_idx, bdb_monitor_idx_apply,
+               &vals, -1, AVL_INORDER );
+
+       ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
+
+       if ( vals != NULL ) {
+               if ( a != NULL ) {
+                       assert( a->a_nvals == a->a_vals );
+
+                       ber_bvarray_free( a->a_vals );
+
+               } else {
+                       Attribute       **ap;
+
+                       for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
+                               ;
+                       *ap = attr_alloc( ad_olmBDBNotIndexed );
+                       a = *ap;
+               }
+               a->a_vals = vals;
+               a->a_nvals = a->a_vals;
+       }
+
+       return 0;
+}
+
+#endif /* BDB_MONITOR_IDX */
index 77aaf6c68853f4d8deb82202b8ad8e6f1068a9f8..00e21d506aec2d18a964f40d1bd3a70b37e38242 100644 (file)
@@ -443,6 +443,15 @@ int bdb_monitor_db_open( BackendDB *be );
 int bdb_monitor_db_close( BackendDB *be );
 int bdb_monitor_db_destroy( BackendDB *be );
 
+#ifdef BDB_MONITOR_IDX
+#define bdb_monitor_idx_add    BDB_SYMBOL(monitor_idx_add)
+int
+bdb_monitor_idx_add(
+       struct bdb_info         *bdb,
+       AttributeDescription    *desc,
+       slap_mask_t             type );
+#endif /* BDB_MONITOR_IDX */
+
 /*
  * cache.c
  */