1 /* dn2id.c - routines to deal with the dn2id index */
4 * Copyright 1998-2000 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;
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 = strlen( e->e_ndn ) + 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",
81 pdn[-1] = DN_SUBTREE_PREFIX;
82 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
85 Debug( LDAP_DEBUG_ANY,
86 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
91 pdn = dn_parent( be, pdn );
94 while( pdn != NULL ) {
95 pdn[-1] = DN_SUBTREE_PREFIX;
96 key.size -= pdn - ptr;
99 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
102 Debug( LDAP_DEBUG_ANY,
103 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
108 pdn = dn_parent( be, pdn );
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 = strlen( e->e_ndn ) + 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",
161 pdn = dn_parent( be, ptr );
164 key.size -= pdn - ptr;
165 pdn[-1] = DN_ONE_PREFIX;
168 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
171 Debug( LDAP_DEBUG_ANY,
172 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
177 pdn[-1] = DN_SUBTREE_PREFIX;
179 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
182 Debug( LDAP_DEBUG_ANY,
183 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
188 pdn = dn_parent( be, pdn );
191 while( pdn != NULL ) {
192 pdn[-1] = DN_SUBTREE_PREFIX;
193 key.size -= pdn - ptr;
196 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
198 Debug( LDAP_DEBUG_ANY,
199 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
204 pdn = dn_parent( be, pdn );
209 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
222 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
223 DB *db = bdb->bi_dn2id->bdi_db;
225 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
228 key.size = strlen( dn ) + 2;
229 key.data = ch_malloc( key.size );
230 ((char *)key.data)[0] = DN_BASE_PREFIX;
231 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
236 data.ulen = sizeof(ID);
237 data.flags = DB_DBT_USERMEM;
240 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
243 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
244 db_strerror( rc ), rc, 0 );
246 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
264 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
265 DB *db = bdb->bi_dn2id->bdi_db;
268 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
271 key.size = strlen( dn ) + 2;
272 key.data = ch_malloc( key.size );
273 ((char *)key.data)[0] = DN_BASE_PREFIX;
278 data.ulen = sizeof(ID);
279 data.flags = DB_DBT_USERMEM;
284 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
289 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
291 if( rc == DB_NOTFOUND ) {
292 char *pdn = dn_parent( be, dn );
294 if( pdn == NULL || *pdn == '\0' ) {
295 Debug( LDAP_DEBUG_TRACE,
296 "<= bdb_dn2id_matched: no match\n",
302 key.size = strlen( dn ) + 2;
304 } else if ( rc == 0 ) {
305 if( data.size != sizeof( ID ) ) {
306 Debug( LDAP_DEBUG_ANY,
307 "<= bdb_dn2id_matched: get size mismatch: "
308 "expected %ld, got %ld\n",
309 (long) sizeof(ID), (long) data.size, 0 );
313 *matchedDN = (char *) dn;
316 Debug( LDAP_DEBUG_TRACE,
317 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
318 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
322 Debug( LDAP_DEBUG_ANY,
323 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
324 db_strerror(rc), rc, 0 );
341 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
342 DB *db = bdb->bi_dn2id->bdi_db;
345 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
349 key.size = strlen( dn ) + 2;
350 key.data = ch_malloc( key.size );
351 ((char *)key.data)[0] = DN_ONE_PREFIX;
352 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
354 /* we actually could do a empty get... */
357 data.ulen = sizeof(id);
358 data.flags = DB_DBT_USERMEM;
360 data.dlen = sizeof(id);
362 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
364 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
366 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
367 db_strerror(rc) ), rc );
381 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
382 DB *db = bdb->bi_dn2id->bdi_db;
384 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn, 0, 0 );
386 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
388 BDB_IDL_ALL(bdb, ids);
393 key.size = strlen( dn ) + 2;
394 key.data = ch_malloc( key.size );
395 ((char *)key.data)[0] = prefix;
396 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
398 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
401 Debug( LDAP_DEBUG_TRACE,
402 "<= bdb_dn2idl: get failed: %s (%d)\n",
403 db_strerror( rc ), rc, 0 );
406 Debug( LDAP_DEBUG_TRACE,
407 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
409 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
417 /* Experimental management routines for a hierarchically structured backend.
419 * Unsupported! Use at your own risk!
421 * Instead of a dn2id database, we use an id2parent database. Each entry in
422 * this database is a struct diskNode, containing the ID of the node's parent
423 * and the RDN of the node.
425 typedef struct diskNode {
431 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
432 * database into memory (into an AVL tree). Next we iterate through each node
433 * of this tree, connecting each child to its parent. The nodes in this AVL
434 * tree are a struct idNode. The immediate (Onelevel) children of a node are
435 * referenced in the i_kids AVL tree. With this arrangement, there is no need
436 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
437 * the DN of an entry is constructed by walking up the list of i_parent
438 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
439 * extremely efficient, even when operating on a populated subtree.
441 * The idNode tree is searched directly from the root when performing id to
442 * entry lookups. The tree is traversed using the i_kids subtrees when
443 * performing dn to id lookups.
445 typedef struct idNode {
447 struct idNode *i_parent;
450 ldap_pvt_thread_rdwr_t i_kids_rdwr;
453 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
454 * the result string. This allows fast construction of catenated strings
455 * without the overhead of strlen/strcat.
466 while (*a++ = *b++) ;
470 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
471 * sorted in lexical order. These are the various helper routines used
472 * for the searches and sorts.
489 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
498 return a->i_id - b->i_id;
507 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
510 idNode * bdb_find_id_node(
515 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
518 idNode * bdb_find_rdn_node(
523 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
526 /* This function links a node into its parent's i_kids tree. */
534 if (a->i_rdn->parent == 0)
536 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
539 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
540 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
541 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
542 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
546 /* This function adds a node into the main AVL tree */
547 idNode *bdb_add_node(
555 node = (idNode *)ch_malloc(sizeof(idNode));
557 node->i_parent = NULL;
559 node->i_rdn = (diskNode *)d;
560 node->i_rdn->rdn.bv_val += (long)d;
561 node->i_rdn->nrdn.bv_val += (long)d;
562 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
563 avl_insert( &bdb->bi_tree, (caddr_t) node,
564 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
566 bdb->bi_troot = node;
570 /* This function initializes the trees at startup time. */
575 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
585 rc = bdb->bi_id2parent->bdi_db->cursor(
586 bdb->bi_id2parent->bdi_db, NULL, &cursor,
587 bdb->bi_db_opflags );
592 /* When be_suffix is turned into struct berval or LDAPDN
593 * life will get a lot easier... Since no DNs live on disk, we
594 * need to operate on the be_suffix to fully qualify our DNs.
595 * We need to know how many components are in the suffix DN,
596 * so we can tell where the suffix ends and our nodes begin.
598 * Note that this code always uses be_suffix[0], so defining
599 * multiple suffixes for a single backend won't work!
601 bdb->bi_sufflen = strlen(be->be_suffix[0]);
603 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
604 for (i=0; rdns[i]; i++);
610 key.data = (char *)&id;
611 key.ulen = sizeof( id );
612 key.flags = DB_DBT_USERMEM;
613 data.flags = DB_DBT_MALLOC;
615 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
616 bdb_add_node( id, data.data, bdb );
618 cursor->c_close( cursor );
620 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
626 /* This function constructs a full DN for a given id. We really should
627 * be passing idNodes directly, to save some effort...
635 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
640 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
641 o = bdb_find_id_node(id, bdb->bi_tree);
642 rlen = bdb->bi_sufflen + 1;
643 nrlen = be->be_nsuffix[0]->bv_len + 1;
644 for (n = o; n; n=n->i_parent) {
645 rlen += n->i_rdn->rdn.bv_len + 1;
646 nrlen += n->i_rdn->nrdn.bv_len + 1;
648 e->e_dn = ch_malloc(rlen + nrlen);
649 e->e_ndn = e->e_dn + rlen;
652 for (n = o; n; n=n->i_parent) {
653 ptr = bdb_strcopy(ptr, n->i_rdn->rdn.bv_val);
655 nptr = bdb_strcopy(nptr, n->i_rdn->nrdn.bv_val);
658 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
662 strcpy(ptr, be->be_suffix[0]);
663 strcpy(nptr, be->be_nsuffix[0]->bv_val);
675 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
678 DB *db = bdb->bi_id2parent->bdi_db;
682 nrlen = dn_rdnlen( be, e->e_ndn );
684 rlen = dn_rdnlen( be, e->e_dn );
689 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
690 d->rdn.bv_len = rlen;
691 d->nrdn.bv_len = nrlen;
692 d->rdn.bv_val = (char *)(d+1);
693 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
694 strncpy(d->rdn.bv_val, e->e_dn, rlen);
695 d->rdn.bv_val[rlen] = '\0';
696 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
697 d->nrdn.bv_val[nrlen] = '\0';
698 d->rdn.bv_val -= (long)d;
699 d->nrdn.bv_val -= (long)d;
702 bdb_dn2id(be, txn, pdn, &d->parent);
710 key.size = sizeof(ID);
711 key.flags = DB_DBT_USERMEM;
714 data.size = sizeof(diskNode) + rlen + nrlen + 2;
715 data.flags = DB_DBT_USERMEM;
717 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
720 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
721 n = bdb_add_node( e->e_id, data.data, bdb);
722 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
725 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
726 bdb_insert_kid(n, bdb->bi_tree);
727 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
742 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
745 DB *db = bdb->bi_id2parent->bdi_db;
749 key.size = sizeof(e->e_id);
752 rc = db->del( db, txn, &key, 0);
754 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
755 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
758 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
759 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
760 (AVL_CMP)node_frdn_cmp);
761 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
764 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
768 bdb->bi_troot = NULL;
769 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
782 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
791 if (be_issuffix(be, in)) {
796 rdns = ldap_explode_dn(in, 0);
797 for (i=0; rdns[i]; i++);
802 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
803 for (--i; i>=0; i--) {
804 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
805 n = bdb_find_rdn_node(rdns[i], p->i_kids);
806 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
810 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
814 } else if (matchedDN) {
818 for (j=i; rdns[j]; j++)
819 len += strlen(rdns[j]) + 1;
820 ptr = ch_malloc(len);
822 for (;rdns[i]; i++) {
823 ptr = bdb_strcopy(ptr, rdns[i]);
828 return n ? 0 : DB_NOTFOUND;
838 return bdb_dn2id_matched(be, txn, dn, id, NULL);
848 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
852 rc = bdb_dn2id(be, txn, dn, &id);
856 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
857 n = bdb_find_id_node(id, bdb->bi_tree);
858 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
866 /* Since we don't store IDLs for onelevel or subtree, we have to construct
867 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
875 return bdb_idl_insert(ids, n->i_id);
886 rc = bdb_idl_insert(ids, n->i_id);
888 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
889 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
891 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
903 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
908 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
909 BDB_IDL_ALL(bdb, ids);
913 rc = bdb_dn2id(be, NULL, dn, &id);
916 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
917 n = bdb_find_id_node(id, bdb->bi_tree);
918 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
921 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
922 if (prefix == DN_ONE_PREFIX) {
923 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
926 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
929 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
932 #endif /* BDB_HIER */