]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2children.c
Add DN_INDICES search filter and has_children support. Move id2children
[openldap] / servers / slapd / back-ldbm / id2children.c
1 /* id2children.c - routines to deal with the id2children index */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #include <ac/string.h>
11
12 #include <ac/socket.h>
13
14 #include "slap.h"
15 #include "back-ldbm.h"
16
17 #ifndef DN_INDICES
18 int
19 id2children_add(
20     Backend     *be,
21     Entry       *p,
22     Entry       *e
23 )
24 {
25         DBCache *db;
26         Datum           key;
27         char            buf[20];
28
29         ldbm_datum_init( key );
30
31         Debug( LDAP_DEBUG_TRACE, "=> id2children_add( %ld, %ld )\n",
32                p ? p->e_id : 0, e->e_id, 0 );
33
34         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
35             LDBM_WRCREAT )) == NULL ) {
36                 Debug( LDAP_DEBUG_ANY,
37                     "<= id2children_add -1 could not open \"id2children%s\"\n",
38                     LDBM_SUFFIX, 0, 0 );
39                 return( -1 );
40         }
41
42         sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
43         key.dptr = buf;
44         key.dsize = strlen( buf ) + 1;
45
46         if ( idl_insert_key( be, db, key, e->e_id ) != 0 ) {
47                 Debug( LDAP_DEBUG_TRACE, "<= id2children_add -1 (idl_insert)\n",
48                     0, 0, 0 );
49                 ldbm_cache_close( be, db );
50                 return( -1 );
51         }
52
53         ldbm_cache_close( be, db );
54
55         Debug( LDAP_DEBUG_TRACE, "<= id2children_add 0\n", 0, 0, 0 );
56         return( 0 );
57 }
58
59
60 int
61 id2children_remove(
62     Backend     *be,
63     Entry       *p,
64     Entry       *e
65 )
66 {
67         DBCache *db;
68         Datum           key;
69         char            buf[20];
70
71         Debug( LDAP_DEBUG_TRACE, "=> id2children_remove( %ld, %ld )\n", p ? p->e_id
72             : 0, e->e_id, 0 );
73
74         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
75             LDBM_WRCREAT )) == NULL ) {
76                 Debug( LDAP_DEBUG_ANY,
77                     "<= id2children_remove -1 could not open \"id2children%s\"\n",
78                     LDBM_SUFFIX, 0, 0 );
79                 return( -1 );
80         }
81
82         ldbm_datum_init( key );
83         sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
84         key.dptr = buf;
85         key.dsize = strlen( buf ) + 1;
86
87         if ( idl_delete_key( be, db, key, e->e_id ) != 0 ) {
88 #if 0
89                 Debug( LDAP_DEBUG_ANY,
90                         "<= id2children_remove: idl_delete_key failure\n",
91                     0, 0, 0 );
92                 ldbm_cache_close( be, db );
93                 return( -1 );
94 #else
95                 Debug( LDAP_DEBUG_ANY,
96                         "<= id2children_remove: ignoring idl_delete_key failure\n",
97                     0, 0, 0 );
98 #endif
99         }
100
101         ldbm_cache_close( be, db );
102
103         Debug( LDAP_DEBUG_TRACE, "<= id2children_remove 0\n", 0, 0, 0 );
104         return( 0 );
105 }
106 #endif
107
108 int
109 has_children(
110     Backend     *be,
111     Entry       *p
112 )
113 {
114         DBCache *db;
115         Datum           key;
116         int             rc = 0;
117         ID_BLOCK                *idl;
118 #ifndef DN_INDICES
119         char            buf[20];
120 #endif
121
122         ldbm_datum_init( key );
123
124         Debug( LDAP_DEBUG_TRACE, "=> has_children( %ld )\n", p->e_id , 0, 0 );
125
126 #ifndef DN_INDICES
127         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
128             LDBM_WRCREAT )) == NULL ) {
129                 Debug( LDAP_DEBUG_ANY,
130                     "<= has_children -1 could not open \"id2children%s\"\n",
131                     LDBM_SUFFIX, 0, 0 );
132                 return( 0 );
133         }
134
135         sprintf( buf, "%c%ld", EQ_PREFIX, p->e_id );
136         key.dptr = buf;
137         key.dsize = strlen( buf ) + 1;
138
139 #else
140         if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX,
141             LDBM_WRCREAT )) == NULL ) {
142                 Debug( LDAP_DEBUG_ANY,
143                     "<= has_children -1 could not open \"dn2id%s\"\n",
144                     LDBM_SUFFIX, 0, 0 );
145                 return( 0 );
146         }
147
148         key.dsize = strlen( p->e_ndn ) + 2;
149         key.dptr = ch_malloc( key.dsize );
150         sprintf( key.dptr, "%c%s", DN_ONE_PREFIX, p->e_ndn );
151 #endif
152
153         idl = idl_fetch( be, db, key );
154
155         ldbm_cache_close( be, db );
156
157         if( idl != NULL ) {
158                 idl_free( idl );
159                 rc = 1;
160         }
161
162         Debug( LDAP_DEBUG_TRACE, "<= has_children( %ld ): %s\n",
163                 p->e_id, rc ? "yes" : "no", 0 );
164         return( rc );
165 }