1 /* dn2id.c - routines to deal with the dn2id index */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
24 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
25 DB *db = bdb->bi_dn2id->bdi_db;
29 struct berval ptr, pdn;
31 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
32 e->e_ndn, (long) e->e_id, 0 );
33 assert( e->e_id != NOID );
36 key.size = e->e_nname.bv_len + 2;
37 buf = ch_malloc( key.size );
39 buf[0] = DN_BASE_PREFIX;
41 ptr.bv_len = e->e_nname.bv_len;
42 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
43 ptr.bv_val[ptr.bv_len] = '\0';
46 data.data = (char *) &e->e_id;
47 data.size = sizeof( e->e_id );
49 /* store it -- don't override */
50 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
52 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
53 db_strerror(rc), rc, 0 );
57 if( !be_issuffix( be, &ptr )) {
58 buf[0] = DN_SUBTREE_PREFIX;
59 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
61 Debug( LDAP_DEBUG_ANY,
62 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
67 rc = dnParent( &ptr, &pdn );
68 if ( rc != LDAP_SUCCESS ) {
69 Debug( LDAP_DEBUG_ANY,
70 "=> bdb_dn2id_add: dnParent(\"%s\") failed\n",
75 key.size = pdn.bv_len + 2;
76 pdn.bv_val[-1] = DN_ONE_PREFIX;
77 key.data = pdn.bv_val-1;
80 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
83 Debug( LDAP_DEBUG_ANY,
84 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
90 while( !be_issuffix( be, &ptr )) {
91 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
93 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
96 Debug( LDAP_DEBUG_ANY,
97 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
101 rc = dnParent( &ptr, &pdn );
102 if ( rc != LDAP_SUCCESS ) {
103 Debug( LDAP_DEBUG_ANY,
104 "=> bdb_dn2id_add: dnParent(\"%s\") failed\n",
109 key.size = pdn.bv_len + 2;
110 key.data = pdn.bv_val - 1;
116 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
127 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
128 DB *db = bdb->bi_dn2id->bdi_db;
132 struct berval pdn, ptr;
134 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
135 e->e_ndn, e->e_id, 0 );
138 key.size = e->e_nname.bv_len + 2;
139 buf = ch_malloc( key.size );
141 key.flags = DB_DBT_USERMEM;
142 buf[0] = DN_BASE_PREFIX;
144 ptr.bv_len = e->e_nname.bv_len;
145 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
146 ptr.bv_val[ptr.bv_len] = '\0';
149 rc = db->del( db, txn, &key, 0 );
151 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
152 db_strerror(rc), rc, 0 );
156 if( !be_issuffix( be, &ptr )) {
157 buf[0] = DN_SUBTREE_PREFIX;
158 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
160 Debug( LDAP_DEBUG_ANY,
161 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
166 rc = dnParent( &ptr, &pdn );
167 if ( rc != LDAP_SUCCESS ) {
168 Debug( LDAP_DEBUG_ANY,
169 "=> bdb_dn2id_delete: dnParent(\"%s\") failed\n",
174 key.size = pdn.bv_len + 2;
175 pdn.bv_val[-1] = DN_ONE_PREFIX;
176 key.data = pdn.bv_val - 1;
179 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
182 Debug( LDAP_DEBUG_ANY,
183 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
189 while( !be_issuffix( be, &ptr )) {
190 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
192 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
194 Debug( LDAP_DEBUG_ANY,
195 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
199 rc = dnParent( &ptr, &pdn );
200 if ( rc != LDAP_SUCCESS ) {
201 Debug( LDAP_DEBUG_ANY,
202 "=> bdb_dn2id_delete: dnParent(\"%s\") failed\n",
207 key.size = pdn.bv_len + 2;
208 key.data = pdn.bv_val - 1;
214 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
227 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
228 DB *db = bdb->bi_dn2id->bdi_db;
230 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
234 *id = bdb_cache_find_entry_ndn2id(be, &bdb->bi_cache, dn);
240 key.size = dn->bv_len + 2;
241 key.data = ch_malloc( key.size );
242 ((char *)key.data)[0] = DN_BASE_PREFIX;
243 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
248 data.ulen = sizeof(ID);
249 data.flags = DB_DBT_USERMEM;
252 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
255 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
256 db_strerror( rc ), rc, 0 );
258 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
276 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
277 DB *db = bdb->bi_dn2id->bdi_db;
282 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
285 key.size = in->bv_len + 2;
286 buf = ch_malloc( key.size );
289 dn.bv_len = key.size - 2;
290 AC_MEMCPY( dn.bv_val, in->bv_val, key.size - 1 );
295 data.ulen = sizeof(ID);
296 data.flags = DB_DBT_USERMEM;
299 dn.bv_val[-1] = DN_BASE_PREFIX;
304 cached_id = bdb_cache_find_entry_ndn2id(be, &bdb->bi_cache, &dn);
306 if (cached_id != NOID) {
309 if ( dn.bv_val != buf+1 ) {
315 rc = db->get(db, txn, &key, &data, bdb->bi_db_opflags );
318 if( rc == DB_NOTFOUND ) {
321 if ( ! be_issuffix( be, &dn ) ) {
322 rc = dnParent( &dn, &pdn );
323 if ( rc != LDAP_SUCCESS ) {
324 Debug( LDAP_DEBUG_TRACE,
325 "<= bdb_dn2id_matched: dnParent(\"%s\") failed\n",
330 Debug( LDAP_DEBUG_TRACE,
331 "<= bdb_dn2id_matched: no match\n",
336 key.size = pdn.bv_len + 2;
338 key.data = pdn.bv_val - 1;
340 } else if ( rc == 0 ) {
341 if( data.size != sizeof( ID ) ) {
342 Debug( LDAP_DEBUG_ANY,
343 "<= bdb_dn2id_matched: get size mismatch: "
344 "expected %ld, got %ld\n",
345 (long) sizeof(ID), (long) data.size, 0 );
348 if( dn.bv_val != buf+1 ) {
352 Debug( LDAP_DEBUG_TRACE,
353 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
354 (long) *id, *id2 == 0 ? "entry" : "matched", dn.bv_val );
358 Debug( LDAP_DEBUG_ANY,
359 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
360 db_strerror(rc), rc, 0 );
377 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
378 DB *db = bdb->bi_dn2id->bdi_db;
381 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
385 key.size = dn->bv_len + 2;
386 key.data = ch_malloc( key.size );
387 ((char *)key.data)[0] = DN_ONE_PREFIX;
388 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
390 /* we actually could do a empty get... */
393 data.ulen = sizeof(id);
394 data.flags = DB_DBT_USERMEM;
396 data.dlen = sizeof(id);
398 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
401 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
403 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
404 db_strerror(rc) ), rc );
418 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
419 DB *db = bdb->bi_dn2id->bdi_db;
421 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
423 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
425 BDB_IDL_ALL(bdb, ids);
430 key.size = dn->bv_len + 2;
431 key.data = ch_malloc( key.size );
432 ((char *)key.data)[0] = prefix;
433 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
435 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
438 Debug( LDAP_DEBUG_TRACE,
439 "<= bdb_dn2idl: get failed: %s (%d)\n",
440 db_strerror( rc ), rc, 0 );
443 Debug( LDAP_DEBUG_TRACE,
444 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
446 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
454 /* Experimental management routines for a hierarchically structured backend.
456 * Unsupported! Use at your own risk!
458 * Instead of a dn2id database, we use an id2parent database. Each entry in
459 * this database is a struct diskNode, containing the ID of the node's parent
460 * and the RDN of the node.
462 typedef struct diskNode {
468 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
469 * database into memory (into an AVL tree). Next we iterate through each node
470 * of this tree, connecting each child to its parent. The nodes in this AVL
471 * tree are a struct idNode. The immediate (Onelevel) children of a node are
472 * referenced in the i_kids AVL tree. With this arrangement, there is no need
473 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
474 * the DN of an entry is constructed by walking up the list of i_parent
475 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
476 * extremely efficient, even when operating on a populated subtree.
478 * The idNode tree is searched directly from the root when performing id to
479 * entry lookups. The tree is traversed using the i_kids subtrees when
480 * performing dn to id lookups.
482 typedef struct idNode {
484 struct idNode *i_parent;
487 ldap_pvt_thread_rdwr_t i_kids_rdwr;
491 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
492 * sorted in lexical order. These are the various helper routines used
493 * for the searches and sorts.
510 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
519 return a->i_id - b->i_id;
529 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
531 /* should be slightly better without ordering drawbacks */
532 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
535 idNode * bdb_find_id_node(
540 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
543 idNode * bdb_find_rdn_node(
548 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
551 /* This function links a node into its parent's i_kids tree. */
559 if (a->i_rdn->parent == 0)
561 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
564 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
565 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
566 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
567 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
571 /* This function adds a node into the main AVL tree */
572 idNode *bdb_add_node(
580 node = (idNode *)ch_malloc(sizeof(idNode));
582 node->i_parent = NULL;
584 node->i_rdn = (diskNode *)d;
585 node->i_rdn->rdn.bv_val += (long)d;
586 node->i_rdn->nrdn.bv_val += (long)d;
587 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
588 avl_insert( &bdb->bi_tree, (caddr_t) node,
589 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
591 bdb->bi_troot = node;
595 /* This function initializes the trees at startup time. */
600 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
610 rc = bdb->bi_id2parent->bdi_db->cursor(
611 bdb->bi_id2parent->bdi_db, NULL, &cursor,
612 bdb->bi_db_opflags );
617 /* When be_suffix is turned into struct berval or LDAPDN
618 * life will get a lot easier... Since no DNs live on disk, we
619 * need to operate on the be_suffix to fully qualify our DNs.
620 * We need to know how many components are in the suffix DN,
621 * so we can tell where the suffix ends and our nodes begin.
623 * Note that this code always uses be_suffix[0], so defining
624 * multiple suffixes for a single backend won't work!
626 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
627 for (i=0; rdns[i]; i++);
633 key.data = (char *)&id;
634 key.ulen = sizeof( id );
635 key.flags = DB_DBT_USERMEM;
636 data.flags = DB_DBT_MALLOC;
638 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
639 bdb_add_node( id, data.data, bdb );
641 cursor->c_close( cursor );
643 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
649 /* This function constructs a full DN for a given id. We really should
650 * be passing idNodes directly, to save some effort...
658 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
663 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
664 o = bdb_find_id_node(id, bdb->bi_tree);
665 rlen = be->be_suffix[0]->bv_len + 1;
666 nrlen = be->be_nsuffix[0]->bv_len + 1;
667 for (n = o; n && n->i_parent; n=n->i_parent) {
668 rlen += n->i_rdn->rdn.bv_len + 1;
669 nrlen += n->i_rdn->nrdn.bv_len + 1;
671 e->e_name.bv_len = rlen - 1;
672 e->e_nname.bv_len = nrlen - 1;
673 e->e_name.bv_val = ch_malloc(rlen + nrlen);
674 e->e_nname.bv_val = e->e_name.bv_val + rlen;
675 ptr = e->e_name.bv_val;
676 nptr = e->e_nname.bv_val;
677 for (n = o; n && n->i_parent; n=n->i_parent) {
678 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
680 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
683 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
685 strcpy(ptr, be->be_suffix[0]->bv_val);
686 strcpy(nptr, be->be_nsuffix[0]->bv_val);
698 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
701 DB *db = bdb->bi_id2parent->bdi_db;
705 nrlen = dn_rdnlen( be, &e->e_nname );
707 rlen = dn_rdnlen( be, &e->e_name );
712 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
713 d->rdn.bv_len = rlen;
714 d->nrdn.bv_len = nrlen;
715 d->rdn.bv_val = (char *)(d+1);
716 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
717 strncpy(d->rdn.bv_val, e->e_dn, rlen);
718 d->rdn.bv_val[rlen] = '\0';
719 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
720 d->nrdn.bv_val[nrlen] = '\0';
721 d->rdn.bv_val -= (long)d;
722 d->nrdn.bv_val -= (long)d;
725 bdb_dn2id(be, txn, pdn, &d->parent);
733 key.size = sizeof(ID);
734 key.flags = DB_DBT_USERMEM;
737 data.size = sizeof(diskNode) + rlen + nrlen + 2;
738 data.flags = DB_DBT_USERMEM;
740 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
743 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
744 n = bdb_add_node( e->e_id, data.data, bdb);
745 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
748 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
749 bdb_insert_kid(n, bdb->bi_tree);
750 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
765 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
768 DB *db = bdb->bi_id2parent->bdi_db;
772 key.size = sizeof(e->e_id);
775 rc = db->del( db, txn, &key, 0);
777 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
778 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
781 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
782 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
783 (AVL_CMP)node_frdn_cmp);
784 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
787 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
791 bdb->bi_troot = NULL;
792 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
805 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
814 if (be_issuffix(be, in)) {
819 rdns = ldap_explode_dn(in->bv_val, 0);
820 for (i=0; rdns[i]; i++);
827 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
828 for (--i; i>=0; i--) {
829 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
830 n = bdb_find_rdn_node(rdns[i], p->i_kids);
831 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
835 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
843 return n ? 0 : DB_NOTFOUND;
853 return bdb_dn2id_matched(be, txn, dn, id, NULL);
863 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
867 rc = bdb_dn2id(be, txn, dn, &id);
871 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
872 n = bdb_find_id_node(id, bdb->bi_tree);
873 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
881 /* Since we don't store IDLs for onelevel or subtree, we have to construct
882 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
890 return bdb_idl_insert(ids, n->i_id);
901 rc = bdb_idl_insert(ids, n->i_id);
903 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
904 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
906 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
918 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
923 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
924 BDB_IDL_ALL(bdb, ids);
928 rc = bdb_dn2id(be, NULL, dn, &id);
931 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
932 n = bdb_find_id_node(id, bdb->bi_tree);
933 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
936 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
937 if (prefix == DN_ONE_PREFIX) {
938 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
941 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
944 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
947 #endif /* BDB_HIER */