]> git.sur5r.net Git - openldap/blob - servers/slapd/back-wt/init.c
Merge remote-tracking branch 'origin/mdb.RE/0.9'
[openldap] / servers / slapd / back-wt / init.c
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2002-2016 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 "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include "back-wt.h"
27 #include "config.h"
28
29 static int
30 wt_db_init( BackendDB *be, ConfigReply *cr )
31 {
32         struct wt_info *wi;
33
34         Debug( LDAP_DEBUG_TRACE,
35                    LDAP_XSTRING(wt_db_init) ": Initializing wt backend\n",
36                    0, 0, 0 );
37
38         /* allocate backend-database-specific stuff */
39     wi = ch_calloc( 1, sizeof(struct wt_info) );
40
41         wi->wi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
42         wi->wi_dbenv_config = ch_strdup("create");
43         wi->wi_lastid = 0;
44         wi->wi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
45         wi->wi_search_stack = NULL;
46
47         be->be_private = wi;
48         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
49
50         return LDAP_SUCCESS;
51 }
52
53 static int
54 wt_db_open( BackendDB *be, ConfigReply *cr )
55 {
56         struct wt_info *wi = (struct wt_info *) be->be_private;
57         int rc;
58         struct stat st;
59         WT_CONNECTION *conn;
60         WT_SESSION *session;
61
62         if ( be->be_suffix == NULL ) {
63                 Debug( LDAP_DEBUG_ANY,
64                            LDAP_XSTRING(wt_db_open) ": need suffix.\n",
65                            1, 0, 0 );
66                 return -1;
67         }
68
69         Debug( LDAP_DEBUG_ARGS,
70                    LDAP_XSTRING(wt_db_open) ": \"%s\"\n",
71                    be->be_suffix[0].bv_val, 0, 0 );
72
73         /* Check existence of home. Any error means trouble */
74         rc = stat( wi->wi_dbenv_home, &st );
75         if( rc ) {
76                 Debug( LDAP_DEBUG_ANY,
77                            LDAP_XSTRING(wt_db_open) ": database \"%s\": "
78                            "cannot access database directory \"%s\" (%d).\n",
79                            be->be_suffix[0].bv_val, wi->wi_dbenv_home, errno );
80                 return -1;
81         }
82
83         /* Open and create database */
84         rc = wiredtiger_open(wi->wi_dbenv_home, NULL,
85                                                  wi->wi_dbenv_config, &conn);
86         if( rc ) {
87                 Debug( LDAP_DEBUG_ANY,
88                            LDAP_XSTRING(wt_db_open) ": database \"%s\": "
89                            "cannot open database \"%s\" (%d).\n",
90                            be->be_suffix[0].bv_val, wi->wi_dbenv_home, errno );
91                 return -1;
92         }
93
94         rc = conn->open_session(conn, NULL, NULL, &session);
95         if( rc ) {
96                 Debug( LDAP_DEBUG_ANY,
97                            LDAP_XSTRING(wt_db_open) ": database \"%s\": "
98                            "cannot open session: \"%s\"\n",
99                            be->be_suffix[0].bv_val, wiredtiger_strerror(rc), 0);
100                 return -1;
101         }
102
103         rc = session->create(session,
104                                                  WT_TABLE_ID2ENTRY,
105                                                  "key_format=Q,"
106                                                  "value_format=Su,"
107                                                  "columns=(id,dn,entry)");
108         if( rc ) {
109                 Debug( LDAP_DEBUG_ANY,
110                            LDAP_XSTRING(wt_db_open) ": database \"%s\": "
111                            "cannot create entry table: \"%s\"\n",
112                            be->be_suffix[0].bv_val, wiredtiger_strerror(rc), 0);
113                 return -1;
114         }
115
116         rc = session->create(session,
117                                                  WT_TABLE_DN2ID,
118                                                  "key_format=S,"
119                                                  "value_format=QQS,"
120                                                  "columns=(ndn,id,pid,revdn)");
121         if( rc ) {
122                 Debug( LDAP_DEBUG_ANY,
123                            LDAP_XSTRING(wt_db_open) ": database \"%s\": "
124                            "cannot create entry table: \"%s\"\n",
125                            be->be_suffix[0].bv_val, wiredtiger_strerror(rc), 0);
126                 return -1;
127         }
128
129         /* not using dn2id index for id2entry table */
130         rc = session->create(session, WT_INDEX_DN, "columns=(dn)");
131         if( rc ) {
132                 Debug( LDAP_DEBUG_ANY,
133                            LDAP_XSTRING(wt_db_open) ": database \"%s\": "
134                            "cannot create dn index: \"%s\"\n",
135                            be->be_suffix[0].bv_val, wiredtiger_strerror(rc), 0);
136                 return -1;
137         }
138
139         rc = session->create(session, WT_INDEX_PID, "columns=(pid)");
140         if( rc ) {
141                 Debug( LDAP_DEBUG_ANY,
142                            LDAP_XSTRING(wt_db_open) ": database \"%s\": "
143                            "cannot create pid index: \"%s\"\n",
144                            be->be_suffix[0].bv_val, wiredtiger_strerror(rc), 0);
145                 return -1;
146         }
147
148         rc = session->create(session, WT_INDEX_REVDN, "columns=(revdn)");
149         if( rc ) {
150                 Debug( LDAP_DEBUG_ANY,
151                            LDAP_XSTRING(wt_db_open) ": database \"%s\": "
152                            "cannot create revdn index: \"%s\"\n",
153                            be->be_suffix[0].bv_val, wiredtiger_strerror(rc), 0);
154                 return -1;
155         }
156
157         rc = wt_last_id( be, session, &wi->wi_lastid);
158         if (rc) {
159                 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
160                                   "last_id() failed: %s(%d).",
161                                   be->be_suffix[0].bv_val, wiredtiger_strerror(rc), rc );
162         Debug( LDAP_DEBUG_ANY,
163                            LDAP_XSTRING(wt_db_open) ": %s\n",
164                            cr->msg, 0, 0 );
165                 return rc;
166         }
167
168         session->close(session, NULL);
169         wi->wi_conn = conn;
170         wi->wi_flags |= WT_IS_OPEN;
171
172     return LDAP_SUCCESS;
173 }
174
175 static int
176 wt_db_close( BackendDB *be, ConfigReply *cr )
177 {
178         struct wt_info *wi = (struct wt_info *) be->be_private;
179         int rc;
180
181         rc = wi->wi_conn->close(wi->wi_conn, NULL);
182         if( rc ) {
183                 Debug( LDAP_DEBUG_ANY,
184                            LDAP_XSTRING(wt_db_close)
185                            ": cannot close database (%d).\n",
186                            errno, 0, 0);
187                 return -1;
188         }
189
190         wi->wi_flags &= ~WT_IS_OPEN;
191
192     return LDAP_SUCCESS;
193 }
194
195 static int
196 wt_db_destroy( Backend *be, ConfigReply *cr )
197 {
198         struct wt_info *wi = (struct wt_info *) be->be_private;
199
200         if( wi->wi_dbenv_home ) {
201                 ch_free( wi->wi_dbenv_home );
202                 wi->wi_dbenv_home = NULL;
203         }
204         if( wi->wi_dbenv_config ) {
205                 ch_free( wi->wi_dbenv_config );
206                 wi->wi_dbenv_config = NULL;
207         }
208
209         wt_attr_index_destroy( wi );
210         ch_free( wi );
211         be->be_private = NULL;
212
213         return LDAP_SUCCESS;
214 }
215
216 int
217 wt_back_initialize( BackendInfo *bi )
218 {
219         static char *controls[] = {
220                 LDAP_CONTROL_ASSERT,
221                 LDAP_CONTROL_MANAGEDSAIT,
222                 LDAP_CONTROL_NOOP,
223                 LDAP_CONTROL_PAGEDRESULTS,
224                 LDAP_CONTROL_PRE_READ,
225                 LDAP_CONTROL_POST_READ,
226                 LDAP_CONTROL_SUBENTRIES,
227                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
228                 NULL
229         };
230
231         /* initialize the database system */
232         Debug( LDAP_DEBUG_TRACE,
233                    LDAP_XSTRING(wt_back_initialize)
234                    ": initialize WiredTiger backend\n",
235                    0, 0, 0 );
236
237         bi->bi_flags |=
238                 SLAP_BFLAG_INCREMENT |
239                 SLAP_BFLAG_SUBENTRIES |
240                 SLAP_BFLAG_ALIASES |
241                 SLAP_BFLAG_REFERRALS;
242
243         bi->bi_controls = controls;
244
245         { /* version check */
246                 Debug( LDAP_DEBUG_TRACE,
247                            LDAP_XSTRING(wt_back_initialize) ": %s\n",
248                            wiredtiger_version(NULL, NULL, NULL), 0, 0 );
249         }
250
251         bi->bi_open = 0;
252         bi->bi_close = 0;
253         bi->bi_config = 0;
254         bi->bi_destroy = 0;
255
256         bi->bi_db_init = wt_db_init;
257         bi->bi_db_config = config_generic_wrapper;
258         bi->bi_db_open = wt_db_open;
259         bi->bi_db_close = wt_db_close;
260         bi->bi_db_destroy = wt_db_destroy;
261
262         bi->bi_op_add = wt_add;
263         bi->bi_op_bind = wt_bind;
264         bi->bi_op_unbind = 0;
265         bi->bi_op_search = wt_search;
266         bi->bi_op_compare = wt_compare;
267         bi->bi_op_modify = wt_modify;
268         bi->bi_op_modrdn = 0;
269         bi->bi_op_delete = wt_delete;
270         bi->bi_op_abandon = 0;
271
272         bi->bi_extended = 0;
273
274         bi->bi_chk_referrals = 0;
275         bi->bi_operational = wt_operational;
276
277         bi->bi_entry_release_rw = wt_entry_release;
278         bi->bi_entry_get_rw = wt_entry_get;
279
280         bi->bi_tool_entry_open = wt_tool_entry_open;
281         bi->bi_tool_entry_close = wt_tool_entry_close;
282         bi->bi_tool_entry_first = backend_tool_entry_first;
283         bi->bi_tool_entry_first_x = wt_tool_entry_first_x;
284         bi->bi_tool_entry_next = wt_tool_entry_next;
285         bi->bi_tool_entry_get = wt_tool_entry_get;
286         bi->bi_tool_entry_put = wt_tool_entry_put;
287         bi->bi_tool_entry_reindex = wt_tool_entry_reindex;
288
289         bi->bi_connection_init = 0;
290         bi->bi_connection_destroy = 0;
291
292         return wt_back_init_cf( bi );
293 }
294
295 #if SLAPD_WT == SLAPD_MOD_DYNAMIC
296
297 /* conditionally define the init_module() function */
298 SLAP_BACKEND_INIT_MODULE( wt )
299
300 #endif /* SLAPD_WT == SLAPD_MOD_DYNAMIC */
301
302 /*
303  * Local variables:
304  * indent-tabs-mode: t
305  * tab-width: 4
306  * c-basic-offset: 4
307  * End:
308  */