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 key.size = strlen( subtree[i] ) + 2;
77 AC_MEMCPY( &((char *)key.data)[1],
78 subtree[i], key.size - 1 );
80 rc = bdb_idl_insert_key( be, db, txn, &key,
84 Debug( LDAP_DEBUG_ANY,
85 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
91 charray_free( subtree );
97 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
111 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
112 DB *db = bdb->bi_dn2id->bdi_db;
114 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
118 key.size = strlen( dn ) + 2;
119 key.data = ch_malloc( key.size );
120 ((char *)key.data)[0] = DN_BASE_PREFIX;
121 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
124 rc = db->del( db, txn, &key, 0 );
126 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
127 db_strerror(rc), rc, 0 );
132 ((char *)(key.data))[0] = DN_ONE_PREFIX;
135 key.size = strlen( pdn ) + 2;
136 AC_MEMCPY( &((char*)key.data)[1],
139 rc = bdb_idl_delete_key( be, db, txn, &key, id );
142 Debug( LDAP_DEBUG_ANY,
143 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
151 char **subtree = dn_subtree( be, dn );
153 if( subtree != NULL ) {
155 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
156 for( i=0; subtree[i] != NULL; i++ ) {
157 key.size = strlen( subtree[i] ) + 2;
158 AC_MEMCPY( &((char *)key.data)[1],
159 subtree[i], key.size - 1 );
161 rc = bdb_idl_delete_key( be, db, txn, &key, id );
164 Debug( LDAP_DEBUG_ANY,
165 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
167 charray_free( subtree );
172 charray_free( subtree );
178 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
191 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
192 DB *db = bdb->bi_dn2id->bdi_db;
194 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
197 key.size = strlen( dn ) + 2;
198 key.data = ch_malloc( key.size );
199 ((char *)key.data)[0] = DN_BASE_PREFIX;
200 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
205 data.ulen = sizeof(ID);
206 data.flags = DB_DBT_USERMEM;
209 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
212 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
213 db_strerror( rc ), rc, 0 );
215 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
233 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
234 DB *db = bdb->bi_dn2id->bdi_db;
238 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
241 key.size = strlen( dn ) + 2;
242 key.data = ch_malloc( key.size );
243 ((char *)key.data)[0] = DN_BASE_PREFIX;
248 data.ulen = sizeof(ID);
249 data.flags = DB_DBT_USERMEM;
254 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
259 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
261 if( rc == DB_NOTFOUND ) {
262 char *pdn = dn_parent( be, dn );
266 if( pdn == NULL || *pdn == '\0' ) {
267 Debug( LDAP_DEBUG_TRACE,
268 "<= bdb_dn2id_matched: no match\n",
276 key.size = strlen( dn ) + 2;
278 } else if ( rc == 0 ) {
279 if( data.size != sizeof( ID ) ) {
280 Debug( LDAP_DEBUG_ANY,
281 "<= bdb_dn2id_matched: get size mismatch: "
282 "expected %ld, got %ld\n",
283 (long) sizeof(ID), (long) data.size, 0 );
288 *matchedDN = (char *) dn;
291 Debug( LDAP_DEBUG_TRACE,
292 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
293 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
297 Debug( LDAP_DEBUG_ANY,
298 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
299 db_strerror(rc), rc, 0 );
317 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
318 DB *db = bdb->bi_dn2id->bdi_db;
321 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
325 key.size = strlen( dn ) + 2;
326 key.data = ch_malloc( key.size );
327 ((char *)key.data)[0] = DN_ONE_PREFIX;
328 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
330 /* we actually could do a empty get... */
333 data.ulen = sizeof(id);
334 data.flags = DB_DBT_USERMEM;
336 data.dlen = sizeof(id);
338 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
340 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
342 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
343 db_strerror(rc) ), rc );
357 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
358 DB *db = bdb->bi_dn2id->bdi_db;
360 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn, 0, 0 );
362 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
364 BDB_IDL_ALL(bdb, ids);
369 key.size = strlen( dn ) + 2;
370 key.data = ch_malloc( key.size );
371 ((char *)key.data)[0] = prefix;
372 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
377 data.ulen = BDB_IDL_UM_SIZEOF;
378 data.flags = DB_DBT_USERMEM;
381 rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
384 Debug( LDAP_DEBUG_TRACE,
385 "<= bdb_dn2idl: get failed: %s (%d)\n",
386 db_strerror( rc ), rc, 0 );
389 Debug( LDAP_DEBUG_TRACE,
390 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
392 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );