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
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
30 struct berval ptr, pdn;
33 LDAP_LOG( BACK_LDBM, ENTRY, "dn2id_add: (%s):%ld\n", dn->bv_val, id, 0 );
35 Debug( LDAP_DEBUG_TRACE, "=> dn2id_add( \"%s\", %ld )\n", dn->bv_val, id, 0 );
40 db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT );
43 LDAP_LOG( BACK_LDBM, ERR,
44 "dn2id_add: couldn't open/create dn2id%s\n", LDBM_SUFFIX, 0, 0 );
46 Debug( LDAP_DEBUG_ANY, "Could not open/create dn2id%s\n",
53 ldbm_datum_init( key );
54 key.dsize = dn->bv_len + 2;
55 buf = ch_malloc( key.dsize );
57 buf[0] = DN_BASE_PREFIX;
59 ptr.bv_len = dn->bv_len;
60 AC_MEMCPY( ptr.bv_val, dn->bv_val, dn->bv_len );
61 ptr.bv_val[ dn->bv_len ] = '\0';
63 ldbm_datum_init( data );
64 data.dptr = (char *) &id;
65 data.dsize = sizeof(ID);
68 rc = ldbm_cache_store( db, key, data, flags );
70 if ( rc != -1 && !be_issuffix( be, &ptr )) {
71 buf[0] = DN_SUBTREE_PREFIX;
72 ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
73 rc = idl_insert_key( be, db, key, id );
74 ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
77 dnParent( &ptr, &pdn );
79 pdn.bv_val[-1] = DN_ONE_PREFIX;
80 key.dsize = pdn.bv_len + 2;
81 key.dptr = pdn.bv_val - 1;
83 ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
84 rc = idl_insert_key( be, db, key, id );
85 ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
89 while ( rc != -1 && !be_issuffix( be, &ptr )) {
90 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
92 ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
93 rc = idl_insert_key( be, db, key, id );
94 ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
97 dnParent( &ptr, &pdn );
98 key.dsize = pdn.bv_len + 2;
99 key.dptr = pdn.bv_val - 1;
104 ldbm_cache_close( be, db );
107 LDAP_LOG( BACK_LDBM, ENTRY, "dn2id_add: return %d\n", rc, 0, 0 );
109 Debug( LDAP_DEBUG_TRACE, "<= dn2id_add %d\n", rc, 0, 0 );
122 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
128 LDAP_LOG( BACK_LDBM, ENTRY, "dn2id: (%s)\n", dn->bv_val, 0, 0 );
130 Debug( LDAP_DEBUG_TRACE, "=> dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
135 /* first check the cache */
136 *idp = cache_find_entry_ndn2id( be, &li->li_cache, dn );
137 if ( *idp != NOID ) {
139 LDAP_LOG( BACK_LDBM, DETAIL1, "dn2id: (%s)%ld in cache.\n", dn, *idp, 0 );
141 Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld (in cache)\n", *idp,
148 db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT );
151 LDAP_LOG( BACK_LDBM, ERR,
152 "dn2id: couldn't open dn2id%s\n", LDBM_SUFFIX, 0, 0 );
154 Debug( LDAP_DEBUG_ANY, "<= dn2id could not open dn2id%s\n",
158 * return code !0 if ldbm cache open failed;
159 * callers should handle this
165 ldbm_datum_init( key );
167 key.dsize = dn->bv_len + 2;
168 key.dptr = ch_malloc( key.dsize );
169 tmp = (unsigned char *)key.dptr;
170 tmp[0] = DN_BASE_PREFIX;
172 AC_MEMCPY( tmp, dn->bv_val, dn->bv_len );
173 tmp[dn->bv_len] = '\0';
175 data = ldbm_cache_fetch( db, key );
177 ldbm_cache_close( be, db );
181 if ( data.dptr == NULL ) {
183 LDAP_LOG( BACK_LDBM, INFO, "dn2id: (%s) NOID\n", dn, 0, 0 );
185 Debug( LDAP_DEBUG_TRACE, "<= dn2id NOID\n", 0, 0, 0 );
192 AC_MEMCPY( (char *) idp, data.dptr, sizeof(ID) );
194 assert( *idp != NOID );
196 ldbm_datum_free( db->dbc_db, data );
199 LDAP_LOG( BACK_LDBM, ENTRY, "dn2id: %ld\n", *idp, 0, 0 );
201 Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld\n", *idp, 0, 0 );
220 LDAP_LOG( BACK_LDBM, ENTRY, "dn2idl: \"%c%s\"\n", prefix, dn->bv_val, 0 );
222 Debug( LDAP_DEBUG_TRACE, "=> dn2idl( \"%c%s\" )\n", prefix, dn->bv_val, 0 );
225 assert( idlp != NULL );
228 if ( prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn) ) {
229 *idlp = idl_allids( be );
233 db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT );
236 LDAP_LOG( BACK_LDBM, ERR,
237 "dn2idl: could not open dn2id%s\n", LDBM_SUFFIX, 0, 0 );
239 Debug( LDAP_DEBUG_ANY, "<= dn2idl could not open dn2id%s\n",
246 ldbm_datum_init( key );
248 key.dsize = dn->bv_len + 2;
249 key.dptr = ch_malloc( key.dsize );
250 tmp = (unsigned char *)key.dptr;
253 AC_MEMCPY( tmp, dn->bv_val, dn->bv_len );
254 tmp[dn->bv_len] = '\0';
256 *idlp = idl_fetch( be, db, key );
258 ldbm_cache_close( be, db );
277 struct berval ptr, pdn;
280 LDAP_LOG( BACK_LDBM, ENTRY,
281 "dn2id_delete: (%s)%ld\n", dn->bv_val, id, 0 );
283 Debug( LDAP_DEBUG_TRACE, "=> dn2id_delete( \"%s\", %ld )\n", dn->bv_val, id, 0 );
287 assert( id != NOID );
289 db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT );
292 LDAP_LOG( BACK_LDBM, ERR,
293 "dn2id_delete: couldn't open db2id%s\n", LDBM_SUFFIX, 0, 0 );
295 Debug( LDAP_DEBUG_ANY,
296 "<= dn2id_delete could not open dn2id%s\n", LDBM_SUFFIX,
303 ldbm_datum_init( key );
304 key.dsize = dn->bv_len + 2;
305 buf = ch_malloc( key.dsize );
307 buf[0] = DN_BASE_PREFIX;
308 ptr.bv_val = buf + 1;
309 ptr.bv_len = dn->bv_len;
310 AC_MEMCPY( ptr.bv_val, dn->bv_val, dn->bv_len );
311 ptr.bv_val[dn->bv_len] = '\0';
313 rc = ldbm_cache_delete( db, key );
315 if( !be_issuffix( be, &ptr )) {
316 buf[0] = DN_SUBTREE_PREFIX;
317 ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
318 (void) idl_delete_key( be, db, key, id );
319 ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
321 dnParent( &ptr, &pdn );
323 pdn.bv_val[-1] = DN_ONE_PREFIX;
324 key.dsize = pdn.bv_len + 2;
325 key.dptr = pdn.bv_val - 1;
328 ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
329 (void) idl_delete_key( be, db, key, id );
330 ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
333 while ( rc != -1 && !be_issuffix( be, &ptr )) {
334 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
336 ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
337 (void) idl_delete_key( be, db, key, id );
338 ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
340 dnParent( &ptr, &pdn );
341 key.dsize = pdn.bv_len + 2;
342 key.dptr = pdn.bv_val - 1;
348 ldbm_cache_close( be, db );
351 LDAP_LOG( BACK_LDBM, ENTRY, "dn2id_delete: return %d\n", rc, 0, 0 );
353 Debug( LDAP_DEBUG_TRACE, "<= dn2id_delete %d\n", rc, 0, 0 );
360 * dn2entry - look up dn in the cache/indexes and return the corresponding
377 LDAP_LOG( BACK_LDBM, ENTRY,
378 "dn2entry_rw: %s entry %s\n", rw ? "w" : "r", dn->bv_val, 0 );
380 Debug(LDAP_DEBUG_TRACE, "dn2entry_%s: dn: \"%s\"\n",
381 rw ? "w" : "r", dn->bv_val, 0);
385 if( matched != NULL ) {
386 /* caller cares about match */
390 if ( dn2id( be, dn, &id ) ) {
391 /* something bad happened to ldbm cache */
397 /* try to return the entry */
398 if ((e = id2entry_rw( be, id, rw )) != NULL ) {
403 LDAP_LOG( BACK_LDBM, ERR,
404 "dn2entry_rw: no entry for valid id (%ld), dn (%s)\n",
407 Debug(LDAP_DEBUG_ANY,
408 "dn2entry_%s: no entry for valid id (%ld), dn \"%s\"\n",
409 rw ? "w" : "r", id, dn->bv_val);
412 /* must have been deleted from underneath us */
413 /* treat as if NOID was found */
416 /* caller doesn't care about match */
417 if( matched == NULL ) return NULL;
419 /* entry does not exist - see how much of the dn does exist */
420 if ( !be_issuffix( be, dn ) && (dnParent( dn, &pdn ), pdn.bv_len) ) {
421 /* get entry with reader lock */
422 if ( (e = dn2entry_r( be, &pdn, matched )) != NULL ) {