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 );
227 key.size = dn->bv_len + 2;
228 key.data = ch_malloc( key.size );
229 ((char *)key.data)[0] = DN_BASE_PREFIX;
230 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
235 data.ulen = sizeof(ID);
236 data.flags = DB_DBT_USERMEM;
239 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
242 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
243 db_strerror( rc ), rc, 0 );
245 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
263 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
264 DB *db = bdb->bi_dn2id->bdi_db;
267 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
270 key.size = in->bv_len + 2;
271 buf = ch_malloc( key.size );
274 AC_MEMCPY( dn, in->bv_val, key.size - 1 );
279 data.ulen = sizeof(ID);
280 data.flags = DB_DBT_USERMEM;
283 dn[-1] = DN_BASE_PREFIX;
288 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
290 if( rc == DB_NOTFOUND ) {
293 if ( ! be_issuffix( be, dn ) ) {
294 rc = dnParent( dn, &pdn );
295 if ( rc != LDAP_SUCCESS ) {
296 Debug( LDAP_DEBUG_TRACE,
297 "<= bdb_dn2id_matched: dnParent(\"%s\") failed\n",
303 if( pdn == NULL || *pdn == '\0' ) {
304 Debug( LDAP_DEBUG_TRACE,
305 "<= bdb_dn2id_matched: no match\n",
310 key.size -= pdn - dn;
314 } else if ( rc == 0 ) {
315 if( data.size != sizeof( ID ) ) {
316 Debug( LDAP_DEBUG_ANY,
317 "<= bdb_dn2id_matched: get size mismatch: "
318 "expected %ld, got %ld\n",
319 (long) sizeof(ID), (long) data.size, 0 );
326 Debug( LDAP_DEBUG_TRACE,
327 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
328 (long) *id, *id2 == 0 ? "entry" : "matched", dn );
332 Debug( LDAP_DEBUG_ANY,
333 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
334 db_strerror(rc), rc, 0 );
351 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
352 DB *db = bdb->bi_dn2id->bdi_db;
355 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
359 key.size = dn->bv_len + 2;
360 key.data = ch_malloc( key.size );
361 ((char *)key.data)[0] = DN_ONE_PREFIX;
362 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
364 /* we actually could do a empty get... */
367 data.ulen = sizeof(id);
368 data.flags = DB_DBT_USERMEM;
370 data.dlen = sizeof(id);
372 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
375 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
377 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
378 db_strerror(rc) ), rc );
392 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
393 DB *db = bdb->bi_dn2id->bdi_db;
395 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
397 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val))
399 BDB_IDL_ALL(bdb, ids);
404 key.size = dn->bv_len + 2;
405 key.data = ch_malloc( key.size );
406 ((char *)key.data)[0] = prefix;
407 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
409 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
412 Debug( LDAP_DEBUG_TRACE,
413 "<= bdb_dn2idl: get failed: %s (%d)\n",
414 db_strerror( rc ), rc, 0 );
417 Debug( LDAP_DEBUG_TRACE,
418 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
420 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
428 /* Experimental management routines for a hierarchically structured backend.
430 * Unsupported! Use at your own risk!
432 * Instead of a dn2id database, we use an id2parent database. Each entry in
433 * this database is a struct diskNode, containing the ID of the node's parent
434 * and the RDN of the node.
436 typedef struct diskNode {
442 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
443 * database into memory (into an AVL tree). Next we iterate through each node
444 * of this tree, connecting each child to its parent. The nodes in this AVL
445 * tree are a struct idNode. The immediate (Onelevel) children of a node are
446 * referenced in the i_kids AVL tree. With this arrangement, there is no need
447 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
448 * the DN of an entry is constructed by walking up the list of i_parent
449 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
450 * extremely efficient, even when operating on a populated subtree.
452 * The idNode tree is searched directly from the root when performing id to
453 * entry lookups. The tree is traversed using the i_kids subtrees when
454 * performing dn to id lookups.
456 typedef struct idNode {
458 struct idNode *i_parent;
461 ldap_pvt_thread_rdwr_t i_kids_rdwr;
465 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
466 * sorted in lexical order. These are the various helper routines used
467 * for the searches and sorts.
484 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
493 return a->i_id - b->i_id;
503 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
505 /* should be slightly better without ordering drawbacks */
506 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
509 idNode * bdb_find_id_node(
514 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
517 idNode * bdb_find_rdn_node(
522 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
525 /* This function links a node into its parent's i_kids tree. */
533 if (a->i_rdn->parent == 0)
535 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
538 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
539 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
540 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
541 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
545 /* This function adds a node into the main AVL tree */
546 idNode *bdb_add_node(
554 node = (idNode *)ch_malloc(sizeof(idNode));
556 node->i_parent = NULL;
558 node->i_rdn = (diskNode *)d;
559 node->i_rdn->rdn.bv_val += (long)d;
560 node->i_rdn->nrdn.bv_val += (long)d;
561 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
562 avl_insert( &bdb->bi_tree, (caddr_t) node,
563 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
565 bdb->bi_troot = node;
569 /* This function initializes the trees at startup time. */
574 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
584 rc = bdb->bi_id2parent->bdi_db->cursor(
585 bdb->bi_id2parent->bdi_db, NULL, &cursor,
586 bdb->bi_db_opflags );
591 /* When be_suffix is turned into struct berval or LDAPDN
592 * life will get a lot easier... Since no DNs live on disk, we
593 * need to operate on the be_suffix to fully qualify our DNs.
594 * We need to know how many components are in the suffix DN,
595 * so we can tell where the suffix ends and our nodes begin.
597 * Note that this code always uses be_suffix[0], so defining
598 * multiple suffixes for a single backend won't work!
600 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
601 for (i=0; rdns[i]; i++);
607 key.data = (char *)&id;
608 key.ulen = sizeof( id );
609 key.flags = DB_DBT_USERMEM;
610 data.flags = DB_DBT_MALLOC;
612 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
613 bdb_add_node( id, data.data, bdb );
615 cursor->c_close( cursor );
617 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
623 /* This function constructs a full DN for a given id. We really should
624 * be passing idNodes directly, to save some effort...
632 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
637 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
638 o = bdb_find_id_node(id, bdb->bi_tree);
639 rlen = be->be_suffix[0]->bv_len + 1;
640 nrlen = be->be_nsuffix[0]->bv_len + 1;
641 for (n = o; n && n->i_parent; n=n->i_parent) {
642 rlen += n->i_rdn->rdn.bv_len + 1;
643 nrlen += n->i_rdn->nrdn.bv_len + 1;
645 e->e_name.bv_len = rlen - 1;
646 e->e_nname.bv_len = nrlen - 1;
647 e->e_name.bv_val = ch_malloc(rlen + nrlen);
648 e->e_nname.bv_val = e->e_name.bv_val + rlen;
649 ptr = e->e_name.bv_val;
650 nptr = e->e_nname.bv_val;
651 for (n = o; n && n->i_parent; n=n->i_parent) {
652 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
654 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
657 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
659 strcpy(ptr, be->be_suffix[0]->bv_val);
660 strcpy(nptr, be->be_nsuffix[0]->bv_val);
672 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
675 DB *db = bdb->bi_id2parent->bdi_db;
679 nrlen = dn_rdnlen( be, &e->e_nname );
681 rlen = dn_rdnlen( be, &e->e_name );
686 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
687 d->rdn.bv_len = rlen;
688 d->nrdn.bv_len = nrlen;
689 d->rdn.bv_val = (char *)(d+1);
690 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
691 strncpy(d->rdn.bv_val, e->e_dn, rlen);
692 d->rdn.bv_val[rlen] = '\0';
693 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
694 d->nrdn.bv_val[nrlen] = '\0';
695 d->rdn.bv_val -= (long)d;
696 d->nrdn.bv_val -= (long)d;
699 bdb_dn2id(be, txn, pdn, &d->parent);
707 key.size = sizeof(ID);
708 key.flags = DB_DBT_USERMEM;
711 data.size = sizeof(diskNode) + rlen + nrlen + 2;
712 data.flags = DB_DBT_USERMEM;
714 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
717 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
718 n = bdb_add_node( e->e_id, data.data, bdb);
719 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
722 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
723 bdb_insert_kid(n, bdb->bi_tree);
724 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
739 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
742 DB *db = bdb->bi_id2parent->bdi_db;
746 key.size = sizeof(e->e_id);
749 rc = db->del( db, txn, &key, 0);
751 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
752 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
755 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
756 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
757 (AVL_CMP)node_frdn_cmp);
758 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
761 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
765 bdb->bi_troot = NULL;
766 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
779 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
788 if (be_issuffix(be, in->bv_val)) {
793 rdns = ldap_explode_dn(in->bv_val, 0);
794 for (i=0; rdns[i]; i++);
801 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
802 for (--i; i>=0; i--) {
803 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
804 n = bdb_find_rdn_node(rdns[i], p->i_kids);
805 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
809 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
817 return n ? 0 : DB_NOTFOUND;
827 return bdb_dn2id_matched(be, txn, dn, id, NULL);
837 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
841 rc = bdb_dn2id(be, txn, dn, &id);
845 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
846 n = bdb_find_id_node(id, bdb->bi_tree);
847 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
855 /* Since we don't store IDLs for onelevel or subtree, we have to construct
856 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
864 return bdb_idl_insert(ids, n->i_id);
875 rc = bdb_idl_insert(ids, n->i_id);
877 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
878 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
880 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
892 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
897 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
898 BDB_IDL_ALL(bdb, ids);
902 rc = bdb_dn2id(be, NULL, dn, &id);
905 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
906 n = bdb_find_id_node(id, bdb->bi_tree);
907 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
910 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
911 if (prefix == DN_ONE_PREFIX) {
912 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
915 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
918 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
921 #endif /* BDB_HIER */