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
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
29 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
32 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
33 "dn2id_add: (%s):%ld\n", dn, id ));
35 Debug( LDAP_DEBUG_TRACE, "=> dn2id_add( \"%s\", %ld )\n", dn, id, 0 );
40 if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
43 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
44 "dn2id_add: couldn't open/create dn2id%s\n", LDBM_SUFFIX ));
46 Debug( LDAP_DEBUG_ANY, "Could not open/create dn2id%s\n",
53 ldbm_datum_init( key );
54 key.dsize = strlen( dn ) + 2;
55 key.dptr = ch_malloc( key.dsize );
56 sprintf( key.dptr, "%c%s", DN_BASE_PREFIX, dn );
58 ldbm_datum_init( data );
59 data.dptr = (char *) &id;
60 data.dsize = sizeof(ID);
63 rc = ldbm_cache_store( db, key, data, flags );
68 char *pdn = dn_parent( NULL, dn );
71 ldbm_datum_init( key );
72 key.dsize = strlen( pdn ) + 2;
73 key.dptr = ch_malloc( key.dsize );
74 sprintf( key.dptr, "%c%s", DN_ONE_PREFIX, pdn );
75 rc = idl_insert_key( be, db, key, id );
82 char **subtree = dn_subtree( NULL, dn );
84 if( subtree != NULL ) {
86 for( i=0; subtree[i] != NULL; i++ ) {
87 ldbm_datum_init( key );
88 key.dsize = strlen( subtree[i] ) + 2;
89 key.dptr = ch_malloc( key.dsize );
90 sprintf( key.dptr, "%c%s",
91 DN_SUBTREE_PREFIX, subtree[i] );
93 rc = idl_insert_key( be, db, key, id );
100 charray_free( subtree );
104 ldbm_cache_close( be, db );
107 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
108 "dn2id_add: return %d\n", rc ));
110 Debug( LDAP_DEBUG_TRACE, "<= dn2id_add %d\n", rc, 0, 0 );
123 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
131 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
132 "dn2id: (%s)\n", dn ));
134 Debug( LDAP_DEBUG_TRACE, "=> dn2id( \"%s\" )\n", dn, 0, 0 );
138 /* first check the cache */
139 if ( (id = cache_find_entry_dn2id( be, &li->li_cache, dn )) != NOID ) {
141 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
142 "dn2id: (%s)%ld in cache.\n", dn, id ));
144 Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld (in cache)\n", id,
151 if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
154 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
155 "dn2id: couldn't open dn2id%s\n", LDBM_SUFFIX ));
157 Debug( LDAP_DEBUG_ANY, "<= dn2id could not open dn2id%s\n",
161 * return code !0 if ldbm cache open failed;
162 * callers should handle this
168 ldbm_datum_init( key );
170 key.dsize = strlen( dn ) + 2;
171 key.dptr = ch_malloc( key.dsize );
172 sprintf( key.dptr, "%c%s", DN_BASE_PREFIX, dn );
174 data = ldbm_cache_fetch( db, key );
176 ldbm_cache_close( be, db );
180 if ( data.dptr == NULL ) {
182 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
183 "dn2id: (%s) NOID\n", dn ));
185 Debug( LDAP_DEBUG_TRACE, "<= dn2id NOID\n", 0, 0, 0 );
191 AC_MEMCPY( (char *) &id, data.dptr, sizeof(ID) );
193 assert( id != NOID );
195 ldbm_datum_free( db->dbc_db, data );
198 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
199 "dn2id: %ld\n", id ));
201 Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld\n", id, 0, 0 );
219 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
220 "dn2idl: \"%c%s\"\n", prefix, dn ));
222 Debug( LDAP_DEBUG_TRACE, "=> dn2idl( \"%c%s\" )\n", prefix, dn, 0 );
226 if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
229 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
230 "dn2idl: could not open dn2id%s\n", LDBM_SUFFIX ));
232 Debug( LDAP_DEBUG_ANY, "<= dn2idl could not open dn2id%s\n",
239 ldbm_datum_init( key );
241 key.dsize = strlen( dn ) + 2;
242 key.dptr = ch_malloc( key.dsize );
243 sprintf( key.dptr, "%c%s", prefix, dn );
245 idl = idl_fetch( be, db, key );
247 ldbm_cache_close( be, db );
267 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
268 "dn2id_delete: (%s)%ld\n", dn, id ));
270 Debug( LDAP_DEBUG_TRACE, "=> dn2id_delete( \"%s\", %ld )\n", dn, id, 0 );
274 assert( id != NOID );
276 if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
279 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
280 "dn2id_delete: couldn't open db2id%s\n", LDBM_SUFFIX ));
282 Debug( LDAP_DEBUG_ANY,
283 "<= dn2id_delete could not open dn2id%s\n", LDBM_SUFFIX,
292 char *pdn = dn_parent( NULL, dn );
295 ldbm_datum_init( key );
296 key.dsize = strlen( pdn ) + 2;
297 key.dptr = ch_malloc( key.dsize );
298 sprintf( key.dptr, "%c%s", DN_ONE_PREFIX, pdn );
300 (void) idl_delete_key( be, db, key, id );
308 char **subtree = dn_subtree( NULL, dn );
310 if( subtree != NULL ) {
312 for( i=0; subtree[i] != NULL; i++ ) {
313 ldbm_datum_init( key );
314 key.dsize = strlen( subtree[i] ) + 2;
315 key.dptr = ch_malloc( key.dsize );
316 sprintf( key.dptr, "%c%s",
317 DN_SUBTREE_PREFIX, subtree[i] );
319 (void) idl_delete_key( be, db, key, id );
324 charray_free( subtree );
328 ldbm_datum_init( key );
330 key.dsize = strlen( dn ) + 2;
331 key.dptr = ch_malloc( key.dsize );
332 sprintf( key.dptr, "%c%s", DN_BASE_PREFIX, dn );
334 rc = ldbm_cache_delete( db, key );
338 ldbm_cache_close( be, db );
341 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
342 "dn2id_delete: return %d\n", rc ));
344 Debug( LDAP_DEBUG_TRACE, "<= dn2id_delete %d\n", rc, 0, 0 );
351 * dn2entry - look up dn in the cache/indexes and return the corresponding
369 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
370 "dn2entry_rw: %s entry %s\n", rw ? "w" : "r",
373 Debug(LDAP_DEBUG_TRACE, "dn2entry_%s: dn: \"%s\"\n",
374 rw ? "w" : "r", dn, 0);
378 if( matched != NULL ) {
379 /* caller cares about match */
383 if ( (id = dn2id( be, dn, &rc_id )) != NOID &&
384 (e = id2entry_rw( be, id, rw )) != NULL )
391 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
392 "dn2entry_rw: no entry for valid id (%ld), dn (%s)\n",
395 Debug(LDAP_DEBUG_ANY,
396 "dn2entry_%s: no entry for valid id (%ld), dn \"%s\"\n",
397 rw ? "w" : "r", id, dn);
400 /* must have been deleted from underneath us */
401 /* treat as if NOID was found */
402 } else if ( rc_id ) {
403 /* something bad happened to ldbm cache */
407 /* caller doesn't care about match */
408 if( matched == NULL ) return NULL;
410 /* entry does not exist - see how much of the dn does exist */
411 /* dn_parent checks returns NULL if dn is suffix */
412 if ( (pdn = dn_parent( be, dn )) != NULL ) {
413 /* get entry with reader lock */
414 if ( (e = dn2entry_r( be, pdn, matched )) != NULL ) {