]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2children.c
Mongo patch: changes from -devel from 981105 snap to present
[openldap] / servers / slapd / back-ldbm / id2children.c
1 /* id2children.c - routines to deal with the id2children index */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <ac/string.h>
7
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12
13 struct dbcache  *ldbm_cache_open();
14 extern Datum    ldbm_cache_fetch();
15 IDList          *idl_fetch();
16
17 int
18 id2children_add(
19     Backend     *be,
20     Entry       *p,
21     Entry       *e
22 )
23 {
24         struct dbcache  *db;
25         Datum           key;
26         int             len, rc;
27         IDList          *idl;
28         char            buf[20];
29
30 #ifdef HAVE_BERKELEY_DB2
31         Datum           data;
32         memset( &key, 0, sizeof( key ) );
33         memset( &data, 0, sizeof( data ) );
34 #endif
35
36         Debug( LDAP_DEBUG_TRACE, "=> id2children_add( %d, %d )\n", p ? p->e_id
37             : 0, e->e_id, 0 );
38
39         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
40             LDBM_WRCREAT )) == NULL ) {
41                 Debug( LDAP_DEBUG_ANY,
42                     "<= id2children_add -1 could not open \"id2children%s\"\n",
43                     LDBM_SUFFIX, 0, 0 );
44                 return( -1 );
45         }
46
47         sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
48         key.dptr = buf;
49         key.dsize = strlen( buf ) + 1;
50
51         if ( idl_insert_key( be, db, key, e->e_id ) != 0 ) {
52                 Debug( LDAP_DEBUG_TRACE, "<= id2children_add -1 (idl_insert)\n",
53                     0, 0, 0 );
54                 ldbm_cache_close( be, db );
55                 return( -1 );
56         }
57
58         ldbm_cache_close( be, db );
59
60         Debug( LDAP_DEBUG_TRACE, "<= id2children_add 0\n", 0, 0, 0 );
61         return( 0 );
62 }
63
64
65 int
66 id2children_remove(
67     Backend     *be,
68     Entry       *p,
69     Entry       *e
70 )
71 {
72         struct dbcache  *db;
73         Datum           key;
74         int             len, rc;
75         IDList          *idl;
76         char            buf[20];
77
78         Debug( LDAP_DEBUG_TRACE, "=> id2children_remove( %d, %d )\n", p ? p->e_id
79             : 0, e->e_id, 0 );
80
81         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
82             LDBM_WRCREAT )) == NULL ) {
83                 Debug( LDAP_DEBUG_ANY,
84                     "<= id2children_add -1 could not open \"id2children%s\"\n",
85                     LDBM_SUFFIX, 0, 0 );
86                 return( -1 );
87         }
88
89         memset( &key, 0, sizeof(key) );
90         sprintf( buf, "%c%d", EQ_PREFIX, p ? p->e_id : 0 );
91         key.dptr = buf;
92         key.dsize = strlen( buf ) + 1;
93
94         if ( idl_delete_key( be, db, key, e->e_id ) != 0 ) {
95                 Debug( LDAP_DEBUG_TRACE, "<= id2children_add -1 (idl_insert)\n",
96                     0, 0, 0 );
97                 ldbm_cache_close( be, db );
98                 return( -1 );
99         }
100
101         ldbm_cache_close( be, db );
102
103         Debug( LDAP_DEBUG_TRACE, "<= id2children_add 0\n", 0, 0, 0 );
104         return( 0 );
105 }
106
107 int
108 has_children(
109     Backend     *be,
110     Entry       *p
111 )
112 {
113         struct dbcache  *db;
114         Datum           key;
115         int             rc;
116         IDList          *idl;
117         char            buf[20];
118
119 #ifdef HAVE_BERKELEY_DB2
120         memset( &key, 0, sizeof( key ) );
121 #endif
122
123         Debug( LDAP_DEBUG_TRACE, "=> has_children( %d )\n", p->e_id , 0, 0 );
124
125         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
126             LDBM_WRCREAT )) == NULL ) {
127                 Debug( LDAP_DEBUG_ANY,
128                     "<= has_children -1 could not open \"id2children%s\"\n",
129                     LDBM_SUFFIX, 0, 0 );
130                 return( 0 );
131         }
132
133         sprintf( buf, "%c%ld", EQ_PREFIX, p->e_id );
134         key.dptr = buf;
135         key.dsize = strlen( buf ) + 1;
136
137         idl = idl_fetch( be, db, key );
138
139         ldbm_cache_close( be, db );
140         rc = idl ? 1 : 0;
141         idl_free( idl );
142
143         Debug( LDAP_DEBUG_TRACE, "<= has_children %d\n", rc, 0, 0 );
144         return( rc );
145 }