]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/key.c
aa6e966fd7ef427758c196fb6dd15e38798c588f
[openldap] / servers / slapd / back-bdb / key.c
1 /* index.c - routines for dealing with attribute indexes */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-bdb.h"
17 #include "idl.h"
18
19 /* read a key */
20 int
21 bdb_key_read(
22         Backend *be,
23         DB *db,
24         DB_TXN *txn,
25         struct berval *k,
26         ID *ids
27 )
28 {
29         int rc;
30         DBT key;
31
32 #ifdef NEW_LOGGING
33         LDAP_LOG( INDEX, ENTRY, "key_read: enter\n", 0, 0, 0 );
34 #else
35         Debug( LDAP_DEBUG_TRACE, "=> key_read\n", 0, 0, 0 );
36 #endif
37
38         DBTzero( &key );
39         bv2DBT(k,&key);
40         key.ulen = key.size;
41         key.flags = DB_DBT_USERMEM;
42
43         rc = bdb_idl_fetch_key( be, db, txn, &key, ids );
44
45         if( rc != LDAP_SUCCESS ) {
46 #ifdef NEW_LOGGING
47                 LDAP_LOG( INDEX, ERR, "bdb_key_read: failed (%d)\n", rc, 0, 0 );
48 #else
49                 Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read: failed (%d)\n",
50                         rc, 0, 0 );
51 #endif
52         } else {
53 #ifdef NEW_LOGGING
54                 LDAP_LOG( INDEX, DETAIL1, 
55                         "bdb_key_read: %ld candidates\n", (long)BDB_IDL_N(ids), 0, 0);
56 #else
57                 Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read %ld candidates\n",
58                         (long) BDB_IDL_N(ids), 0, 0 );
59 #endif
60         }
61
62         return rc;
63 }
64
65 /* Add or remove stuff from index files */
66 int
67 bdb_key_change(
68         Backend *be,
69         DB *db,
70         DB_TXN *txn,
71         struct berval *k,
72         ID id,
73         int op
74 )
75 {
76         int     rc;
77         DBT     key;
78
79 #ifdef NEW_LOGGING
80         LDAP_LOG( INDEX, ENTRY, "key_change: %s ID %lx\n",
81                 op == SLAP_INDEX_ADD_OP ? "Add" : "Delete", (long) id, 0 );
82 #else
83         Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n",
84                 op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id, 0 );
85 #endif
86
87         DBTzero( &key );
88         bv2DBT(k,&key);
89         key.ulen = key.size;
90         key.flags = DB_DBT_USERMEM;
91
92         if (op == SLAP_INDEX_ADD_OP) {
93                 /* Add values */
94                 rc = bdb_idl_insert_key( be, db, txn, &key, id );
95                 if ( rc == DB_KEYEXIST ) rc = 0;
96         } else {
97                 /* Delete values */
98                 rc = bdb_idl_delete_key( be, db, txn, &key, id );
99                 if ( rc == DB_NOTFOUND ) rc = 0;
100         }
101
102 #ifdef NEW_LOGGING
103         LDAP_LOG( INDEX, RESULTS, "key_change: return %d\n", rc, 0, 0 );
104 #else
105         Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc, 0, 0 );
106 #endif
107
108         return rc;
109 }