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 );
55 if( !be_issuffix( be, ptr )) {
56 buf[0] = DN_SUBTREE_PREFIX;
57 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
59 Debug( LDAP_DEBUG_ANY,
60 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
65 rc = dnParent( ptr, (const char **)&pdn );
66 if ( rc != LDAP_SUCCESS ) {
67 Debug( LDAP_DEBUG_ANY,
68 "=> bdb_dn2id_add: dnParent(\"%s\") failed\n",
73 key.size -= pdn - ptr;
74 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( pdn != NULL ) {
88 if ( be_issuffix( be, pdn ))
90 pdn[-1] = DN_SUBTREE_PREFIX;
92 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
95 Debug( LDAP_DEBUG_ANY,
96 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
101 if ( be_issuffix( be, pdn ) ) {
104 rc = dnParent( pdn, &pdn );
105 if ( rc != LDAP_SUCCESS ) {
106 Debug( LDAP_DEBUG_ANY,
107 "=> bdb_dn2id_add: dnParent(\"%s\") failed\n",
112 key.size -= pdn - ptr;
118 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
129 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
130 DB *db = bdb->bi_dn2id->bdi_db;
135 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
136 e->e_ndn, e->e_id, 0 );
139 key.size = e->e_nname.bv_len + 2;
140 buf = ch_malloc( key.size );
142 key.flags = DB_DBT_USERMEM;
143 buf[0] = DN_BASE_PREFIX;
145 AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
148 rc = db->del( db, txn, &key, 0 );
150 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
151 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 - ptr;
175 pdn[-1] = DN_ONE_PREFIX;
178 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
181 Debug( LDAP_DEBUG_ANY,
182 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
188 while( pdn != NULL ) {
189 if ( be_issuffix( be, pdn ))
192 pdn[-1] = DN_SUBTREE_PREFIX;
194 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
196 Debug( LDAP_DEBUG_ANY,
197 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
202 rc = dnParent( pdn, &pdn );
203 if ( rc != LDAP_SUCCESS ) {
204 Debug( LDAP_DEBUG_ANY,
205 "=> bdb_dn2id_delete: dnParent(\"%s\") failed\n",
210 key.size -= pdn - ptr;
216 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
229 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
230 DB *db = bdb->bi_dn2id->bdi_db;
232 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
235 key.size = dn->bv_len + 2;
236 key.data = ch_malloc( key.size );
237 ((char *)key.data)[0] = DN_BASE_PREFIX;
238 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
243 data.ulen = sizeof(ID);
244 data.flags = DB_DBT_USERMEM;
247 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
250 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
251 db_strerror( rc ), rc, 0 );
253 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
271 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
272 DB *db = bdb->bi_dn2id->bdi_db;
275 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
278 key.size = in->bv_len + 2;
279 buf = ch_malloc( key.size );
282 AC_MEMCPY( dn, in->bv_val, key.size - 1 );
287 data.ulen = sizeof(ID);
288 data.flags = DB_DBT_USERMEM;
291 dn[-1] = DN_BASE_PREFIX;
296 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
298 if( rc == DB_NOTFOUND ) {
301 if ( ! be_issuffix( be, dn ) ) {
302 rc = dnParent( dn, &pdn );
303 if ( rc != LDAP_SUCCESS ) {
304 Debug( LDAP_DEBUG_TRACE,
305 "<= bdb_dn2id_matched: dnParent(\"%s\") failed\n",
311 if( pdn == NULL || *pdn == '\0' ) {
312 Debug( LDAP_DEBUG_TRACE,
313 "<= bdb_dn2id_matched: no match\n",
318 key.size -= pdn - dn;
322 } else if ( rc == 0 ) {
323 if( data.size != sizeof( ID ) ) {
324 Debug( LDAP_DEBUG_ANY,
325 "<= bdb_dn2id_matched: get size mismatch: "
326 "expected %ld, got %ld\n",
327 (long) sizeof(ID), (long) data.size, 0 );
334 Debug( LDAP_DEBUG_TRACE,
335 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
336 (long) *id, *id2 == 0 ? "entry" : "matched", dn );
340 Debug( LDAP_DEBUG_ANY,
341 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
342 db_strerror(rc), rc, 0 );
359 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
360 DB *db = bdb->bi_dn2id->bdi_db;
363 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
367 key.size = dn->bv_len + 2;
368 key.data = ch_malloc( key.size );
369 ((char *)key.data)[0] = DN_ONE_PREFIX;
370 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
372 /* we actually could do a empty get... */
375 data.ulen = sizeof(id);
376 data.flags = DB_DBT_USERMEM;
378 data.dlen = sizeof(id);
380 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
383 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
385 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
386 db_strerror(rc) ), rc );
400 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
401 DB *db = bdb->bi_dn2id->bdi_db;
403 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
405 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val))
407 BDB_IDL_ALL(bdb, ids);
412 key.size = dn->bv_len + 2;
413 key.data = ch_malloc( key.size );
414 ((char *)key.data)[0] = prefix;
415 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
417 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
420 Debug( LDAP_DEBUG_TRACE,
421 "<= bdb_dn2idl: get failed: %s (%d)\n",
422 db_strerror( rc ), rc, 0 );
425 Debug( LDAP_DEBUG_TRACE,
426 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
428 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
436 /* Experimental management routines for a hierarchically structured backend.
438 * Unsupported! Use at your own risk!
440 * Instead of a dn2id database, we use an id2parent database. Each entry in
441 * this database is a struct diskNode, containing the ID of the node's parent
442 * and the RDN of the node.
444 typedef struct diskNode {
450 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
451 * database into memory (into an AVL tree). Next we iterate through each node
452 * of this tree, connecting each child to its parent. The nodes in this AVL
453 * tree are a struct idNode. The immediate (Onelevel) children of a node are
454 * referenced in the i_kids AVL tree. With this arrangement, there is no need
455 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
456 * the DN of an entry is constructed by walking up the list of i_parent
457 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
458 * extremely efficient, even when operating on a populated subtree.
460 * The idNode tree is searched directly from the root when performing id to
461 * entry lookups. The tree is traversed using the i_kids subtrees when
462 * performing dn to id lookups.
464 typedef struct idNode {
466 struct idNode *i_parent;
469 ldap_pvt_thread_rdwr_t i_kids_rdwr;
473 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
474 * sorted in lexical order. These are the various helper routines used
475 * for the searches and sorts.
492 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
501 return a->i_id - b->i_id;
511 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
513 /* should be slightly better without ordering drawbacks */
514 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
517 idNode * bdb_find_id_node(
522 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
525 idNode * bdb_find_rdn_node(
530 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
533 /* This function links a node into its parent's i_kids tree. */
541 if (a->i_rdn->parent == 0)
543 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
546 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
547 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
548 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
549 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
553 /* This function adds a node into the main AVL tree */
554 idNode *bdb_add_node(
562 node = (idNode *)ch_malloc(sizeof(idNode));
564 node->i_parent = NULL;
566 node->i_rdn = (diskNode *)d;
567 node->i_rdn->rdn.bv_val += (long)d;
568 node->i_rdn->nrdn.bv_val += (long)d;
569 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
570 avl_insert( &bdb->bi_tree, (caddr_t) node,
571 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
573 bdb->bi_troot = node;
577 /* This function initializes the trees at startup time. */
582 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
592 rc = bdb->bi_id2parent->bdi_db->cursor(
593 bdb->bi_id2parent->bdi_db, NULL, &cursor,
594 bdb->bi_db_opflags );
599 /* When be_suffix is turned into struct berval or LDAPDN
600 * life will get a lot easier... Since no DNs live on disk, we
601 * need to operate on the be_suffix to fully qualify our DNs.
602 * We need to know how many components are in the suffix DN,
603 * so we can tell where the suffix ends and our nodes begin.
605 * Note that this code always uses be_suffix[0], so defining
606 * multiple suffixes for a single backend won't work!
608 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
609 for (i=0; rdns[i]; i++);
615 key.data = (char *)&id;
616 key.ulen = sizeof( id );
617 key.flags = DB_DBT_USERMEM;
618 data.flags = DB_DBT_MALLOC;
620 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
621 bdb_add_node( id, data.data, bdb );
623 cursor->c_close( cursor );
625 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
631 /* This function constructs a full DN for a given id. We really should
632 * be passing idNodes directly, to save some effort...
640 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
645 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
646 o = bdb_find_id_node(id, bdb->bi_tree);
647 rlen = be->be_suffix[0]->bv_len + 1;
648 nrlen = be->be_nsuffix[0]->bv_len + 1;
649 for (n = o; n && n->i_parent; n=n->i_parent) {
650 rlen += n->i_rdn->rdn.bv_len + 1;
651 nrlen += n->i_rdn->nrdn.bv_len + 1;
653 e->e_name.bv_len = rlen - 1;
654 e->e_nname.bv_len = nrlen - 1;
655 e->e_name.bv_val = ch_malloc(rlen + nrlen);
656 e->e_nname.bv_val = e->e_name.bv_val + rlen;
657 ptr = e->e_name.bv_val;
658 nptr = e->e_nname.bv_val;
659 for (n = o; n && n->i_parent; n=n->i_parent) {
660 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
662 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
665 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
667 strcpy(ptr, be->be_suffix[0]->bv_val);
668 strcpy(nptr, be->be_nsuffix[0]->bv_val);
680 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
683 DB *db = bdb->bi_id2parent->bdi_db;
687 nrlen = dn_rdnlen( be, &e->e_nname );
689 rlen = dn_rdnlen( be, &e->e_name );
694 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
695 d->rdn.bv_len = rlen;
696 d->nrdn.bv_len = nrlen;
697 d->rdn.bv_val = (char *)(d+1);
698 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
699 strncpy(d->rdn.bv_val, e->e_dn, rlen);
700 d->rdn.bv_val[rlen] = '\0';
701 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
702 d->nrdn.bv_val[nrlen] = '\0';
703 d->rdn.bv_val -= (long)d;
704 d->nrdn.bv_val -= (long)d;
707 bdb_dn2id(be, txn, pdn, &d->parent);
715 key.size = sizeof(ID);
716 key.flags = DB_DBT_USERMEM;
719 data.size = sizeof(diskNode) + rlen + nrlen + 2;
720 data.flags = DB_DBT_USERMEM;
722 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
725 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
726 n = bdb_add_node( e->e_id, data.data, bdb);
727 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
730 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
731 bdb_insert_kid(n, bdb->bi_tree);
732 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
747 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
750 DB *db = bdb->bi_id2parent->bdi_db;
754 key.size = sizeof(e->e_id);
757 rc = db->del( db, txn, &key, 0);
759 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
760 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
763 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
764 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
765 (AVL_CMP)node_frdn_cmp);
766 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
769 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
773 bdb->bi_troot = NULL;
774 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
787 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
796 if (be_issuffix(be, in->bv_val)) {
801 rdns = ldap_explode_dn(in->bv_val, 0);
802 for (i=0; rdns[i]; i++);
809 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
810 for (--i; i>=0; i--) {
811 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
812 n = bdb_find_rdn_node(rdns[i], p->i_kids);
813 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
817 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
825 return n ? 0 : DB_NOTFOUND;
835 return bdb_dn2id_matched(be, txn, dn, id, NULL);
845 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
849 rc = bdb_dn2id(be, txn, dn, &id);
853 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
854 n = bdb_find_id_node(id, bdb->bi_tree);
855 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
863 /* Since we don't store IDLs for onelevel or subtree, we have to construct
864 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
872 return bdb_idl_insert(ids, n->i_id);
883 rc = bdb_idl_insert(ids, n->i_id);
885 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
886 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
888 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
900 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
905 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
906 BDB_IDL_ALL(bdb, ids);
910 rc = bdb_dn2id(be, NULL, dn, &id);
913 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
914 n = bdb_find_id_node(id, bdb->bi_tree);
915 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
918 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
919 if (prefix == DN_ONE_PREFIX) {
920 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
923 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
926 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
929 #endif /* BDB_HIER */