]> git.sur5r.net Git - openldap/commitdiff
Switched to dn_parent1, cleaned up dn2id_add/delete
authorHoward Chu <hyc@openldap.org>
Sat, 8 Dec 2001 10:10:04 +0000 (10:10 +0000)
committerHoward Chu <hyc@openldap.org>
Sat, 8 Dec 2001 10:10:04 +0000 (10:10 +0000)
servers/slapd/back-bdb/add.c
servers/slapd/back-bdb/delete.c
servers/slapd/back-bdb/dn2entry.c
servers/slapd/back-bdb/dn2id.c
servers/slapd/back-bdb/modrdn.c
servers/slapd/back-bdb/proto-bdb.h
servers/slapd/back-bdb/search.c
servers/slapd/back-bdb/tools.c

index f9235b5009e47c5a2e996062f32e75843ecc4ce4..b098bb499cd27af461d0cabd6b3030e86b5d0ba5 100644 (file)
@@ -93,7 +93,7 @@ retry:        rc = txn_abort( ltid );
         * If the parent does not exist, only allow the "root" user to
         * add the entry.
         */
-       pdn = dn_parent( be, e->e_ndn );
+       pdn = dn_parent1( be, e->e_ndn );
 
        if( pdn != NULL && *pdn != '\0' ) {
                Entry *matched = NULL;
@@ -304,10 +304,6 @@ return_results:
        }
 
 done:
-       if( pdn != NULL ) {
-               free(pdn);
-       }
-
        if (p != NULL) {
                /* free parent and writer lock */
                bdb_entry_return( be, p ); 
index 6b034d40d7091f3a5309ade068c763b6b5039331..47b9ca6708d12e5c8cfa799c1f2735f53496d638 100644 (file)
@@ -116,7 +116,7 @@ retry:      /* transaction retry */
                goto done;
        }
 
-       pdn = dn_parent( be, ndn );
+       pdn = dn_parent1( be, ndn );
 
        if( pdn != NULL && *pdn != '\0' ) {
                /* get parent */
@@ -231,7 +231,7 @@ retry:      /* transaction retry */
        }
 
        /* delete from dn2id */
-       rc = bdb_dn2id_delete( be, ltid, pdn, e->e_ndn, e->e_id );
+       rc = bdb_dn2id_delete( be, ltid, pdn, e );
        if ( rc != 0 ) {
                switch( rc ) {
                case DB_LOCK_DEADLOCK:
@@ -321,10 +321,6 @@ return_results:
        }
 
 done:
-       if( pdn != NULL ) {
-               free( pdn );
-       }
-
        /* free entry */
        if( e != NULL ) {
                bdb_entry_return( be, e );
index b84a4a5020237aea7d5d4067a055dd4e583c8197..5ad00ac20977efb39d0df482f89a043069048704 100644 (file)
@@ -49,7 +49,6 @@ bdb_dn2entry(
        if( matchedDN == NULL ) {
                rc = bdb_id2entry( be, tid, id, e );
        } else {
-               ch_free( matchedDN );
                rc = bdb_id2entry( be, tid, id, matched );
        }
 
index 8bef765bce2dc39bbd92b1a2082cfceb18b99ed8..22695f460595d8cdbfea1b97cf9f40089bcd5ef0 100644 (file)
@@ -18,13 +18,14 @@ int
 bdb_dn2id_add(
        BackendDB       *be,
        DB_TXN *txn,
-       const char      *pdn,
+       char *pdn,
        Entry           *e )
 {
-       int             rc;
-       DBT             key, data;
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
        DB *db = bdb->bi_dn2id->bdi_db;
+       int             rc;
+       DBT             key, data;
+       char            *buf, *ptr;
 
        Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
                e->e_ndn, (long) e->e_id, 0 );
@@ -32,9 +33,11 @@ bdb_dn2id_add(
 
        DBTzero( &key );
        key.size = strlen( e->e_ndn ) + 2;
-       key.data = ch_malloc( key.size );
-       ((char *)key.data)[0] = DN_BASE_PREFIX;
-       AC_MEMCPY( &((char *)key.data)[1], e->e_ndn, key.size - 1 );
+       buf = ch_malloc( key.size );
+       key.data = buf;
+       buf[0] = DN_BASE_PREFIX;
+       ptr = buf + 1;
+       AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
 
        DBTzero( &data );
        data.data = (char *) &e->e_id;
@@ -48,55 +51,66 @@ bdb_dn2id_add(
                goto done;
        }
 
-       {
-               ((char *)(key.data))[0] = DN_ONE_PREFIX;
+       buf[0] = DN_SUBTREE_PREFIX;
+       rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
+       if( rc != 0 ) {
+               Debug( LDAP_DEBUG_ANY,
+                       "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
+                       ptr, rc, 0 );
+               goto done;
+       }
 
-               if( pdn != NULL ) {
-                       key.size = strlen( pdn ) + 2;
-                       AC_MEMCPY( &((char*)key.data)[1],
-                               pdn, key.size - 1 );
+       pdn = dn_parent1( be, ptr );
 
-                       rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
+       if( pdn != NULL ) {
+               key.size -= pdn - ptr;
+               pdn[-1] = DN_ONE_PREFIX;
+               key.data = pdn - 1;
 
-                       if( rc != 0 ) {
-                               Debug( LDAP_DEBUG_ANY,
-                                       "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
+               rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
+
+               if( rc != 0 ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
                                        pdn, rc, 0 );
-                               goto done;
-                       }
+                       goto done;
                }
+
+               pdn[-1] = DN_SUBTREE_PREFIX;
+               rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
+
+               if( rc != 0 ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
+                                       pdn, rc, 0 );
+                       goto done;
+               }
+               ptr = pdn;
+               pdn = dn_parent1( be, pdn );
        }
 
-       {
-               char **subtree = dn_subtree( be, e->e_ndn );
-
-               if( subtree != NULL ) {
-                       int i;
-                       ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
-                       for( i=0; subtree[i] != NULL; i++ ) {
-                               if( be_issuffix( be, subtree[i] ))
-                                       continue;
-                               key.size = strlen( subtree[i] ) + 2;
-                               AC_MEMCPY( &((char *)key.data)[1],
-                                       subtree[i], key.size - 1 );
-
-                               rc = bdb_idl_insert_key( be, db, txn, &key,
-                                       e->e_id );
-
-                               if( rc != 0 ) {
-                                       Debug( LDAP_DEBUG_ANY,
-                                               "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
-                                               subtree[i], rc, 0 );
-                                       break;
-                               }
-                       }
+       while( pdn != NULL ) {
+               if( be_issuffix( be, pdn ))
+                       break;
+
+               pdn[-1] = DN_SUBTREE_PREFIX;
+               key.size -= pdn - ptr;
+               key.data = pdn - 1;
+
+               rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
 
-                       charray_free( subtree );
+               if( rc != 0 ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
+                                       pdn, rc, 0 );
+                       break;
                }
+               ptr = pdn;
+               pdn = dn_parent1( be, pdn );
        }
 
 done:
-       ch_free( key.data );
+       ch_free( buf );
        Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
        return rc;
 }
@@ -105,24 +119,26 @@ int
 bdb_dn2id_delete(
        BackendDB       *be,
        DB_TXN *txn,
-       const char      *pdn,
-       const char      *dn,
-       ID              id )
+       char    *pdn,
+       Entry           *e )
 {
-       int             rc;
-       DBT             key;
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
        DB *db = bdb->bi_dn2id->bdi_db;
+       int             rc;
+       DBT             key;
+       char            *buf, *ptr;
 
        Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
-               dn, id, 0 );
+               e->e_ndn, e->e_id, 0 );
 
        DBTzero( &key );
-       key.size = strlen( dn ) + 2;
-       key.data = ch_malloc( key.size );
+       key.size = strlen( e->e_ndn ) + 2;
+       buf = ch_malloc( key.size );
+       key.data = buf;
        key.flags = DB_DBT_USERMEM;
-       ((char *)key.data)[0] = DN_BASE_PREFIX;
-       AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
+       buf[0] = DN_BASE_PREFIX;
+       ptr = buf+1;
+       AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
 
        /* delete it */
        rc = db->del( db, txn, &key, 0 );
@@ -132,53 +148,66 @@ bdb_dn2id_delete(
                goto done;
        }
 
-       {
-               ((char *)(key.data))[0] = DN_ONE_PREFIX;
+       buf[0] = DN_SUBTREE_PREFIX;
+       rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
+       if( rc != 0 ) {
+               Debug( LDAP_DEBUG_ANY,
+                       "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
+                       ptr, rc, 0 );
+               goto done;
+       }
 
-               if( pdn != NULL ) {
-                       key.size = strlen( pdn ) + 2;
-                       AC_MEMCPY( &((char*)key.data)[1],
-                               pdn, key.size - 1 );
+       pdn = dn_parent1( be, ptr );
 
-                       rc = bdb_idl_delete_key( be, db, txn, &key, id );
+       if( pdn != NULL ) {
+               key.size -= pdn - ptr;
+               pdn[-1] = DN_ONE_PREFIX;
+               key.data = pdn - 1;
 
-                       if( rc != 0 ) {
-                               Debug( LDAP_DEBUG_ANY,
-                                       "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
-                                       pdn, rc, 0 );
-                               goto done;
-                       }
+               rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
+
+               if( rc != 0 ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
+                               pdn, rc, 0 );
+                       goto done;
+               }
+
+               pdn[-1] = DN_SUBTREE_PREFIX;
+
+               rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
+
+               if( rc != 0 ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
+                               pdn, rc, 0 );
+                       goto done;
                }
+               ptr = pdn;
+               pdn = dn_parent1( be, pdn );
        }
 
-       {
-               char **subtree = dn_subtree( be, dn );
-
-               if( subtree != NULL ) {
-                       int i;
-                       ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
-                       for( i=0; subtree[i] != NULL; i++ ) {
-                               key.size = strlen( subtree[i] ) + 2;
-                               AC_MEMCPY( &((char *)key.data)[1],
-                                       subtree[i], key.size - 1 );
-
-                               rc = bdb_idl_delete_key( be, db, txn, &key, id );
-
-                               if( rc != 0 ) {
-                                       Debug( LDAP_DEBUG_ANY,
-                                               "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
-                                               subtree[i], rc, 0 );
-                                       charray_free( subtree );
-                                       goto done;
-                               }
-                       }
+       while( pdn != NULL ) {
+               if( be_issuffix( be, pdn ))
+                       break;
+
+               pdn[-1] = DN_SUBTREE_PREFIX;
+               key.size -= pdn - ptr;
+               key.data = pdn - 1;
 
-                       charray_free( subtree );
+               rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
+               if( rc != 0 ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
+                               pdn, rc, 0 );
+                       goto done;
                }
+               ptr = pdn;
+               pdn = dn_parent1( be, pdn );
        }
 
 done:
-       ch_free( key.data );
+       ch_free( buf );
        Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
        return rc;
 }
@@ -237,7 +266,6 @@ bdb_dn2id_matched(
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
        DB *db = bdb->bi_dn2id->bdi_db;
        const char *dn = in;
-       char *tmp = NULL;
 
        Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
 
@@ -263,20 +291,16 @@ bdb_dn2id_matched(
                rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
 
                if( rc == DB_NOTFOUND ) {
-                       char *pdn = dn_parent( be, dn );
-                       ch_free( tmp );
-                       tmp = NULL;
+                       char *pdn = dn_parent1( be, dn );
 
                        if( pdn == NULL || *pdn == '\0' ) {
                                Debug( LDAP_DEBUG_TRACE,
                                        "<= bdb_dn2id_matched: no match\n",
                                        0, 0, 0 );
-                               ch_free( pdn );
                                break;
                        }
 
                        dn = pdn;
-                       tmp = pdn;
                        key.size = strlen( dn ) + 2;
 
                } else if ( rc == 0 ) {
@@ -285,7 +309,6 @@ bdb_dn2id_matched(
                                        "<= bdb_dn2id_matched: get size mismatch: "
                                        "expected %ld, got %ld\n",
                                        (long) sizeof(ID), (long) data.size, 0 );
-                               ch_free( tmp );
                        }
 
                        if( in != dn ) {
@@ -301,7 +324,6 @@ bdb_dn2id_matched(
                        Debug( LDAP_DEBUG_ANY,
                                "<= bdb_dn2id_matched: get failed: %s (%d)\n",
                                db_strerror(rc), rc, 0 );
-                       ch_free( tmp );
                        break;
                }
        }
@@ -720,9 +742,8 @@ int
 bdb_dn2id_delete(
        BackendDB       *be,
        DB_TXN *txn,
-       const char      *pdn,
-       const char      *dn,
-       ID              id )
+       char    *pdn,
+       Entry   *e )
 {
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
        int rc;
@@ -731,13 +752,13 @@ bdb_dn2id_delete(
        idNode *n;
 
        DBTzero(&key);
-       key.size = sizeof(id);
-       key.data = &id;
+       key.size = sizeof(e->e_id);
+       key.data = &e->e_id;
 
        rc = db->del( db, txn, &key, 0);
 
        ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
-       n = avl_delete(&bdb->bi_tree, (void *)id, (AVL_CMP)node_find_cmp);
+       n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
        if (n) {
                if (n->i_parent) {
                        ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
@@ -749,7 +770,7 @@ bdb_dn2id_delete(
                ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
                free(n);
        }
-       if (id == 1)
+       if (e->e_id == 1)
                bdb->bi_troot = NULL;
        ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
 
index 454ec68375d379cbad432928f665ab8cf7af8766..7bf0378bf1b59d02ac8eba1846d41c40049eb013 100644 (file)
@@ -160,7 +160,7 @@ retry:      /* transaction retry */
                goto done;
        }
 
-       p_ndn = dn_parent( be, e->e_ndn );
+       p_ndn = dn_parent1( be, e->e_ndn );
        np_ndn = p_ndn;
        if ( p_ndn != NULL && p_ndn[ 0 ] != '\0' ) {
                /* Make sure parent entry exist and we can write its 
@@ -204,7 +204,7 @@ retry:      /* transaction retry */
                        "bdb_modrdn: wr to children of entry %s OK\n",
                        p_ndn, 0, 0 );
                
-               p_dn = dn_parent( be, e->e_dn );
+               p_dn = dn_parent1( be, e->e_dn );
 
                Debug( LDAP_DEBUG_TRACE,
                        "bdb_modrdn: parent dn=%s\n",
@@ -239,7 +239,7 @@ retry:      /* transaction retry */
                                        "bdb_modrdn: wr to children of entry \"\" OK\n",
                                        0, 0, 0 );
                
-                               p_dn = ch_strdup( "" );
+                               p_dn = "";
 
                                Debug( LDAP_DEBUG_TRACE,
                                        "bdb_modrdn: parent dn=\"\"\n",
@@ -551,7 +551,7 @@ retry:      /* transaction retry */
        }
        
        /* delete old one */
-       rc = bdb_dn2id_delete( be, ltid, p_ndn, e->e_ndn, e->e_id );
+       rc = bdb_dn2id_delete( be, ltid, p_ndn, e );
        if ( rc != 0 ) {
                switch( rc ) {
                case DB_LOCK_DEADLOCK:
@@ -644,8 +644,6 @@ done:
        if( new_ndn != NULL ) free( new_ndn );
 
        if( np_ndn == p_ndn ) np_ndn = NULL;
-       if( p_dn != NULL ) free( p_dn );
-       if( p_ndn != NULL ) free( p_ndn );
 
        /* LDAP v2 supporting correct attribute handling. */
        if( new_rdn_types != NULL ) charray_free(new_rdn_types);
index ed31f363f5c105b57f44324a1fe1771151c3a0d8..64b682689d3388cfbd53e935524da667be6c6802 100644 (file)
@@ -82,15 +82,14 @@ int bdb_dn2id_matched(
 int bdb_dn2id_add(
        BackendDB *be,
        DB_TXN *tid,
-       const char *pdn,
+       char *pdn,
        Entry *e );
 
 int bdb_dn2id_delete(
        BackendDB *be,
        DB_TXN *tid,
-       const char *pdn,
-       const char *dn,
-       ID id );
+       char *pdn,
+       Entry *e );
 
 int bdb_dn2id_children(
        BackendDB *be,
index fe42b3e6b8a4000934da0e63a1a829beb100530c..f578eca689aa576db0add12b2c1453bed1b26cc5 100644 (file)
@@ -328,13 +328,11 @@ bdb_search(
 
                        /* need to skip alias which deref into scope */
                        if( scope & LDAP_SCOPE_ONELEVEL ) {
-                               char *pdn = dn_parent( NULL, e->e_ndn );
+                               char *pdn = dn_parent1( NULL, e->e_ndn );
                                if ( pdn != NULL ) {
                                        if( strcmp( pdn, realbase ) ) {
-                                               free( pdn );
                                                goto loop_continue;
                                        }
-                                       free(pdn);
                                }
 
                        } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
@@ -379,13 +377,10 @@ bdb_search(
 
                        /* check scope */
                        if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
-                               if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
-                                       (void) dn_normalize( dn );
+                               if ( (dn = dn_parent1( be, e->e_ndn )) != NULL ) {
                                        scopeok = (dn == realbase)
                                                ? 1
                                                : (strcmp( dn, realbase ) ? 0 : 1 );
-                                       free( dn );
-
                                } else {
                                        scopeok = (realbase == NULL || *realbase == '\0');
                                }
index 5001506612ce6877a7ebaea256a963c0185ecb9f..8b68ddeb650818cad01caea0dfac290567761a4d 100644 (file)
@@ -145,9 +145,8 @@ ID bdb_tool_entry_put(
        }
 
        /* add dn2id indices */
-       pdn = dn_parent( be, e->e_ndn );
+       pdn = dn_parent1( be, e->e_ndn );
        rc = bdb_dn2id_add( be, tid, pdn, e );
-       if( pdn ) free( pdn );
        if( rc != 0 ) {
                Debug( LDAP_DEBUG_ANY,
                        "=> bdb_tool_entry_put: dn2id_add failed: %s (%d)\n",
@@ -238,9 +237,8 @@ int bdb_tool_entry_reindex(
                (long) id, e->e_dn, 0 );
 
        /* add dn2id indices */
-       pdn = dn_parent( be, e->e_ndn );
+       pdn = dn_parent1( be, e->e_ndn );
        rc = bdb_dn2id_add( be, tid, pdn, e );
-       if( pdn ) free( pdn );
        if( rc != 0 && rc != DB_KEYEXIST ) {
                Debug( LDAP_DEBUG_ANY,
                        "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",