1 /* id2entry.c - routines to deal with the id2entry database */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2016 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>.
20 #include <ac/string.h>
25 typedef struct Ecount {
26 ber_len_t len; /* total entry size */
27 ber_len_t dlen; /* contiguous data size */
34 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
36 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data,
38 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
40 #define ID2VKSZ (sizeof(ID)+2)
44 const MDB_val *usrkey,
48 unsigned short *uv, *cv;
52 memcpy(&ui, usrkey->mv_data, sizeof(ID));
53 memcpy(&ci, curkey->mv_data, sizeof(ID));
60 return uv[sizeof(ID)/2] - cv[sizeof(ID)/2];
63 /* Values are stored as
64 * [normalized-value NUL ] original-value NUL 2-byte-len
65 * The trailing 2-byte-len is zero if there is no normalized value.
66 * Otherwise, it is the length of the original-value.
68 int mdb_mval_put(Operation *op, MDB_cursor *mc, ID id, Attribute *a)
70 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
78 memcpy(ivk, &id, sizeof(id));
79 s = mdb->mi_adxs[a->a_desc->ad_index];
80 memcpy(ivk+sizeof(ID), &s, 2);
82 key.mv_size = sizeof(ivk);
84 for (i=0; i<a->a_numvals; i++) {
85 len = a->a_nvals[i].bv_len + 1 + 2;
86 if (a->a_nvals != a->a_vals)
87 len += a->a_vals[i].bv_len + 1;
89 buf = op->o_tmpalloc( len, op->o_tmpmemctx );
91 memcpy(buf, a->a_nvals[i].bv_val, a->a_nvals[i].bv_len);
92 buf += a->a_nvals[i].bv_len;
94 if (a->a_nvals != a->a_vals) {
95 s = a->a_vals[i].bv_len;
96 memcpy(buf, a->a_vals[i].bv_val, a->a_vals[i].bv_len);
97 buf += a->a_vals[i].bv_len;
104 rc = mdb_cursor_put(mc, &key, &data, 0);
105 op->o_tmpfree( data.mv_data, op->o_tmpmemctx );
112 int mdb_mval_del(Operation *op, MDB_cursor *mc, ID id, Attribute *a)
114 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
122 memcpy(ivk, &id, sizeof(id));
123 s = mdb->mi_adxs[a->a_desc->ad_index];
124 memcpy(ivk+sizeof(ID), &s, 2);
126 key.mv_size = sizeof(ivk);
129 for (i=0; i<a->a_numvals; i++) {
130 data.mv_data = a->a_nvals[i].bv_val;
131 data.mv_size = a->a_nvals[i].bv_len+1;
132 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH_RANGE);
135 rc = mdb_cursor_del(mc, 0);
140 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
143 rc = mdb_cursor_del(mc, MDB_NODUPDATA);
148 static int mdb_mval_get(Operation *op, MDB_cursor *mc, ID id, Attribute *a, int have_nvals)
150 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
158 memcpy(ivk, &id, sizeof(id));
159 s = mdb->mi_adxs[a->a_desc->ad_index];
160 memcpy(ivk+sizeof(ID), &s, 2);
162 key.mv_size = sizeof(ivk);
165 a->a_nvals = a->a_vals + a->a_numvals + 1;
167 a->a_nvals = a->a_vals;
168 for (i=0; i<a->a_numvals; i++) {
170 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
172 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
175 ptr = (char*)data.mv_data + data.mv_size - 2;
178 a->a_nvals[i].bv_val = data.mv_data;
179 a->a_vals[i].bv_len = s;
180 a->a_vals[i].bv_val = ptr - a->a_vals[i].bv_len - 1;
181 a->a_nvals[i].bv_len = a->a_vals[i].bv_val - a->a_nvals[i].bv_val - 1;
184 a->a_vals[i].bv_val = data.mv_data;
185 a->a_vals[i].bv_len = data.mv_size - 3;
188 BER_BVZERO(&a->a_vals[i]);
190 BER_BVZERO(&a->a_nvals[i]);
195 #define ADD_FLAGS (MDB_NOOVERWRITE|MDB_APPEND)
197 static int mdb_id2entry_put(
204 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
207 int rc, adding = flag;
209 /* We only store rdns, and they go in the dn2id database. */
211 key.mv_data = &e->e_id;
212 key.mv_size = sizeof(ID);
214 rc = mdb_entry_partsize( mdb, txn, e, &ec );
220 if (e->e_id < mdb->mi_nextid)
223 if (mdb->mi_maxentrysize && ec.len > mdb->mi_maxentrysize)
224 return LDAP_ADMINLIMIT_EXCEEDED;
227 data.mv_size = ec.dlen;
229 rc = mdb_cursor_put( mc, &key, &data, flag );
231 rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
232 if (rc == MDB_SUCCESS) {
233 rc = mdb_entry_encode( op, e, &data, &ec );
234 if( rc != LDAP_SUCCESS )
236 /* Handle adds of large multi-valued attrs here.
237 * Modifies handle them directly.
239 if (adding && ec.multi) {
242 rc = mdb_cursor_open( txn, mdb->mi_dbis[MDB_ID2VAL], &mvc );
245 for ( a = ec.multi; a; a=a->a_next ) {
246 if (!(a->a_flags & SLAP_ATTR_BIG_MULTI))
248 rc = mdb_mval_put( op, mvc, e->e_id, a );
249 if( rc != LDAP_SUCCESS )
252 mdb_cursor_close( mvc );
258 /* Was there a hole from slapadd? */
259 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
263 Debug( LDAP_DEBUG_ANY,
264 "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
265 mdb_strerror(rc), rc,
267 if ( rc != MDB_KEYEXIST )
274 * This routine adds (or updates) an entry on disk.
276 int mdb_id2entry_add(
282 return mdb_id2entry_put(op, txn, mc, e, ADD_FLAGS);
285 int mdb_id2entry_update(
291 return mdb_id2entry_put(op, txn, mc, e, 0);
304 key.mv_size = sizeof(ID);
307 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
308 /* stubs from missing parents - DB is actually invalid */
309 if ( rc == MDB_SUCCESS && !data->mv_size )
320 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
327 key.mv_size = sizeof(ID);
330 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
331 if ( rc == MDB_NOTFOUND ) {
332 /* Looking for root entry on an empty-dn suffix? */
333 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
334 struct berval gluebv = BER_BVC("glue");
335 Entry *r = mdb_entry_alloc(op, 2, 4);
336 Attribute *a = r->e_attrs;
340 r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
342 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
343 a->a_desc = slap_schema.si_ad_objectClass;
344 a->a_nvals = a->a_vals;
351 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
352 a->a_desc = slap_schema.si_ad_structuralObjectClass;
354 a->a_nvals = a->a_vals;
363 /* stubs from missing parents - DB is actually invalid */
364 if ( rc == MDB_SUCCESS && !data.mv_size )
368 rc = mdb_entry_decode( op, mdb_cursor_txn( mc ), &data, id, e );
372 (*e)->e_name.bv_val = NULL;
373 (*e)->e_nname.bv_val = NULL;
378 int mdb_id2entry_delete(
383 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
384 MDB_dbi dbi = mdb->mi_id2entry;
389 key.mv_data = &e->e_id;
390 key.mv_size = sizeof(ID);
392 /* delete from database */
393 rc = mdb_del( tid, dbi, &key, NULL );
396 rc = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
400 rc = mdb_cursor_get( mvc, &key, NULL, MDB_SET_RANGE );
401 if (rc && rc != MDB_NOTFOUND)
403 while (*(ID *)key.mv_data == e->e_id ) {
404 rc = mdb_cursor_del( mvc, MDB_NODUPDATA );
407 mdb_cursor_get( mvc, &key, NULL, MDB_GET_CURRENT );
412 static Entry * mdb_entry_alloc(
417 Entry *e = op->o_tmpalloc( sizeof(Entry) +
418 nattrs * sizeof(Attribute) +
419 nvals * sizeof(struct berval), op->o_tmpmemctx );
420 BER_BVZERO(&e->e_bv);
423 e->e_attrs = (Attribute *)(e+1);
424 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
432 int mdb_entry_return(
439 if ( e->e_private ) {
440 if ( op->o_hdr && op->o_tmpmfuncs ) {
441 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
442 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
443 op->o_tmpfree( e, op->o_tmpmemctx );
445 ch_free( e->e_nname.bv_val );
446 ch_free( e->e_name.bv_val );
455 int mdb_entry_release(
460 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
461 struct mdb_op_info *moi = NULL;
464 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
465 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
468 if ( slapMode & SLAP_SERVER_MODE ) {
470 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
472 if ( oex->oe_key == mdb ) {
473 mdb_entry_return( op, e );
474 moi = (mdb_op_info *)oex;
475 /* If it was setup by entry_get we should probably free it */
476 if (( moi->moi_flag & (MOI_FREEIT|MOI_KEEPER)) == MOI_FREEIT ) {
478 if ( moi->moi_ref < 1 ) {
479 mdb_txn_reset( moi->moi_txn );
481 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
482 op->o_tmpfree( moi, op->o_tmpmemctx );
491 mdb_entry_return( op, e );
496 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
502 AttributeDescription *at,
506 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
507 struct mdb_op_info *moi = NULL;
511 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
513 Debug( LDAP_DEBUG_ARGS,
514 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
515 Debug( LDAP_DEBUG_ARGS,
516 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
517 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
519 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
524 /* can we find entry */
525 rc = mdb_dn2entry( op, txn, NULL, ndn, &e, NULL, 0 );
531 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
534 Debug( LDAP_DEBUG_ACL,
535 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
537 rc = LDAP_NO_SUCH_OBJECT;
541 Debug( LDAP_DEBUG_ACL,
542 "=> mdb_entry_get: found entry: \"%s\"\n",
545 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
546 Debug( LDAP_DEBUG_ACL,
547 "<= mdb_entry_get: failed to find objectClass %s\n",
548 oc->soc_cname.bv_val, 0, 0 );
549 rc = LDAP_NO_SUCH_ATTRIBUTE;
553 /* NOTE: attr_find() or attrs_find()? */
554 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
555 Debug( LDAP_DEBUG_ACL,
556 "<= mdb_entry_get: failed to find attribute %s\n",
557 at->ad_cname.bv_val, 0, 0 );
558 rc = LDAP_NO_SUCH_ATTRIBUTE;
563 if( rc != LDAP_SUCCESS ) {
565 mdb_entry_release( op, e, rw );
570 Debug( LDAP_DEBUG_TRACE,
571 "mdb_entry_get: rc=%d\n",
577 mdb_reader_free( void *key, void *data )
581 if ( txn ) mdb_txn_abort( txn );
584 /* free up any keys used by the main thread */
586 mdb_reader_flush( MDB_env *env )
589 void *ctx = ldap_pvt_thread_pool_context();
591 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
592 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
593 mdb_reader_free( env, data );
597 extern MDB_txn *mdb_tool_txn;
600 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
605 mdb_op_info *moi = NULL;
608 assert( op != NULL );
610 if ( !mdb || !moip ) return -1;
612 /* If no op was provided, try to find the ctx anyway... */
614 ctx = op->o_threadctx;
616 ctx = ldap_pvt_thread_pool_context();
620 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
621 if ( oex->oe_key == mdb ) break;
623 moi = (mdb_op_info *)oex;
631 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
633 moi = ch_malloc(sizeof(mdb_op_info));
635 moi->moi_flag = MOI_FREEIT;
638 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
639 moi->moi_oe.oe_key = mdb;
645 /* This op started as a reader, but now wants to write. */
646 if ( moi->moi_flag & MOI_READER ) {
648 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
650 /* This op is continuing an existing write txn */
654 if ( !moi->moi_txn ) {
655 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
656 moi->moi_txn = mdb_tool_txn;
659 if ( get_lazyCommit( op ))
660 flag |= MDB_NOMETASYNC;
661 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, flag, &moi->moi_txn );
663 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
664 mdb_strerror(rc), rc, 0 );
672 /* OK, this is a reader */
673 if ( !moi->moi_txn ) {
674 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
675 moi->moi_txn = mdb_tool_txn;
679 /* Shouldn't happen unless we're single-threaded */
680 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
682 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
683 mdb_strerror(rc), rc, 0 );
687 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
688 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
690 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
691 mdb_strerror(rc), rc, 0 );
695 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
696 data, mdb_reader_free, NULL, NULL ) ) ) {
697 mdb_txn_abort( moi->moi_txn );
699 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
707 moi->moi_flag |= MOI_READER;
710 if ( moi->moi_ref < 1 ) {
714 rc = mdb_txn_renew( moi->moi_txn );
725 int mdb_txn( Operation *op, int txnop, OpExtra **ptr )
727 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
728 mdb_op_info **moip = (mdb_op_info **)ptr, *moi = *moip;
733 rc = mdb_opinfo_get( op, mdb, 0, moip );
736 moi->moi_flag |= MOI_KEEPER;
739 case SLAP_TXN_COMMIT:
740 rc = mdb_txn_commit( moi->moi_txn );
743 op->o_tmpfree( moi, op->o_tmpmemctx );
747 mdb_txn_abort( moi->moi_txn );
748 op->o_tmpfree( moi, op->o_tmpmemctx );
755 /* Count up the sizes of the components of an entry */
756 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
760 int i, nat = 0, nval = 0, nnval = 0, doff = 0;
764 len = 4*sizeof(int); /* nattrs, nvals, ocflags, offset */
766 for (a=e->e_attrs; a; a=a->a_next) {
767 /* For AttributeDesc, we only store the attr index */
769 if (a->a_desc->ad_index >= MDB_MAXADS) {
770 Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n",
774 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
775 int rc = mdb_ad_get(mdb, txn, a->a_desc);
779 len += 2*sizeof(int); /* AD index, numvals */
780 dlen += 2*sizeof(int);
781 nval += a->a_numvals + 1; /* empty berval at end */
782 if (a->a_numvals > mdb->mi_multi_hi)
783 a->a_flags |= SLAP_ATTR_BIG_MULTI;
784 if (a->a_flags & SLAP_ATTR_BIG_MULTI)
785 doff += a->a_numvals;
786 for (i=0; i<a->a_numvals; i++) {
787 int alen = a->a_vals[i].bv_len + 1 + sizeof(int); /* len */
789 if (a->a_flags & SLAP_ATTR_BIG_MULTI) {
796 if (a->a_nvals != a->a_vals) {
797 nval += a->a_numvals + 1;
799 if (a->a_flags & SLAP_ATTR_BIG_MULTI)
800 doff += a->a_numvals;
801 for (i=0; i<a->a_numvals; i++) {
802 int alen = a->a_nvals[i].bv_len + 1 + sizeof(int);
804 if (!(a->a_flags & SLAP_ATTR_BIG_MULTI))
810 dlen = (dlen + sizeof(ID)-1) & ~(sizeof(ID)-1);
815 eh->offset = nat + nval - nnval - doff;
819 /* Flag bits for an encoded attribute */
820 #define MDB_AT_SORTED (1<<(sizeof(unsigned int)*CHAR_BIT-1))
821 /* the values are in sorted order */
822 #define MDB_AT_MULTI (1<<(sizeof(unsigned int)*CHAR_BIT-2))
823 /* the values of this multi-valued attr are stored separately */
825 #define MDB_AT_NVALS (1<<(sizeof(unsigned int)*CHAR_BIT-1))
826 /* this attribute has normalized values */
828 /* Flatten an Entry into a buffer. The buffer starts with the count of the
829 * number of attributes in the entry, the total number of values in the
830 * entry, and the e_ocflags. It then contains a list of integers for each
831 * attribute. For each attribute the first integer gives the index of the
832 * matching AttributeDescription, followed by the number of values in the
833 * attribute. If the MDB_AT_SORTED bit of the attr index is set, the
834 * attribute's values are already sorted. If the MDB_AT_MULTI bit of the
835 * attr index is set, the values are stored separately.
837 * If the MDB_AT_NVALS bit of numvals is set, the attribute also has
838 * normalized values present. (Note - a_numvals is an unsigned int, so this
839 * means it's possible to receive an attribute that we can't encode due
840 * to size overflow. In practice, this should not be an issue.)
842 * Then the length of each value is listed. If there are normalized values,
843 * their lengths come next. This continues for each attribute. After all
844 * of the lengths for the last attribute, the actual values are copied,
845 * with a NUL terminator after each value.
846 * The buffer is padded to the sizeof(ID). The entire buffer size is
847 * precomputed so that a single malloc can be performed.
849 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
851 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
858 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
859 (long) e->e_id, e->e_dn, 0 );
861 /* make sure e->e_ocflags is set */
862 if (is_entry_referral(e))
865 lp = (unsigned int *)data->mv_data;
868 *lp++ = (unsigned int)e->e_ocflags;
870 ptr = (unsigned char *)(lp + eh->offset);
872 for (a=e->e_attrs; a; a=a->a_next) {
873 if (!a->a_desc->ad_index)
874 return LDAP_UNDEFINED_TYPE;
875 l = mdb->mi_adxs[a->a_desc->ad_index];
876 if (a->a_flags & SLAP_ATTR_SORTED_VALS)
878 if (a->a_flags & SLAP_ATTR_BIG_MULTI)
882 if (a->a_nvals != a->a_vals)
885 if (a->a_flags & SLAP_ATTR_BIG_MULTI) {
889 for (i=0; a->a_vals[i].bv_val; i++);
890 assert( i == a->a_numvals );
891 for (i=0; i<a->a_numvals; i++) {
892 *lp++ = a->a_vals[i].bv_len;
893 memcpy(ptr, a->a_vals[i].bv_val,
894 a->a_vals[i].bv_len);
895 ptr += a->a_vals[i].bv_len;
898 if (a->a_nvals != a->a_vals) {
899 for (i=0; i<a->a_numvals; i++) {
900 *lp++ = a->a_nvals[i].bv_len;
901 memcpy(ptr, a->a_nvals[i].bv_val,
902 a->a_nvals[i].bv_len);
903 ptr += a->a_nvals[i].bv_len;
911 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
912 (long) e->e_id, e->e_dn, 0 );
917 /* Retrieve an Entry that was stored using entry_encode above.
919 * Note: everything is stored in a single contiguous block, so
920 * you can not free individual attributes or names from this
921 * structure. Attempting to do so will likely corrupt memory.
924 int mdb_entry_decode(Operation *op, MDB_txn *txn, MDB_val *data, ID id, Entry **e)
926 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
927 int i, j, nattrs, nvals;
932 AttributeDescription *ad;
933 unsigned int *lp = (unsigned int *)data->mv_data;
936 MDB_cursor *mvc = NULL;
938 Debug( LDAP_DEBUG_TRACE,
939 "=> mdb_entry_decode:\n",
944 x = mdb_entry_alloc(op, nattrs, nvals);
945 x->e_ocflags = *lp++;
952 ptr = (unsigned char *)(lp + i);
954 for (;nattrs>0; nattrs--) {
955 int have_nval = 0, multi = 0;
956 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
958 if (i & MDB_AT_SORTED) {
960 a->a_flags |= SLAP_ATTR_SORTED_VALS;
962 if (i & MDB_AT_MULTI) {
964 a->a_flags |= SLAP_ATTR_BIG_MULTI;
967 if (i > mdb->mi_numads) {
968 rc = mdb_ad_read(mdb, txn);
971 if (i > mdb->mi_numads) {
972 Debug( LDAP_DEBUG_ANY,
973 "mdb_entry_decode: attribute index %d not recognized\n",
979 a->a_desc = mdb->mi_ads[i];
980 a->a_numvals = *lp++;
981 if (a->a_numvals & MDB_AT_NVALS) {
982 a->a_numvals ^= MDB_AT_NVALS;
988 rc = mdb_cursor_open(txn, mdb->mi_dbis[MDB_ID2VAL], &mvc);
992 mdb_mval_get(op, mvc, id, a, have_nval);
993 bptr += a->a_numvals + 1;
995 bptr += a->a_numvals + 1;
997 for (i=0; i<a->a_numvals; i++) {
998 bptr->bv_len = *lp++;
999 bptr->bv_val = (char *)ptr;
1000 ptr += bptr->bv_len+1;
1003 bptr->bv_val = NULL;
1009 for (i=0; i<a->a_numvals; i++) {
1010 bptr->bv_len = *lp++;
1011 bptr->bv_val = (char *)ptr;
1012 ptr += bptr->bv_len+1;
1015 bptr->bv_val = NULL;
1019 a->a_nvals = a->a_vals;
1023 /* FIXME: This is redundant once a sorted entry is saved into the DB */
1024 if (( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
1025 && !(a->a_flags & SLAP_ATTR_SORTED_VALS)) {
1026 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
1027 if ( rc == LDAP_SUCCESS ) {
1028 a->a_flags |= SLAP_ATTR_SORTED_VALS;
1029 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
1030 /* should never happen */
1031 Debug( LDAP_DEBUG_ANY,
1032 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
1033 a->a_desc->ad_cname.bv_val, j, 0 );
1040 a[-1].a_next = NULL;
1042 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",
1049 mdb_cursor_close(mvc);