]> git.sur5r.net Git - openldap/blob - servers/slapd/back-wt/id2entry.c
e135d18f70ee091633a31c7915b664850f0a4753
[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-2015 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                 Debug( LDAP_DEBUG_ANY,
145                            LDAP_XSTRING(wt_id2entry)
146                            ": search failed: %s (%d)\n",
147                            wiredtiger_strerror(rc), rc, 0 );
148                 goto done;
149         }
150
151         cursor->get_value(cursor, &item);
152         rc = wt_entry_header( &item,  &eh );
153         eoff = eh.data - (char *)item.data;
154         eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size;
155         eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
156         memset(eh.bv.bv_val, 0xff, eh.bv.bv_len);
157         eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
158         memcpy(eh.data, item.data, item.size);
159         eh.data += eoff;
160         rc = entry_decode( &eh, &e );
161         if ( rc ) {
162                 Debug( LDAP_DEBUG_ANY,
163                            LDAP_XSTRING(wt_id2entry)
164                            ": entry decode error: %s (%d)\n",
165                            rc, 0, 0 );
166                 goto done;
167         }
168         e->e_id = id;
169         *ep = e;
170
171 done:
172         if(cursor){
173                 cursor->close(cursor);
174         }
175         return rc;
176 }
177
178 int wt_entry_return(
179         Entry *e
180         )
181 {
182         if ( !e ) {
183                 return 0;
184         }
185
186     /* Our entries are allocated in two blocks; the data comes from
187          * the db itself and the Entry structure and associated pointers
188          * are allocated in entry_decode. The db data pointer is saved
189          * in e_bv.
190          */
191         if ( e->e_bv.bv_val ) {
192 #if 0
193                 /* See if the DNs were changed by modrdn */
194                 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
195                         e->e_bv.bv_val + e->e_bv.bv_len ) {
196                         ch_free(e->e_name.bv_val);
197                         ch_free(e->e_nname.bv_val);
198                 }
199 #endif
200                 e->e_name.bv_val = NULL;
201                 e->e_nname.bv_val = NULL;
202                 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
203                 if( !(slapMode & SLAP_TOOL_MODE) ) {
204                         free( e->e_bv.bv_val );
205                 }
206                 BER_BVZERO( &e->e_bv );
207         }
208
209         entry_free( e );
210 }
211
212 int wt_entry_release(
213         Operation *op,
214         Entry *e,
215         int rw )
216 {
217         struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
218         return wt_entry_return( e );
219 }
220
221 /*
222  * return LDAP_SUCCESS IFF we can retrieve the specified entry.
223  */
224 int wt_entry_get(
225         Operation *op,
226         struct berval *ndn,
227         ObjectClass *oc,
228         AttributeDescription *at,
229         int rw,
230         Entry **ent )
231 {
232         return 0;
233 }
234
235 /*
236  * Local variables:
237  * indent-tabs-mode: t
238  * tab-width: 4
239  * c-basic-offset: 4
240  * End:
241  */