1 /* ndbio.cpp - get/set/del data for NDB */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2010 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>
30 /* For reference only */
31 typedef struct MedVar {
32 Int16 len; /* length is always little-endian */
37 static int ndb_name_cmp( const void *v1, const void *v2 );
38 static int ndb_oc_dup_err( void *v1, void *v2 );
42 ndb_name_cmp( const void *v1, const void *v2 )
44 NdbOcInfo *oc1 = (NdbOcInfo *)v1, *oc2 = (NdbOcInfo *)v2;
45 return ber_bvstrcasecmp( &oc1->no_name, &oc2->no_name );
49 ndb_oc_dup_err( void *v1, void *v2 )
51 NdbOcInfo *oc = (NdbOcInfo *)v2;
53 oc->no_oc = (ObjectClass *)v1;
57 /* Find an existing NdbAttrInfo */
58 extern "C" NdbAttrInfo *
59 ndb_ai_find( struct ndb_info *ni, AttributeType *at )
62 atmp.na_name = at->sat_cname;
64 return (NdbAttrInfo *)avl_find( ni->ni_ai_tree, &atmp, ndb_name_cmp );
67 /* Find or create an NdbAttrInfo */
68 extern "C" NdbAttrInfo *
69 ndb_ai_get( struct ndb_info *ni, struct berval *aname )
71 NdbAttrInfo atmp, *ai;
72 atmp.na_name = *aname;
74 ai = (NdbAttrInfo *)avl_find( ni->ni_ai_tree, &atmp, ndb_name_cmp );
77 AttributeDescription *ad = NULL;
79 if ( slap_bv2ad( aname, &ad, &text ))
82 ai = (NdbAttrInfo *)ch_malloc( sizeof( NdbAttrInfo ));
84 ai->na_attr = ai->na_desc->ad_type;
85 ai->na_name = ai->na_attr->sat_cname;
89 ai->na_len = ai->na_attr->sat_atype.at_syntax_len;
90 /* Reasonable default */
92 if ( ai->na_attr->sat_syntax == slap_schema.si_syn_distinguishedName )
98 if ( ai->na_len > 1024 )
100 avl_insert( &ni->ni_ai_tree, ai, ndb_name_cmp, avl_dup_error );
106 ndb_ai_check( struct ndb_info *ni, NdbOcInfo *oci, AttributeType **attrs, char **ptr, int *col,
112 for ( i=0; attrs[i]; i++ ) {
113 if ( attrs[i] == slap_schema.si_ad_objectClass->ad_type )
115 /* skip attrs that are in a superior */
116 if ( oci->no_oc && oci->no_oc->soc_sups ) {
119 for ( j=0; oci->no_oc->soc_sups[j]; j++ ) {
120 oc = oci->no_oc->soc_sups[j];
121 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT )
123 if ( oc->soc_required ) {
124 for ( k=0; oc->soc_required[k]; k++ ) {
125 if ( attrs[i] == oc->soc_required[k] ) {
132 if ( oc->soc_allowed ) {
133 for ( k=0; oc->soc_allowed[k]; k++ ) {
134 if ( attrs[i] == oc->soc_allowed[k] ) {
146 ai = ndb_ai_get( ni, &attrs[i]->sat_cname );
148 /* can never happen */
152 /* An attrset may have already been connected */
153 if (( oci->no_flag & NDB_INFO_ATSET ) && ai->na_oi == oci )
156 /* An indexed attr is defined before its OC is */
159 ai->na_column = (*col)++;
162 oci->no_attrs[oci->no_nattrs++] = ai;
164 /* An attrset attr may already be defined */
165 if ( ai->na_oi != oci ) {
167 for ( j=0; j<oci->no_nsets; j++ )
168 if ( oci->no_sets[j] == ai->na_oi ) break;
169 if ( j >= oci->no_nsets ) {
170 /* FIXME: data loss if more sets are in use */
171 if ( oci->no_nsets < NDB_MAX_OCSETS ) {
172 oci->no_sets[oci->no_nsets++] = ai->na_oi;
179 if ( ai->na_flag & NDB_INFO_ATBLOB ) {
180 *ptr += sprintf( *ptr, ", `%s` BLOB", ai->na_attr->sat_cname.bv_val );
182 *ptr += sprintf( *ptr, ", `%s` VARCHAR(%d)", ai->na_attr->sat_cname.bv_val,
191 ndb_oc_create( struct ndb_info *ni, NdbOcInfo *oci, int create )
193 char buf[4096], *ptr;
197 ptr = buf + sprintf( buf,
198 "CREATE TABLE `%s` (eid bigint unsigned NOT NULL, vid int unsigned NOT NULL",
199 oci->no_table.bv_val );
203 if ( oci->no_oc->soc_required ) {
204 for ( i=0; oci->no_oc->soc_required[i]; i++ );
207 if ( oci->no_oc->soc_allowed ) {
208 for ( i=0; oci->no_oc->soc_allowed[i]; i++ );
211 /* assume all are present */
212 oci->no_attrs = (struct ndb_attrinfo **)ch_malloc( col * sizeof(struct ndb_attrinfo *));
215 ldap_pvt_thread_rdwr_wlock( &ni->ni_ai_rwlock );
216 if ( oci->no_oc->soc_required ) {
217 rc = ndb_ai_check( ni, oci, oci->no_oc->soc_required, &ptr, &col, create );
219 if ( !rc && oci->no_oc->soc_allowed ) {
220 rc = ndb_ai_check( ni, oci, oci->no_oc->soc_allowed, &ptr, &col, create );
222 ldap_pvt_thread_rdwr_wunlock( &ni->ni_ai_rwlock );
224 /* shrink down to just the needed size */
225 oci->no_attrs = (struct ndb_attrinfo **)ch_realloc( oci->no_attrs,
226 oci->no_nattrs * sizeof(struct ndb_attrinfo *));
229 ptr = lutil_strcopy( ptr, ", PRIMARY KEY(eid, vid) ) ENGINE=ndb PARTITION BY KEY(eid)" );
230 rc = mysql_real_query( &ni->ni_sql, buf, ptr - buf );
232 Debug( LDAP_DEBUG_ANY,
233 "ndb_oc_create: CREATE TABLE %s failed, %s (%d)\n",
234 oci->no_table.bv_val, mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
240 /* Read table definitions from the DB and populate ObjectClassInfo */
242 ndb_oc_read( struct ndb_info *ni, const NdbDictionary::Dictionary *myDict )
244 const NdbDictionary::Table *myTable;
245 const NdbDictionary::Column *myCol;
246 NdbOcInfo *oci, octmp;
249 NdbDictionary::Dictionary::List myList;
253 rc = myDict->listObjects( myList, NdbDictionary::Object::UserTable );
254 /* Populate our objectClass structures */
255 for ( i=0; i<myList.count; i++ ) {
256 /* Ignore other DBs */
257 if ( strcmp( myList.elements[i].database, ni->ni_dbname ))
259 /* Ignore internal tables */
260 if ( !strncmp( myList.elements[i].name, "OL_", 3 ))
262 ber_str2bv( myList.elements[i].name, 0, 0, &octmp.no_name );
263 oci = (NdbOcInfo *)avl_find( ni->ni_oc_tree, &octmp, ndb_name_cmp );
267 oc = oc_bvfind( &octmp.no_name );
269 /* undefined - shouldn't happen */
272 myTable = myDict->getTable( myList.elements[i].name );
273 oci = (NdbOcInfo *)ch_malloc( sizeof( NdbOcInfo )+oc->soc_cname.bv_len+1 );
274 oci->no_table.bv_val = (char *)(oci+1);
275 strcpy( oci->no_table.bv_val, oc->soc_cname.bv_val );
276 oci->no_table.bv_len = oc->soc_cname.bv_len;
277 oci->no_name = oci->no_table;
283 /* Make space for all attrs, even tho sups will be dropped */
284 if ( oci->no_oc->soc_required ) {
285 for ( j=0; oci->no_oc->soc_required[j]; j++ );
288 if ( oci->no_oc->soc_allowed ) {
289 for ( j=0; oci->no_oc->soc_allowed[j]; j++ );
292 oci->no_attrs = (struct ndb_attrinfo **)ch_malloc( col * sizeof(struct ndb_attrinfo *));
293 avl_insert( &ni->ni_oc_tree, oci, ndb_name_cmp, avl_dup_error );
295 col = myTable->getNoOfColumns();
296 /* Skip 0 and 1, eid and vid */
297 for ( j = 2; j<col; j++ ) {
298 myCol = myTable->getColumn( j );
299 ber_str2bv( myCol->getName(), 0, 0, &bv );
300 ai = ndb_ai_get( ni, &bv );
301 /* shouldn't happen */
306 ai->na_len = myCol->getLength();
307 if ( myCol->getType() == NdbDictionary::Column::Blob )
308 ai->na_flag |= NDB_INFO_ATBLOB;
311 /* Link to any attrsets */
312 for ( i=0; i<myList.count; i++ ) {
313 /* Ignore other DBs */
314 if ( strcmp( myList.elements[i].database, ni->ni_dbname ))
316 /* Ignore internal tables */
317 if ( !strncmp( myList.elements[i].name, "OL_", 3 ))
319 ber_str2bv( myList.elements[i].name, 0, 0, &octmp.no_name );
320 oci = (NdbOcInfo *)avl_find( ni->ni_oc_tree, &octmp, ndb_name_cmp );
321 /* shouldn't happen */
325 if ( oci->no_oc->soc_required ) {
326 rc = ndb_ai_check( ni, oci, oci->no_oc->soc_required, NULL, &col, 0 );
328 if ( oci->no_oc->soc_allowed ) {
329 rc = ndb_ai_check( ni, oci, oci->no_oc->soc_allowed, NULL, &col, 0 );
331 /* shrink down to just the needed size */
332 oci->no_attrs = (struct ndb_attrinfo **)ch_realloc( oci->no_attrs,
333 oci->no_nattrs * sizeof(struct ndb_attrinfo *));
339 ndb_oc_list( struct ndb_info *ni, const NdbDictionary::Dictionary *myDict,
340 struct berval *oname, int implied, NdbOcs *out )
342 const NdbDictionary::Table *myTable;
343 NdbOcInfo *oci, octmp;
348 if ( ber_bvstrcasecmp( oname, &slap_schema.si_oc_top->soc_cname )) {
349 octmp.no_name = *oname;
350 oci = (NdbOcInfo *)avl_find( ni->ni_oc_tree, &octmp, ndb_name_cmp );
354 oc = oc_bvfind( oname );
356 /* undefined - shouldn't happen */
357 return LDAP_INVALID_SYNTAX;
360 if ( oc->soc_sups ) {
363 for ( i=0; oc->soc_sups[i]; i++ ) {
364 rc = ndb_oc_list( ni, myDict, &oc->soc_sups[i]->soc_cname, 1, out );
369 oc = slap_schema.si_oc_top;
371 /* Only insert once */
372 for ( i=0; i<out->no_ntext; i++ )
373 if ( out->no_text[i].bv_val == oc->soc_cname.bv_val )
375 if ( i == out->no_ntext ) {
376 for ( i=0; i<out->no_nitext; i++ )
377 if ( out->no_itext[i].bv_val == oc->soc_cname.bv_val )
379 if ( i == out->no_nitext ) {
381 out->no_itext[out->no_nitext++] = oc->soc_cname;
383 out->no_text[out->no_ntext++] = oc->soc_cname;
387 /* ignore top, etc... */
388 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT )
392 ldap_pvt_thread_rdwr_runlock( &ni->ni_oc_rwlock );
393 oci = (NdbOcInfo *)ch_malloc( sizeof( NdbOcInfo )+oc->soc_cname.bv_len+1 );
394 oci->no_table.bv_val = (char *)(oci+1);
395 strcpy( oci->no_table.bv_val, oc->soc_cname.bv_val );
396 oci->no_table.bv_len = oc->soc_cname.bv_len;
397 oci->no_name = oci->no_table;
402 ldap_pvt_thread_rdwr_wlock( &ni->ni_oc_rwlock );
403 if ( avl_insert( &ni->ni_oc_tree, oci, ndb_name_cmp, ndb_oc_dup_err )) {
404 octmp.no_oc = oci->no_oc;
406 oci = (NdbOcInfo *)octmp.no_oc;
408 /* see if the oc table already exists in the DB */
409 myTable = myDict->getTable( oci->no_table.bv_val );
410 rc = ndb_oc_create( ni, oci, myTable == NULL );
411 ldap_pvt_thread_rdwr_wunlock( &ni->ni_oc_rwlock );
412 ldap_pvt_thread_rdwr_rlock( &ni->ni_oc_rwlock );
415 /* Only insert once */
416 for ( i=0; i<out->no_ninfo; i++ )
417 if ( out->no_info[i] == oci )
419 if ( i == out->no_ninfo )
420 out->no_info[out->no_ninfo++] = oci;
425 ndb_aset_get( struct ndb_info *ni, struct berval *sname, struct berval *attrs, NdbOcInfo **ret )
427 NdbOcInfo *oci, octmp;
430 octmp.no_name = *sname;
431 oci = (NdbOcInfo *)avl_find( ni->ni_oc_tree, &octmp, ndb_name_cmp );
433 return LDAP_ALREADY_EXISTS;
435 for ( i=0; !BER_BVISNULL( &attrs[i] ); i++ ) {
436 if ( !at_bvfind( &attrs[i] ))
437 return LDAP_NO_SUCH_ATTRIBUTE;
441 oci = (NdbOcInfo *)ch_calloc( 1, sizeof( NdbOcInfo ) + sizeof( ObjectClass ) +
442 i*sizeof(AttributeType *) + sname->bv_len+1 );
443 oci->no_oc = (ObjectClass *)(oci+1);
444 oci->no_oc->soc_required = (AttributeType **)(oci->no_oc+1);
445 oci->no_table.bv_val = (char *)(oci->no_oc->soc_required+i);
447 for ( i=0; !BER_BVISNULL( &attrs[i] ); i++ )
448 oci->no_oc->soc_required[i] = at_bvfind( &attrs[i] );
450 strcpy( oci->no_table.bv_val, sname->bv_val );
451 oci->no_table.bv_len = sname->bv_len;
452 oci->no_name = oci->no_table;
453 oci->no_oc->soc_cname = oci->no_name;
454 oci->no_flag = NDB_INFO_ATSET;
456 if ( !ber_bvcmp( sname, &slap_schema.si_oc_extensibleObject->soc_cname ))
457 oci->no_oc->soc_kind = slap_schema.si_oc_extensibleObject->soc_kind;
459 rc = ndb_oc_create( ni, oci, 0 );
461 rc = avl_insert( &ni->ni_oc_tree, oci, ndb_name_cmp, avl_dup_error );
471 ndb_aset_create( struct ndb_info *ni, NdbOcInfo *oci )
473 char buf[4096], *ptr;
477 ptr = buf + sprintf( buf,
478 "CREATE TABLE IF NOT EXISTS `%s` (eid bigint unsigned NOT NULL, vid int unsigned NOT NULL",
479 oci->no_table.bv_val );
481 for ( i=0; i<oci->no_nattrs; i++ ) {
482 if ( oci->no_attrs[i]->na_oi != oci )
484 ai = oci->no_attrs[i];
485 ptr += sprintf( ptr, ", `%s` VARCHAR(%d)", ai->na_attr->sat_cname.bv_val,
487 if ( ai->na_flag & NDB_INFO_INDEX ) {
488 ptr += sprintf( ptr, ", INDEX (`%s`)", ai->na_attr->sat_cname.bv_val );
491 ptr = lutil_strcopy( ptr, ", PRIMARY KEY(eid, vid) ) ENGINE=ndb PARTITION BY KEY(eid)" );
492 i = mysql_real_query( &ni->ni_sql, buf, ptr - buf );
494 Debug( LDAP_DEBUG_ANY,
495 "ndb_aset_create: CREATE TABLE %s failed, %s (%d)\n",
496 oci->no_table.bv_val, mysql_error(&ni->ni_sql), mysql_errno(&ni->ni_sql) );
502 ndb_oc_check( BackendDB *be, Ndb *ndb,
503 struct berval *ocsin, NdbOcs *out )
505 struct ndb_info *ni = (struct ndb_info *) be->be_private;
506 const NdbDictionary::Dictionary *myDict = ndb->getDictionary();
514 /* Find all objectclasses and their superiors. List
515 * the superiors first.
518 ldap_pvt_thread_rdwr_rlock( &ni->ni_oc_rwlock );
519 for ( i=0; !BER_BVISNULL( &ocsin[i] ); i++ ) {
520 rc = ndb_oc_list( ni, myDict, &ocsin[i], 0, out );
523 ldap_pvt_thread_rdwr_runlock( &ni->ni_oc_rwlock );
531 static int ndb_flush_blobs;
533 /* set all the unique attrs of this objectclass into the table
538 const NdbDictionary::Table *myTable,
546 char buf[65538], *ptr;
547 Attribute **an, **ao, *a;
551 Uint64 eid = e->e_id;
556 an = (Attribute **)ch_malloc( 2 * nattrs * sizeof(Attribute *));
559 /* Turn lists of attrs into arrays for easier access */
560 for ( i=0; i<nattrs; i++ ) {
561 if ( attrs[i]->na_oi != no ) {
566 for ( a=e->e_attrs; a; a=a->a_next ) {
567 if ( a->a_desc == slap_schema.si_ad_objectClass )
569 if ( a->a_desc->ad_type == attrs[i]->na_attr ) {
570 /* Don't process same attr twice */
571 if ( a->a_flags & SLAP_ATTR_IXADD )
574 a->a_flags |= SLAP_ATTR_IXADD;
579 if ( a && a->a_numvals > max )
581 for ( a=old; a; a=a->a_next ) {
582 if ( a->a_desc == slap_schema.si_ad_objectClass )
584 if ( a->a_desc->ad_type == attrs[i]->na_attr )
588 if ( a && a->a_numvals > max )
592 for ( i=0; i<max; i++ ) {
594 for ( j=0; j<nattrs; j++ ) {
595 if ( !an[j] && !ao[j] )
598 if ( an[j] && an[j]->a_numvals > i ) {
599 /* both old and new are present, compare for changes */
600 if ( ao[j] && ao[j]->a_numvals > i ) {
601 if ( ber_bvcmp( &ao[j]->a_nvals[i], &an[j]->a_nvals[i] ))
607 if ( ao[j] && ao[j]->a_numvals > i )
613 myop = txn->getNdbOperation( myTable );
618 if ( myop->writeTuple()) {
622 if ( myop->insertTuple()) {
626 if ( myop->equal( EID_COLUMN, eid )) {
629 if ( myop->equal( VID_COLUMN, i )) {
633 if ( attrs[j]->na_flag & NDB_INFO_ATBLOB ) {
634 NdbBlob *myBlob = myop->getBlobHandle( attrs[j]->na_column );
637 Debug( LDAP_DEBUG_TRACE, "ndb_oc_attrs: getBlobHandle failed %s (%d)\n",
638 myop->getNdbError().message, myop->getNdbError().code, 0 );
641 if ( slapMode & SLAP_TOOL_MODE )
643 if ( changed & V_INS ) {
644 if ( myBlob->setValue( an[j]->a_vals[i].bv_val, an[j]->a_vals[i].bv_len )) {
645 Debug( LDAP_DEBUG_TRACE, "ndb_oc_attrs: blob->setValue failed %s (%d)\n",
646 myBlob->getNdbError().message, myBlob->getNdbError().code, 0 );
650 if ( myBlob->setValue( NULL, 0 )) {
651 Debug( LDAP_DEBUG_TRACE, "ndb_oc_attrs: blob->setValue failed %s (%d)\n",
652 myBlob->getNdbError().message, myBlob->getNdbError().code, 0 );
657 if ( changed & V_INS ) {
658 if ( an[j]->a_vals[i].bv_len > attrs[j]->na_len ) {
659 Debug( LDAP_DEBUG_ANY, "ndb_oc_attrs: attribute %s too long for column\n",
660 attrs[j]->na_name.bv_val, 0, 0 );
661 rc = LDAP_CONSTRAINT_VIOLATION;
665 *ptr++ = an[j]->a_vals[i].bv_len & 0xff;
666 if ( attrs[j]->na_len > 255 ) {
668 *ptr++ = an[j]->a_vals[i].bv_len >> 8;
670 memcpy( ptr, an[j]->a_vals[i].bv_val, an[j]->a_vals[i].bv_len );
675 if ( myop->setValue( attrs[j]->na_column, ptr )) {
687 Debug( LDAP_DEBUG_TRACE, "ndb_oc_attrs: failed %s (%d)\n",
688 myop->getNdbError().message, myop->getNdbError().code, 0 );
695 const NdbDictionary::Dictionary *myDict,
696 NdbTransaction *txn, NdbOcInfo *no, Entry *e )
698 const NdbDictionary::Table *myTable;
701 for ( i=0; i<no->no_nsets; i++ ) {
702 rc = ndb_oc_put( myDict, txn, no->no_sets[i], e );
707 myTable = myDict->getTable( no->no_table.bv_val );
711 return ndb_oc_attrs( txn, myTable, e, no, no->no_attrs, no->no_nattrs, NULL );
714 /* This is now only used for Adds. Modifies call ndb_oc_attrs directly. */
721 struct ndb_info *ni = (struct ndb_info *) be->be_private;
723 const NdbDictionary::Dictionary *myDict = NA->ndb->getDictionary();
727 /* Get the entry's objectClass attribute */
728 aoc = attr_find( NA->e->e_attrs, slap_schema.si_ad_objectClass );
732 ndb_oc_check( be, NA->ndb, aoc->a_nvals, &myOcs );
733 myOcs.no_info[myOcs.no_ninfo++] = ni->ni_opattrs;
735 /* Walk thru objectclasses, find all the attributes belonging to a class */
736 for ( i=0; i<myOcs.no_ninfo; i++ ) {
737 rc = ndb_oc_put( myDict, NA->txn, myOcs.no_info[i], NA->e );
741 /* slapadd tries to batch multiple entries per txn, but entry data is
742 * transient and blob data is required to remain valid for the whole txn.
743 * So we need to flush blobs before their source data disappears.
745 if (( slapMode & SLAP_TOOL_MODE ) && ndb_flush_blobs )
746 NA->txn->execute( NdbTransaction::NoCommit );
752 ndb_oc_get( Operation *op, NdbOcInfo *no, int *j, int *nocs, NdbOcInfo ***oclist )
757 for ( i=0; i<no->no_nsets; i++ ) {
758 ndb_oc_get( op, no->no_sets[i], j, nocs, oclist );
761 /* Don't insert twice */
763 for ( i=0; i<*j; i++ )
769 ol2 = (NdbOcInfo **)op->o_tmprealloc( *oclist, *nocs * sizeof(NdbOcInfo *), op->o_tmpmemctx );
776 /* Retrieve attribute data for given entry. The entry's DN and eid should
777 * already be populated.
786 struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
787 const NdbDictionary::Dictionary *myDict = NA->ndb->getDictionary();
788 const NdbDictionary::Table *myTable;
789 NdbIndexScanOperation **myop = NULL;
794 NdbOcInfo *oci, **oclist = NULL;
795 char abuf[65536], *ptr, **attrs = NULL;
799 /* FIXME: abuf should be dynamically allocated */
801 int i, j, k, nocs, nattrs, rc = LDAP_OTHER;
805 ndb_oc_check( op->o_bd, NA->ndb, NA->ocs, &myOcs );
806 myOcs.no_info[myOcs.no_ninfo++] = ni->ni_opattrs;
807 nocs = myOcs.no_ninfo;
809 oclist = (NdbOcInfo **)op->o_tmpcalloc( 1, nocs * sizeof(NdbOcInfo *), op->o_tmpmemctx );
811 for ( i=0, j=0; i<myOcs.no_ninfo; i++ ) {
812 ndb_oc_get( op, myOcs.no_info[i], &j, &nocs, &oclist );
817 for ( i=0; i<nocs; i++ )
818 nattrs += oclist[i]->no_nattrs;
820 ocx = (int *)op->o_tmpalloc( nocs * sizeof(int), op->o_tmpmemctx );
822 attrs = (char **)op->o_tmpalloc( nattrs * sizeof(char *), op->o_tmpmemctx );
824 myop = (NdbIndexScanOperation **)op->o_tmpalloc( nattrs * sizeof(NdbIndexScanOperation *), op->o_tmpmemctx );
828 for ( i=0; i<nocs; i++ ) {
831 myop[i] = NA->txn->getNdbIndexScanOperation( "PRIMARY", oci->no_table.bv_val );
834 if ( myop[i]->readTuples( update ? NdbOperation::LM_Exclusive : NdbOperation::LM_CommittedRead ))
836 if ( myop[i]->setBound( 0U, NdbIndexScanOperation::BoundEQ, &eid ))
839 for ( j=0; j<oci->no_nattrs; j++ ) {
840 if ( oci->no_attrs[j]->na_oi != oci )
842 if ( oci->no_attrs[j]->na_flag & NDB_INFO_ATBLOB ) {
843 NdbBlob *bi = myop[i]->getBlobHandle( oci->no_attrs[j]->na_column );
844 attrs[k++] = (char *)bi;
848 if ( oci->no_attrs[j]->na_len > 255 )
850 ptr += oci->no_attrs[j]->na_len + 1;
851 myop[i]->getValue( oci->no_attrs[j]->na_column, attrs[k++] );
856 /* Must use IgnoreError, because an entry with multiple objectClasses may not
857 * actually have attributes defined in each class / table.
859 if ( NA->txn->execute( NdbTransaction::NoCommit, NdbOperation::AO_IgnoreError, 1) < 0 )
863 for ( i=0; i<nocs; i++ ) {
864 if (( j = myop[i]->nextResult(true) )) {
866 Debug( LDAP_DEBUG_TRACE,
867 "ndb_entry_get_data: first nextResult(%d) failed: %s (%d)\n",
868 i, myop[i]->getNdbError().message, myop[i]->getNdbError().code );
876 for ( i=0; i<nocs; i++ ) {
878 for ( j=0; j<oci->no_nattrs; j++ ) {
881 if ( oci->no_attrs[j]->na_oi != oci )
885 } else if ( oci->no_attrs[j]->na_flag & NDB_INFO_ATBLOB ) {
887 NdbBlob *bi = (NdbBlob *)vi;
889 bi->getNull( isNull );
896 buf = (unsigned char *)attrs[k];
898 if ( oci->no_attrs[j]->na_len > 255 ) {
900 len |= (buf[1] << 8);
912 a = attrs_alloc( nattrs+1 );
915 a->a_desc = slap_schema.si_ad_objectClass;
917 ber_bvarray_dup_x( &a->a_vals, NA->ocs, NULL );
918 a->a_nvals = a->a_vals;
919 a->a_numvals = myOcs.no_ntext;
921 BER_BVZERO( &bv[1] );
924 a = NA->e->e_attrs->a_next;
926 for ( i=0; i<nocs; k=ocx[i], i++ ) {
928 for ( j=0; j<oci->no_nattrs; j++ ) {
931 if ( oci->no_attrs[j]->na_oi != oci )
933 buf = (unsigned char *)attrs[k++];
940 if ( oci->no_attrs[j]->na_flag & NDB_INFO_ATBLOB ) {
941 void *vi = (void *)buf;
942 NdbBlob *bi = (NdbBlob *)vi;
946 bi->getNull( isNull );
951 bi->getLength( len );
953 bv[0].bv_val = (char *)ch_malloc( len+1 );
955 if ( bi->readData( bv[0].bv_val, len2 )) {
956 Debug( LDAP_DEBUG_TRACE,
957 "ndb_entry_get_data: blob readData failed: %s (%d), len %d\n",
958 bi->getNdbError().message, bi->getNdbError().code, len2 );
960 bv[0].bv_val[len] = '\0';
961 ber_bvarray_add_x( &a->a_vals, bv, NULL );
963 bv[0].bv_len = buf[0];
964 if ( oci->no_attrs[j]->na_len > 255 ) {
966 bv[0].bv_len |= (buf[1] << 8);
967 bv[0].bv_val = (char *)buf+2;
970 bv[0].bv_val = (char *)buf+1;
973 if ( bv[0].bv_len == 0 ) {
977 bv[0].bv_val[bv[0].bv_len] = '\0';
978 value_add_one( &a->a_vals, bv );
980 a->a_desc = oci->no_attrs[j]->na_desc;
981 attr_normalize_one( a->a_desc, bv, &nbv, NULL );
983 if ( !BER_BVISNULL( &nbv )) {
984 ber_bvarray_add_x( &a->a_nvals, &nbv, NULL );
985 } else if ( !a->a_nvals ) {
986 a->a_nvals = a->a_vals;
992 for ( i=0; i<nocs; i++ ) {
995 if ((j = myop[i]->nextResult(true))) {
997 Debug( LDAP_DEBUG_TRACE,
998 "ndb_entry_get_data: last nextResult(%d) failed: %s (%d)\n",
999 i, myop[i]->getNdbError().message, myop[i]->getNdbError().code );
1011 op->o_tmpfree( myop, op->o_tmpmemctx );
1014 op->o_tmpfree( attrs, op->o_tmpmemctx );
1017 op->o_tmpfree( ocx, op->o_tmpmemctx );
1020 op->o_tmpfree( oclist, op->o_tmpmemctx );
1028 NdbTransaction *txn, Uint64 eid, NdbOcInfo *no )
1030 NdbIndexScanOperation *myop;
1033 for ( i=0; i<no->no_nsets; i++ ) {
1034 rc = ndb_oc_del( txn, eid, no->no_sets[i] );
1035 if ( rc ) return rc;
1038 myop = txn->getNdbIndexScanOperation( "PRIMARY", no->no_table.bv_val );
1041 if ( myop->readTuples( NdbOperation::LM_Exclusive ))
1043 if ( myop->setBound( 0U, NdbIndexScanOperation::BoundEQ, &eid ))
1046 txn->execute(NoCommit);
1047 while ( myop->nextResult(true) == 0) {
1049 myop->deleteCurrentTuple();
1050 } while (myop->nextResult(false) == 0);
1051 txn->execute(NoCommit);
1063 struct ndb_info *ni = (struct ndb_info *) be->be_private;
1064 Uint64 eid = NA->e->e_id;
1068 ndb_oc_check( be, NA->ndb, NA->ocs, &myOcs );
1069 myOcs.no_info[myOcs.no_ninfo++] = ni->ni_opattrs;
1071 for ( i=0; i<myOcs.no_ninfo; i++ ) {
1072 if ( ndb_oc_del( NA->txn, eid, myOcs.no_info[i] ))
1088 /* Walk thru RDNs */
1089 end = dn->bv_val + dn->bv_len;
1090 for ( i=0; i<NDB_MAX_RDNS; i++ ) {
1091 for ( beg = end-1; beg > dn->bv_val; beg-- ) {
1097 if ( beg >= dn->bv_val ) {
1099 /* RDN is too long */
1100 if ( len > NDB_RDN_LEN )
1101 return LDAP_CONSTRAINT_VIOLATION;
1102 memcpy( rdns->nr_buf[i]+1, beg, len );
1106 rdns->nr_buf[i][0] = len;
1109 /* Too many RDNs in DN */
1110 if ( i == NDB_MAX_RDNS && beg > dn->bv_val ) {
1111 return LDAP_CONSTRAINT_VIOLATION;
1124 char dummy[2] = {0,0};
1126 /* Walk thru RDNs */
1127 for ( i=0; i<rdns->nr_num; i++ ) {
1128 if ( myop->equal( i+RDN_COLUMN, rdns->nr_buf[i] ))
1131 for ( ; i<NDB_MAX_RDNS; i++ ) {
1132 if ( myop->equal( i+RDN_COLUMN, dummy ))
1138 /* Store the DN2ID_TABLE fields */
1146 struct ndb_info *ni = (struct ndb_info *) be->be_private;
1147 const NdbDictionary::Dictionary *myDict = NA->ndb->getDictionary();
1148 const NdbDictionary::Table *myTable = myDict->getTable( DN2ID_TABLE );
1153 /* Get the entry's objectClass attribute; it's ok to be
1154 * absent on a fresh insert
1156 aoc = attr_find( NA->e->e_attrs, slap_schema.si_ad_objectClass );
1157 if ( update && !aoc )
1158 return LDAP_OBJECT_CLASS_VIOLATION;
1160 myop = NA->txn->getNdbOperation( myTable );
1164 if ( myop->updateTuple())
1167 if ( myop->insertTuple())
1171 if ( ndb_rdns2keys( myop, NA->rdns ))
1176 Uint64 eid = NA->e->e_id;
1177 if ( myop->setValue( EID_COLUMN, eid ))
1181 /* Set list of objectClasses */
1182 /* List is <sp> <class> <sp> <class> <sp> ... so that
1183 * searches for " class " will yield accurate results
1186 char *ptr, buf[sizeof(MedVar)];
1190 ndb_oc_check( be, NA->ndb, aoc->a_nvals, &myOcs );
1193 for ( i=0; i<myOcs.no_ntext; i++ ) {
1195 if ( ptr + myOcs.no_text[i].bv_len + 1 >= &buf[sizeof(buf)] )
1197 ptr = lutil_strcopy( ptr, myOcs.no_text[i].bv_val );
1201 /* implicit classes */
1202 if ( myOcs.no_nitext ) {
1205 for ( i=0; i<myOcs.no_nitext; i++ ) {
1207 if ( ptr + myOcs.no_itext[i].bv_len + 1 >= &buf[sizeof(buf)] )
1209 ptr = lutil_strcopy( ptr, myOcs.no_itext[i].bv_val );
1217 if ( myop->setValue( OCS_COLUMN, buf ))
1221 /* Set any indexed attrs */
1222 for ( a = NA->e->e_attrs; a; a=a->a_next ) {
1223 ai = ndb_ai_find( ni, a->a_desc->ad_type );
1224 if ( ai && ( ai->na_flag & NDB_INFO_INDEX )) {
1225 char *ptr, buf[sizeof(MedVar)];
1229 len = a->a_vals[0].bv_len;
1230 /* FIXME: data loss */
1231 if ( len > ai->na_len )
1233 buf[0] = len & 0xff;
1234 if ( ai->na_len > 255 ) {
1237 memcpy( ptr, a->a_vals[0].bv_val, len );
1238 if ( myop->setValue( ai->na_ixcol, buf ))
1246 extern "C" struct berval *
1254 struct berval *list, tmp;
1258 while ( *str == delim ) {
1263 while ( str[len-1] == delim ) {
1267 for ( i = 1, beg = str;; i++ ) {
1268 beg = strchr( beg, delim );
1271 if ( beg >= str + len )
1277 list = (struct berval *)slap_sl_malloc( (num+1)*sizeof(struct berval), ctx);
1279 for ( i = 0, beg = str; i<num; i++ ) {
1281 beg = strchr( beg, delim );
1282 if ( beg >= str + len )
1285 tmp.bv_len = beg - tmp.bv_val;
1287 tmp.bv_len = len - (tmp.bv_val - str);
1289 ber_dupbv_x( &list[i], &tmp, ctx );
1293 BER_BVZERO( &list[i] );
1297 extern "C" struct berval *
1306 int len = ref[0] | (ref[1] << 8);
1308 /* don't return the implied classes */
1309 implied = (char *)memchr( ref+2, '@', len );
1311 len = implied - ref - 2;
1315 return ndb_str2bvarray( (char *)ref+2, len, ' ', ctx );
1318 /* Retrieve the DN2ID_TABLE fields. Can call with NULL ocs if just verifying
1319 * the existence of a DN.
1326 struct berval *matched
1329 struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
1330 const NdbDictionary::Dictionary *myDict = NA->ndb->getDictionary();
1331 const NdbDictionary::Table *myTable = myDict->getTable( DN2ID_TABLE );
1332 NdbOperation *myop[NDB_MAX_RDNS];
1333 NdbRecAttr *eid[NDB_MAX_RDNS], *oc[NDB_MAX_RDNS];
1334 char idbuf[NDB_MAX_RDNS][2*sizeof(ID)];
1335 char ocbuf[NDB_MAX_RDNS][NDB_OC_BUFLEN];
1338 BER_BVZERO( matched );
1344 myop[0] = NA->txn->getNdbOperation( myTable );
1349 if ( myop[0]->readTuple( update ? NdbOperation::LM_Exclusive : NdbOperation::LM_CommittedRead )) {
1353 if ( !NA->rdns->nr_num && ndb_dn2rdns( &NA->e->e_name, NA->rdns )) {
1354 return LDAP_NO_SUCH_OBJECT;
1357 if ( ndb_rdns2keys( myop[0], NA->rdns )) {
1361 eid[0] = myop[0]->getValue( EID_COLUMN, idbuf[0] );
1369 oc[0] = myop[0]->getValue( OCS_COLUMN, ocbuf[0] );
1375 if ( NA->txn->execute(NdbTransaction::NoCommit, NdbOperation::AO_IgnoreError, 1) < 0 ) {
1379 switch( myop[0]->getNdbError().code ) {
1381 if ( !eid[0]->isNULL() && ( NA->e->e_id = eid[0]->u_64_value() )) {
1382 /* If we didn't care about OCs, or we got them */
1383 if ( NA->ocs || ocbuf[0][0] || ocbuf[0][1] ) {
1384 /* If wanted, return them */
1386 NA->ocs = ndb_ref2oclist( ocbuf[0], op->o_tmpmemctx );
1391 case NDB_NO_SUCH_OBJECT: /* no such tuple: look for closest parent */
1394 char dummy[2] = {0,0};
1396 /* get to last RDN, then back up 1 */
1397 k = NA->rdns->nr_num - 1;
1399 for ( i=0; i<k; i++ ) {
1400 myop[i] = NA->txn->getNdbOperation( myTable );
1403 if ( myop[i]->readTuple( NdbOperation::LM_CommittedRead ))
1405 for ( j=0; j<=i; j++ ) {
1406 if ( myop[i]->equal( j+RDN_COLUMN, NA->rdns->nr_buf[j] ))
1409 for ( ;j<NDB_MAX_RDNS; j++ ) {
1410 if ( myop[i]->equal( j+RDN_COLUMN, dummy ))
1413 eid[i] = myop[i]->getValue( EID_COLUMN, idbuf[i] );
1420 oc[i] = myop[0]->getValue( OCS_COLUMN, ocbuf[i] );
1426 if ( NA->txn->execute(NdbTransaction::NoCommit, NdbOperation::AO_IgnoreError, 1) < 0 ) {
1429 for ( --i; i>=0; i-- ) {
1430 if ( myop[i]->getNdbError().code == 0 ) {
1431 for ( j=0; j<=i; j++ )
1432 matched->bv_len += NA->rdns->nr_buf[j][0];
1433 NA->erdns = NA->rdns->nr_num;
1434 NA->rdns->nr_num = j;
1435 matched->bv_len += i;
1436 matched->bv_val = NA->e->e_name.bv_val +
1437 NA->e->e_name.bv_len - matched->bv_len;
1438 if ( !eid[i]->isNULL() )
1439 NA->e->e_id = eid[i]->u_64_value();
1441 NA->ocs = ndb_ref2oclist( ocbuf[i], op->o_tmpmemctx );
1446 return LDAP_NO_SUCH_OBJECT;
1460 struct ndb_info *ni = (struct ndb_info *) be->be_private;
1461 const NdbDictionary::Dictionary *myDict = NA->ndb->getDictionary();
1462 const NdbDictionary::Table *myTable = myDict->getTable( DN2ID_TABLE );
1465 myop = NA->txn->getNdbOperation( myTable );
1468 if ( myop->deleteTuple())
1471 if ( ndb_rdns2keys( myop, NA->rdns ))
1484 struct ndb_info *ni = (struct ndb_info *) be->be_private;
1485 const NdbDictionary::Dictionary *myDict = ndb->getDictionary();
1486 const NdbDictionary::Table *myTable = myDict->getTable( NEXTID_TABLE );
1491 Debug( LDAP_DEBUG_ANY, "ndb_next_id: " NEXTID_TABLE " table is missing\n",
1496 rc = ndb->getAutoIncrementValue( myTable, nid, 1000 );
1502 extern "C" { static void ndb_thread_hfree( void *key, void *data ); };
1504 ndb_thread_hfree( void *key, void *data )
1506 Ndb *ndb = (Ndb *)data;
1515 struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
1518 if ( ldap_pvt_thread_pool_getkey( op->o_threadctx, ni, &data, NULL )) {
1521 ldap_pvt_thread_mutex_lock( &ni->ni_conn_mutex );
1522 myNdb = new Ndb( ni->ni_cluster[ni->ni_nextconn++], ni->ni_dbname );
1523 if ( ni->ni_nextconn >= ni->ni_nconns )
1524 ni->ni_nextconn = 0;
1525 ldap_pvt_thread_mutex_unlock( &ni->ni_conn_mutex );
1529 rc = myNdb->init(1024);
1532 Debug( LDAP_DEBUG_ANY, "ndb_thread_handle: err %d\n",
1536 data = (void *)myNdb;
1537 if (( rc = ldap_pvt_thread_pool_setkey( op->o_threadctx, ni,
1538 data, ndb_thread_hfree, NULL, NULL ))) {
1540 Debug( LDAP_DEBUG_ANY, "ndb_thread_handle: err %d\n",
1554 AttributeDescription *ad,
1558 struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
1563 /* Get our NDB handle */
1564 rc = ndb_thread_handle( op, &NA.ndb );
1566 NA.txn = NA.ndb->startTransaction();
1568 Debug( LDAP_DEBUG_TRACE,
1569 LDAP_XSTRING(ndb_entry_get) ": startTransaction failed: %s (%d)\n",
1570 NA.ndb->getNdbError().message, NA.ndb->getNdbError().code, 0 );
1582 rc = ndb_entry_get_info( op, &NA, rw, NULL );
1587 rc = ndb_entry_get_data( op, &NA, 0 );
1588 ber_bvarray_free( NA.ocs );
1590 if ( oc && !is_entry_objectclass_or_sub( &e, oc )) {
1591 attrs_free( e.e_attrs );
1597 *ent = entry_alloc();
1599 ber_dupbv( &(*ent)->e_name, ndn );
1600 ber_dupbv( &(*ent)->e_nname, ndn );
1608 /* Congestion avoidance code
1609 * for Deadlock Rollback
1613 ndb_trans_backoff( int num_retries )
1617 int pow_retries = 1;
1618 unsigned long key = 0;
1619 unsigned long max_key = -1;
1620 struct timeval timeout;
1622 lutil_entropy( (unsigned char *) &key, sizeof( unsigned long ));
1624 for ( i = 0; i < num_retries; i++ ) {
1625 if ( i >= 5 ) break;
1629 delay = 16384 * (key * (double) pow_retries / (double) max_key);
1630 delay = delay ? delay : 1;
1632 Debug( LDAP_DEBUG_TRACE, "delay = %d, num_retries = %d\n", delay, num_retries, 0 );
1634 timeout.tv_sec = delay / 1000000;
1635 timeout.tv_usec = delay % 1000000;
1636 select( 0, NULL, NULL, NULL, &timeout );
1640 ndb_check_referral( Operation *op, SlapReply *rs, NdbArgs *NA )
1642 struct berval dn, ndn;
1644 dif = NA->erdns - NA->rdns->nr_num;
1646 /* Set full DN of matched into entry */
1647 for ( i=0; i<dif; i++ ) {
1648 dnParent( &NA->e->e_name, &dn );
1649 dnParent( &NA->e->e_nname, &ndn );
1651 NA->e->e_nname = ndn;
1654 /* return referral only if "disclose" is granted on the object */
1655 if ( access_allowed( op, NA->e, slap_schema.si_ad_entry,
1656 NULL, ACL_DISCLOSE, NULL )) {
1658 for ( i=0; !BER_BVISNULL( &NA->ocs[i] ); i++ );
1660 a.a_desc = slap_schema.si_ad_objectClass;
1662 a.a_nvals = NA->ocs;
1664 NA->e->e_attrs = &a;
1665 if ( is_entry_referral( NA->e )) {
1666 NA->e->e_attrs = NULL;
1667 ndb_entry_get_data( op, NA, 0 );
1668 rs->sr_ref = get_entry_referrals( op, NA->e );
1670 rs->sr_err = LDAP_REFERRAL;
1671 rs->sr_flags |= REP_REF_MUSTBEFREED;
1673 attrs_free( NA->e->e_attrs );
1675 NA->e->e_attrs = NULL;