]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/key.c
don't segfault if a database doesn't have the suffix
[openldap] / servers / slapd / back-bdb / key.c
1 /* index.c - routines for dealing with attribute indexes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-bdb.h"
26 #include "idl.h"
27
28 /* read a key */
29 int
30 bdb_key_read(
31         Backend *be,
32         DB *db,
33         DB_TXN *txn,
34         struct berval *k,
35         ID *ids
36 )
37 {
38         int rc;
39         DBT key;
40
41 #ifdef NEW_LOGGING
42         LDAP_LOG( INDEX, ENTRY, "key_read: enter\n", 0, 0, 0 );
43 #else
44         Debug( LDAP_DEBUG_TRACE, "=> key_read\n", 0, 0, 0 );
45 #endif
46
47         DBTzero( &key );
48         bv2DBT(k,&key);
49         key.ulen = key.size;
50         key.flags = DB_DBT_USERMEM;
51
52         rc = bdb_idl_fetch_key( be, db, txn, &key, ids );
53
54         if( rc != LDAP_SUCCESS ) {
55 #ifdef NEW_LOGGING
56                 LDAP_LOG( INDEX, ERR, "bdb_key_read: failed (%d)\n", rc, 0, 0 );
57 #else
58                 Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read: failed (%d)\n",
59                         rc, 0, 0 );
60 #endif
61         } else {
62 #ifdef NEW_LOGGING
63                 LDAP_LOG( INDEX, DETAIL1, 
64                         "bdb_key_read: %ld candidates\n", (long)BDB_IDL_N(ids), 0, 0);
65 #else
66                 Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read %ld candidates\n",
67                         (long) BDB_IDL_N(ids), 0, 0 );
68 #endif
69         }
70
71         return rc;
72 }
73
74 /* Add or remove stuff from index files */
75 int
76 bdb_key_change(
77         Backend *be,
78         DB *db,
79         DB_TXN *txn,
80         struct berval *k,
81         ID id,
82         int op
83 )
84 {
85         int     rc;
86         DBT     key;
87
88 #ifdef NEW_LOGGING
89         LDAP_LOG( INDEX, ENTRY, "key_change: %s ID %lx\n",
90                 op == SLAP_INDEX_ADD_OP ? "Add" : "Delete", (long) id, 0 );
91 #else
92         Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n",
93                 op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id, 0 );
94 #endif
95
96         DBTzero( &key );
97         bv2DBT(k,&key);
98         key.ulen = key.size;
99         key.flags = DB_DBT_USERMEM;
100
101         if (op == SLAP_INDEX_ADD_OP) {
102                 /* Add values */
103                 rc = bdb_idl_insert_key( be, db, txn, &key, id );
104                 if ( rc == DB_KEYEXIST ) rc = 0;
105         } else {
106                 /* Delete values */
107                 rc = bdb_idl_delete_key( be, db, txn, &key, id );
108                 if ( rc == DB_NOTFOUND ) rc = 0;
109         }
110
111 #ifdef NEW_LOGGING
112         LDAP_LOG( INDEX, RESULTS, "key_change: return %d\n", rc, 0, 0 );
113 #else
114         Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc, 0, 0 );
115 #endif
116
117         return rc;
118 }