]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/key.c
c84028542d2747eb102dd53f9081c218208142be
[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         Debug( LDAP_DEBUG_TRACE, "=> key_read\n", 0, 0, 0 );
42
43         DBTzero( &key );
44         bv2DBT(k,&key);
45         key.ulen = key.size;
46         key.flags = DB_DBT_USERMEM;
47
48         rc = bdb_idl_fetch_key( be, db, txn, &key, ids );
49
50         if( rc != LDAP_SUCCESS ) {
51                 Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read: failed (%d)\n",
52                         rc, 0, 0 );
53         } else {
54                 Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read %ld candidates\n",
55                         (long) BDB_IDL_N(ids), 0, 0 );
56         }
57
58         return rc;
59 }
60
61 /* Add or remove stuff from index files */
62 int
63 bdb_key_change(
64         Backend *be,
65         DB *db,
66         DB_TXN *txn,
67         struct berval *k,
68         ID id,
69         int op
70 )
71 {
72         int     rc;
73         DBT     key;
74
75         Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n",
76                 op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id, 0 );
77
78         DBTzero( &key );
79         bv2DBT(k,&key);
80         key.ulen = key.size;
81         key.flags = DB_DBT_USERMEM;
82
83         if (op == SLAP_INDEX_ADD_OP) {
84                 /* Add values */
85                 rc = bdb_idl_insert_key( be, db, txn, &key, id );
86                 if ( rc == DB_KEYEXIST ) rc = 0;
87         } else {
88                 /* Delete values */
89                 rc = bdb_idl_delete_key( be, db, txn, &key, id );
90                 if ( rc == DB_NOTFOUND ) rc = 0;
91         }
92
93         Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc, 0, 0 );
94
95         return rc;
96 }