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 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
364 DB *db = bdb->bi_dn2id->bdi_db;
368 LDAP_LOG ( INDEX, ARGS,
369 "=> bdb_dn2id_children( %s )\n", dn->bv_val, 0, 0 );
371 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
376 key.size = dn->bv_len + 2;
377 key.data = ch_malloc( key.size );
378 ((char *)key.data)[0] = DN_ONE_PREFIX;
379 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
381 /* we actually could do a empty get... */
384 data.ulen = sizeof(id);
385 data.flags = DB_DBT_USERMEM;
387 data.dlen = sizeof(id);
389 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags | flags );
393 LDAP_LOG ( INDEX, DETAIL1,
394 "<= bdb_dn2id_children( %s ): %s (%d)\n",
395 dn->bv_val, rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
396 db_strerror(rc)), rc );
398 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
400 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
401 db_strerror(rc) ), rc );
416 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
417 DB *db = bdb->bi_dn2id->bdi_db;
420 LDAP_LOG ( INDEX, ARGS,
421 "=> bdb_dn2ididl( \"%s\" )\n", dn->bv_val, 0, 0 );
423 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
426 #ifndef BDB_MULTIPLE_SUFFIXES
427 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
429 BDB_IDL_ALL(bdb, ids);
435 key.size = dn->bv_len + 2;
437 key.flags = DB_DBT_USERMEM;
438 key.data = ch_malloc( key.size );
439 ((char *)key.data)[0] = prefix;
440 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
442 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
446 LDAP_LOG ( INDEX, ERR,
447 "<= bdb_dn2ididl: get failed: %s (%d)\n", db_strerror(rc), rc, 0 );
449 Debug( LDAP_DEBUG_TRACE,
450 "<= bdb_dn2idl: get failed: %s (%d)\n",
451 db_strerror( rc ), rc, 0 );
456 LDAP_LOG ( INDEX, RESULTS,
457 "<= bdb_dn2ididl: id=%ld first=%ld last=%ld\n",
458 (long) ids[0], (long) BDB_IDL_FIRST( ids ),
459 (long) BDB_IDL_LAST( ids ) );
461 Debug( LDAP_DEBUG_TRACE,
462 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
464 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
473 /* Experimental management routines for a hierarchically structured backend.
475 * Unsupported! Use at your own risk!
477 * Instead of a dn2id database, we use an id2parent database. Each entry in
478 * this database is a struct diskNode, containing the ID of the node's parent
479 * and the RDN of the node.
481 typedef struct diskNode {
487 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
488 * database into memory (into an AVL tree). Next we iterate through each node
489 * of this tree, connecting each child to its parent. The nodes in this AVL
490 * tree are a struct idNode. The immediate (Onelevel) children of a node are
491 * referenced in the i_kids AVL tree. With this arrangement, there is no need
492 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
493 * the DN of an entry is constructed by walking up the list of i_parent
494 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
495 * extremely efficient, even when operating on a populated subtree.
497 * The idNode tree is searched directly from the root when performing id to
498 * entry lookups. The tree is traversed using the i_kids subtrees when
499 * performing dn to id lookups.
501 typedef struct idNode {
503 struct idNode *i_parent;
506 ldap_pvt_thread_rdwr_t i_kids_rdwr;
510 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
511 * sorted in lexical order. These are the various helper routines used
512 * for the searches and sorts.
520 return *(const ID *)id - ((const idNode *)node)->i_id;
529 const struct berval *nrdn = v_nrdn;
530 const idNode *n = v_n;
531 return ber_bvcmp(nrdn, &n->i_rdn->nrdn);
540 const idNode *a = v_a, *b = v_b;
541 return a->i_id - b->i_id;
550 const idNode *a = v_a, *b = v_b;
551 /* should be slightly better without ordering drawbacks */
552 return ber_bvcmp(&a->i_rdn->nrdn, &b->i_rdn->nrdn);
555 idNode * bdb_find_id_node(
560 return avl_find(tree, &id, node_find_cmp);
563 idNode * bdb_find_rdn_node(
568 return avl_find(tree, nrdn, node_frdn_cmp);
571 /* This function links a node into its parent's i_kids tree. */
572 static int bdb_insert_kid(
578 Avlnode *tree = v_tree;
581 if (a->i_rdn->parent == 0)
583 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
586 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
587 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
588 node_rdn_cmp, avl_dup_error );
589 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
593 /* This function adds a node into the main AVL tree */
594 idNode *bdb_add_node(
602 node = (idNode *)ch_malloc(sizeof(idNode));
604 node->i_parent = NULL;
606 node->i_rdn = (diskNode *)d;
607 node->i_rdn->rdn.bv_val += (long)d;
608 node->i_rdn->nrdn.bv_val += (long)d;
609 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
610 avl_insert( &bdb->bi_tree, (caddr_t) node, node_add_cmp, avl_dup_error );
612 bdb->bi_troot = node;
616 /* This function initializes the trees at startup time. */
621 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
630 rc = bdb->bi_id2parent->bdi_db->cursor(
631 bdb->bi_id2parent->bdi_db, NULL, &cursor,
632 bdb->bi_db_opflags );
639 key.data = (char *)&id;
640 key.ulen = sizeof( id );
641 key.flags = DB_DBT_USERMEM;
642 data.flags = DB_DBT_MALLOC;
644 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
645 bdb_add_node( id, data.data, bdb );
647 cursor->c_close( cursor );
649 rc = avl_apply(bdb->bi_tree, bdb_insert_kid, bdb->bi_tree,
655 /* This function constructs a full DN for a given id. We really should
656 * be passing idNodes directly, to save some effort...
664 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
669 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
670 o = bdb_find_id_node(id, bdb->bi_tree);
671 rlen = be->be_suffix[0].bv_len + 1;
672 nrlen = be->be_nsuffix[0].bv_len + 1;
673 for (n = o; n && n->i_parent; n=n->i_parent) {
674 rlen += n->i_rdn->rdn.bv_len + 1;
675 nrlen += n->i_rdn->nrdn.bv_len + 1;
677 e->e_name.bv_len = rlen - 1;
678 e->e_nname.bv_len = nrlen - 1;
679 e->e_name.bv_val = ch_malloc(rlen + nrlen);
680 e->e_nname.bv_val = e->e_name.bv_val + rlen;
681 ptr = e->e_name.bv_val;
682 nptr = e->e_nname.bv_val;
683 for (n = o; n && n->i_parent; n=n->i_parent) {
684 ptr = lutil_strcopy(ptr, n->i_rdn->rdn.bv_val);
686 nptr = lutil_strcopy(nptr, n->i_rdn->nrdn.bv_val);
689 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
691 strcpy(ptr, be->be_suffix[0].bv_val);
692 strcpy(nptr, be->be_nsuffix[0].bv_val);
704 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
707 DB *db = bdb->bi_id2parent->bdi_db;
711 nrlen = dn_rdnlen( be, &e->e_nname );
713 rlen = dn_rdnlen( be, &e->e_name );
718 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
719 d->rdn.bv_len = rlen;
720 d->nrdn.bv_len = nrlen;
721 d->rdn.bv_val = (char *)(d+1);
722 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
723 strncpy(d->rdn.bv_val, e->e_dn, rlen);
724 d->rdn.bv_val[rlen] = '\0';
725 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
726 d->nrdn.bv_val[nrlen] = '\0';
727 d->rdn.bv_val -= (long)d;
728 d->nrdn.bv_val -= (long)d;
731 bdb_dn2id(be, txn, pdn, &d->parent, 0);
739 key.size = sizeof(ID);
740 key.flags = DB_DBT_USERMEM;
743 data.size = sizeof(diskNode) + rlen + nrlen + 2;
744 data.flags = DB_DBT_USERMEM;
746 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
749 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
750 n = bdb_add_node( e->e_id, data.data, bdb);
751 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
754 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
755 bdb_insert_kid(n, bdb->bi_tree);
756 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
771 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
774 DB *db = bdb->bi_id2parent->bdi_db;
778 key.size = sizeof(e->e_id);
781 rc = db->del( db, txn, &key, 0);
783 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
784 n = avl_delete(&bdb->bi_tree, &e->e_id, node_find_cmp);
787 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
788 avl_delete(&n->i_parent->i_kids, &n->i_rdn->nrdn, node_frdn_cmp);
789 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
792 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
796 bdb->bi_troot = NULL;
797 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
811 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
821 if (be_issuffix(be, in)) {
826 p1 = in->bv_val + in->bv_len - be->be_nsuffix[0].bv_len - 1;
829 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
831 for (p2 = p1-1; (p2 >= in->bv_val) && !DN_SEPARATOR(*p2); p2--);
833 rdn.bv_len = p1-rdn.bv_val;
836 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
837 n = bdb_find_rdn_node(&rdn, p->i_kids);
838 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
839 if (!n || p2 < in->bv_val) break;
842 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
863 return bdb_dn2id_matched(be, txn, dn, id, NULL, flags);
874 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
878 rc = bdb_dn2id(be, txn, dn, &id, flags);
882 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
883 n = bdb_find_id_node(id, bdb->bi_tree);
884 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
892 /* Since we don't store IDLs for onelevel or subtree, we have to construct
893 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
903 return bdb_idl_insert(ids, n->i_id);
916 rc = bdb_idl_insert(ids, n->i_id);
918 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
919 rc = avl_apply(n->i_kids, insert_sub, ids, -1, AVL_INORDER);
920 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
932 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
937 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
938 BDB_IDL_ALL(bdb, ids);
942 rc = bdb_dn2id(be, NULL, dn, &id, 0);
945 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
946 n = bdb_find_id_node(id, bdb->bi_tree);
947 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
950 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
951 if (prefix == DN_ONE_PREFIX) {
952 rc = avl_apply(n->i_kids, insert_one, ids, -1, AVL_INORDER);
957 rc = avl_apply(n->i_kids, insert_sub, ids, -1, AVL_INORDER);
959 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
962 #endif /* BDB_HIER */