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 buf[0] = DN_SUBTREE_PREFIX;
55 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
57 Debug( LDAP_DEBUG_ANY,
58 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
63 pdn = dn_parent( be, ptr );
66 key.size -= pdn - ptr;
67 pdn[-1] = DN_ONE_PREFIX;
70 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
73 Debug( LDAP_DEBUG_ANY,
74 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
79 pdn[-1] = DN_SUBTREE_PREFIX;
80 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
83 Debug( LDAP_DEBUG_ANY,
84 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
89 pdn = dn_parent( be, pdn );
92 while( pdn != NULL ) {
93 if( be_issuffix( be, pdn ))
96 pdn[-1] = DN_SUBTREE_PREFIX;
97 key.size -= pdn - ptr;
100 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
103 Debug( LDAP_DEBUG_ANY,
104 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
109 pdn = dn_parent( be, pdn );
114 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
125 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
126 DB *db = bdb->bi_dn2id->bdi_db;
131 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
132 e->e_ndn, e->e_id, 0 );
135 key.size = strlen( e->e_ndn ) + 2;
136 buf = ch_malloc( key.size );
138 key.flags = DB_DBT_USERMEM;
139 buf[0] = DN_BASE_PREFIX;
141 AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
144 rc = db->del( db, txn, &key, 0 );
146 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
147 db_strerror(rc), rc, 0 );
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",
160 pdn = dn_parent( be, ptr );
163 key.size -= pdn - ptr;
164 pdn[-1] = DN_ONE_PREFIX;
167 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
170 Debug( LDAP_DEBUG_ANY,
171 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
176 pdn[-1] = DN_SUBTREE_PREFIX;
178 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
181 Debug( LDAP_DEBUG_ANY,
182 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
187 pdn = dn_parent( be, pdn );
190 while( pdn != NULL ) {
191 if( be_issuffix( be, pdn ))
194 pdn[-1] = DN_SUBTREE_PREFIX;
195 key.size -= pdn - ptr;
198 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
200 Debug( LDAP_DEBUG_ANY,
201 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
206 pdn = dn_parent( be, pdn );
211 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
224 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
225 DB *db = bdb->bi_dn2id->bdi_db;
227 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
230 key.size = strlen( dn ) + 2;
231 key.data = ch_malloc( key.size );
232 ((char *)key.data)[0] = DN_BASE_PREFIX;
233 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
238 data.ulen = sizeof(ID);
239 data.flags = DB_DBT_USERMEM;
242 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
245 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
246 db_strerror( rc ), rc, 0 );
248 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
266 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
267 DB *db = bdb->bi_dn2id->bdi_db;
270 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
273 key.size = strlen( dn ) + 2;
274 key.data = ch_malloc( key.size );
275 ((char *)key.data)[0] = DN_BASE_PREFIX;
280 data.ulen = sizeof(ID);
281 data.flags = DB_DBT_USERMEM;
286 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
291 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
293 if( rc == DB_NOTFOUND ) {
294 char *pdn = dn_parent( be, dn );
296 if( pdn == NULL || *pdn == '\0' ) {
297 Debug( LDAP_DEBUG_TRACE,
298 "<= bdb_dn2id_matched: no match\n",
304 key.size = strlen( dn ) + 2;
306 } else if ( rc == 0 ) {
307 if( data.size != sizeof( ID ) ) {
308 Debug( LDAP_DEBUG_ANY,
309 "<= bdb_dn2id_matched: get size mismatch: "
310 "expected %ld, got %ld\n",
311 (long) sizeof(ID), (long) data.size, 0 );
315 *matchedDN = (char *) dn;
318 Debug( LDAP_DEBUG_TRACE,
319 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
320 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
324 Debug( LDAP_DEBUG_ANY,
325 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
326 db_strerror(rc), rc, 0 );
343 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
344 DB *db = bdb->bi_dn2id->bdi_db;
347 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
351 key.size = strlen( dn ) + 2;
352 key.data = ch_malloc( key.size );
353 ((char *)key.data)[0] = DN_ONE_PREFIX;
354 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
356 /* we actually could do a empty get... */
359 data.ulen = sizeof(id);
360 data.flags = DB_DBT_USERMEM;
362 data.dlen = sizeof(id);
364 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
366 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
368 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
369 db_strerror(rc) ), rc );
383 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
384 DB *db = bdb->bi_dn2id->bdi_db;
386 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn, 0, 0 );
388 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
390 BDB_IDL_ALL(bdb, ids);
395 key.size = strlen( dn ) + 2;
396 key.data = ch_malloc( key.size );
397 ((char *)key.data)[0] = prefix;
398 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
400 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
403 Debug( LDAP_DEBUG_TRACE,
404 "<= bdb_dn2idl: get failed: %s (%d)\n",
405 db_strerror( rc ), rc, 0 );
408 Debug( LDAP_DEBUG_TRACE,
409 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
411 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
419 /* Experimental management routines for a hierarchically structured backend.
421 * Unsupported! Use at your own risk!
423 * Instead of a dn2id database, we use an id2parent database. Each entry in
424 * this database is a struct diskNode, containing the ID of the node's parent
425 * and the RDN of the node.
427 typedef struct diskNode {
433 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
434 * database into memory (into an AVL tree). Next we iterate through each node
435 * of this tree, connecting each child to its parent. The nodes in this AVL
436 * tree are a struct idNode. The immediate (Onelevel) children of a node are
437 * referenced in the i_kids AVL tree. With this arrangement, there is no need
438 * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
439 * the DN of an entry is constructed by walking up the list of i_parent
440 * pointers, so no full DN is stored on disk anywhere. This makes modrdn
441 * extremely efficient, even when operating on a populated subtree.
443 * The idNode tree is searched directly from the root when performing id to
444 * entry lookups. The tree is traversed using the i_kids subtrees when
445 * performing dn to id lookups.
447 typedef struct idNode {
449 struct idNode *i_parent;
452 ldap_pvt_thread_rdwr_t i_kids_rdwr;
455 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
456 * the result string. This allows fast construction of catenated strings
457 * without the overhead of strlen/strcat.
468 while (*a++ = *b++) ;
472 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
473 * sorted in lexical order. These are the various helper routines used
474 * for the searches and sorts.
491 return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
500 return a->i_id - b->i_id;
509 return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
512 idNode * bdb_find_id_node(
517 return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
520 idNode * bdb_find_rdn_node(
525 return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
528 /* This function links a node into its parent's i_kids tree. */
536 if (a->i_rdn->parent == 0)
538 a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
541 ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
542 rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
543 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
544 ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
548 /* This function adds a node into the main AVL tree */
549 idNode *bdb_add_node(
557 node = (idNode *)ch_malloc(sizeof(idNode));
559 node->i_parent = NULL;
561 node->i_rdn = (diskNode *)d;
562 node->i_rdn->rdn.bv_val += (long)d;
563 node->i_rdn->nrdn.bv_val += (long)d;
564 ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
565 avl_insert( &bdb->bi_tree, (caddr_t) node,
566 (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
568 bdb->bi_troot = node;
572 /* This function initializes the trees at startup time. */
577 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
587 rc = bdb->bi_id2parent->bdi_db->cursor(
588 bdb->bi_id2parent->bdi_db, NULL, &cursor,
589 bdb->bi_db_opflags );
594 /* When be_suffix is turned into struct berval or LDAPDN
595 * life will get a lot easier... Since no DNs live on disk, we
596 * need to operate on the be_suffix to fully qualify our DNs.
597 * We need to know how many components are in the suffix DN,
598 * so we can tell where the suffix ends and our nodes begin.
600 * Note that this code always uses be_suffix[0], so defining
601 * multiple suffixes for a single backend won't work!
603 bdb->bi_sufflen = strlen(be->be_suffix[0]);
605 rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
606 for (i=0; rdns[i]; i++);
612 key.data = (char *)&id;
613 key.ulen = sizeof( id );
614 key.flags = DB_DBT_USERMEM;
615 data.flags = DB_DBT_MALLOC;
617 while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
618 bdb_add_node( id, data.data, bdb );
620 cursor->c_close( cursor );
622 rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
628 /* This function constructs a full DN for a given id. We really should
629 * be passing idNodes directly, to save some effort...
637 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
642 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
643 o = bdb_find_id_node(id, bdb->bi_tree);
644 rlen = bdb->bi_sufflen + 1;
645 nrlen = be->be_nsuffix[0]->bv_len + 1;
646 for (n = o; n; n=n->i_parent) {
647 rlen += n->i_rdn->rdn.bv_len + 1;
648 nrlen += n->i_rdn->nrdn.bv_len + 1;
650 e->e_dn = ch_malloc(rlen + nrlen);
651 e->e_ndn = e->e_dn + rlen;
654 for (n = o; n; n=n->i_parent) {
655 ptr = bdb_strcopy(ptr, n->i_rdn->rdn.bv_val);
657 nptr = bdb_strcopy(nptr, n->i_rdn->nrdn.bv_val);
660 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
664 strcpy(ptr, be->be_suffix[0]);
665 strcpy(nptr, be->be_nsuffix[0]->bv_val);
677 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
680 DB *db = bdb->bi_id2parent->bdi_db;
684 nrlen = dn_rdnlen( be, e->e_ndn );
686 rlen = dn_rdnlen( be, e->e_dn );
691 d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
692 d->rdn.bv_len = rlen;
693 d->nrdn.bv_len = nrlen;
694 d->rdn.bv_val = (char *)(d+1);
695 d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
696 strncpy(d->rdn.bv_val, e->e_dn, rlen);
697 d->rdn.bv_val[rlen] = '\0';
698 strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
699 d->nrdn.bv_val[nrlen] = '\0';
700 d->rdn.bv_val -= (long)d;
701 d->nrdn.bv_val -= (long)d;
704 bdb_dn2id(be, txn, pdn, &d->parent);
712 key.size = sizeof(ID);
713 key.flags = DB_DBT_USERMEM;
716 data.size = sizeof(diskNode) + rlen + nrlen + 2;
717 data.flags = DB_DBT_USERMEM;
719 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
722 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
723 n = bdb_add_node( e->e_id, data.data, bdb);
724 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
727 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
728 bdb_insert_kid(n, bdb->bi_tree);
729 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
744 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
747 DB *db = bdb->bi_id2parent->bdi_db;
751 key.size = sizeof(e->e_id);
754 rc = db->del( db, txn, &key, 0);
756 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
757 n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
760 ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
761 avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
762 (AVL_CMP)node_frdn_cmp);
763 ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
766 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
770 bdb->bi_troot = NULL;
771 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
784 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
793 if (be_issuffix(be, in)) {
798 rdns = ldap_explode_dn(in, 0);
799 for (i=0; rdns[i]; i++);
804 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
805 for (--i; i>=0; i--) {
806 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
807 n = bdb_find_rdn_node(rdns[i], p->i_kids);
808 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
812 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
816 } else if (matchedDN) {
820 for (j=i; rdns[j]; j++)
821 len += strlen(rdns[j]) + 1;
822 ptr = ch_malloc(len);
824 for (;rdns[i]; i++) {
825 ptr = bdb_strcopy(ptr, rdns[i]);
830 return n ? 0 : DB_NOTFOUND;
840 return bdb_dn2id_matched(be, txn, dn, id, NULL);
850 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
854 rc = bdb_dn2id(be, txn, dn, &id);
858 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
859 n = bdb_find_id_node(id, bdb->bi_tree);
860 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
868 /* Since we don't store IDLs for onelevel or subtree, we have to construct
869 * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
877 return bdb_idl_insert(ids, n->i_id);
888 rc = bdb_idl_insert(ids, n->i_id);
890 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
891 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
893 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
905 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
910 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
911 BDB_IDL_ALL(bdb, ids);
915 rc = bdb_dn2id(be, NULL, dn, &id);
918 ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
919 n = bdb_find_id_node(id, bdb->bi_tree);
920 ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
923 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
924 if (prefix == DN_ONE_PREFIX) {
925 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
928 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
931 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
934 #endif /* BDB_HIER */