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",
65 pdn = dn_parent( be, ptr );
68 key.size -= pdn - ptr;
69 pdn[-1] = DN_ONE_PREFIX;
72 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
75 Debug( LDAP_DEBUG_ANY,
76 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
82 while( pdn != NULL ) {
83 if ( be_issuffix( be, pdn ))
85 pdn[-1] = DN_SUBTREE_PREFIX;
87 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
90 Debug( LDAP_DEBUG_ANY,
91 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
96 pdn = dn_parent( be, pdn );
97 key.size -= pdn - ptr;
103 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
114 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
115 DB *db = bdb->bi_dn2id->bdi_db;
120 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
121 e->e_ndn, e->e_id, 0 );
124 key.size = e->e_nname.bv_len + 2;
125 buf = ch_malloc( key.size );
127 key.flags = DB_DBT_USERMEM;
128 buf[0] = DN_BASE_PREFIX;
130 AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
133 rc = db->del( db, txn, &key, 0 );
135 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
136 db_strerror(rc), rc, 0 );
140 if( !be_issuffix( be, ptr )) {
141 buf[0] = DN_SUBTREE_PREFIX;
142 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
144 Debug( LDAP_DEBUG_ANY,
145 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
151 pdn = dn_parent( be, ptr );
154 key.size -= pdn - ptr;
155 pdn[-1] = DN_ONE_PREFIX;
158 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
161 Debug( LDAP_DEBUG_ANY,
162 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
168 while( pdn != NULL ) {
169 if ( be_issuffix( be, pdn ))
172 pdn[-1] = DN_SUBTREE_PREFIX;
174 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
176 Debug( LDAP_DEBUG_ANY,
177 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
182 pdn = dn_parent( be, pdn );
183 key.size -= pdn - ptr;
189 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
202 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
203 DB *db = bdb->bi_dn2id->bdi_db;
205 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
208 key.size = dn->bv_len + 2;
209 key.data = ch_malloc( key.size );
210 ((char *)key.data)[0] = DN_BASE_PREFIX;
211 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
216 data.ulen = sizeof(ID);
217 data.flags = DB_DBT_USERMEM;
220 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
223 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
224 db_strerror( rc ), rc, 0 );
226 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
244 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
245 DB *db = bdb->bi_dn2id->bdi_db;
248 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
251 key.size = in->bv_len + 2;
252 buf = ch_malloc( key.size );
255 AC_MEMCPY( dn, in->bv_val, key.size - 1 );
260 data.ulen = sizeof(ID);
261 data.flags = DB_DBT_USERMEM;
264 dn[-1] = DN_BASE_PREFIX;
269 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
271 if( rc == DB_NOTFOUND ) {
272 char *pdn = dn_parent( be, dn );
274 if( pdn == NULL || *pdn == '\0' ) {
275 Debug( LDAP_DEBUG_TRACE,
276 "<= bdb_dn2id_matched: no match\n",
281 key.size -= pdn - dn;
285 } else if ( rc == 0 ) {
286 if( data.size != sizeof( ID ) ) {
287 Debug( LDAP_DEBUG_ANY,
288 "<= bdb_dn2id_matched: get size mismatch: "
289 "expected %ld, got %ld\n",
290 (long) sizeof(ID), (long) data.size, 0 );
297 Debug( LDAP_DEBUG_TRACE,
298 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
299 (long) *id, *id2 == 0 ? "entry" : "matched", dn );
303 Debug( LDAP_DEBUG_ANY,
304 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
305 db_strerror(rc), rc, 0 );
322 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
323 DB *db = bdb->bi_dn2id->bdi_db;
326 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
330 key.size = dn->bv_len + 2;
331 key.data = ch_malloc( key.size );
332 ((char *)key.data)[0] = DN_ONE_PREFIX;
333 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
335 /* we actually could do a empty get... */
338 data.ulen = sizeof(id);
339 data.flags = DB_DBT_USERMEM;
341 data.dlen = sizeof(id);
343 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
346 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
348 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
349 db_strerror(rc) ), rc );
363 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
364 DB *db = bdb->bi_dn2id->bdi_db;
366 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
368 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val))
370 BDB_IDL_ALL(bdb, ids);
375 key.size = dn->bv_len + 2;
376 key.data = ch_malloc( key.size );
377 ((char *)key.data)[0] = prefix;
378 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
380 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
383 Debug( LDAP_DEBUG_TRACE,
384 "<= bdb_dn2idl: get failed: %s (%d)\n",
385 db_strerror( rc ), rc, 0 );
388 Debug( LDAP_DEBUG_TRACE,
389 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
391 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
399 /* Experimental management routines for a hierarchically structured backend.
401 * Unsupported! Use at your own risk!
403 * Instead of a dn2id database, we use an id2parent database. Each entry in
404 * this database is a struct diskNode, containing the ID of the node's parent
405 * and the RDN of the node.
407 typedef struct diskNode {
413 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
414 * database into memory (into an AVL tree). Next we iterate through each node
415 * of this tree, connecting each child to its parent. The nodes in this AVL
416 * tree are a struct idNode. The immediate (Onelevel) children of a node are
417 * referenced in the i_kids AVL tree. With this arrangement, there is no need
418 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
419 * the DN of an entry is constructed by walking up the list of i_parent
420 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
421 * extremely efficient, even when operating on a populated subtree.
423 * The idNode tree is searched directly from the root when performing id to
424 * entry lookups. The tree is traversed using the i_kids subtrees when
425 * performing dn to id lookups.
427 typedef struct idNode {
429 struct idNode *i_parent;
432 ldap_pvt_thread_rdwr_t i_kids_rdwr;
436 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
437 * sorted in lexical order. These are the various helper routines used
438 * for the searches and sorts.
455 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
464 return a->i_id - b->i_id;
474 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
476 /* should be slightly better without ordering drawbacks */
477 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
480 idNode * bdb_find_id_node(
485 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
488 idNode * bdb_find_rdn_node(
493 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
496 /* This function links a node into its parent's i_kids tree. */
504 if (a->i_rdn->parent == 0)
506 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
509 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
510 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
511 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
512 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
516 /* This function adds a node into the main AVL tree */
517 idNode *bdb_add_node(
525 node = (idNode *)ch_malloc(sizeof(idNode));
527 node->i_parent = NULL;
529 node->i_rdn = (diskNode *)d;
530 node->i_rdn->rdn.bv_val += (long)d;
531 node->i_rdn->nrdn.bv_val += (long)d;
532 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
533 avl_insert( &bdb->bi_tree, (caddr_t) node,
534 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
536 bdb->bi_troot = node;
540 /* This function initializes the trees at startup time. */
545 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
555 rc = bdb->bi_id2parent->bdi_db->cursor(
556 bdb->bi_id2parent->bdi_db, NULL, &cursor,
557 bdb->bi_db_opflags );
562 /* When be_suffix is turned into struct berval or LDAPDN
563 * life will get a lot easier... Since no DNs live on disk, we
564 * need to operate on the be_suffix to fully qualify our DNs.
565 * We need to know how many components are in the suffix DN,
566 * so we can tell where the suffix ends and our nodes begin.
568 * Note that this code always uses be_suffix[0], so defining
569 * multiple suffixes for a single backend won't work!
571 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
572 for (i=0; rdns[i]; i++);
578 key.data = (char *)&id;
579 key.ulen = sizeof( id );
580 key.flags = DB_DBT_USERMEM;
581 data.flags = DB_DBT_MALLOC;
583 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
584 bdb_add_node( id, data.data, bdb );
586 cursor->c_close( cursor );
588 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
594 /* This function constructs a full DN for a given id. We really should
595 * be passing idNodes directly, to save some effort...
603 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
608 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
609 o = bdb_find_id_node(id, bdb->bi_tree);
610 rlen = be->be_suffix[0]->bv_len + 1;
611 nrlen = be->be_nsuffix[0]->bv_len + 1;
612 for (n = o; n && n->i_parent; n=n->i_parent) {
613 rlen += n->i_rdn->rdn.bv_len + 1;
614 nrlen += n->i_rdn->nrdn.bv_len + 1;
616 e->e_name.bv_len = rlen - 1;
617 e->e_nname.bv_len = nrlen - 1;
618 e->e_name.bv_val = ch_malloc(rlen + nrlen);
619 e->e_nname.bv_val = e->e_name.bv_val + rlen;
620 ptr = e->e_name.bv_val;
621 nptr = e->e_nname.bv_val;
622 for (n = o; n && n->i_parent; n=n->i_parent) {
623 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
625 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
628 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
630 strcpy(ptr, be->be_suffix[0]->bv_val);
631 strcpy(nptr, be->be_nsuffix[0]->bv_val);
643 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
646 DB *db = bdb->bi_id2parent->bdi_db;
650 nrlen = dn_rdnlen( be, &e->e_nname );
652 rlen = dn_rdnlen( be, &e->e_name );
657 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
658 d->rdn.bv_len = rlen;
659 d->nrdn.bv_len = nrlen;
660 d->rdn.bv_val = (char *)(d+1);
661 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
662 strncpy(d->rdn.bv_val, e->e_dn, rlen);
663 d->rdn.bv_val[rlen] = '\0';
664 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
665 d->nrdn.bv_val[nrlen] = '\0';
666 d->rdn.bv_val -= (long)d;
667 d->nrdn.bv_val -= (long)d;
670 bdb_dn2id(be, txn, pdn, &d->parent);
678 key.size = sizeof(ID);
679 key.flags = DB_DBT_USERMEM;
682 data.size = sizeof(diskNode) + rlen + nrlen + 2;
683 data.flags = DB_DBT_USERMEM;
685 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
688 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
689 n = bdb_add_node( e->e_id, data.data, bdb);
690 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
693 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
694 bdb_insert_kid(n, bdb->bi_tree);
695 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
710 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
713 DB *db = bdb->bi_id2parent->bdi_db;
717 key.size = sizeof(e->e_id);
720 rc = db->del( db, txn, &key, 0);
722 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
723 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
726 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
727 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
728 (AVL_CMP)node_frdn_cmp);
729 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
732 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
736 bdb->bi_troot = NULL;
737 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
750 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
759 if (be_issuffix(be, in->bv_val)) {
764 rdns = ldap_explode_dn(in->bv_val, 0);
765 for (i=0; rdns[i]; i++);
772 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
773 for (--i; i>=0; i--) {
774 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
775 n = bdb_find_rdn_node(rdns[i], p->i_kids);
776 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
780 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
788 return n ? 0 : DB_NOTFOUND;
798 return bdb_dn2id_matched(be, txn, dn, id, NULL);
808 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
812 rc = bdb_dn2id(be, txn, dn, &id);
816 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
817 n = bdb_find_id_node(id, bdb->bi_tree);
818 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
826 /* Since we don't store IDLs for onelevel or subtree, we have to construct
827 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
835 return bdb_idl_insert(ids, n->i_id);
846 rc = bdb_idl_insert(ids, n->i_id);
848 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
849 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
851 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
863 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
868 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
869 BDB_IDL_ALL(bdb, ids);
873 rc = bdb_dn2id(be, NULL, dn, &id);
876 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
877 n = bdb_find_id_node(id, bdb->bi_tree);
878 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
881 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
882 if (prefix == DN_ONE_PREFIX) {
883 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
886 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
889 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
892 #endif /* BDB_HIER */