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 if ((*id = bdb_cache_find_entry_ndn2id(be,&bdb->bi_cache,dn)) != NOID) {
233 key.size = dn->bv_len + 2;
234 key.data = ch_malloc( key.size );
235 ((char *)key.data)[0] = DN_BASE_PREFIX;
236 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
241 data.ulen = sizeof(ID);
242 data.flags = DB_DBT_USERMEM;
245 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
248 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
249 db_strerror( rc ), rc, 0 );
251 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
269 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
270 DB *db = bdb->bi_dn2id->bdi_db;
274 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
277 key.size = in->bv_len + 2;
278 buf = ch_malloc( key.size );
281 AC_MEMCPY( dn, in->bv_val, key.size - 1 );
286 data.ulen = sizeof(ID);
287 data.flags = DB_DBT_USERMEM;
290 dn[-1] = DN_BASE_PREFIX;
295 cached_id = bdb_cache_find_entry_ndn2id(be,&bdb->bi_cache,dn);
297 if (cached_id != NOID) {
306 rc = db->get(db, txn, &key, &data, bdb->bi_db_opflags );
309 if( rc == DB_NOTFOUND ) {
312 if ( ! be_issuffix( be, dn ) ) {
313 rc = dnParent( dn, &pdn );
314 if ( rc != LDAP_SUCCESS ) {
315 Debug( LDAP_DEBUG_TRACE,
316 "<= bdb_dn2id_matched: dnParent(\"%s\") failed\n",
322 if( pdn == NULL || *pdn == '\0' ) {
323 Debug( LDAP_DEBUG_TRACE,
324 "<= bdb_dn2id_matched: no match\n",
329 key.size -= pdn - dn;
333 } else if ( rc == 0 ) {
334 if( data.size != sizeof( ID ) ) {
335 Debug( LDAP_DEBUG_ANY,
336 "<= bdb_dn2id_matched: get size mismatch: "
337 "expected %ld, got %ld\n",
338 (long) sizeof(ID), (long) data.size, 0 );
345 Debug( LDAP_DEBUG_TRACE,
346 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
347 (long) *id, *id2 == 0 ? "entry" : "matched", dn );
351 Debug( LDAP_DEBUG_ANY,
352 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
353 db_strerror(rc), rc, 0 );
370 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
371 DB *db = bdb->bi_dn2id->bdi_db;
374 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
378 key.size = dn->bv_len + 2;
379 key.data = ch_malloc( key.size );
380 ((char *)key.data)[0] = DN_ONE_PREFIX;
381 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
383 /* we actually could do a empty get... */
386 data.ulen = sizeof(id);
387 data.flags = DB_DBT_USERMEM;
389 data.dlen = sizeof(id);
391 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
394 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
396 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
397 db_strerror(rc) ), rc );
411 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
412 DB *db = bdb->bi_dn2id->bdi_db;
414 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
416 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val))
418 BDB_IDL_ALL(bdb, ids);
423 key.size = dn->bv_len + 2;
424 key.data = ch_malloc( key.size );
425 ((char *)key.data)[0] = prefix;
426 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
428 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
431 Debug( LDAP_DEBUG_TRACE,
432 "<= bdb_dn2idl: get failed: %s (%d)\n",
433 db_strerror( rc ), rc, 0 );
436 Debug( LDAP_DEBUG_TRACE,
437 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
439 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
447 /* Experimental management routines for a hierarchically structured backend.
449 * Unsupported! Use at your own risk!
451 * Instead of a dn2id database, we use an id2parent database. Each entry in
452 * this database is a struct diskNode, containing the ID of the node's parent
453 * and the RDN of the node.
455 typedef struct diskNode {
461 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
462 * database into memory (into an AVL tree). Next we iterate through each node
463 * of this tree, connecting each child to its parent. The nodes in this AVL
464 * tree are a struct idNode. The immediate (Onelevel) children of a node are
465 * referenced in the i_kids AVL tree. With this arrangement, there is no need
466 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
467 * the DN of an entry is constructed by walking up the list of i_parent
468 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
469 * extremely efficient, even when operating on a populated subtree.
471 * The idNode tree is searched directly from the root when performing id to
472 * entry lookups. The tree is traversed using the i_kids subtrees when
473 * performing dn to id lookups.
475 typedef struct idNode {
477 struct idNode *i_parent;
480 ldap_pvt_thread_rdwr_t i_kids_rdwr;
484 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
485 * sorted in lexical order. These are the various helper routines used
486 * for the searches and sorts.
503 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
512 return a->i_id - b->i_id;
522 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
524 /* should be slightly better without ordering drawbacks */
525 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
528 idNode * bdb_find_id_node(
533 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
536 idNode * bdb_find_rdn_node(
541 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
544 /* This function links a node into its parent's i_kids tree. */
552 if (a->i_rdn->parent == 0)
554 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
557 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
558 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
559 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
560 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
564 /* This function adds a node into the main AVL tree */
565 idNode *bdb_add_node(
573 node = (idNode *)ch_malloc(sizeof(idNode));
575 node->i_parent = NULL;
577 node->i_rdn = (diskNode *)d;
578 node->i_rdn->rdn.bv_val += (long)d;
579 node->i_rdn->nrdn.bv_val += (long)d;
580 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
581 avl_insert( &bdb->bi_tree, (caddr_t) node,
582 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
584 bdb->bi_troot = node;
588 /* This function initializes the trees at startup time. */
593 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
603 rc = bdb->bi_id2parent->bdi_db->cursor(
604 bdb->bi_id2parent->bdi_db, NULL, &cursor,
605 bdb->bi_db_opflags );
610 /* When be_suffix is turned into struct berval or LDAPDN
611 * life will get a lot easier... Since no DNs live on disk, we
612 * need to operate on the be_suffix to fully qualify our DNs.
613 * We need to know how many components are in the suffix DN,
614 * so we can tell where the suffix ends and our nodes begin.
616 * Note that this code always uses be_suffix[0], so defining
617 * multiple suffixes for a single backend won't work!
619 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
620 for (i=0; rdns[i]; i++);
626 key.data = (char *)&id;
627 key.ulen = sizeof( id );
628 key.flags = DB_DBT_USERMEM;
629 data.flags = DB_DBT_MALLOC;
631 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
632 bdb_add_node( id, data.data, bdb );
634 cursor->c_close( cursor );
636 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
642 /* This function constructs a full DN for a given id. We really should
643 * be passing idNodes directly, to save some effort...
651 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
656 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
657 o = bdb_find_id_node(id, bdb->bi_tree);
658 rlen = be->be_suffix[0]->bv_len + 1;
659 nrlen = be->be_nsuffix[0]->bv_len + 1;
660 for (n = o; n && n->i_parent; n=n->i_parent) {
661 rlen += n->i_rdn->rdn.bv_len + 1;
662 nrlen += n->i_rdn->nrdn.bv_len + 1;
664 e->e_name.bv_len = rlen - 1;
665 e->e_nname.bv_len = nrlen - 1;
666 e->e_name.bv_val = ch_malloc(rlen + nrlen);
667 e->e_nname.bv_val = e->e_name.bv_val + rlen;
668 ptr = e->e_name.bv_val;
669 nptr = e->e_nname.bv_val;
670 for (n = o; n && n->i_parent; n=n->i_parent) {
671 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
673 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
676 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
678 strcpy(ptr, be->be_suffix[0]->bv_val);
679 strcpy(nptr, be->be_nsuffix[0]->bv_val);
691 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
694 DB *db = bdb->bi_id2parent->bdi_db;
698 nrlen = dn_rdnlen( be, &e->e_nname );
700 rlen = dn_rdnlen( be, &e->e_name );
705 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
706 d->rdn.bv_len = rlen;
707 d->nrdn.bv_len = nrlen;
708 d->rdn.bv_val = (char *)(d+1);
709 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
710 strncpy(d->rdn.bv_val, e->e_dn, rlen);
711 d->rdn.bv_val[rlen] = '\0';
712 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
713 d->nrdn.bv_val[nrlen] = '\0';
714 d->rdn.bv_val -= (long)d;
715 d->nrdn.bv_val -= (long)d;
718 bdb_dn2id(be, txn, pdn, &d->parent);
726 key.size = sizeof(ID);
727 key.flags = DB_DBT_USERMEM;
730 data.size = sizeof(diskNode) + rlen + nrlen + 2;
731 data.flags = DB_DBT_USERMEM;
733 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
736 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
737 n = bdb_add_node( e->e_id, data.data, bdb);
738 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
741 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
742 bdb_insert_kid(n, bdb->bi_tree);
743 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
758 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
761 DB *db = bdb->bi_id2parent->bdi_db;
765 key.size = sizeof(e->e_id);
768 rc = db->del( db, txn, &key, 0);
770 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
771 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
774 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
775 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
776 (AVL_CMP)node_frdn_cmp);
777 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
780 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
784 bdb->bi_troot = NULL;
785 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
798 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
807 if (be_issuffix(be, in->bv_val)) {
812 rdns = ldap_explode_dn(in->bv_val, 0);
813 for (i=0; rdns[i]; i++);
820 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
821 for (--i; i>=0; i--) {
822 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
823 n = bdb_find_rdn_node(rdns[i], p->i_kids);
824 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
828 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
836 return n ? 0 : DB_NOTFOUND;
846 return bdb_dn2id_matched(be, txn, dn, id, NULL);
856 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
860 rc = bdb_dn2id(be, txn, dn, &id);
864 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
865 n = bdb_find_id_node(id, bdb->bi_tree);
866 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
874 /* Since we don't store IDLs for onelevel or subtree, we have to construct
875 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
883 return bdb_idl_insert(ids, n->i_id);
894 rc = bdb_idl_insert(ids, n->i_id);
896 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
897 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
899 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
911 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
916 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
917 BDB_IDL_ALL(bdb, ids);
921 rc = bdb_dn2id(be, NULL, dn, &id);
924 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
925 n = bdb_find_id_node(id, bdb->bi_tree);
926 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
929 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
930 if (prefix == DN_ONE_PREFIX) {
931 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
934 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
937 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
940 #endif /* BDB_HIER */