]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2entry.c
Initial revision
[openldap] / servers / slapd / back-ldbm / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry index */
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include "slap.h"
7 #include "back-ldbm.h"
8
9 extern struct dbcache   *ldbm_cache_open();
10 extern Datum            ldbm_cache_fetch();
11 extern char             *dn_parent();
12 extern Entry            *str2entry();
13 extern Entry            *cache_find_entry_id();
14 extern char             *entry2str();
15 extern pthread_mutex_t  entry2str_mutex;
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;
24
25         Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %d, \"%s\" )\n", e->e_id,
26             e->e_dn, 0 );
27
28         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
29             == NULL ) {
30                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
31                     LDBM_SUFFIX, 0, 0 );
32                 return( -1 );
33         }
34
35         key.dptr = (char *) &e->e_id;
36         key.dsize = sizeof(ID);
37
38         pthread_mutex_lock( &entry2str_mutex );
39         data.dptr = entry2str( e, &len, 1 );
40         data.dsize = len + 1;
41
42         /* store it - LDBM_SYNC ensures id2entry is always consistent */
43         rc = ldbm_cache_store( db, key, data, LDBM_REPLACE|LDBM_SYNC );
44
45         pthread_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         return( rc );
52 }
53
54 int
55 id2entry_delete( Backend *be, Entry *e )
56 {
57         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
58         struct dbcache  *db;
59         Datum           key;
60         int             rc;
61
62         Debug( LDAP_DEBUG_TRACE, "=> id2entry_delete( %d, \"%s\" )\n", e->e_id,
63             e->e_dn, 0 );
64
65         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
66             == NULL ) {
67                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
68                     LDBM_SUFFIX, 0, 0 );
69                 return( -1 );
70         }
71
72         if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
73                 Debug( LDAP_DEBUG_ANY, "could not delete %d (%s) from cache\n",
74                     e->e_id, e->e_dn, 0 );
75         }
76
77         key.dptr = (char *) &e->e_id;
78         key.dsize = sizeof(ID);
79
80         rc = ldbm_cache_delete( db, key );
81
82         ldbm_cache_close( be, db );
83
84         Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
85         return( rc );
86 }
87
88 Entry *
89 id2entry( Backend *be, ID id )
90 {
91         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
92         struct dbcache  *db;
93         Datum           key, data;
94         Entry           *e;
95
96         Debug( LDAP_DEBUG_TRACE, "=> id2entry( %ld )\n", id, 0, 0 );
97
98         if ( (e = cache_find_entry_id( &li->li_cache, id )) != NULL ) {
99                 Debug( LDAP_DEBUG_TRACE, "<= id2entry 0x%x (cache)\n", e, 0,
100                     0 );
101                 return( e );
102         }
103
104         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
105             == NULL ) {
106                 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
107                     LDBM_SUFFIX, 0, 0 );
108                 return( NULL );
109         }
110
111         key.dptr = (char *) &id;
112         key.dsize = sizeof(ID);
113
114         data = ldbm_cache_fetch( db, key );
115
116         if ( data.dptr == NULL ) {
117                 Debug( LDAP_DEBUG_TRACE, "<= id2entry( %ld ) not found\n", id,
118                     0, 0 );
119                 ldbm_cache_close( be, db );
120                 return( NULL );
121         }
122
123         if ( (e = str2entry( data.dptr )) != NULL ) {
124                 e->e_id = id;
125                 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
126         }
127
128         ldbm_datum_free( db->dbc_db, data );
129         ldbm_cache_close( be, db );
130
131         Debug( LDAP_DEBUG_TRACE, "<= id2entry( %ld ) 0x%x (disk)\n", id, e, 0 );
132         return( e );
133 }