]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2entry.c
Removed unnecessary definition that is already in core.schema.
[openldap] / servers / slapd / back-ldbm / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry index */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8
9 #include "slap.h"
10 #include "back-ldbm.h"
11
12 /*
13  * This routine adds (or updates) an entry on disk.
14  * The cache should already be updated. 
15  */
16
17 int
18 id2entry_add( Backend *be, Entry *e )
19 {
20         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
21         struct dbcache  *db;
22         Datum           key, data;
23         int             len, rc, flags;
24
25         ldbm_datum_init( key );
26         ldbm_datum_init( data );
27
28         Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
29             e->e_dn, 0 );
30
31         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
32             == NULL ) {
33                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
34                     LDBM_SUFFIX, 0, 0 );
35                 return( -1 );
36         }
37
38         key.dptr = (char *) &e->e_id;
39         key.dsize = sizeof(ID);
40
41         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
42         data.dptr = entry2str( e, &len, 1 );
43         data.dsize = len + 1;
44
45         /* store it */
46         flags = LDBM_REPLACE;
47         if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
48         rc = ldbm_cache_store( db, key, data, flags );
49
50         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
51
52         ldbm_cache_close( be, db );
53
54         Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
55
56         return( rc );
57 }
58
59 int
60 id2entry_delete( Backend *be, Entry *e )
61 {
62         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
63         struct dbcache  *db;
64         Datum           key;
65         int             rc;
66
67         Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
68             e->e_dn, 0 );
69
70 #ifdef notdef
71 #ifdef LDAP_DEBUG
72         /* check for writer lock */
73         assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
74 #endif
75 #endif
76
77         ldbm_datum_init( key );
78
79         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
80                 == NULL ) {
81                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
82                     LDBM_SUFFIX, 0, 0 );
83                 return( -1 );
84         }
85
86         if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
87                 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
88                     e->e_id, e->e_dn, 0 );
89         }
90
91         key.dptr = (char *) &e->e_id;
92         key.dsize = sizeof(ID);
93
94         rc = ldbm_cache_delete( db, key );
95
96         ldbm_cache_close( be, db );
97
98         Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
99         return( rc );
100 }
101
102 /* returns entry with reader/writer lock */
103 Entry *
104 id2entry_rw( Backend *be, ID id, int rw )
105 {
106         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
107         struct dbcache  *db;
108         Datum           key, data;
109         Entry           *e;
110
111         ldbm_datum_init( key );
112         ldbm_datum_init( data );
113
114         Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
115                 rw ? "w" : "r", id, 0 );
116
117         if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
118                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
119                         rw ? "w" : "r", id, (unsigned long) e );
120                 return( e );
121         }
122
123         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
124                 == NULL ) {
125                 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
126                     LDBM_SUFFIX, 0, 0 );
127                 return( NULL );
128         }
129
130         key.dptr = (char *) &id;
131         key.dsize = sizeof(ID);
132
133         data = ldbm_cache_fetch( db, key );
134
135         if ( data.dptr == NULL ) {
136                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
137                         rw ? "w" : "r", id, 0 );
138                 ldbm_cache_close( be, db );
139                 return( NULL );
140         }
141
142         e = str2entry( data.dptr );
143
144         ldbm_datum_free( db->dbc_db, data );
145         ldbm_cache_close( be, db );
146
147         if ( e == NULL ) {
148                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
149                         rw ? "w" : "r", id, 0 );
150                 return( NULL );
151         }
152
153         if ( e->e_id != id ) {
154                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (wrong id %ld on disk)\n",
155                         rw ? "w" : "r", id, e->e_id );
156                 entry_free( e );
157                 return( NULL );
158         }
159
160         if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
161                 entry_free( e );
162
163                 /* XXX this is a kludge.
164                  * maybe the entry got added underneath us
165                  * There are many underlying race condtions in the cache/disk code.
166                  */
167                 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
168                         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
169                                 rw ? "w" : "r", id, (unsigned long) e );
170                         return( e );
171                 }
172
173                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
174                         rw ? "w" : "r", id, 0 );
175                 return NULL;
176         }
177
178         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
179                 rw ? "w" : "r", id, (unsigned long) e );
180
181         return( e );
182 }