]> git.sur5r.net Git - openldap/blob - servers/slapd/back-wt/id2entry.c
25c023c4654a19556052d345b21679095fbdbbf4
[openldap] / servers / slapd / back-wt / id2entry.c
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2002-2017 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
18  * based on back-bdb for inclusion in OpenLDAP Software.
19  * WiredTiger is a product of MongoDB Inc.
20  */
21
22 #include "back-wt.h"
23 #include "config.h"
24
25 static int wt_id2entry_put(
26         Operation *op,
27         WT_SESSION *session,
28         Entry *e,
29         const char *config )
30 {
31         struct berval bv;
32         WT_CURSOR *cursor = NULL;
33         WT_ITEM item;
34         int rc;
35
36         rc = entry_encode( e, &bv );
37         if(rc != LDAP_SUCCESS){
38                 return -1;
39         }
40         item.size = bv.bv_len;
41         item.data = bv.bv_val;
42
43         rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL,
44                                                           config, &cursor);
45         if ( rc ) {
46                 Debug( LDAP_DEBUG_ANY,
47                            LDAP_XSTRING(wt_id2entry_put)
48                            ": open_cursor failed: %s (%d)\n",
49                            wiredtiger_strerror(rc), rc, 0 );
50                 goto done;
51         }
52         cursor->set_key(cursor, e->e_id);
53         cursor->set_value(cursor, e->e_ndn, &item);
54         rc = cursor->insert(cursor);
55         if ( rc ) {
56                 Debug( LDAP_DEBUG_ANY,
57                            LDAP_XSTRING(wt_id2entry_put)
58                            ": insert failed: %s (%d)\n",
59                            wiredtiger_strerror(rc), rc, 0 );
60                 goto done;
61         }
62
63 done:
64         ch_free( bv.bv_val );
65         if(cursor){
66                 cursor->close(cursor);
67         }
68         return rc;
69 }
70
71 int wt_id2entry_add(
72         Operation *op,
73         WT_SESSION *session,
74         Entry *e )
75 {
76         return wt_id2entry_put(op, session, e, "overwrite=false");
77 }
78
79 int wt_id2entry_update(
80         Operation *op,
81         WT_SESSION *session,
82         Entry *e )
83 {
84         return wt_id2entry_put(op, session, e, "overwrite=true");
85 }
86
87 int wt_id2entry_delete(
88         Operation *op,
89         WT_SESSION *session,
90         Entry *e )
91 {
92         int rc;
93         WT_CURSOR *cursor = NULL;
94         rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL,
95                                                           NULL, &cursor);
96         if ( rc ) {
97                 Debug( LDAP_DEBUG_ANY,
98                            LDAP_XSTRING(wt_id2entry_delete)
99                            ": open_cursor failed: %s (%d)\n",
100                            wiredtiger_strerror(rc), rc, 0 );
101                 goto done;
102         }
103         cursor->set_key(cursor, e->e_id);
104         rc = cursor->remove(cursor);
105         if ( rc ) {
106                 Debug( LDAP_DEBUG_ANY,
107                            LDAP_XSTRING(wt_id2entry_delete)
108                            ": remove failed: %s (%d)\n",
109                            wiredtiger_strerror(rc), rc, 0 );
110                 goto done;
111         }
112
113 done:
114         if(cursor){
115                 cursor->close(cursor);
116         }
117         return rc;
118 }
119
120 int wt_id2entry( BackendDB *be,
121                                  WT_SESSION *session,
122                                  ID id,
123                                  Entry **ep ){
124         int rc;
125         WT_CURSOR *cursor = NULL;
126         WT_ITEM item;
127         EntryHeader eh;
128         int eoff;
129         Entry *e = NULL;
130
131         rc = session->open_cursor(session, WT_TABLE_ID2ENTRY"(entry)", NULL,
132                                                           NULL, &cursor);
133         if ( rc ) {
134                 Debug( LDAP_DEBUG_ANY,
135                            LDAP_XSTRING(wt_id2entry)
136                            ": open_cursor failed: %s (%d)\n",
137                            wiredtiger_strerror(rc), rc, 0 );
138                 goto done;
139         }
140
141         cursor->set_key(cursor, id);
142         rc = cursor->search(cursor);
143         if ( rc ) {
144                 goto done;
145         }
146
147         cursor->get_value(cursor, &item);
148         rc = wt_entry_header( &item,  &eh );
149         eoff = eh.data - (char *)item.data;
150         eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size;
151         eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
152         memset(eh.bv.bv_val, 0xff, eh.bv.bv_len);
153         eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
154         memcpy(eh.data, item.data, item.size);
155         eh.data += eoff;
156         rc = entry_decode( &eh, &e );
157         if ( rc ) {
158                 Debug( LDAP_DEBUG_ANY,
159                            LDAP_XSTRING(wt_id2entry)
160                            ": entry decode error: %s (%d)\n",
161                            rc, 0, 0 );
162                 goto done;
163         }
164         e->e_id = id;
165         *ep = e;
166
167 done:
168         if(cursor){
169                 cursor->close(cursor);
170         }
171         return rc;
172 }
173
174 int wt_entry_return(
175         Entry *e
176         )
177 {
178         if ( !e ) {
179                 return 0;
180         }
181
182     /* Our entries are allocated in two blocks; the data comes from
183          * the db itself and the Entry structure and associated pointers
184          * are allocated in entry_decode. The db data pointer is saved
185          * in e_bv.
186          */
187         if ( e->e_bv.bv_val ) {
188 #if 0
189                 /* See if the DNs were changed by modrdn */
190                 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
191                         e->e_bv.bv_val + e->e_bv.bv_len ) {
192                         ch_free(e->e_name.bv_val);
193                         ch_free(e->e_nname.bv_val);
194                 }
195 #endif
196                 e->e_name.bv_val = NULL;
197                 e->e_nname.bv_val = NULL;
198                 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
199                 if( !(slapMode & SLAP_TOOL_MODE) ) {
200                         free( e->e_bv.bv_val );
201                 }
202                 BER_BVZERO( &e->e_bv );
203         }
204
205         entry_free( e );
206 }
207
208 int wt_entry_release(
209         Operation *op,
210         Entry *e,
211         int rw )
212 {
213         struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
214         return wt_entry_return( e );
215 }
216
217 /*
218  * return LDAP_SUCCESS IFF we can retrieve the specified entry.
219  */
220 int wt_entry_get(
221         Operation *op,
222         struct berval *ndn,
223         ObjectClass *oc,
224         AttributeDescription *at,
225         int rw,
226         Entry **ent )
227 {
228         return 0;
229 }
230
231 /*
232  * Local variables:
233  * indent-tabs-mode: t
234  * tab-width: 4
235  * c-basic-offset: 4
236  * End:
237  */