]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2entry.c
2aee65d523b745b0279badce13f31ab2acb01ba3
[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 int
13 id2entry_add( Backend *be, Entry *e )
14 {
15         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
16         struct dbcache  *db;
17         Datum           key, data;
18         int             len, rc, flags;
19
20         ldbm_datum_init( key );
21         ldbm_datum_init( data );
22
23         Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %lu, \"%s\" )\n", e->e_id,
24             e->e_dn, 0 );
25
26         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
27             == NULL ) {
28                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
29                     LDBM_SUFFIX, 0, 0 );
30                 return( -1 );
31         }
32
33         key.dptr = (char *) &e->e_id;
34         key.dsize = sizeof(ID);
35
36         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
37         data.dptr = entry2str( e, &len, 1 );
38         data.dsize = len + 1;
39
40         /* store it */
41         flags = LDBM_REPLACE;
42         if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
43         rc = ldbm_cache_store( db, key, data, flags );
44
45         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
46
47         ldbm_cache_close( be, db );
48         (void) cache_add_entry_lock( &li->li_cache, e, 0 );
49
50         Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
51
52         /* XXX should entries be born locked, i.e. apply writer lock here? */
53         return( rc );
54 }
55
56 int
57 id2entry_delete( Backend *be, Entry *e )
58 {
59         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
60         struct dbcache  *db;
61         Datum           key;
62         int             rc;
63
64         Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %lu, \"%s\" )\n", e->e_id,
65             e->e_dn, 0 );
66
67         /* XXX - check for writer lock - should also check no reader pending */
68 #ifdef LDAP_DEBUG
69         assert(ldap_pvt_thread_rdwr_wchk(&e->e_rdwr));
70 #endif
71
72         ldbm_datum_init( key );
73
74         /* XXX - check for writer lock - should also check no reader pending */
75         Debug (LDAP_DEBUG_TRACE,
76                 "rdwr_Xchk: readers_reading: %d writer_writing: %d\n",
77                 e->e_rdwr.lt_readers_reading, e->e_rdwr.lt_writer_writing, 0);
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 %lu (%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 /* XXX returns entry with reader/writer lock */
103 Entry *
104 id2entry( 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 0x%lx (cache)\n",
119                         rw ? "w" : "r", (unsigned long)e, 0 );
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         /* acquire required reader/writer lock */
154         if (entry_rdwr_lock(e, rw)) {
155                 /* XXX set DELETE flag?? */
156                 entry_free(e);
157                 return(NULL);
158         }
159
160         e->e_id = id;
161         (void) cache_add_entry_lock( &li->li_cache, e, 0 );
162
163         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (disk)\n",
164                 rw ? "w" : "r", id, 0 );
165         return( e );
166 }
167
168 Entry *
169 id2entry_r( Backend *be, ID id )
170 {
171         return( id2entry( be, id, 0 ) );
172 }
173
174 Entry *
175 id2entry_w( Backend *be, ID id )
176 {
177         return( id2entry( be, id, 1 ) );
178 }
179