]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/key.c
f44e89a3a7c298790947fabb7f868923abe1e122
[openldap] / servers / slapd / back-ldbm / 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 1998-2005 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-ldbm.h"
26
27 /* read a key */
28 int
29 key_read(
30     Backend     *be,
31         DBCache *db,
32     struct berval *k,
33         ID_BLOCK **idout
34 )
35 {
36         Datum           key;
37         ID_BLOCK                *idl;
38
39         Debug( LDAP_DEBUG_TRACE, "=> key_read\n", 0, 0, 0 );
40
41
42         ldbm_datum_init( key );
43         key.dptr = k->bv_val;
44         key.dsize = k->bv_len;
45
46         idl = idl_fetch( be, db, key );
47
48         Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
49                idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
50
51
52         *idout = idl;
53         return LDAP_SUCCESS;
54 }
55
56 /* Add or remove stuff from index files */
57 int
58 key_change(
59     Backend             *be,
60     DBCache     *db,
61     struct berval *k,
62     ID                  id,
63     int                 op
64 )
65 {
66         int     rc;
67         Datum   key;
68
69         Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n",
70                 op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id, 0 );
71
72
73         ldbm_datum_init( key );
74         key.dptr = k->bv_val;
75         key.dsize = k->bv_len;
76
77         ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
78         if (op == SLAP_INDEX_ADD_OP) {
79             /* Add values */
80             rc = idl_insert_key( be, db, key, id );
81
82         } else {
83             /* Delete values */
84             rc = idl_delete_key( be, db, key, id );
85         }
86         ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
87
88
89         Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc, 0, 0 );
90
91
92         ldap_pvt_thread_yield();
93
94         return rc;
95 }