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;
28 char *buf, *ptr, *pdn;
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 = e->e_nname.bv_len + 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",
82 while( pdn != NULL ) {
83 if ( be_issuffix( be, pdn ))
85 pdn[-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",
96 pdn = dn_parent( be, pdn );
97 key.size -= pdn - ptr;
103 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;
120 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
121 e->e_ndn, e->e_id, 0 );
124 key.size = e->e_nname.bv_len + 2;
125 buf = ch_malloc( key.size );
127 key.flags = DB_DBT_USERMEM;
128 buf[0] = DN_BASE_PREFIX;
130 AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
133 rc = db->del( db, txn, &key, 0 );
135 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
136 db_strerror(rc), rc, 0 );
140 if( !be_issuffix( be, ptr )) {
141 buf[0] = DN_SUBTREE_PREFIX;
142 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
144 Debug( LDAP_DEBUG_ANY,
145 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
151 pdn = dn_parent( be, ptr );
154 key.size -= pdn - ptr;
155 pdn[-1] = DN_ONE_PREFIX;
158 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
161 Debug( LDAP_DEBUG_ANY,
162 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
168 while( pdn != NULL ) {
169 if ( be_issuffix( be, pdn ))
172 pdn[-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",
182 pdn = dn_parent( be, pdn );
183 key.size -= pdn - ptr;
189 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
202 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
203 DB *db = bdb->bi_dn2id->bdi_db;
205 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
208 key.size = dn->bv_len + 2;
209 key.data = ch_malloc( key.size );
210 ((char *)key.data)[0] = DN_BASE_PREFIX;
211 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
216 data.ulen = sizeof(ID);
217 data.flags = DB_DBT_USERMEM;
220 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
223 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
224 db_strerror( rc ), rc, 0 );
226 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
244 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
245 DB *db = bdb->bi_dn2id->bdi_db;
248 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
251 key.size = in->bv_len + 2;
252 buf = ch_malloc( key.size );
255 AC_MEMCPY( dn, in->bv_val, key.size - 1 );
260 data.ulen = sizeof(ID);
261 data.flags = DB_DBT_USERMEM;
266 dn[-1] = DN_BASE_PREFIX;
271 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
273 if( rc == DB_NOTFOUND ) {
274 char *pdn = dn_parent( be, dn );
276 if( pdn == NULL || *pdn == '\0' ) {
277 Debug( LDAP_DEBUG_TRACE,
278 "<= bdb_dn2id_matched: no match\n",
283 key.size -= pdn - dn;
287 } else if ( rc == 0 ) {
288 if( data.size != sizeof( ID ) ) {
289 Debug( LDAP_DEBUG_ANY,
290 "<= bdb_dn2id_matched: get size mismatch: "
291 "expected %ld, got %ld\n",
292 (long) sizeof(ID), (long) data.size, 0 );
296 *matchedDN = (char *) dn;
299 Debug( LDAP_DEBUG_TRACE,
300 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
301 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
305 Debug( LDAP_DEBUG_ANY,
306 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
307 db_strerror(rc), rc, 0 );
324 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
325 DB *db = bdb->bi_dn2id->bdi_db;
328 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
332 key.size = dn->bv_len + 2;
333 key.data = ch_malloc( key.size );
334 ((char *)key.data)[0] = DN_ONE_PREFIX;
335 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
337 /* we actually could do a empty get... */
340 data.ulen = sizeof(id);
341 data.flags = DB_DBT_USERMEM;
343 data.dlen = sizeof(id);
345 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
347 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
349 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
350 db_strerror(rc) ), rc );
364 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
365 DB *db = bdb->bi_dn2id->bdi_db;
367 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
369 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val))
371 BDB_IDL_ALL(bdb, ids);
376 key.size = dn->bv_len + 2;
377 key.data = ch_malloc( key.size );
378 ((char *)key.data)[0] = prefix;
379 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
381 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
384 Debug( LDAP_DEBUG_TRACE,
385 "<= bdb_dn2idl: get failed: %s (%d)\n",
386 db_strerror( rc ), rc, 0 );
389 Debug( LDAP_DEBUG_TRACE,
390 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
392 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
400 /* Experimental management routines for a hierarchically structured backend.
402 * Unsupported! Use at your own risk!
404 * Instead of a dn2id database, we use an id2parent database. Each entry in
405 * this database is a struct diskNode, containing the ID of the node's parent
406 * and the RDN of the node.
408 typedef struct diskNode {
414 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
415 * database into memory (into an AVL tree). Next we iterate through each node
416 * of this tree, connecting each child to its parent. The nodes in this AVL
417 * tree are a struct idNode. The immediate (Onelevel) children of a node are
418 * referenced in the i_kids AVL tree. With this arrangement, there is no need
419 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
420 * the DN of an entry is constructed by walking up the list of i_parent
421 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
422 * extremely efficient, even when operating on a populated subtree.
424 * The idNode tree is searched directly from the root when performing id to
425 * entry lookups. The tree is traversed using the i_kids subtrees when
426 * performing dn to id lookups.
428 typedef struct idNode {
430 struct idNode *i_parent;
433 ldap_pvt_thread_rdwr_t i_kids_rdwr;
437 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
438 * sorted in lexical order. These are the various helper routines used
439 * for the searches and sorts.
456 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
465 return a->i_id - b->i_id;
474 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
477 idNode * bdb_find_id_node(
482 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
485 idNode * bdb_find_rdn_node(
490 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
493 /* This function links a node into its parent's i_kids tree. */
501 if (a->i_rdn->parent == 0)
503 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
506 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
507 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
508 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
509 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
513 /* This function adds a node into the main AVL tree */
514 idNode *bdb_add_node(
522 node = (idNode *)ch_malloc(sizeof(idNode));
524 node->i_parent = NULL;
526 node->i_rdn = (diskNode *)d;
527 node->i_rdn->rdn.bv_val += (long)d;
528 node->i_rdn->nrdn.bv_val += (long)d;
529 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
530 avl_insert( &bdb->bi_tree, (caddr_t) node,
531 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
533 bdb->bi_troot = node;
537 /* This function initializes the trees at startup time. */
542 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
552 rc = bdb->bi_id2parent->bdi_db->cursor(
553 bdb->bi_id2parent->bdi_db, NULL, &cursor,
554 bdb->bi_db_opflags );
559 /* When be_suffix is turned into struct berval or LDAPDN
560 * life will get a lot easier... Since no DNs live on disk, we
561 * need to operate on the be_suffix to fully qualify our DNs.
562 * We need to know how many components are in the suffix DN,
563 * so we can tell where the suffix ends and our nodes begin.
565 * Note that this code always uses be_suffix[0], so defining
566 * multiple suffixes for a single backend won't work!
568 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
569 for (i=0; rdns[i]; i++);
575 key.data = (char *)&id;
576 key.ulen = sizeof( id );
577 key.flags = DB_DBT_USERMEM;
578 data.flags = DB_DBT_MALLOC;
580 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
581 bdb_add_node( id, data.data, bdb );
583 cursor->c_close( cursor );
585 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
591 /* This function constructs a full DN for a given id. We really should
592 * be passing idNodes directly, to save some effort...
600 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
605 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
606 o = bdb_find_id_node(id, bdb->bi_tree);
607 rlen = be->be_suffix[0]->bv_len + 1;
608 nrlen = be->be_nsuffix[0]->bv_len + 1;
609 for (n = o; n; n=n->i_parent) {
610 rlen += n->i_rdn->rdn.bv_len + 1;
611 nrlen += n->i_rdn->nrdn.bv_len + 1;
613 e->e_name.bv_len = rlen - 1;
614 e->e_nname.bv_len = nrlen - 1;
615 e->e_name.bv_val = ch_malloc(rlen + nrlen);
616 e->e_nname.bv_val = e->e_name.bv_val + rlen;
617 ptr = e->e_name.bv_val;
618 nptr = e->e_nname.bv_val;
619 for (n = o; n; n=n->i_parent) {
620 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
622 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
625 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
629 strcpy(ptr, be->be_suffix[0]->bv_val);
630 strcpy(nptr, be->be_nsuffix[0]->bv_val);
642 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
645 DB *db = bdb->bi_id2parent->bdi_db;
649 nrlen = dn_rdnlen( be, e->e_ndn );
651 rlen = dn_rdnlen( be, e->e_dn );
656 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
657 d->rdn.bv_len = rlen;
658 d->nrdn.bv_len = nrlen;
659 d->rdn.bv_val = (char *)(d+1);
660 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
661 strncpy(d->rdn.bv_val, e->e_dn, rlen);
662 d->rdn.bv_val[rlen] = '\0';
663 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
664 d->nrdn.bv_val[nrlen] = '\0';
665 d->rdn.bv_val -= (long)d;
666 d->nrdn.bv_val -= (long)d;
669 bdb_dn2id(be, txn, pdn, &d->parent);
677 key.size = sizeof(ID);
678 key.flags = DB_DBT_USERMEM;
681 data.size = sizeof(diskNode) + rlen + nrlen + 2;
682 data.flags = DB_DBT_USERMEM;
684 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
687 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
688 n = bdb_add_node( e->e_id, data.data, bdb);
689 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
692 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
693 bdb_insert_kid(n, bdb->bi_tree);
694 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
709 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
712 DB *db = bdb->bi_id2parent->bdi_db;
716 key.size = sizeof(e->e_id);
719 rc = db->del( db, txn, &key, 0);
721 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
722 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
725 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
726 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
727 (AVL_CMP)node_frdn_cmp);
728 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
731 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
735 bdb->bi_troot = NULL;
736 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
749 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
758 if (be_issuffix(be, in->bv_val)) {
763 rdns = ldap_explode_dn(in->bv_val, 0);
764 for (i=0; rdns[i]; i++);
769 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
770 for (--i; i>=0; i--) {
771 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
772 n = bdb_find_rdn_node(rdns[i], p->i_kids);
773 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
777 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
781 } else if (matchedDN) {
785 for (j=i; rdns[j]; j++)
786 len += strlen(rdns[j]) + 1;
787 ptr = ch_malloc(len);
789 for (;rdns[i]; i++) {
790 ptr = slap_strcopy(ptr, rdns[i]);
795 return n ? 0 : DB_NOTFOUND;
805 return bdb_dn2id_matched(be, txn, dn, id, NULL);
815 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
819 rc = bdb_dn2id(be, txn, dn, &id);
823 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
824 n = bdb_find_id_node(id, bdb->bi_tree);
825 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
833 /* Since we don't store IDLs for onelevel or subtree, we have to construct
834 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
842 return bdb_idl_insert(ids, n->i_id);
853 rc = bdb_idl_insert(ids, n->i_id);
855 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
856 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
858 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
870 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
875 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
876 BDB_IDL_ALL(bdb, ids);
880 rc = bdb_dn2id(be, NULL, dn, &id);
883 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
884 n = bdb_find_id_node(id, bdb->bi_tree);
885 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
888 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
889 if (prefix == DN_ONE_PREFIX) {
890 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
893 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
896 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
899 #endif /* BDB_HIER */