]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ndb/tools.cpp
MySQL NDB Cluster backend (experimental)
[openldap] / servers / slapd / back-ndb / tools.cpp
1 /* tools.cpp - tools for slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2008 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 initially developed by Howard Chu for inclusion
18  * in OpenLDAP Software. This work was sponsored by MySQL.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ac/string.h>
25 #include <ac/errno.h>
26
27 #include "lutil.h"
28
29 #include "back-ndb.h"
30
31 typedef struct dn_id {
32         ID id;
33         struct berval dn;
34 } dn_id;
35
36 #define HOLE_SIZE       4096
37 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
38 static unsigned nhmax = HOLE_SIZE;
39 static unsigned nholes;
40 static Avlnode *myParents;
41
42 static Ndb *myNdb;
43 static NdbTransaction *myScanTxn;
44 static NdbIndexScanOperation *myScanOp;
45
46 static NdbRecAttr *myScanID, *myScanOC;
47 static NdbRecAttr *myScanDN[NDB_MAX_RDNS];
48 static char myDNbuf[2048];
49 static char myIdbuf[2*sizeof(ID)];
50 static char myOcbuf[NDB_OC_BUFLEN];
51 static NdbRdns myRdns;
52
53 static NdbTransaction *myPutTxn;
54 static int myPutCnt;
55
56 static struct berval *myOcList;
57 static struct berval myDn;
58
59 extern "C"
60 int ndb_tool_entry_open(
61         BackendDB *be, int mode )
62 {
63         struct ndb_info *ni = (struct ndb_info *) be->be_private;
64
65         myNdb = new Ndb( ni->ni_cluster[0], ni->ni_dbname );
66         return myNdb->init(1024);
67 }
68
69 extern "C"
70 int ndb_tool_entry_close(
71         BackendDB *be )
72 {
73         if ( myPutTxn ) {
74                 int rc = myPutTxn->execute(NdbTransaction::Commit);
75                 if( rc != 0 ) {
76                         char text[1024];
77                         snprintf( text, sizeof(text),
78                                         "txn_commit failed: %s (%d)",
79                                         myPutTxn->getNdbError().message, myPutTxn->getNdbError().code );
80                         Debug( LDAP_DEBUG_ANY,
81                                 "=> " LDAP_XSTRING(ndb_tool_entry_put) ": %s\n",
82                                 text, 0, 0 );
83                 }
84                 myPutTxn->close();
85                 myPutTxn = NULL;
86         }
87         myPutCnt = 0;
88
89         if( nholes ) {
90                 unsigned i;
91                 fprintf( stderr, "Error, entries missing!\n");
92                 for (i=0; i<nholes; i++) {
93                         fprintf(stderr, "  entry %ld: %s\n",
94                                 holes[i].id, holes[i].dn.bv_val);
95                 }
96                 return -1;
97         }
98
99         return 0;
100 }
101
102 extern "C"
103 ID ndb_tool_entry_next(
104         BackendDB *be )
105 {
106         struct ndb_info *ni = (struct ndb_info *) be->be_private;
107         char *ptr;
108         ID id;
109         int i;
110
111         assert( be != NULL );
112         assert( slapMode & SLAP_TOOL_MODE );
113
114         if ( myScanOp->nextResult() ) {
115                 myScanOp->close();
116                 myScanOp = NULL;
117                 myScanTxn->close();
118                 myScanTxn = NULL;
119                 return NOID;
120         }
121         id = myScanID->u_64_value();
122
123         if ( myOcList ) {
124                 ber_bvarray_free( myOcList );
125         }
126         myOcList = ndb_ref2oclist( myOcbuf );
127         for ( i=0; i<NDB_MAX_RDNS; i++ ) {
128                 if ( myScanDN[i]->isNULL() || !myRdns.nr_buf[i] )
129                         break;
130         }
131         myRdns.nr_num = i;
132         ptr = myDNbuf;
133         for ( --i; i>=0; i-- ) {
134                 char *buf;
135                 int len;
136                 buf = myRdns.nr_buf[i];
137                 len = *buf++;
138                 ptr = lutil_strncopy( ptr, buf, len );
139                 if ( i )
140                         *ptr++ = ',';
141         }
142         *ptr = '\0';
143         myDn.bv_val = myDNbuf;
144         myDn.bv_len = ptr - myDNbuf;
145
146         return id;
147 }
148
149 extern "C"
150 ID ndb_tool_entry_first(
151         BackendDB *be )
152 {
153         struct ndb_info *ni = (struct ndb_info *) be->be_private;
154         int i, rc;
155
156         myScanTxn = myNdb->startTransaction();
157         if ( !myScanTxn )
158                 return NOID;
159
160         myScanOp = myScanTxn->getNdbIndexScanOperation( "PRIMARY", DN2ID_TABLE );
161         if ( !myScanOp )
162                 return NOID;
163
164         if ( myScanOp->readTuples( NdbOperation::LM_CommittedRead, NdbScanOperation::SF_KeyInfo ))
165                 return NOID;
166
167         myScanID = myScanOp->getValue( EID_COLUMN, myIdbuf );
168         myScanOC = myScanOp->getValue( OCS_COLUMN, myOcbuf );
169         for ( i=0; i<NDB_MAX_RDNS; i++ ) {
170                 myScanDN[i] = myScanOp->getValue( i+RDN_COLUMN, myRdns.nr_buf[i] );
171         }
172         if ( myScanTxn->execute( NdbTransaction::NoCommit, NdbOperation::AbortOnError, 1 ))
173                 return NOID;
174
175         return ndb_tool_entry_next( be );
176 }
177
178 extern "C"
179 ID ndb_tool_dn2id_get(
180         Backend *be,
181         struct berval *dn
182 )
183 {
184         struct ndb_info *ni = (struct ndb_info *) be->be_private;
185         NdbArgs NA;
186         NdbRdns rdns;
187         Entry e;
188         char text[1024];
189         int rc;
190
191         if ( BER_BVISEMPTY(dn) )
192                 return 0;
193
194         NA.ndb = myNdb;
195         NA.txn = myNdb->startTransaction();
196         if ( !NA.txn ) {
197                 snprintf( text, sizeof(text),
198                         "startTransaction failed: %s (%d)",
199                         myNdb->getNdbError().message, myNdb->getNdbError().code );
200                 Debug( LDAP_DEBUG_ANY,
201                         "=> " LDAP_XSTRING(ndb_tool_dn2id_get) ": %s\n",
202                          text, 0, 0 );
203                 return NOID;
204         }
205         if ( myOcList ) {
206                 ber_bvarray_free( myOcList );
207                 myOcList = NULL;
208         }
209         NA.e = &e;
210         e.e_name = *dn;
211         NA.rdns = &rdns;
212         NA.ocs = NULL;
213         rc = ndb_entry_get_info( be, &NA, 0, NULL );
214         myOcList = NA.ocs;
215         NA.txn->close();
216         if ( rc )
217                 return NOID;
218         
219         myDn = *dn;
220
221         return e.e_id;
222 }
223
224 extern "C"
225 Entry* ndb_tool_entry_get( BackendDB *be, ID id )
226 {
227         NdbArgs NA;
228         int rc;
229         char text[1024];
230
231         assert( be != NULL );
232         assert( slapMode & SLAP_TOOL_MODE );
233
234         NA.txn = myNdb->startTransaction();
235         if ( !NA.txn ) {
236                 snprintf( text, sizeof(text),
237                         "start_transaction failed: %s (%d)",
238                         myNdb->getNdbError().message, myNdb->getNdbError().code );
239                 Debug( LDAP_DEBUG_ANY,
240                         "=> " LDAP_XSTRING(ndb_tool_entry_get) ": %s\n",
241                          text, 0, 0 );
242                 return NULL;
243         }
244
245         NA.e = entry_alloc();
246         NA.e->e_id = id;
247         ber_dupbv( &NA.e->e_name, &myDn );
248         dnNormalize( 0, NULL, NULL, &NA.e->e_name, &NA.e->e_nname, NULL );
249
250
251         NA.ndb = myNdb;
252         NA.ocs = myOcList;
253         rc = ndb_entry_get_data( be, &NA, 0 );
254
255         if ( rc ) {
256                 entry_free( NA.e );
257                 NA.e = NULL;
258         }
259         NA.txn->close();
260
261         return NA.e;
262 }
263
264 static struct berval glueval[] = {
265         BER_BVC("glue"),
266         BER_BVNULL
267 };
268
269 static int ndb_dnid_cmp( const void *v1, const void *v2 )
270 {
271         struct dn_id *dn1 = (struct dn_id *)v1,
272                 *dn2 = (struct dn_id *)v2;
273         return ber_bvcmp( &dn1->dn, &dn2->dn );
274 }
275
276 static int ndb_tool_next_id(
277         BackendDB *be,
278         NdbArgs *NA,
279         struct berval *text,
280         int hole )
281 {
282         struct berval ndn = NA->e->e_nname;
283         int rc;
284
285         if (ndn.bv_len == 0) {
286                 NA->e->e_id = 0;
287                 return 0;
288         }
289
290         rc = ndb_entry_get_info( be, NA, 0, NULL );
291         if ( rc ) {
292                 Attribute *a, tmp = {0};
293                 if ( !be_issuffix( be, &ndn ) ) {
294                         struct dn_id *dptr;
295                         struct berval npdn;
296                         dnParent( &ndn, &npdn );
297                         NA->e->e_nname = npdn;
298                         NA->rdns->nr_num--;
299                         rc = ndb_tool_next_id( be, NA, text, 1 );
300                         NA->e->e_nname = ndn;
301                         NA->rdns->nr_num++;
302                         if ( rc ) {
303                                 return rc;
304                         }
305                         /* If parent didn't exist, it was created just now
306                          * and its ID is now in e->e_id.
307                          */
308                         dptr = (struct dn_id *)ch_malloc( sizeof( struct dn_id ) + npdn.bv_len + 1);
309                         dptr->id = NA->e->e_id;
310                         dptr->dn.bv_val = (char *)(dptr+1);
311                         strcpy(dptr->dn.bv_val, npdn.bv_val );
312                         dptr->dn.bv_len = npdn.bv_len;
313                         if ( avl_insert( &myParents, dptr, ndb_dnid_cmp, avl_dup_error )) {
314                                 ch_free( dptr );
315                         }
316                 }
317                 rc = ndb_next_id( be, myNdb, &NA->e->e_id );
318                 if ( rc ) {
319                         snprintf( text->bv_val, text->bv_len,
320                                 "next_id failed: %s (%d)",
321                                 myNdb->getNdbError().message, myNdb->getNdbError().code );
322                         Debug( LDAP_DEBUG_ANY,
323                                 "=> ndb_tool_next_id: %s\n", text->bv_val, 0, 0 );
324                         return rc;
325                 }
326                 if ( hole ) {
327                         a = NA->e->e_attrs;
328                         NA->e->e_attrs = &tmp;
329                         tmp.a_desc = slap_schema.si_ad_objectClass;
330                         tmp.a_vals = glueval;
331                         tmp.a_nvals = tmp.a_vals;
332                         tmp.a_numvals = 1;
333                 }
334                 rc = ndb_entry_put_info( be, NA, 0 );
335                 if ( hole ) {
336                         NA->e->e_attrs = a;
337                 }
338                 if ( rc ) {
339                         snprintf( text->bv_val, text->bv_len, 
340                                 "ndb_entry_put_info failed: %s (%d)",
341                                 myNdb->getNdbError().message, myNdb->getNdbError().code );
342                 Debug( LDAP_DEBUG_ANY,
343                         "=> ndb_tool_next_id: %s\n", text->bv_val, 0, 0 );
344                 } else if ( hole ) {
345                         if ( nholes == nhmax - 1 ) {
346                                 if ( holes == hbuf ) {
347                                         holes = (dn_id *)ch_malloc( nhmax * sizeof(dn_id) * 2 );
348                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
349                                 } else {
350                                         holes = (dn_id *)ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
351                                 }
352                                 nhmax *= 2;
353                         }
354                         ber_dupbv( &holes[nholes].dn, &ndn );
355                         holes[nholes++].id = NA->e->e_id;
356                 }
357         } else if ( !hole ) {
358                 unsigned i;
359
360                 for ( i=0; i<nholes; i++) {
361                         if ( holes[i].id == NA->e->e_id ) {
362                                 int j;
363                                 free(holes[i].dn.bv_val);
364                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
365                                 holes[j].id = 0;
366                                 nholes--;
367                                 rc = ndb_entry_put_info( be, NA, 1 );
368                                 break;
369                         } else if ( holes[i].id > NA->e->e_id ) {
370                                 break;
371                         }
372                 }
373         }
374         return rc;
375 }
376
377 extern "C"
378 ID ndb_tool_entry_put(
379         BackendDB *be,
380         Entry *e,
381         struct berval *text )
382 {
383         struct ndb_info *ni = (struct ndb_info *) be->be_private;
384         struct dn_id dtmp, *dptr;
385         NdbArgs NA;
386         NdbRdns rdns;
387         int rc, slow = 0;
388
389         assert( be != NULL );
390         assert( slapMode & SLAP_TOOL_MODE );
391
392         assert( text != NULL );
393         assert( text->bv_val != NULL );
394         assert( text->bv_val[0] == '\0' );      /* overconservative? */
395
396         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(ndb_tool_entry_put)
397                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
398
399         if ( !be_issuffix( be, &e->e_nname )) {
400                 dnParent( &e->e_nname, &dtmp.dn );
401                 dptr = (struct dn_id *)avl_find( myParents, &dtmp, ndb_dnid_cmp );
402                 if ( !dptr )
403                         slow = 1;
404         }
405
406         rdns.nr_num = 0;
407
408         if ( !slow ) {
409                 rc = ndb_next_id( be, myNdb, &e->e_id );
410                 if ( rc ) {
411                         snprintf( text->bv_val, text->bv_len,
412                                 "next_id failed: %s (%d)",
413                                 myNdb->getNdbError().message, myNdb->getNdbError().code );
414                         Debug( LDAP_DEBUG_ANY,
415                                 "=> ndb_tool_next_id: %s\n", text->bv_val, 0, 0 );
416                         return rc;
417                 }
418         }
419
420         if ( !myPutTxn )
421                 myPutTxn = myNdb->startTransaction();
422         if ( !myPutTxn ) {
423                 snprintf( text->bv_val, text->bv_len,
424                         "start_transaction failed: %s (%d)",
425                         myNdb->getNdbError().message, myNdb->getNdbError().code );
426                 Debug( LDAP_DEBUG_ANY,
427                         "=> " LDAP_XSTRING(ndb_tool_entry_put) ": %s\n",
428                          text->bv_val, 0, 0 );
429                 return NOID;
430         }
431
432         /* add dn2id indices */
433         ndb_dn2rdns( &e->e_name, &rdns );
434         NA.rdns = &rdns;
435         NA.e = e;
436         NA.ndb = myNdb;
437         NA.txn = myPutTxn;
438         if ( slow ) {
439                 rc = ndb_tool_next_id( be, &NA, text, 0 );
440                 if( rc != 0 ) {
441                         goto done;
442                 }
443         } else {
444                 rc = ndb_entry_put_info( be, &NA, 0 );
445                 if ( rc != 0 ) {
446                         goto done;
447                 }
448         }
449
450         /* id2entry index */
451         rc = ndb_entry_put_data( be, &NA, 0 );
452         if( rc != 0 ) {
453                 snprintf( text->bv_val, text->bv_len,
454                                 "ndb_entry_put_data failed: %s (%d)",
455                                 myNdb->getNdbError().message, myNdb->getNdbError().code );
456                 Debug( LDAP_DEBUG_ANY,
457                         "=> " LDAP_XSTRING(ndb_tool_entry_put) ": %s\n",
458                         text->bv_val, 0, 0 );
459                 goto done;
460         }
461
462 done:
463         if( rc == 0 ) {
464                 myPutCnt++;
465                 if ( !( myPutCnt & 0x0f )) {
466                         rc = myPutTxn->execute(NdbTransaction::Commit);
467                         if( rc != 0 ) {
468                                 snprintf( text->bv_val, text->bv_len,
469                                         "txn_commit failed: %s (%d)",
470                                         myPutTxn->getNdbError().message, myPutTxn->getNdbError().code );
471                                 Debug( LDAP_DEBUG_ANY,
472                                         "=> " LDAP_XSTRING(ndb_tool_entry_put) ": %s\n",
473                                         text->bv_val, 0, 0 );
474                                 e->e_id = NOID;
475                         }
476                         myPutTxn->close();
477                         myPutTxn = NULL;
478                 }
479         } else {
480                 snprintf( text->bv_val, text->bv_len,
481                         "txn_aborted! %s (%d)",
482                         myPutTxn->getNdbError().message, myPutTxn->getNdbError().code );
483                 Debug( LDAP_DEBUG_ANY,
484                         "=> " LDAP_XSTRING(ndb_tool_entry_put) ": %s\n",
485                         text->bv_val, 0, 0 );
486                 e->e_id = NOID;
487                 myPutTxn->close();
488         }
489
490         return e->e_id;
491 }
492
493 extern "C"
494 int ndb_tool_entry_reindex(
495         BackendDB *be,
496         ID id,
497         AttributeDescription **adv )
498 {
499         struct ndb_info *ni = (struct ndb_info *) be->be_private;
500
501         Debug( LDAP_DEBUG_ARGS,
502                 "=> " LDAP_XSTRING(ndb_tool_entry_reindex) "( %ld )\n",
503                 (long) id, 0, 0 );
504
505         return 0;
506 }
507
508 extern "C"
509 ID ndb_tool_entry_modify(
510         BackendDB *be,
511         Entry *e,
512         struct berval *text )
513 {
514         struct ndb_info *ni = (struct ndb_info *) be->be_private;
515         int rc;
516
517         Debug( LDAP_DEBUG_TRACE,
518                 "=> " LDAP_XSTRING(ndb_tool_entry_modify) "( %ld, \"%s\" )\n",
519                 (long) e->e_id, e->e_dn, 0 );
520
521 done:
522         return e->e_id;
523 }
524