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>
26 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
27 DB *db = bdb->bi_dn2id->bdi_db;
29 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
30 e->e_ndn, (long) e->e_id, 0 );
31 assert( e->e_id != NOID );
34 key.size = strlen( e->e_ndn ) + 2;
35 key.data = ch_malloc( key.size );
36 ((char *)key.data)[0] = DN_BASE_PREFIX;
37 AC_MEMCPY( &((char *)key.data)[1], e->e_ndn, key.size - 1 );
40 data.data = (char *) &e->e_id;
41 data.size = sizeof( e->e_id );
43 /* store it -- don't override */
44 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
46 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
47 db_strerror(rc), rc, 0 );
52 ((char *)(key.data))[0] = DN_ONE_PREFIX;
55 key.size = strlen( pdn ) + 2;
56 AC_MEMCPY( &((char*)key.data)[1],
59 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
62 Debug( LDAP_DEBUG_ANY,
63 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
71 char **subtree = dn_subtree( be, e->e_ndn );
73 if( subtree != NULL ) {
75 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
76 for( i=0; subtree[i] != NULL; i++ ) {
77 if( be_issuffix( be, subtree[i] ))
79 key.size = strlen( subtree[i] ) + 2;
80 AC_MEMCPY( &((char *)key.data)[1],
81 subtree[i], key.size - 1 );
83 rc = bdb_idl_insert_key( be, db, txn, &key,
87 Debug( LDAP_DEBUG_ANY,
88 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
94 charray_free( subtree );
100 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;
117 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
121 key.size = strlen( dn ) + 2;
122 key.data = ch_malloc( key.size );
123 key.flags = DB_DBT_USERMEM;
124 ((char *)key.data)[0] = DN_BASE_PREFIX;
125 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
128 rc = db->del( db, txn, &key, 0 );
130 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
131 db_strerror(rc), rc, 0 );
136 ((char *)(key.data))[0] = DN_ONE_PREFIX;
139 key.size = strlen( pdn ) + 2;
140 AC_MEMCPY( &((char*)key.data)[1],
143 rc = bdb_idl_delete_key( be, db, txn, &key, id );
146 Debug( LDAP_DEBUG_ANY,
147 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
155 char **subtree = dn_subtree( be, dn );
157 if( subtree != NULL ) {
159 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
160 for( i=0; subtree[i] != NULL; i++ ) {
161 key.size = strlen( subtree[i] ) + 2;
162 AC_MEMCPY( &((char *)key.data)[1],
163 subtree[i], key.size - 1 );
165 rc = bdb_idl_delete_key( be, db, txn, &key, id );
168 Debug( LDAP_DEBUG_ANY,
169 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
171 charray_free( subtree );
176 charray_free( subtree );
182 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
195 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
196 DB *db = bdb->bi_dn2id->bdi_db;
198 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
201 key.size = strlen( dn ) + 2;
202 key.data = ch_malloc( key.size );
203 ((char *)key.data)[0] = DN_BASE_PREFIX;
204 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
209 data.ulen = sizeof(ID);
210 data.flags = DB_DBT_USERMEM;
213 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
216 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
217 db_strerror( rc ), rc, 0 );
219 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
237 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
238 DB *db = bdb->bi_dn2id->bdi_db;
242 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
245 key.size = strlen( dn ) + 2;
246 key.data = ch_malloc( key.size );
247 ((char *)key.data)[0] = DN_BASE_PREFIX;
252 data.ulen = sizeof(ID);
253 data.flags = DB_DBT_USERMEM;
258 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
263 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
265 if( rc == DB_NOTFOUND ) {
266 char *pdn = dn_parent( be, dn );
270 if( pdn == NULL || *pdn == '\0' ) {
271 Debug( LDAP_DEBUG_TRACE,
272 "<= bdb_dn2id_matched: no match\n",
280 key.size = strlen( dn ) + 2;
282 } else if ( rc == 0 ) {
283 if( data.size != sizeof( ID ) ) {
284 Debug( LDAP_DEBUG_ANY,
285 "<= bdb_dn2id_matched: get size mismatch: "
286 "expected %ld, got %ld\n",
287 (long) sizeof(ID), (long) data.size, 0 );
292 *matchedDN = (char *) dn;
295 Debug( LDAP_DEBUG_TRACE,
296 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
297 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
301 Debug( LDAP_DEBUG_ANY,
302 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
303 db_strerror(rc), rc, 0 );
321 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
322 DB *db = bdb->bi_dn2id->bdi_db;
325 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
329 key.size = strlen( dn ) + 2;
330 key.data = ch_malloc( key.size );
331 ((char *)key.data)[0] = DN_ONE_PREFIX;
332 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
334 /* we actually could do a empty get... */
337 data.ulen = sizeof(id);
338 data.flags = DB_DBT_USERMEM;
340 data.dlen = sizeof(id);
342 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
344 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
346 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
347 db_strerror(rc) ), rc );
361 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
362 DB *db = bdb->bi_dn2id->bdi_db;
364 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn, 0, 0 );
366 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
368 BDB_IDL_ALL(bdb, ids);
373 key.size = strlen( dn ) + 2;
374 key.data = ch_malloc( key.size );
375 ((char *)key.data)[0] = prefix;
376 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
378 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
381 Debug( LDAP_DEBUG_TRACE,
382 "<= bdb_dn2idl: get failed: %s (%d)\n",
383 db_strerror( rc ), rc, 0 );
386 Debug( LDAP_DEBUG_TRACE,
387 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
389 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
397 /* Experimental management routines for a hierarchically structured backend.
399 * Unsupported! Use at your own risk!
401 * Instead of a dn2id database, we use an id2parent database. Each entry in
402 * this database is a struct diskNode, containing the ID of the node's parent
403 * and the RDN of the node.
405 typedef struct diskNode {
411 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
412 * database into memory (into an AVL tree). Next we iterate through each node
413 * of this tree, connecting each child to its parent. The nodes in this AVL
414 * tree are a struct idNode. The immediate (Onelevel) children of a node are
415 * referenced in the i_kids AVL tree. With this arrangement, there is no need
416 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
417 * the DN of an entry is constructed by walking up the list of i_parent
418 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
419 * extremely efficient, even when operating on a populated subtree.
421 * The idNode tree is searched directly from the root when performing id to
422 * entry lookups. The tree is traversed using the i_kids subtrees when
423 * performing dn to id lookups.
425 typedef struct idNode {
427 struct idNode *i_parent;
430 ldap_pvt_thread_rdwr_t i_kids_rdwr;
433 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
434 * the result string. This allows fast construction of catenated strings
435 * without the overhead of strlen/strcat.
446 while (*a++ = *b++) ;
450 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
451 * sorted in lexical order. These are the various helper routines used
452 * for the searches and sorts.
469 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
478 return a->i_id - b->i_id;
487 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
490 idNode * bdb_find_id_node(
495 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
498 idNode * bdb_find_rdn_node(
503 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
506 /* This function links a node into its parent's i_kids tree. */
514 if (a->i_rdn->parent == 0)
516 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
519 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
520 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
521 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
522 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
526 /* This function adds a node into the main AVL tree */
527 idNode *bdb_add_node(
535 node = (idNode *)ch_malloc(sizeof(idNode));
537 node->i_parent = NULL;
539 node->i_rdn = (diskNode *)d;
540 node->i_rdn->rdn.bv_val += (long)d;
541 node->i_rdn->nrdn.bv_val += (long)d;
542 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
543 avl_insert( &bdb->bi_tree, (caddr_t) node,
544 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
546 bdb->bi_troot = node;
550 /* This function initializes the trees at startup time. */
555 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
565 rc = bdb->bi_id2parent->bdi_db->cursor(
566 bdb->bi_id2parent->bdi_db, NULL, &cursor,
567 bdb->bi_db_opflags );
572 /* When be_suffix is turned into struct berval or LDAPDN
573 * life will get a lot easier... Since no DNs live on disk, we
574 * need to operate on the be_suffix to fully qualify our DNs.
575 * We need to know how many components are in the suffix DN,
576 * so we can tell where the suffix ends and our nodes begin.
578 * Note that this code always uses be_suffix[0], so defining
579 * multiple suffixes for a single backend won't work!
581 bdb->bi_sufflen = strlen(be->be_suffix[0]);
583 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
584 for (i=0; rdns[i]; i++);
590 key.data = (char *)&id;
591 key.ulen = sizeof( id );
592 key.flags = DB_DBT_USERMEM;
593 data.flags = DB_DBT_MALLOC;
595 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
596 bdb_add_node( id, data.data, bdb );
598 cursor->c_close( cursor );
600 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
606 /* This function constructs a full DN for a given id. We really should
607 * be passing idNodes directly, to save some effort...
615 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
620 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
621 o = bdb_find_id_node(id, bdb->bi_tree);
622 rlen = bdb->bi_sufflen + 1;
623 nrlen = be->be_nsuffix[0]->bv_len + 1;
624 for (n = o; n; n=n->i_parent) {
625 rlen += n->i_rdn->rdn.bv_len + 1;
626 nrlen += n->i_rdn->nrdn.bv_len + 1;
628 e->e_dn = ch_malloc(rlen + nrlen);
629 e->e_ndn = e->e_dn + rlen;
632 for (n = o; n; n=n->i_parent) {
633 ptr = bdb_strcopy(ptr, n->i_rdn->rdn.bv_val);
635 nptr = bdb_strcopy(nptr, n->i_rdn->nrdn.bv_val);
638 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
642 strcpy(ptr, be->be_suffix[0]);
643 strcpy(nptr, be->be_nsuffix[0]->bv_val);
655 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
658 DB *db = bdb->bi_id2parent->bdi_db;
659 char *nrdn = dn_rdn( be, e->e_ndn );
668 rdn = dn_rdn( be, e->e_dn );
671 nrlen = strlen(nrdn);
673 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
674 d->rdn.bv_len = rlen;
675 d->nrdn.bv_len = nrlen;
676 d->rdn.bv_val = (char *)(d+1);
677 d->nrdn.bv_val = bdb_strcopy(d->rdn.bv_val, rdn) + 1;
678 strcpy(d->nrdn.bv_val, nrdn);
679 d->rdn.bv_val -= (long)d;
680 d->nrdn.bv_val -= (long)d;
682 if (nrdn[0]) free(nrdn);
683 if (rdn[0]) free(rdn);
686 bdb_dn2id(be, txn, pdn, &d->parent);
694 key.size = sizeof(ID);
695 key.flags = DB_DBT_USERMEM;
698 data.size = sizeof(diskNode) + rlen + nrlen + 2;
699 data.flags = DB_DBT_USERMEM;
701 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
704 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
705 n = bdb_add_node( e->e_id, data.data, bdb);
706 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
709 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
710 bdb_insert_kid(n, bdb->bi_tree);
711 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
727 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
730 DB *db = bdb->bi_id2parent->bdi_db;
734 key.size = sizeof(id);
737 rc = db->del( db, txn, &key, 0);
739 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
740 n = avl_delete(&bdb->bi_tree, (void *)id, (AVL_CMP)node_find_cmp);
743 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
744 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
745 (AVL_CMP)node_frdn_cmp);
746 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
749 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
753 bdb->bi_troot = NULL;
754 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
767 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
776 if (be_issuffix(be, in)) {
781 rdns = ldap_explode_dn(in, 0);
782 for (i=0; rdns[i]; i++);
787 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
788 for (--i; i>=0; i--) {
789 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
790 n = bdb_find_rdn_node(rdns[i], p->i_kids);
791 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
795 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
799 } else if (matchedDN) {
803 for (j=i; rdns[j]; j++)
804 len += strlen(rdns[j]) + 1;
805 ptr = ch_malloc(len);
807 for (;rdns[i]; i++) {
808 ptr = bdb_strcopy(ptr, rdns[i]);
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 */