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 key.flags = DB_DBT_USERMEM;
121 ((char *)key.data)[0] = DN_BASE_PREFIX;
122 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
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 *)(key.data))[0] = DN_ONE_PREFIX;
136 key.size = strlen( pdn ) + 2;
137 AC_MEMCPY( &((char*)key.data)[1],
140 rc = bdb_idl_delete_key( be, db, txn, &key, id );
143 Debug( LDAP_DEBUG_ANY,
144 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
152 char **subtree = dn_subtree( be, dn );
154 if( subtree != NULL ) {
156 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
157 for( i=0; subtree[i] != NULL; i++ ) {
158 key.size = strlen( subtree[i] ) + 2;
159 AC_MEMCPY( &((char *)key.data)[1],
160 subtree[i], key.size - 1 );
162 rc = bdb_idl_delete_key( be, db, txn, &key, id );
165 Debug( LDAP_DEBUG_ANY,
166 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
168 charray_free( subtree );
173 charray_free( subtree );
179 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
192 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
193 DB *db = bdb->bi_dn2id->bdi_db;
195 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
198 key.size = strlen( dn ) + 2;
199 key.data = ch_malloc( key.size );
200 ((char *)key.data)[0] = DN_BASE_PREFIX;
201 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
206 data.ulen = sizeof(ID);
207 data.flags = DB_DBT_USERMEM;
210 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
213 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
214 db_strerror( rc ), rc, 0 );
216 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
234 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
235 DB *db = bdb->bi_dn2id->bdi_db;
239 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
242 key.size = strlen( dn ) + 2;
243 key.data = ch_malloc( key.size );
244 ((char *)key.data)[0] = DN_BASE_PREFIX;
249 data.ulen = sizeof(ID);
250 data.flags = DB_DBT_USERMEM;
255 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
260 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
262 if( rc == DB_NOTFOUND ) {
263 char *pdn = dn_parent( be, dn );
267 if( pdn == NULL || *pdn == '\0' ) {
268 Debug( LDAP_DEBUG_TRACE,
269 "<= bdb_dn2id_matched: no match\n",
277 key.size = strlen( dn ) + 2;
279 } else if ( rc == 0 ) {
280 if( data.size != sizeof( ID ) ) {
281 Debug( LDAP_DEBUG_ANY,
282 "<= bdb_dn2id_matched: get size mismatch: "
283 "expected %ld, got %ld\n",
284 (long) sizeof(ID), (long) data.size, 0 );
289 *matchedDN = (char *) dn;
292 Debug( LDAP_DEBUG_TRACE,
293 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
294 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
298 Debug( LDAP_DEBUG_ANY,
299 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
300 db_strerror(rc), rc, 0 );
318 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
319 DB *db = bdb->bi_dn2id->bdi_db;
322 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
326 key.size = strlen( dn ) + 2;
327 key.data = ch_malloc( key.size );
328 ((char *)key.data)[0] = DN_ONE_PREFIX;
329 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
331 /* we actually could do a empty get... */
334 data.ulen = sizeof(id);
335 data.flags = DB_DBT_USERMEM;
337 data.dlen = sizeof(id);
339 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
341 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
343 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
344 db_strerror(rc) ), rc );
358 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
359 DB *db = bdb->bi_dn2id->bdi_db;
361 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn, 0, 0 );
363 if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
365 BDB_IDL_ALL(bdb, ids);
370 key.size = strlen( dn ) + 2;
371 key.data = ch_malloc( key.size );
372 ((char *)key.data)[0] = prefix;
373 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
378 data.ulen = BDB_IDL_UM_SIZEOF;
379 data.flags = DB_DBT_USERMEM;
382 rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
385 Debug( LDAP_DEBUG_TRACE,
386 "<= bdb_dn2idl: get failed: %s (%d)\n",
387 db_strerror( rc ), rc, 0 );
390 Debug( LDAP_DEBUG_TRACE,
391 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
393 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );