1 /* dn2id.c - routines to deal with the dn2id index */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
25 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26 DB *db = bdb->bi_dn2id->bdi_db;
30 struct berval ptr, pdn;
33 LDAP_LOG ( INDEX, ARGS, "bdb_dn2id_add( \"%s\", 0x%08lx )\n",
34 e->e_ndn, (long) e->e_id, 0 );
36 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
37 e->e_ndn, (long) e->e_id, 0 );
39 assert( e->e_id != NOID );
42 key.size = e->e_nname.bv_len + 2;
44 key.flags = DB_DBT_USERMEM;
45 buf = ch_malloc( key.size );
47 buf[0] = DN_BASE_PREFIX;
49 ptr.bv_len = e->e_nname.bv_len;
50 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
51 ptr.bv_val[ptr.bv_len] = '\0';
54 data.data = (char *) &e->e_id;
55 data.size = sizeof( e->e_id );
57 /* store it -- don't override */
58 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
61 LDAP_LOG ( INDEX, ERR, "bdb_dn2id_add: put failed: %s %d\n",
62 db_strerror(rc), rc, 0 );
64 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
65 db_strerror(rc), rc, 0 );
70 #ifndef BDB_MULTIPLE_SUFFIXES
71 if( !be_issuffix( be, &ptr )) {
73 buf[0] = DN_SUBTREE_PREFIX;
74 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
77 LDAP_LOG ( INDEX, ERR,
78 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
81 Debug( LDAP_DEBUG_ANY,
82 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
88 #ifdef BDB_MULTIPLE_SUFFIXES
89 if( !be_issuffix( be, &ptr )) {
91 dnParent( &ptr, &pdn );
93 key.size = pdn.bv_len + 2;
95 pdn.bv_val[-1] = DN_ONE_PREFIX;
96 key.data = pdn.bv_val-1;
99 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
103 LDAP_LOG ( INDEX, ERR,
104 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
107 Debug( LDAP_DEBUG_ANY,
108 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
113 #ifndef BDB_MULTIPLE_SUFFIXES
116 while( !be_issuffix( be, &ptr )) {
120 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
122 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
126 LDAP_LOG ( INDEX, ERR,
127 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
130 Debug( LDAP_DEBUG_ANY,
131 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
136 #ifdef BDB_MULTIPLE_SUFFIXES
137 if( be_issuffix( be, &ptr )) break;
139 dnParent( &ptr, &pdn );
141 key.size = pdn.bv_len + 2;
143 key.data = pdn.bv_val - 1;
146 #ifdef BDB_MULTIPLE_SUFFIXES
153 LDAP_LOG ( INDEX, RESULTS, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
155 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
167 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
168 DB *db = bdb->bi_dn2id->bdi_db;
172 struct berval pdn, ptr;
175 LDAP_LOG ( INDEX, ARGS,
176 "=> bdb_dn2id_delete ( \"%s\", 0x%08lx )\n", e->e_ndn, e->e_id, 0);
178 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
179 e->e_ndn, e->e_id, 0 );
183 key.size = e->e_nname.bv_len + 2;
184 buf = ch_malloc( key.size );
186 key.flags = DB_DBT_USERMEM;
187 buf[0] = DN_BASE_PREFIX;
189 ptr.bv_len = e->e_nname.bv_len;
190 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
191 ptr.bv_val[ptr.bv_len] = '\0';
194 rc = db->del( db, txn, &key, 0 );
197 LDAP_LOG ( INDEX, ERR,
198 "=> bdb_dn2id_delete: delete failed: %s %d\n",
199 db_strerror(rc), rc, 0 );
201 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
202 db_strerror(rc), rc, 0 );
207 #ifndef BDB_MULTIPLE_SUFFIXES
208 if( !be_issuffix( be, &ptr )) {
210 buf[0] = DN_SUBTREE_PREFIX;
211 rc = db->del( db, txn, &key, 0 );
214 LDAP_LOG ( INDEX, ERR,
215 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
218 Debug( LDAP_DEBUG_ANY,
219 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
225 #ifdef BDB_MULTIPLE_SUFFIXES
226 if( !be_issuffix( be, &ptr )) {
228 dnParent( &ptr, &pdn );
230 key.size = pdn.bv_len + 2;
232 pdn.bv_val[-1] = DN_ONE_PREFIX;
233 key.data = pdn.bv_val - 1;
236 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
240 LDAP_LOG ( INDEX, ERR,
241 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
244 Debug( LDAP_DEBUG_ANY,
245 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
250 #ifndef BDB_MULTIPLE_SUFFIXES
253 while( !be_issuffix( be, &ptr )) {
257 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
259 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
262 LDAP_LOG ( INDEX, ERR,
263 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
266 Debug( LDAP_DEBUG_ANY,
267 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
272 #ifdef BDB_MULTIPLE_SUFFIXES
273 if( be_issuffix( be, &ptr )) break;
275 dnParent( &ptr, &pdn );
277 key.size = pdn.bv_len + 2;
279 key.data = pdn.bv_val - 1;
282 #ifdef BDB_MULTIPLE_SUFFIXES
289 LDAP_LOG ( INDEX, RESULTS, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
291 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
306 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
307 DB *db = bdb->bi_dn2id->bdi_db;
310 LDAP_LOG ( INDEX, ARGS, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
312 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
318 key.size = dn->bv_len + 2;
319 key.data = sl_malloc( key.size, ctx );
320 ((char *)key.data)[0] = DN_BASE_PREFIX;
321 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
326 data.ulen = sizeof(ID);
327 data.flags = DB_DBT_USERMEM;
330 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
334 LDAP_LOG ( INDEX, ERR, "<= bdb_dn2id: get failed %s (%d)\n",
335 db_strerror(rc), rc, 0 );
337 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
338 db_strerror( rc ), rc, 0 );
342 LDAP_LOG ( INDEX, RESULTS,
343 "<= bdb_dn2id: got id=0x%08lx\n", *id, 0, 0 );
345 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
350 sl_free( key.data, ctx );
363 LDAP_LOG ( INDEX, ARGS,
364 "=> bdb_dn2id_children( %s )\n", e->e_nname.bv_val, 0, 0 );
366 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
367 e->e_nname.bv_val, 0, 0 );
370 if ( BEI(e)->bei_kids ) {
373 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
378 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
379 DB *db = bdb->bi_dn2id->bdi_db;
383 key.size = e->e_nname.bv_len + 2;
384 key.data = sl_malloc( key.size, op->o_tmpmemctx );
385 ((char *)key.data)[0] = DN_ONE_PREFIX;
386 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
388 /* we actually could do a empty get... */
391 data.ulen = sizeof(id);
392 data.flags = DB_DBT_USERMEM;
394 data.dlen = sizeof(id);
396 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
397 sl_free( key.data, op->o_tmpmemctx );
399 if ( rc == DB_NOTFOUND ) {
400 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
406 LDAP_LOG ( INDEX, DETAIL1,
407 "<= bdb_dn2id_children( %s ): %s (%d)\n",
408 e->e_nname.bv_val, rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
409 db_strerror(rc)), rc );
411 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
413 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
414 db_strerror(rc) ), rc );
429 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
430 DB *db = bdb->bi_dn2id->bdi_db;
433 LDAP_LOG ( INDEX, ARGS,
434 "=> bdb_dn2ididl( \"%s\" )\n", dn->bv_val, 0, 0 );
436 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
439 #ifndef BDB_MULTIPLE_SUFFIXES
440 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
442 BDB_IDL_ALL(bdb, ids);
448 key.size = dn->bv_len + 2;
450 key.flags = DB_DBT_USERMEM;
451 key.data = ch_malloc( key.size );
452 ((char *)key.data)[0] = prefix;
453 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
455 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
459 LDAP_LOG ( INDEX, ERR,
460 "<= bdb_dn2ididl: get failed: %s (%d)\n", db_strerror(rc), rc, 0 );
462 Debug( LDAP_DEBUG_TRACE,
463 "<= bdb_dn2idl: get failed: %s (%d)\n",
464 db_strerror( rc ), rc, 0 );
469 LDAP_LOG ( INDEX, RESULTS,
470 "<= bdb_dn2ididl: id=%ld first=%ld last=%ld\n",
471 (long) ids[0], (long) BDB_IDL_FIRST( ids ),
472 (long) BDB_IDL_LAST( ids ) );
474 Debug( LDAP_DEBUG_TRACE,
475 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
477 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
486 /* Experimental management routines for a hierarchically structured backend.
488 * Unsupported! Use at your own risk!
490 * Instead of a dn2id database, we use an id2parent database. Each entry in
491 * this database is a struct diskNode, containing the ID of the node's parent
492 * and the RDN of the node.
494 typedef struct diskNode {
500 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
501 * database into memory (into an AVL tree). Next we iterate through each node
502 * of this tree, connecting each child to its parent. The nodes in this AVL
503 * tree are a struct idNode. The immediate (Onelevel) children of a node are
504 * referenced in the i_kids AVL tree. With this arrangement, there is no need
505 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
506 * the DN of an entry is constructed by walking up the list of i_parent
507 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
508 * extremely efficient, even when operating on a populated subtree.
510 * The idNode tree is searched directly from the root when performing id to
511 * entry lookups. The tree is traversed using the i_kids subtrees when
512 * performing dn to id lookups.
514 typedef struct idNode {
516 struct idNode *i_parent;
519 ldap_pvt_thread_rdwr_t i_kids_rdwr;
523 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
524 * sorted in lexical order. These are the various helper routines used
525 * for the searches and sorts.
533 return *(const ID *)id - ((const idNode *)node)->i_id;
542 const struct berval *nrdn = v_nrdn;
543 const idNode *n = v_n;
544 return ber_bvcmp(nrdn, &n->i_rdn->nrdn);
553 const idNode *a = v_a, *b = v_b;
554 return a->i_id - b->i_id;
563 const idNode *a = v_a, *b = v_b;
564 /* should be slightly better without ordering drawbacks */
565 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
568 idNode * bdb_find_id_node(
573 return avl_find(tree, &id, node_find_cmp);
576 idNode * bdb_find_rdn_node(
581 return avl_find(tree, nrdn, node_frdn_cmp);
584 /* This function links a node into its parent's i_kids tree. */
585 static int bdb_insert_kid(
591 Avlnode *tree = v_tree;
594 if (a->i_rdn->parent == 0)
596 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
599 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
600 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
601 node_rdn_cmp, avl_dup_error );
602 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
606 /* This function adds a node into the main AVL tree */
607 idNode *bdb_add_node(
615 node = (idNode *)ch_malloc(sizeof(idNode));
617 node->i_parent = NULL;
619 node->i_rdn = (diskNode *)d;
620 node->i_rdn->rdn.bv_val += (long)d;
621 node->i_rdn->nrdn.bv_val += (long)d;
622 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
623 avl_insert( &bdb->bi_tree, (caddr_t) node, node_add_cmp, avl_dup_error );
625 bdb->bi_troot = node;
629 /* This function initializes the trees at startup time. */
634 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
643 rc = bdb->bi_id2parent->bdi_db->cursor(
644 bdb->bi_id2parent->bdi_db, NULL, &cursor,
645 bdb->bi_db_opflags );
652 key.data = (char *)&id;
653 key.ulen = sizeof( id );
654 key.flags = DB_DBT_USERMEM;
655 data.flags = DB_DBT_MALLOC;
657 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
658 bdb_add_node( id, data.data, bdb );
660 cursor->c_close( cursor );
662 rc = avl_apply(bdb->bi_tree, bdb_insert_kid, bdb->bi_tree,
668 /* This function constructs a full DN for a given id. We really should
669 * be passing idNodes directly, to save some effort...
677 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
682 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
683 o = bdb_find_id_node(id, bdb->bi_tree);
684 rlen = be->be_suffix[0].bv_len + 1;
685 nrlen = be->be_nsuffix[0].bv_len + 1;
686 for (n = o; n && n->i_parent; n=n->i_parent) {
687 rlen += n->i_rdn->rdn.bv_len + 1;
688 nrlen += n->i_rdn->nrdn.bv_len + 1;
690 e->e_name.bv_len = rlen - 1;
691 e->e_nname.bv_len = nrlen - 1;
692 e->e_name.bv_val = ch_malloc(rlen + nrlen);
693 e->e_nname.bv_val = e->e_name.bv_val + rlen;
694 ptr = e->e_name.bv_val;
695 nptr = e->e_nname.bv_val;
696 for (n = o; n && n->i_parent; n=n->i_parent) {
697 ptr = lutil_strcopy(ptr, n->i_rdn->rdn.bv_val);
699 nptr = lutil_strcopy(nptr, n->i_rdn->nrdn.bv_val);
702 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
704 strcpy(ptr, be->be_suffix[0].bv_val);
705 strcpy(nptr, be->be_nsuffix[0].bv_val);
717 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
720 DB *db = bdb->bi_id2parent->bdi_db;
724 nrlen = dn_rdnlen( be, &e->e_nname );
726 rlen = dn_rdnlen( be, &e->e_name );
731 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
732 d->rdn.bv_len = rlen;
733 d->nrdn.bv_len = nrlen;
734 d->rdn.bv_val = (char *)(d+1);
735 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
736 strncpy(d->rdn.bv_val, e->e_dn, rlen);
737 d->rdn.bv_val[rlen] = '\0';
738 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
739 d->nrdn.bv_val[nrlen] = '\0';
740 d->rdn.bv_val -= (long)d;
741 d->nrdn.bv_val -= (long)d;
744 bdb_dn2id(be, txn, pdn, &d->parent, 0);
752 key.size = sizeof(ID);
753 key.flags = DB_DBT_USERMEM;
756 data.size = sizeof(diskNode) + rlen + nrlen + 2;
757 data.flags = DB_DBT_USERMEM;
759 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
762 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
763 n = bdb_add_node( e->e_id, data.data, bdb);
764 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
767 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
768 bdb_insert_kid(n, bdb->bi_tree);
769 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
784 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
787 DB *db = bdb->bi_id2parent->bdi_db;
791 key.size = sizeof(e->e_id);
794 rc = db->del( db, txn, &key, 0);
796 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
797 n = avl_delete(&bdb->bi_tree, &e->e_id, node_find_cmp);
800 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
801 avl_delete(&n->i_parent->i_kids, &n->i_rdn->nrdn, node_frdn_cmp);
802 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
805 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
809 bdb->bi_troot = NULL;
810 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
824 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
834 if (be_issuffix(be, in)) {
839 p1 = in->bv_val + in->bv_len - be->be_nsuffix[0].bv_len - 1;
842 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
844 for (p2 = p1-1; (p2 >= in->bv_val) && !DN_SEPARATOR(*p2); p2--);
846 rdn.bv_len = p1-rdn.bv_val;
849 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
850 n = bdb_find_rdn_node(&rdn, p->i_kids);
851 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
852 if (!n || p2 < in->bv_val) break;
855 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
876 return bdb_dn2id_matched(be, txn, dn, id, NULL, flags);
887 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
891 rc = bdb_dn2id(be, txn, dn, &id, flags);
895 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
896 n = bdb_find_id_node(id, bdb->bi_tree);
897 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
905 /* Since we don't store IDLs for onelevel or subtree, we have to construct
906 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
916 return bdb_idl_insert(ids, n->i_id);
929 rc = bdb_idl_insert(ids, n->i_id);
931 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
932 rc = avl_apply(n->i_kids, insert_sub, ids, -1, AVL_INORDER);
933 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
945 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
950 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
951 BDB_IDL_ALL(bdb, ids);
955 rc = bdb_dn2id(be, NULL, dn, &id, 0);
958 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
959 n = bdb_find_id_node(id, bdb->bi_tree);
960 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
963 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
964 if (prefix == DN_ONE_PREFIX) {
965 rc = avl_apply(n->i_kids, insert_one, ids, -1, AVL_INORDER);
970 rc = avl_apply(n->i_kids, insert_sub, ids, -1, AVL_INORDER);
972 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
975 #endif /* BDB_HIER */