]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
ce84fe5c7353ca1d0e9182c4214d8cd43de25f3a
[openldap] / servers / slapd / back-bdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 int bdb_id2entry_add(
16         BackendDB *be,
17         DB_TXN *tid,
18         Entry *e )
19 {
20         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
21         DB *db = bdb->bi_id2entry->bdi_db;
22         DBT key, data;
23         struct berval bv;
24         int rc;
25
26         DBTzero( &key );
27         key.data = (char *) &e->e_id;
28         key.size = sizeof(ID);
29
30         rc = entry_encode( e, &bv );
31         if( rc != LDAP_SUCCESS ) {
32                 return -1;
33         }
34
35         DBTzero( &data );
36         bv2DBT( &bv, &data );
37
38         rc = db->put( db, tid, &key, &data, DB_NOOVERWRITE );
39
40         free( bv.bv_val );
41         return rc;
42 }
43
44 int bdb_id2entry_update(
45         BackendDB *be,
46         DB_TXN *tid,
47         Entry *e )
48 {
49         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
50         DB *db = bdb->bi_id2entry->bdi_db;
51         DBT key, data;
52         struct berval bv;
53         int rc;
54
55         DBTzero( &key );
56         key.data = (char *) &e->e_id;
57         key.size = sizeof(ID);
58
59         rc = entry_encode( e, &bv );
60         if( rc != LDAP_SUCCESS ) {
61                 return -1;
62         }
63
64         DBTzero( &data );
65         bv2DBT( &bv, &data );
66
67         rc = db->put( db, tid, &key, &data, 0 );
68
69         free( bv.bv_val );
70         return rc;
71 }
72
73 int bdb_id2entry(
74         BackendDB *be,
75         DB_TXN *tid,
76         ID id,
77         Entry **e )
78 {
79         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
80         DB *db = bdb->bi_id2entry->bdi_db;
81         DBT key, data;
82         struct berval bv;
83         int rc;
84
85         *e = NULL;
86
87         DBTzero( &key );
88         key.data = (char *) &id;
89         key.size = sizeof(ID);
90
91         DBTzero( &data );
92         data.flags = DB_DBT_MALLOC;
93
94         /* fetch it */
95         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
96
97         if( rc != 0 ) {
98                 return rc;
99         }
100
101         DBT2bv( &data, &bv );
102
103         rc = entry_decode( &bv, e );
104
105         if( rc == 0 ) {
106                 (*e)->e_id = id;
107         } else {
108                 /* only free on error. On success, the entry was
109                  * decoded in place.
110                  */
111                 ch_free( data.data );
112         }
113         return rc;
114 }
115
116 int bdb_id2entry_delete(
117         BackendDB *be,
118         DB_TXN *tid,
119         ID id )
120 {
121         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
122         DB *db = bdb->bi_id2entry->bdi_db;
123         DBT key;
124         int rc;
125
126         DBTzero( &key );
127         key.data = (char *) &id;
128         key.size = sizeof(ID);
129
130         rc = db->del( db, tid, &key, 0 );
131
132         return rc;
133 }
134
135 int bdb_entry_return(
136         BackendDB *be,
137         Entry *e )
138 {
139         /* Our entries are almost always contiguous blocks, so a single
140          * free() on the Entry pointer suffices. The exception is when
141          * an entry has been modified, in which case the attr list will
142          * have been alloc'd separately and its address will no longer
143          * be a constant offset from (e).
144          */
145         if( (void *) e->e_attrs != (void *) (e+1))
146         {
147                 attrs_free(e->e_attrs);
148         }
149         if (e->e_private)
150                 free(e->e_private);
151
152         ch_free(e);
153
154         return 0;
155 }
156 int bdb_entry_release(
157         BackendDB *be,
158         Connection *c,
159         Operation *o,
160         Entry *e,
161         int rw )
162 {
163         /* A tool will call this with NULL Connection and Operation
164          * pointers. We don't need to do anything in that case,
165          * because the tool is getting entries into a realloc'd
166          * buffer.
167          */
168         if (c && o)
169                 return bdb_entry_return(be, e);
170 }