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",
29 e->e_ndn, (long) e->e_id, 0 );
30 assert( e->e_id != NOID );
33 key.size = strlen( e->e_ndn ) + 2;
34 key.data = ch_malloc( key.size );
35 ((char *)key.data)[0] = DN_BASE_PREFIX;
36 AC_MEMCPY( &((char *)key.data)[1], e->e_ndn, key.size - 1 );
39 data.data = (char *) &e->e_id;
40 data.size = sizeof( e->e_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 *)(key.data))[0] = DN_ONE_PREFIX;
54 key.size = strlen( pdn ) + 2;
55 AC_MEMCPY( &((char*)key.data)[1],
58 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
61 Debug( LDAP_DEBUG_ANY,
62 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
70 char **subtree = dn_subtree( be, e->e_ndn );
72 if( subtree != NULL ) {
74 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
75 for( i=0; subtree[i] != NULL; i++ ) {
76 if (be_issuffix(be, subtree[i]))
78 key.size = strlen( subtree[i] ) + 2;
79 AC_MEMCPY( &((char *)key.data)[1],
80 subtree[i], key.size - 1 );
82 rc = bdb_idl_insert_key( be, db, txn, &key,
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 );
113 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
114 DB *db = bdb->bi_dn2id->bdi_db;
116 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
120 key.size = strlen( dn ) + 2;
121 key.data = ch_malloc( key.size );
122 key.flags = DB_DBT_USERMEM;
123 ((char *)key.data)[0] = DN_BASE_PREFIX;
124 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
127 rc = db->del( db, txn, &key, 0 );
129 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
130 db_strerror(rc), rc, 0 );
135 ((char *)(key.data))[0] = DN_ONE_PREFIX;
138 key.size = strlen( pdn ) + 2;
139 AC_MEMCPY( &((char*)key.data)[1],
142 rc = bdb_idl_delete_key( be, db, txn, &key, id );
145 Debug( LDAP_DEBUG_ANY,
146 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
154 char **subtree = dn_subtree( be, dn );
156 if( subtree != NULL ) {
158 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
159 for( i=0; subtree[i] != NULL; i++ ) {
160 key.size = strlen( subtree[i] ) + 2;
161 AC_MEMCPY( &((char *)key.data)[1],
162 subtree[i], key.size - 1 );
164 rc = bdb_idl_delete_key( be, db, txn, &key, id );
167 Debug( LDAP_DEBUG_ANY,
168 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
170 charray_free( subtree );
175 charray_free( subtree );
181 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
194 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
195 DB *db = bdb->bi_dn2id->bdi_db;
197 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
200 key.size = strlen( dn ) + 2;
201 key.data = ch_malloc( key.size );
202 ((char *)key.data)[0] = DN_BASE_PREFIX;
203 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
208 data.ulen = sizeof(ID);
209 data.flags = DB_DBT_USERMEM;
212 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
215 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
216 db_strerror( rc ), rc, 0 );
218 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
236 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
237 DB *db = bdb->bi_dn2id->bdi_db;
241 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
244 key.size = strlen( dn ) + 2;
245 key.data = ch_malloc( key.size );
246 ((char *)key.data)[0] = DN_BASE_PREFIX;
251 data.ulen = sizeof(ID);
252 data.flags = DB_DBT_USERMEM;
257 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
262 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
264 if( rc == DB_NOTFOUND ) {
265 char *pdn = dn_parent( be, dn );
269 if( pdn == NULL || *pdn == '\0' ) {
270 Debug( LDAP_DEBUG_TRACE,
271 "<= bdb_dn2id_matched: no match\n",
279 key.size = strlen( dn ) + 2;
281 } else if ( rc == 0 ) {
282 if( data.size != sizeof( ID ) ) {
283 Debug( LDAP_DEBUG_ANY,
284 "<= bdb_dn2id_matched: get size mismatch: "
285 "expected %ld, got %ld\n",
286 (long) sizeof(ID), (long) data.size, 0 );
291 *matchedDN = (char *) dn;
294 Debug( LDAP_DEBUG_TRACE,
295 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
296 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
300 Debug( LDAP_DEBUG_ANY,
301 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
302 db_strerror(rc), rc, 0 );
320 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
321 DB *db = bdb->bi_dn2id->bdi_db;
324 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
328 key.size = strlen( dn ) + 2;
329 key.data = ch_malloc( key.size );
330 ((char *)key.data)[0] = DN_ONE_PREFIX;
331 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
333 /* we actually could do a empty get... */
336 data.ulen = sizeof(id);
337 data.flags = DB_DBT_USERMEM;
339 data.dlen = sizeof(id);
341 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
343 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
345 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
346 db_strerror(rc) ), rc );
360 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
361 DB *db = bdb->bi_dn2id->bdi_db;
363 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn, 0, 0 );
365 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
367 BDB_IDL_ALL(bdb, ids);
372 key.size = strlen( dn ) + 2;
373 key.data = ch_malloc( key.size );
374 ((char *)key.data)[0] = prefix;
375 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
377 rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
380 Debug( LDAP_DEBUG_TRACE,
381 "<= bdb_dn2idl: get failed: %s (%d)\n",
382 db_strerror( rc ), rc, 0 );
385 Debug( LDAP_DEBUG_TRACE,
386 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
388 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );