1 /* init.cpp - initialize ndb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This work was initially developed by Howard Chu for inclusion
18 * in OpenLDAP Software. This work was sponsored by MySQL.
24 #include <ac/string.h>
25 #include <ac/unistd.h>
26 #include <ac/stdlib.h>
34 static BI_db_init ndb_db_init;
35 static BI_db_close ndb_db_close;
36 static BI_db_open ndb_db_open;
37 static BI_db_destroy ndb_db_destroy;
40 static struct berval ndb_optable = BER_BVC("OL_opattrs");
42 static struct berval ndb_opattrs[] = {
43 BER_BVC("structuralObjectClass"),
45 BER_BVC("creatorsName"),
46 BER_BVC("createTimestamp"),
48 BER_BVC("modifiersName"),
49 BER_BVC("modifyTimestamp"),
53 static int ndb_oplens[] = {
54 0, /* structuralOC, default */
56 0, /* creatorsName, default */
57 26, /* createTimestamp */
59 0, /* modifiersName, default */
60 26, /* modifyTimestamp */
65 ndb_db_init( BackendDB *be, ConfigReply *cr )
70 Debug( LDAP_DEBUG_TRACE,
71 LDAP_XSTRING(ndb_db_init) ": Initializing ndb database\n",
74 /* allocate backend-database-specific stuff */
75 ni = (struct ndb_info *) ch_calloc( 1, sizeof(struct ndb_info) );
78 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
80 ni->ni_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
82 ldap_pvt_thread_rdwr_init( &ni->ni_ai_rwlock );
83 ldap_pvt_thread_rdwr_init( &ni->ni_oc_rwlock );
84 ldap_pvt_thread_mutex_init( &ni->ni_conn_mutex );
87 rc = ndb_monitor_db_init( be );
94 ndb_db_close( BackendDB *be, ConfigReply *cr );
97 ndb_db_open( BackendDB *be, ConfigReply *cr )
99 struct ndb_info *ni = (struct ndb_info *) be->be_private;
100 char sqlbuf[BUFSIZ], *ptr;
103 if ( be->be_suffix == NULL ) {
104 snprintf( cr->msg, sizeof( cr->msg ),
105 "ndb_db_open: need suffix" );
106 Debug( LDAP_DEBUG_ANY, "%s\n",
111 Debug( LDAP_DEBUG_ARGS,
112 LDAP_XSTRING(ndb_db_open) ": \"%s\"\n",
113 be->be_suffix[0].bv_val, 0, 0 );
115 if ( ni->ni_nconns < 1 )
118 ni->ni_cluster = (Ndb_cluster_connection **)ch_malloc( ni->ni_nconns * sizeof( Ndb_cluster_connection *));
119 for ( i=0; i<ni->ni_nconns; i++ ) {
120 ni->ni_cluster[i] = new Ndb_cluster_connection( ni->ni_connectstr );
121 rc = ni->ni_cluster[i]->connect( 4, 5, 1 );
123 for ( i=0; i<ni->ni_nconns; i++ ) {
124 rc = ni->ni_cluster[i]->wait_until_ready( 30, 0 );
127 mysql_init( &ni->ni_sql );
128 if ( !mysql_real_connect( &ni->ni_sql, ni->ni_hostname, ni->ni_username, ni->ni_password,
129 "", ni->ni_port, ni->ni_socket, ni->ni_clflag )) {
130 snprintf( cr->msg, sizeof( cr->msg ),
131 "ndb_db_open: mysql_real_connect failed, %s (%d)",
132 mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
137 sprintf( sqlbuf, "CREATE DATABASE IF NOT EXISTS %s", ni->ni_dbname );
138 rc = mysql_query( &ni->ni_sql, sqlbuf );
140 snprintf( cr->msg, sizeof( cr->msg ),
141 "ndb_db_open: CREATE DATABASE %s failed, %s (%d)",
142 ni->ni_dbname, mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
146 sprintf( sqlbuf, "USE %s", ni->ni_dbname );
147 rc = mysql_query( &ni->ni_sql, sqlbuf );
149 snprintf( cr->msg, sizeof( cr->msg ),
150 "ndb_db_open: USE DATABASE %s failed, %s (%d)",
151 ni->ni_dbname, mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
156 ptr += sprintf( ptr, "CREATE TABLE IF NOT EXISTS " DN2ID_TABLE " ("
157 "eid bigint unsigned NOT NULL, "
158 "object_classes VARCHAR(1024) NOT NULL, "
159 "a0 VARCHAR(128) NOT NULL DEFAULT '', "
160 "a1 VARCHAR(128) NOT NULL DEFAULT '', "
161 "a2 VARCHAR(128) NOT NULL DEFAULT '', "
162 "a3 VARCHAR(128) NOT NULL DEFAULT '', "
163 "a4 VARCHAR(128) NOT NULL DEFAULT '', "
164 "a5 VARCHAR(128) NOT NULL DEFAULT '', "
165 "a6 VARCHAR(128) NOT NULL DEFAULT '', "
166 "a7 VARCHAR(128) NOT NULL DEFAULT '', "
167 "a8 VARCHAR(128) NOT NULL DEFAULT '', "
168 "a9 VARCHAR(128) NOT NULL DEFAULT '', "
169 "a10 VARCHAR(128) NOT NULL DEFAULT '', "
170 "a11 VARCHAR(128) NOT NULL DEFAULT '', "
171 "a12 VARCHAR(128) NOT NULL DEFAULT '', "
172 "a13 VARCHAR(128) NOT NULL DEFAULT '', "
173 "a14 VARCHAR(128) NOT NULL DEFAULT '', "
174 "a15 VARCHAR(128) NOT NULL DEFAULT '', "
175 "PRIMARY KEY (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15), "
176 "UNIQUE KEY eid (eid) USING HASH" );
177 /* Create index columns */
178 if ( ni->ni_attridxs ) {
184 for ( ln = ni->ni_attridxs; ln; ln=ln->ln_next ) {
185 NdbAttrInfo *ai = (NdbAttrInfo *)ln->ln_data;
186 ptr += sprintf( ptr, "`%s` VARCHAR(%d), ",
187 ai->na_name.bv_val, ai->na_len );
189 ptr = lutil_strcopy(ptr, "KEY " INDEX_NAME " (" );
191 for ( ln = ni->ni_attridxs; ln; ln=ln->ln_next ) {
192 NdbAttrInfo *ai = (NdbAttrInfo *)ln->ln_data;
193 if ( newcol ) *ptr++ = ',';
195 ptr = lutil_strcopy( ptr, ai->na_name.bv_val );
197 ai->na_ixcol = newcol + 18;
202 strcpy( ptr, ") ENGINE=ndb" );
203 rc = mysql_query( &ni->ni_sql, sqlbuf );
205 snprintf( cr->msg, sizeof( cr->msg ),
206 "ndb_db_open: CREATE TABLE " DN2ID_TABLE " failed, %s (%d)",
207 mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
211 rc = mysql_query( &ni->ni_sql, "CREATE TABLE IF NOT EXISTS " NEXTID_TABLE " ("
212 "a bigint unsigned AUTO_INCREMENT PRIMARY KEY ) ENGINE=ndb" );
214 snprintf( cr->msg, sizeof( cr->msg ),
215 "ndb_db_open: CREATE TABLE " NEXTID_TABLE " failed, %s (%d)",
216 mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
223 rc = ndb_aset_get( ni, &ndb_optable, ndb_opattrs, &oci );
225 snprintf( cr->msg, sizeof( cr->msg ),
226 "ndb_db_open: ndb_aset_get( %s ) failed (%d)",
227 ndb_optable.bv_val, rc );
230 for ( i=0; ndb_oplens[i] >= 0; i++ ) {
232 oci->no_attrs[i]->na_len = ndb_oplens[i];
234 rc = ndb_aset_create( ni, oci );
236 snprintf( cr->msg, sizeof( cr->msg ),
237 "ndb_db_open: ndb_aset_create( %s ) failed (%d)",
238 ndb_optable.bv_val, rc );
241 ni->ni_opattrs = oci;
243 /* Create attribute sets */
247 for ( ln = ni->ni_attrsets; ln; ln=ln->ln_next ) {
248 NdbOcInfo *oci = (NdbOcInfo *)ln->ln_data;
249 rc = ndb_aset_create( ni, oci );
251 snprintf( cr->msg, sizeof( cr->msg ),
252 "ndb_db_open: ndb_aset_create( %s ) failed (%d)",
253 oci->no_name.bv_val, rc );
258 /* Initialize any currently used objectClasses */
261 const NdbDictionary::Dictionary *myDict;
263 ndb = new Ndb( ni->ni_cluster[0], ni->ni_dbname );
266 myDict = ndb->getDictionary();
267 ndb_oc_read( ni, myDict );
273 rc = ndb_monitor_db_open( be );
282 Debug( LDAP_DEBUG_ANY, "%s\n",
284 ndb_db_close( be, NULL );
289 ndb_db_close( BackendDB *be, ConfigReply *cr )
292 struct ndb_info *ni = (struct ndb_info *) be->be_private;
294 mysql_close( &ni->ni_sql );
295 for ( i=0; i<ni->ni_nconns; i++ ) {
296 delete ni->ni_cluster[i];
297 ni->ni_cluster[i] = NULL;
299 ch_free( ni->ni_cluster );
300 ni->ni_cluster = NULL;
303 /* monitor handling */
304 (void)ndb_monitor_db_close( be );
311 ndb_db_destroy( BackendDB *be, ConfigReply *cr )
313 struct ndb_info *ni = (struct ndb_info *) be->be_private;
316 /* monitor handling */
317 (void)ndb_monitor_db_destroy( be );
320 ldap_pvt_thread_mutex_destroy( &ni->ni_conn_mutex );
321 ldap_pvt_thread_rdwr_destroy( &ni->ni_ai_rwlock );
322 ldap_pvt_thread_rdwr_destroy( &ni->ni_oc_rwlock );
325 be->be_private = NULL;
336 /* initialize the underlying database system */
337 Debug( LDAP_DEBUG_TRACE,
338 LDAP_XSTRING(ndb_back_initialize) ": initialize ndb backend\n", 0, 0, 0 );
347 bi->bi_db_init = ndb_db_init;
348 bi->bi_db_config = config_generic_wrapper;
349 bi->bi_db_open = ndb_db_open;
350 bi->bi_db_close = ndb_db_close;
351 bi->bi_db_destroy = ndb_db_destroy;
353 bi->bi_op_add = ndb_back_add;
354 bi->bi_op_bind = ndb_back_bind;
355 bi->bi_op_compare = ndb_back_compare;
356 bi->bi_op_delete = ndb_back_delete;
357 bi->bi_op_modify = ndb_back_modify;
358 bi->bi_op_modrdn = ndb_back_modrdn;
359 bi->bi_op_search = ndb_back_search;
361 bi->bi_op_unbind = 0;
364 bi->bi_extended = ndb_extended;
366 bi->bi_chk_referrals = ndb_referrals;
368 bi->bi_operational = ndb_operational;
369 bi->bi_has_subordinates = ndb_has_subordinates;
370 bi->bi_entry_release_rw = 0;
371 bi->bi_entry_get_rw = ndb_entry_get;
374 * hooks for slap tools
376 bi->bi_tool_entry_open = ndb_tool_entry_open;
377 bi->bi_tool_entry_close = ndb_tool_entry_close;
378 bi->bi_tool_entry_first = ndb_tool_entry_first;
379 bi->bi_tool_entry_next = ndb_tool_entry_next;
380 bi->bi_tool_entry_get = ndb_tool_entry_get;
381 bi->bi_tool_entry_put = ndb_tool_entry_put;
383 bi->bi_tool_entry_reindex = ndb_tool_entry_reindex;
384 bi->bi_tool_sync = 0;
385 bi->bi_tool_dn2id_get = ndb_tool_dn2id_get;
386 bi->bi_tool_entry_modify = ndb_tool_entry_modify;
389 bi->bi_connection_init = 0;
390 bi->bi_connection_destroy = 0;
392 rc = ndb_back_init_cf( bi );
397 #if SLAPD_NDB == SLAPD_MOD_DYNAMIC
399 /* conditionally define the init_module() function */
400 SLAP_BACKEND_INIT_MODULE( ndb )
402 #endif /* SLAPD_NDB == SLAPD_MOD_DYNAMIC */