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;
29 struct berval ptr, pdn;
31 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
32 e->e_ndn, (long) e->e_id, 0 );
33 assert( e->e_id != NOID );
36 key.size = e->e_nname.bv_len + 2;
37 buf = ch_malloc( key.size );
39 buf[0] = DN_BASE_PREFIX;
41 ptr.bv_len = e->e_nname.bv_len;
42 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
43 ptr.bv_val[ptr.bv_len] = '\0';
46 data.data = (char *) &e->e_id;
47 data.size = sizeof( e->e_id );
49 /* store it -- don't override */
50 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
52 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
53 db_strerror(rc), rc, 0 );
57 if( !be_issuffix( be, &ptr )) {
58 buf[0] = DN_SUBTREE_PREFIX;
59 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
61 Debug( LDAP_DEBUG_ANY,
62 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
67 dnParent( &ptr, &pdn );
69 key.size = pdn.bv_len + 2;
70 pdn.bv_val[-1] = DN_ONE_PREFIX;
71 key.data = pdn.bv_val-1;
74 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
77 Debug( LDAP_DEBUG_ANY,
78 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
84 while( !be_issuffix( be, &ptr )) {
85 ptr.bv_val[-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",
95 dnParent( &ptr, &pdn );
97 key.size = pdn.bv_len + 2;
98 key.data = pdn.bv_val - 1;
104 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
115 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
116 DB *db = bdb->bi_dn2id->bdi_db;
120 struct berval pdn, ptr;
122 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
123 e->e_ndn, e->e_id, 0 );
126 key.size = e->e_nname.bv_len + 2;
127 buf = ch_malloc( key.size );
129 key.flags = DB_DBT_USERMEM;
130 buf[0] = DN_BASE_PREFIX;
132 ptr.bv_len = e->e_nname.bv_len;
133 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
134 ptr.bv_val[ptr.bv_len] = '\0';
137 rc = db->del( db, txn, &key, 0 );
139 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
140 db_strerror(rc), rc, 0 );
144 if( !be_issuffix( be, &ptr )) {
145 buf[0] = DN_SUBTREE_PREFIX;
146 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
148 Debug( LDAP_DEBUG_ANY,
149 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
154 dnParent( &ptr, &pdn );
156 key.size = pdn.bv_len + 2;
157 pdn.bv_val[-1] = DN_ONE_PREFIX;
158 key.data = pdn.bv_val - 1;
161 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
164 Debug( LDAP_DEBUG_ANY,
165 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
171 while( !be_issuffix( be, &ptr )) {
172 ptr.bv_val[-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",
181 dnParent( &ptr, &pdn );
183 key.size = pdn.bv_len + 2;
184 key.data = pdn.bv_val - 1;
190 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
203 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
204 DB *db = bdb->bi_dn2id->bdi_db;
206 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
210 *id = bdb_cache_find_entry_ndn2id(be, &bdb->bi_cache, dn);
216 key.size = dn->bv_len + 2;
217 key.data = ch_malloc( key.size );
218 ((char *)key.data)[0] = DN_BASE_PREFIX;
219 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
224 data.ulen = sizeof(ID);
225 data.flags = DB_DBT_USERMEM;
228 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
231 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
232 db_strerror( rc ), rc, 0 );
234 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
252 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
253 DB *db = bdb->bi_dn2id->bdi_db;
258 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
261 key.size = in->bv_len + 2;
262 buf = ch_malloc( key.size );
265 dn.bv_len = key.size - 2;
266 AC_MEMCPY( dn.bv_val, in->bv_val, key.size - 1 );
271 data.ulen = sizeof(ID);
272 data.flags = DB_DBT_USERMEM;
275 dn.bv_val[-1] = DN_BASE_PREFIX;
280 cached_id = bdb_cache_find_entry_ndn2id(be, &bdb->bi_cache, &dn);
282 if (cached_id != NOID) {
285 if ( dn.bv_val != buf+1 ) {
291 rc = db->get(db, txn, &key, &data, bdb->bi_db_opflags );
294 if( rc == DB_NOTFOUND ) {
297 if ( ! be_issuffix( be, &dn ) ) {
298 dnParent( &dn, &pdn );
300 Debug( LDAP_DEBUG_TRACE,
301 "<= bdb_dn2id_matched: no match\n",
306 key.size = pdn.bv_len + 2;
308 key.data = pdn.bv_val - 1;
310 } else if ( rc == 0 ) {
311 if( data.size != sizeof( ID ) ) {
312 Debug( LDAP_DEBUG_ANY,
313 "<= bdb_dn2id_matched: get size mismatch: "
314 "expected %ld, got %ld\n",
315 (long) sizeof(ID), (long) data.size, 0 );
318 if( dn.bv_val != buf+1 ) {
322 Debug( LDAP_DEBUG_TRACE,
323 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
324 (long) *id, *id2 == 0 ? "entry" : "matched", dn.bv_val );
328 Debug( LDAP_DEBUG_ANY,
329 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
330 db_strerror(rc), rc, 0 );
347 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
348 DB *db = bdb->bi_dn2id->bdi_db;
351 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
355 key.size = dn->bv_len + 2;
356 key.data = ch_malloc( key.size );
357 ((char *)key.data)[0] = DN_ONE_PREFIX;
358 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
360 /* we actually could do a empty get... */
363 data.ulen = sizeof(id);
364 data.flags = DB_DBT_USERMEM;
366 data.dlen = sizeof(id);
368 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
371 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
373 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
374 db_strerror(rc) ), rc );
388 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
389 DB *db = bdb->bi_dn2id->bdi_db;
391 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
393 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
395 BDB_IDL_ALL(bdb, ids);
400 key.size = dn->bv_len + 2;
401 key.data = ch_malloc( key.size );
402 ((char *)key.data)[0] = prefix;
403 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
405 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
408 Debug( LDAP_DEBUG_TRACE,
409 "<= bdb_dn2idl: get failed: %s (%d)\n",
410 db_strerror( rc ), rc, 0 );
413 Debug( LDAP_DEBUG_TRACE,
414 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
416 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
424 /* Experimental management routines for a hierarchically structured backend.
426 * Unsupported! Use at your own risk!
428 * Instead of a dn2id database, we use an id2parent database. Each entry in
429 * this database is a struct diskNode, containing the ID of the node's parent
430 * and the RDN of the node.
432 typedef struct diskNode {
438 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
439 * database into memory (into an AVL tree). Next we iterate through each node
440 * of this tree, connecting each child to its parent. The nodes in this AVL
441 * tree are a struct idNode. The immediate (Onelevel) children of a node are
442 * referenced in the i_kids AVL tree. With this arrangement, there is no need
443 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
444 * the DN of an entry is constructed by walking up the list of i_parent
445 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
446 * extremely efficient, even when operating on a populated subtree.
448 * The idNode tree is searched directly from the root when performing id to
449 * entry lookups. The tree is traversed using the i_kids subtrees when
450 * performing dn to id lookups.
452 typedef struct idNode {
454 struct idNode *i_parent;
457 ldap_pvt_thread_rdwr_t i_kids_rdwr;
461 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
462 * sorted in lexical order. These are the various helper routines used
463 * for the searches and sorts.
480 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
489 return a->i_id - b->i_id;
499 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
501 /* should be slightly better without ordering drawbacks */
502 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
505 idNode * bdb_find_id_node(
510 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
513 idNode * bdb_find_rdn_node(
518 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
521 /* This function links a node into its parent's i_kids tree. */
529 if (a->i_rdn->parent == 0)
531 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
534 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
535 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
536 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
537 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
541 /* This function adds a node into the main AVL tree */
542 idNode *bdb_add_node(
550 node = (idNode *)ch_malloc(sizeof(idNode));
552 node->i_parent = NULL;
554 node->i_rdn = (diskNode *)d;
555 node->i_rdn->rdn.bv_val += (long)d;
556 node->i_rdn->nrdn.bv_val += (long)d;
557 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
558 avl_insert( &bdb->bi_tree, (caddr_t) node,
559 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
561 bdb->bi_troot = node;
565 /* This function initializes the trees at startup time. */
570 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
580 rc = bdb->bi_id2parent->bdi_db->cursor(
581 bdb->bi_id2parent->bdi_db, NULL, &cursor,
582 bdb->bi_db_opflags );
587 /* When be_suffix is turned into struct berval or LDAPDN
588 * life will get a lot easier... Since no DNs live on disk, we
589 * need to operate on the be_suffix to fully qualify our DNs.
590 * We need to know how many components are in the suffix DN,
591 * so we can tell where the suffix ends and our nodes begin.
593 * Note that this code always uses be_suffix[0], so defining
594 * multiple suffixes for a single backend won't work!
596 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
597 for (i=0; rdns[i]; i++);
603 key.data = (char *)&id;
604 key.ulen = sizeof( id );
605 key.flags = DB_DBT_USERMEM;
606 data.flags = DB_DBT_MALLOC;
608 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
609 bdb_add_node( id, data.data, bdb );
611 cursor->c_close( cursor );
613 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
619 /* This function constructs a full DN for a given id. We really should
620 * be passing idNodes directly, to save some effort...
628 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
633 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
634 o = bdb_find_id_node(id, bdb->bi_tree);
635 rlen = be->be_suffix[0]->bv_len + 1;
636 nrlen = be->be_nsuffix[0]->bv_len + 1;
637 for (n = o; n && n->i_parent; n=n->i_parent) {
638 rlen += n->i_rdn->rdn.bv_len + 1;
639 nrlen += n->i_rdn->nrdn.bv_len + 1;
641 e->e_name.bv_len = rlen - 1;
642 e->e_nname.bv_len = nrlen - 1;
643 e->e_name.bv_val = ch_malloc(rlen + nrlen);
644 e->e_nname.bv_val = e->e_name.bv_val + rlen;
645 ptr = e->e_name.bv_val;
646 nptr = e->e_nname.bv_val;
647 for (n = o; n && n->i_parent; n=n->i_parent) {
648 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
650 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
653 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
655 strcpy(ptr, be->be_suffix[0]->bv_val);
656 strcpy(nptr, be->be_nsuffix[0]->bv_val);
668 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
671 DB *db = bdb->bi_id2parent->bdi_db;
675 nrlen = dn_rdnlen( be, &e->e_nname );
677 rlen = dn_rdnlen( be, &e->e_name );
682 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
683 d->rdn.bv_len = rlen;
684 d->nrdn.bv_len = nrlen;
685 d->rdn.bv_val = (char *)(d+1);
686 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
687 strncpy(d->rdn.bv_val, e->e_dn, rlen);
688 d->rdn.bv_val[rlen] = '\0';
689 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
690 d->nrdn.bv_val[nrlen] = '\0';
691 d->rdn.bv_val -= (long)d;
692 d->nrdn.bv_val -= (long)d;
695 bdb_dn2id(be, txn, pdn, &d->parent);
703 key.size = sizeof(ID);
704 key.flags = DB_DBT_USERMEM;
707 data.size = sizeof(diskNode) + rlen + nrlen + 2;
708 data.flags = DB_DBT_USERMEM;
710 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
713 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
714 n = bdb_add_node( e->e_id, data.data, bdb);
715 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
718 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
719 bdb_insert_kid(n, bdb->bi_tree);
720 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
735 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
738 DB *db = bdb->bi_id2parent->bdi_db;
742 key.size = sizeof(e->e_id);
745 rc = db->del( db, txn, &key, 0);
747 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
748 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
751 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
752 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
753 (AVL_CMP)node_frdn_cmp);
754 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
757 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
761 bdb->bi_troot = NULL;
762 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
775 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
784 if (be_issuffix(be, in)) {
789 rdns = ldap_explode_dn(in->bv_val, 0);
790 for (i=0; rdns[i]; i++);
797 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
798 for (--i; i>=0; i--) {
799 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
800 n = bdb_find_rdn_node(rdns[i], p->i_kids);
801 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
805 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
813 return n ? 0 : DB_NOTFOUND;
823 return bdb_dn2id_matched(be, txn, dn, id, NULL);
833 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
837 rc = bdb_dn2id(be, txn, dn, &id);
841 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
842 n = bdb_find_id_node(id, bdb->bi_tree);
843 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
851 /* Since we don't store IDLs for onelevel or subtree, we have to construct
852 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
860 return bdb_idl_insert(ids, n->i_id);
871 rc = bdb_idl_insert(ids, n->i_id);
873 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
874 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
876 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
888 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
893 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
894 BDB_IDL_ALL(bdb, ids);
898 rc = bdb_dn2id(be, NULL, dn, &id);
901 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
902 n = bdb_find_id_node(id, bdb->bi_tree);
903 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
906 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
907 if (prefix == DN_ONE_PREFIX) {
908 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
911 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
914 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
917 #endif /* BDB_HIER */