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;
28 char *buf, *ptr, *pdn;
30 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
31 e->e_ndn, (long) e->e_id, 0 );
32 assert( e->e_id != NOID );
35 key.size = e->e_nname.bv_len + 2;
36 buf = ch_malloc( key.size );
38 buf[0] = DN_BASE_PREFIX;
40 AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
43 data.data = (char *) &e->e_id;
44 data.size = sizeof( e->e_id );
46 /* store it -- don't override */
47 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
49 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
50 db_strerror(rc), rc, 0 );
54 if( !be_issuffix( be, ptr )) {
55 buf[0] = DN_SUBTREE_PREFIX;
56 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
58 Debug( LDAP_DEBUG_ANY,
59 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
64 rc = dnParent( ptr, (const char **)&pdn );
65 if ( rc != LDAP_SUCCESS ) {
66 Debug( LDAP_DEBUG_ANY,
67 "=> bdb_dn2id_add: dnParent(\"%s\") failed\n",
72 key.size -= pdn - ptr;
73 pdn[-1] = DN_ONE_PREFIX;
77 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
80 Debug( LDAP_DEBUG_ANY,
81 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
87 while( !be_issuffix( be, ptr )) {
88 ptr[-1] = DN_SUBTREE_PREFIX;
90 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
93 Debug( LDAP_DEBUG_ANY,
94 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
98 rc = dnParent( ptr, &pdn );
99 if ( rc != LDAP_SUCCESS ) {
100 Debug( LDAP_DEBUG_ANY,
101 "=> bdb_dn2id_add: dnParent(\"%s\") failed\n",
106 key.size -= pdn - ptr;
113 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
124 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
125 DB *db = bdb->bi_dn2id->bdi_db;
130 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
131 e->e_ndn, e->e_id, 0 );
134 key.size = e->e_nname.bv_len + 2;
135 buf = ch_malloc( key.size );
137 key.flags = DB_DBT_USERMEM;
138 buf[0] = DN_BASE_PREFIX;
140 AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
143 rc = db->del( db, txn, &key, 0 );
145 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
146 db_strerror(rc), rc, 0 );
150 if( !be_issuffix( be, ptr )) {
151 buf[0] = DN_SUBTREE_PREFIX;
152 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
154 Debug( LDAP_DEBUG_ANY,
155 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
160 rc = dnParent( ptr, &pdn );
161 if ( rc != LDAP_SUCCESS ) {
162 Debug( LDAP_DEBUG_ANY,
163 "=> bdb_dn2id_delete: dnParent(\"%s\") failed\n",
168 key.size -= pdn - ptr;
169 pdn[-1] = DN_ONE_PREFIX;
173 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
176 Debug( LDAP_DEBUG_ANY,
177 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
183 while( !be_issuffix( be, ptr )) {
184 ptr[-1] = DN_SUBTREE_PREFIX;
186 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
188 Debug( LDAP_DEBUG_ANY,
189 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
193 rc = dnParent( ptr, &pdn );
194 if ( rc != LDAP_SUCCESS ) {
195 Debug( LDAP_DEBUG_ANY,
196 "=> bdb_dn2id_delete: dnParent(\"%s\") failed\n",
201 key.size -= pdn - ptr;
208 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
221 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
222 DB *db = bdb->bi_dn2id->bdi_db;
224 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
228 *id = bdb_cache_find_entry_ndn2id(be, &bdb->bi_cache, dn);
234 key.size = dn->bv_len + 2;
235 key.data = ch_malloc( key.size );
236 ((char *)key.data)[0] = DN_BASE_PREFIX;
237 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
242 data.ulen = sizeof(ID);
243 data.flags = DB_DBT_USERMEM;
246 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
249 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
250 db_strerror( rc ), rc, 0 );
252 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
270 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
271 DB *db = bdb->bi_dn2id->bdi_db;
276 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
279 key.size = in->bv_len + 2;
280 buf = ch_malloc( key.size );
283 dn.bv_len = key.size - 2;
284 AC_MEMCPY( dn.bv_val, in->bv_val, key.size - 1 );
289 data.ulen = sizeof(ID);
290 data.flags = DB_DBT_USERMEM;
293 dn.bv_val[-1] = DN_BASE_PREFIX;
298 cached_id = bdb_cache_find_entry_ndn2id(be, &bdb->bi_cache, &dn);
300 if (cached_id != NOID) {
303 if ( dn.bv_val != buf+1 ) {
309 rc = db->get(db, txn, &key, &data, bdb->bi_db_opflags );
312 if( rc == DB_NOTFOUND ) {
315 if ( ! be_issuffix( be, dn.bv_val ) ) {
316 rc = dnParent( dn.bv_val, &pdn );
317 if ( rc != LDAP_SUCCESS ) {
318 Debug( LDAP_DEBUG_TRACE,
319 "<= bdb_dn2id_matched: dnParent(\"%s\") failed\n",
325 if( pdn == NULL || *pdn == '\0' ) {
326 Debug( LDAP_DEBUG_TRACE,
327 "<= bdb_dn2id_matched: no match\n",
332 key.size -= pdn - dn.bv_val;
334 dn.bv_len = key.size - 2;
337 } else if ( rc == 0 ) {
338 if( data.size != sizeof( ID ) ) {
339 Debug( LDAP_DEBUG_ANY,
340 "<= bdb_dn2id_matched: get size mismatch: "
341 "expected %ld, got %ld\n",
342 (long) sizeof(ID), (long) data.size, 0 );
345 if( dn.bv_val != buf+1 ) {
349 Debug( LDAP_DEBUG_TRACE,
350 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
351 (long) *id, *id2 == 0 ? "entry" : "matched", dn );
355 Debug( LDAP_DEBUG_ANY,
356 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
357 db_strerror(rc), rc, 0 );
374 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
375 DB *db = bdb->bi_dn2id->bdi_db;
378 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
382 key.size = dn->bv_len + 2;
383 key.data = ch_malloc( key.size );
384 ((char *)key.data)[0] = DN_ONE_PREFIX;
385 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
387 /* we actually could do a empty get... */
390 data.ulen = sizeof(id);
391 data.flags = DB_DBT_USERMEM;
393 data.dlen = sizeof(id);
395 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
398 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
400 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
401 db_strerror(rc) ), rc );
415 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
416 DB *db = bdb->bi_dn2id->bdi_db;
418 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
420 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val))
422 BDB_IDL_ALL(bdb, ids);
427 key.size = dn->bv_len + 2;
428 key.data = ch_malloc( key.size );
429 ((char *)key.data)[0] = prefix;
430 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
432 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
435 Debug( LDAP_DEBUG_TRACE,
436 "<= bdb_dn2idl: get failed: %s (%d)\n",
437 db_strerror( rc ), rc, 0 );
440 Debug( LDAP_DEBUG_TRACE,
441 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
443 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
451 /* Experimental management routines for a hierarchically structured backend.
453 * Unsupported! Use at your own risk!
455 * Instead of a dn2id database, we use an id2parent database. Each entry in
456 * this database is a struct diskNode, containing the ID of the node's parent
457 * and the RDN of the node.
459 typedef struct diskNode {
465 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
466 * database into memory (into an AVL tree). Next we iterate through each node
467 * of this tree, connecting each child to its parent. The nodes in this AVL
468 * tree are a struct idNode. The immediate (Onelevel) children of a node are
469 * referenced in the i_kids AVL tree. With this arrangement, there is no need
470 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
471 * the DN of an entry is constructed by walking up the list of i_parent
472 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
473 * extremely efficient, even when operating on a populated subtree.
475 * The idNode tree is searched directly from the root when performing id to
476 * entry lookups. The tree is traversed using the i_kids subtrees when
477 * performing dn to id lookups.
479 typedef struct idNode {
481 struct idNode *i_parent;
484 ldap_pvt_thread_rdwr_t i_kids_rdwr;
488 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
489 * sorted in lexical order. These are the various helper routines used
490 * for the searches and sorts.
507 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
516 return a->i_id - b->i_id;
526 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
528 /* should be slightly better without ordering drawbacks */
529 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
532 idNode * bdb_find_id_node(
537 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
540 idNode * bdb_find_rdn_node(
545 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
548 /* This function links a node into its parent's i_kids tree. */
556 if (a->i_rdn->parent == 0)
558 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
561 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
562 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
563 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
564 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
568 /* This function adds a node into the main AVL tree */
569 idNode *bdb_add_node(
577 node = (idNode *)ch_malloc(sizeof(idNode));
579 node->i_parent = NULL;
581 node->i_rdn = (diskNode *)d;
582 node->i_rdn->rdn.bv_val += (long)d;
583 node->i_rdn->nrdn.bv_val += (long)d;
584 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
585 avl_insert( &bdb->bi_tree, (caddr_t) node,
586 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
588 bdb->bi_troot = node;
592 /* This function initializes the trees at startup time. */
597 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
607 rc = bdb->bi_id2parent->bdi_db->cursor(
608 bdb->bi_id2parent->bdi_db, NULL, &cursor,
609 bdb->bi_db_opflags );
614 /* When be_suffix is turned into struct berval or LDAPDN
615 * life will get a lot easier... Since no DNs live on disk, we
616 * need to operate on the be_suffix to fully qualify our DNs.
617 * We need to know how many components are in the suffix DN,
618 * so we can tell where the suffix ends and our nodes begin.
620 * Note that this code always uses be_suffix[0], so defining
621 * multiple suffixes for a single backend won't work!
623 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
624 for (i=0; rdns[i]; i++);
630 key.data = (char *)&id;
631 key.ulen = sizeof( id );
632 key.flags = DB_DBT_USERMEM;
633 data.flags = DB_DBT_MALLOC;
635 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
636 bdb_add_node( id, data.data, bdb );
638 cursor->c_close( cursor );
640 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
646 /* This function constructs a full DN for a given id. We really should
647 * be passing idNodes directly, to save some effort...
655 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
660 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
661 o = bdb_find_id_node(id, bdb->bi_tree);
662 rlen = be->be_suffix[0]->bv_len + 1;
663 nrlen = be->be_nsuffix[0]->bv_len + 1;
664 for (n = o; n && n->i_parent; n=n->i_parent) {
665 rlen += n->i_rdn->rdn.bv_len + 1;
666 nrlen += n->i_rdn->nrdn.bv_len + 1;
668 e->e_name.bv_len = rlen - 1;
669 e->e_nname.bv_len = nrlen - 1;
670 e->e_name.bv_val = ch_malloc(rlen + nrlen);
671 e->e_nname.bv_val = e->e_name.bv_val + rlen;
672 ptr = e->e_name.bv_val;
673 nptr = e->e_nname.bv_val;
674 for (n = o; n && n->i_parent; n=n->i_parent) {
675 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
677 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
680 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
682 strcpy(ptr, be->be_suffix[0]->bv_val);
683 strcpy(nptr, be->be_nsuffix[0]->bv_val);
695 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
698 DB *db = bdb->bi_id2parent->bdi_db;
702 nrlen = dn_rdnlen( be, &e->e_nname );
704 rlen = dn_rdnlen( be, &e->e_name );
709 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
710 d->rdn.bv_len = rlen;
711 d->nrdn.bv_len = nrlen;
712 d->rdn.bv_val = (char *)(d+1);
713 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
714 strncpy(d->rdn.bv_val, e->e_dn, rlen);
715 d->rdn.bv_val[rlen] = '\0';
716 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
717 d->nrdn.bv_val[nrlen] = '\0';
718 d->rdn.bv_val -= (long)d;
719 d->nrdn.bv_val -= (long)d;
722 bdb_dn2id(be, txn, pdn, &d->parent);
730 key.size = sizeof(ID);
731 key.flags = DB_DBT_USERMEM;
734 data.size = sizeof(diskNode) + rlen + nrlen + 2;
735 data.flags = DB_DBT_USERMEM;
737 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
740 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
741 n = bdb_add_node( e->e_id, data.data, bdb);
742 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
745 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
746 bdb_insert_kid(n, bdb->bi_tree);
747 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
762 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
765 DB *db = bdb->bi_id2parent->bdi_db;
769 key.size = sizeof(e->e_id);
772 rc = db->del( db, txn, &key, 0);
774 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
775 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
778 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
779 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
780 (AVL_CMP)node_frdn_cmp);
781 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
784 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
788 bdb->bi_troot = NULL;
789 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
802 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
811 if (be_issuffix(be, in->bv_val)) {
816 rdns = ldap_explode_dn(in->bv_val, 0);
817 for (i=0; rdns[i]; i++);
824 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
825 for (--i; i>=0; i--) {
826 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
827 n = bdb_find_rdn_node(rdns[i], p->i_kids);
828 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
832 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
840 return n ? 0 : DB_NOTFOUND;
850 return bdb_dn2id_matched(be, txn, dn, id, NULL);
860 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
864 rc = bdb_dn2id(be, txn, dn, &id);
868 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
869 n = bdb_find_id_node(id, bdb->bi_tree);
870 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
878 /* Since we don't store IDLs for onelevel or subtree, we have to construct
879 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
887 return bdb_idl_insert(ids, n->i_id);
898 rc = bdb_idl_insert(ids, n->i_id);
900 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
901 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
903 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
915 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
920 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
921 BDB_IDL_ALL(bdb, ids);
925 rc = bdb_dn2id(be, NULL, dn, &id);
928 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
929 n = bdb_find_id_node(id, bdb->bi_tree);
930 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
933 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
934 if (prefix == DN_ONE_PREFIX) {
935 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
938 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
941 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
944 #endif /* BDB_HIER */