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>
25 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26 DB *db = bdb->bi_dn2id->bdi_db;
28 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
33 key.size = strlen( dn ) + 2;
34 key.data = ch_malloc( key.size );
35 ((char *)key.data)[0] = DN_BASE_PREFIX;
36 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
39 data.data = (char *) &id;
40 data.size = sizeof( id );
42 /* store it -- don't override */
43 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
45 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
46 db_strerror(rc), rc, 0 );
51 char *pdn = dn_parent( NULL, dn );
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, id );
62 Debug( LDAP_DEBUG_ANY,
63 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
73 char **subtree = dn_subtree( NULL, dn );
75 if( subtree != NULL ) {
77 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
78 for( i=0; subtree[i] != NULL; 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, id );
86 Debug( LDAP_DEBUG_ANY,
87 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
93 charray_free( subtree );
99 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
112 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
113 DB *db = bdb->bi_dn2id->bdi_db;
115 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
119 key.size = strlen( dn ) + 2;
120 key.data = ch_malloc( key.size );
121 ((char *)key.data)[0] = DN_BASE_PREFIX;
122 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
124 /* store it -- don't override */
125 rc = db->del( db, txn, &key, 0 );
127 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
128 db_strerror(rc), rc, 0 );
133 char *pdn = dn_parent( NULL, dn );
134 ((char *)(key.data))[0] = DN_ONE_PREFIX;
137 key.size = strlen( pdn ) + 2;
138 AC_MEMCPY( &((char*)key.data)[1],
141 rc = bdb_idl_delete_key( be, db, txn, &key, id );
144 Debug( LDAP_DEBUG_ANY,
145 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
155 char **subtree = dn_subtree( NULL, 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, 0 );
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, 0 );
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 *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, 0 );
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 );
367 key.size = strlen( dn ) + 2;
368 key.data = ch_malloc( key.size );
369 ((char *)key.data)[0] = prefix;
370 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
375 data.ulen = sizeof(ID);
376 data.flags = DB_DBT_USERMEM;
379 rc = db->get( db, NULL, &key, &data, 0 );
382 Debug( LDAP_DEBUG_TRACE,
383 "<= bdb_dn2idl: get failed: %s (%d)\n",
384 db_strerror( rc ), rc, 0 );
386 Debug( LDAP_DEBUG_TRACE,
387 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
388 ids[0], BDB_IDL_FIRST( ids ), BDB_IDL_LAST( ids ) );