1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2012 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 #define IDL_MAX(x,y) ( (x) > (y) ? (x) : (y) )
26 #define IDL_MIN(x,y) ( (x) < (y) ? (x) : (y) )
27 #define IDL_CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
30 static void idl_check( ID *ids )
32 if( MDB_IDL_IS_RANGE( ids ) ) {
33 assert( MDB_IDL_RANGE_FIRST(ids) <= MDB_IDL_RANGE_LAST(ids) );
36 for( i=1; i < ids[0]; i++ ) {
37 assert( ids[i+1] > ids[i] );
43 static void idl_dump( ID *ids )
45 if( MDB_IDL_IS_RANGE( ids ) ) {
46 Debug( LDAP_DEBUG_ANY,
47 "IDL: range ( %ld - %ld )\n",
48 (long) MDB_IDL_RANGE_FIRST( ids ),
49 (long) MDB_IDL_RANGE_LAST( ids ) );
53 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
55 for( i=1; i<=ids[0]; i++ ) {
57 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
59 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
62 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
67 #endif /* IDL_DEBUG > 1 */
68 #endif /* IDL_DEBUG > 0 */
70 unsigned mdb_idl_search( ID *ids, ID id )
72 #define IDL_BINARY_SEARCH 1
73 #ifdef IDL_BINARY_SEARCH
75 * binary search of id in ids
76 * if found, returns position of id
77 * if not found, returns first postion greater than id
89 unsigned pivot = n >> 1;
90 cursor = base + pivot + 1;
91 val = IDL_CMP( id, ids[cursor] );
96 } else if ( val > 0 ) {
111 /* (reverse) linear search */
118 for( i=ids[0]; i; i-- ) {
128 int mdb_idl_insert( ID *ids, ID id )
133 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
139 if (MDB_IDL_IS_RANGE( ids )) {
140 /* if already in range, treat as a dup */
141 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
143 if (id < MDB_IDL_RANGE_FIRST(ids))
145 else if (id > MDB_IDL_RANGE_LAST(ids))
150 x = mdb_idl_search( ids, id );
158 if ( x <= ids[0] && ids[x] == id ) {
163 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
166 ids[2] = ids[ids[0]-1];
167 } else if ( ids[ids[0]-1] < id ) {
170 ids[2] = ids[ids[0]-1];
176 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
189 static int mdb_idl_delete( ID *ids, ID id )
194 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
200 if (MDB_IDL_IS_RANGE( ids )) {
201 /* If deleting a range boundary, adjust */
204 else if ( ids[2] == id )
206 /* deleting from inside a range is a no-op */
208 /* If the range has collapsed, re-adjust */
209 if ( ids[1] > ids[2] )
211 else if ( ids[1] == ids[2] )
216 x = mdb_idl_search( ids, id );
224 if( x > ids[0] || ids[x] != id ) {
228 } else if ( --ids[0] == 0 ) {
234 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
252 if ( len == 4 /* LUTIL_HASH_BYTES */ ) {
253 unsigned char *c = val;
254 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
268 MDB_cursor **saved_cursor,
271 MDB_val data, key2, *kptr;
276 MDB_cursor_op opflag;
280 Debug( LDAP_DEBUG_ARGS,
281 "mdb_idl_fetch_key: %s\n",
282 mdb_show_key( keybuf, key->mv_data, key->mv_size ), 0, 0 );
284 assert( ids != NULL );
286 if ( saved_cursor && *saved_cursor ) {
288 } else if ( get_flag == LDAP_FILTER_GE ) {
289 opflag = MDB_SET_RANGE;
290 } else if ( get_flag == LDAP_FILTER_LE ) {
296 /* If we're not reusing an existing cursor, get a new one */
297 if( opflag != MDB_NEXT ) {
298 rc = mdb_cursor_open( txn, dbi, &cursor );
300 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
301 "cursor failed: %s (%d)\n", mdb_strerror(rc), rc, 0 );
305 cursor = *saved_cursor;
308 /* If this is a LE lookup, save original key so we can determine
309 * when to stop. If this is a GE lookup, save the key since it
310 * will be overwritten.
312 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
313 key2.mv_data = keybuf;
314 key2.mv_size = key->mv_size;
315 AC_MEMCPY( keybuf, key->mv_data, key->mv_size );
321 rc = mdb_cursor_get( cursor, kptr, &data, opflag );
323 /* skip presence key on range inequality lookups */
324 while (rc == 0 && kptr->mv_size != len) {
325 rc = mdb_cursor_get( cursor, kptr, &data, MDB_NEXT_NODUP );
327 /* If we're doing a LE compare and the new key is greater than
328 * our search key, we're done
330 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->mv_data,
331 key->mv_data, key->mv_size ) > 0 ) {
336 rc = mdb_cursor_get( cursor, key, &data, MDB_GET_MULTIPLE );
338 memcpy( i, data.mv_data, data.mv_size );
339 i += data.mv_size / sizeof(ID);
340 rc = mdb_cursor_get( cursor, key, &data, MDB_NEXT_MULTIPLE );
342 if ( rc == MDB_NOTFOUND ) rc = 0;
343 ids[0] = i - &ids[1];
344 /* On disk, a range is denoted by 0 in the first element */
346 if (ids[0] != MDB_IDL_RANGE_SIZE) {
347 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
348 "range size mismatch: expected %d, got %ld\n",
349 MDB_IDL_RANGE_SIZE, ids[0], 0 );
350 mdb_cursor_close( cursor );
353 MDB_IDL_RANGE( ids, ids[2], ids[3] );
355 data.mv_size = MDB_IDL_SIZEOF(ids);
358 if ( saved_cursor && rc == 0 ) {
359 if ( !*saved_cursor )
360 *saved_cursor = cursor;
363 mdb_cursor_close( cursor );
365 if( rc == MDB_NOTFOUND ) {
368 } else if( rc != 0 ) {
369 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
370 "get failed: %s (%d)\n",
371 mdb_strerror(rc), rc, 0 );
374 } else if ( data.mv_size == 0 || data.mv_size % sizeof( ID ) ) {
375 /* size not multiple of ID size */
376 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
377 "odd size: expected %ld multiple, got %ld\n",
378 (long) sizeof( ID ), (long) data.mv_size, 0 );
381 } else if ( data.mv_size != MDB_IDL_SIZEOF(ids) ) {
383 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
384 "get size mismatch: expected %ld, got %ld\n",
385 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.mv_size, 0 );
402 unsigned int flag = MDB_NODUPDATA;
403 #ifndef MISALIGNED_OK
409 Debug( LDAP_DEBUG_ARGS,
410 "mdb_idl_insert_keys: %lx %s\n",
411 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ), 0 );
414 assert( id != NOID );
416 #ifndef MISALIGNED_OK
417 if (keys[0].bv_len & 0x03)
420 for ( k=0; keys[k].bv_val; k++ ) {
421 /* Fetch the first data item for this key, to see if it
422 * exists and if it's a range.
424 #ifndef MISALIGNED_OK
425 if (keys[k].bv_len & 0x03) {
426 key.mv_size = sizeof(kbuf);
428 memcpy(key.mv_data, keys[k].bv_val, keys[k].bv_len);
432 key.mv_size = keys[k].bv_len;
433 key.mv_data = keys[k].bv_val;
435 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
439 memcpy(&lo, data.mv_data, sizeof(ID));
441 /* not a range, count the number of items */
443 rc = mdb_cursor_count( cursor, &count );
448 if ( count >= MDB_IDL_DB_MAX ) {
449 /* No room, convert to a range */
451 rc = mdb_cursor_get( cursor, &key, &data, MDB_LAST_DUP );
452 if ( rc != 0 && rc != MDB_NOTFOUND ) {
453 err = "c_get last_dup";
458 /* Update hi/lo if needed */
461 } else if ( id > hi ) {
464 /* delete the old key */
465 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
470 /* Store the range */
471 data.mv_size = sizeof(ID);
474 rc = mdb_cursor_put( cursor, &key, &data, 0 );
480 rc = mdb_cursor_put( cursor, &key, &data, 0 );
486 rc = mdb_cursor_put( cursor, &key, &data, 0 );
492 /* There's room, just store it */
493 if ( slapMode & SLAP_TOOL_QUICK )
498 /* It's a range, see if we need to rewrite
503 if ( id < lo || id > hi ) {
505 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
508 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
510 data.mv_size = sizeof(ID);
512 /* Replace the current lo/hi */
513 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
520 } else if ( rc == MDB_NOTFOUND ) {
521 put1: data.mv_data = &id;
522 data.mv_size = sizeof(ID);
523 rc = mdb_cursor_put( cursor, &key, &data, flag );
524 /* Don't worry if it's already there */
525 if ( rc == MDB_KEYEXIST )
532 /* initial c_get failed, nothing was done */
534 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_keys: "
535 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
552 #ifndef MISALIGNED_OK
558 Debug( LDAP_DEBUG_ARGS,
559 "mdb_idl_delete_keys: %lx %s\n",
560 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ), 0 );
562 assert( id != NOID );
564 #ifndef MISALIGNED_OK
565 if (keys[0].bv_len & 0x03)
568 for ( k=0; keys[k].bv_val; k++) {
569 /* Fetch the first data item for this key, to see if it
570 * exists and if it's a range.
572 #ifndef MISALIGNED_OK
573 if (keys[k].bv_len & 0x03) {
574 key.mv_size = sizeof(kbuf);
576 memcpy(key.mv_data, keys[k].bv_val, keys[k].bv_len);
580 key.mv_size = keys[k].bv_len;
581 key.mv_data = keys[k].bv_val;
583 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
586 memcpy( &tmp, data.mv_data, sizeof(ID) );
589 /* Not a range, just delete it */
591 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
596 rc = mdb_cursor_del( cursor, 0 );
602 /* It's a range, see if we need to rewrite
607 if ( id == lo || id == hi ) {
608 ID lo2 = lo, hi2 = hi;
611 } else if ( id == hi ) {
615 /* The range has collapsed... */
616 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
623 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
628 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
631 /* Replace the current lo/hi */
632 data.mv_size = sizeof(ID);
633 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
642 /* initial c_get failed, nothing was done */
644 if ( rc == MDB_NOTFOUND )
647 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
648 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
658 * idl_intersection - return a = a intersection b
661 mdb_idl_intersection(
667 ID cursora = 0, cursorb = 0, cursorc;
670 if ( MDB_IDL_IS_ZERO( a ) || MDB_IDL_IS_ZERO( b ) ) {
675 idmin = IDL_MAX( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
676 idmax = IDL_MIN( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
677 if ( idmin > idmax ) {
680 } else if ( idmin == idmax ) {
686 if ( MDB_IDL_IS_RANGE( a ) ) {
687 if ( MDB_IDL_IS_RANGE(b) ) {
688 /* If both are ranges, just shrink the boundaries */
693 /* Else swap so that b is the range, a is a list */
701 /* If a range completely covers the list, the result is
702 * just the list. If idmin to idmax is contiguous, just
703 * turn it into a range.
705 if ( MDB_IDL_IS_RANGE( b )
706 && MDB_IDL_RANGE_FIRST( b ) <= MDB_IDL_RANGE_FIRST( a )
707 && MDB_IDL_RANGE_LAST( b ) >= MDB_IDL_RANGE_LAST( a ) ) {
708 if (idmax - idmin + 1 == a[0])
717 /* Fine, do the intersection one element at a time.
718 * First advance to idmin in both IDLs.
720 cursora = cursorb = idmin;
721 ida = mdb_idl_first( a, &cursora );
722 idb = mdb_idl_first( b, &cursorb );
725 while( ida <= idmax || idb <= idmax ) {
728 ida = mdb_idl_next( a, &cursora );
729 idb = mdb_idl_next( b, &cursorb );
730 } else if ( ida < idb ) {
731 ida = mdb_idl_next( a, &cursora );
733 idb = mdb_idl_next( b, &cursorb );
746 * idl_union - return a = a union b
754 ID cursora = 0, cursorb = 0, cursorc;
756 if ( MDB_IDL_IS_ZERO( b ) ) {
760 if ( MDB_IDL_IS_ZERO( a ) ) {
765 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ) {
766 over: ida = IDL_MIN( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
767 idb = IDL_MAX( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
774 ida = mdb_idl_first( a, &cursora );
775 idb = mdb_idl_first( b, &cursorb );
779 /* The distinct elements of a are cat'd to b */
780 while( ida != NOID || idb != NOID ) {
782 if( ++cursorc > MDB_IDL_UM_MAX ) {
786 ida = mdb_idl_next( a, &cursora );
790 ida = mdb_idl_next( a, &cursora );
791 idb = mdb_idl_next( b, &cursorb );
795 /* b is copied back to a in sorted order */
800 while (cursorb <= b[0] || cursorc <= a[0]) {
805 if (cursorb <= b[0] && b[cursorb] < idb)
806 a[cursora++] = b[cursorb++];
819 * mdb_idl_notin - return a intersection ~b (or a minus b)
828 ID cursora = 0, cursorb = 0;
830 if( MDB_IDL_IS_ZERO( a ) ||
831 MDB_IDL_IS_ZERO( b ) ||
832 MDB_IDL_IS_RANGE( b ) )
834 MDB_IDL_CPY( ids, a );
838 if( MDB_IDL_IS_RANGE( a ) ) {
839 MDB_IDL_CPY( ids, a );
843 ida = mdb_idl_first( a, &cursora ),
844 idb = mdb_idl_first( b, &cursorb );
848 while( ida != NOID ) {
850 /* we could shortcut this */
852 ida = mdb_idl_next( a, &cursora );
854 } else if ( ida < idb ) {
856 ida = mdb_idl_next( a, &cursora );
858 } else if ( ida > idb ) {
859 idb = mdb_idl_next( b, &cursorb );
862 ida = mdb_idl_next( a, &cursora );
863 idb = mdb_idl_next( b, &cursorb );
871 ID mdb_idl_first( ID *ids, ID *cursor )
880 if ( MDB_IDL_IS_RANGE( ids ) ) {
881 if( *cursor < ids[1] ) {
890 pos = mdb_idl_search( ids, *cursor );
900 ID mdb_idl_next( ID *ids, ID *cursor )
902 if ( MDB_IDL_IS_RANGE( ids ) ) {
903 if( ids[2] < ++(*cursor) ) {
909 if ( ++(*cursor) <= ids[0] ) {
916 /* Add one ID to an unsorted list. We ensure that the first element is the
917 * minimum and the last element is the maximum, for fast range compaction.
918 * this means IDLs up to length 3 are always sorted...
920 int mdb_idl_append_one( ID *ids, ID id )
922 if (MDB_IDL_IS_RANGE( ids )) {
923 /* if already in range, treat as a dup */
924 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
926 if (id < MDB_IDL_RANGE_FIRST(ids))
928 else if (id > MDB_IDL_RANGE_LAST(ids))
940 if ( ids[0] > 1 && id < ids[ids[0]] ) {
947 if ( ids[0] >= MDB_IDL_UM_MAX ) {
956 /* Append sorted list b to sorted list a. The result is unsorted but
957 * a[1] is the min of the result and a[a[0]] is the max.
959 int mdb_idl_append( ID *a, ID *b )
961 ID ida, idb, tmp, swap = 0;
963 if ( MDB_IDL_IS_ZERO( b ) ) {
967 if ( MDB_IDL_IS_ZERO( a ) ) {
972 ida = MDB_IDL_LAST( a );
973 idb = MDB_IDL_LAST( b );
974 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ||
975 a[0] + b[0] >= MDB_IDL_UM_MAX ) {
976 a[2] = IDL_MAX( ida, idb );
977 a[1] = IDL_MIN( a[1], b[1] );
982 if ( b[0] > 1 && ida > idb ) {
999 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
1010 /* Quicksort + Insertion sort for small arrays */
1013 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1016 mdb_idl_sort( ID *ids, ID *tmp )
1018 int *istack = (int *)tmp; /* Private stack, not used by caller */
1019 int i,j,k,l,ir,jstack;
1022 if ( MDB_IDL_IS_RANGE( ids ))
1029 if (ir - l < SMALL) { /* Insertion sort */
1030 for (j=l+1;j<=ir;j++) {
1032 for (i=j-1;i>=1;i--) {
1033 if (ids[i] <= a) break;
1038 if (jstack == 0) break;
1039 ir = istack[jstack--];
1040 l = istack[jstack--];
1042 k = (l + ir) >> 1; /* Choose median of left, center, right */
1043 SWAP(ids[k], ids[l+1]);
1044 if (ids[l] > ids[ir]) {
1045 SWAP(ids[l], ids[ir]);
1047 if (ids[l+1] > ids[ir]) {
1048 SWAP(ids[l+1], ids[ir]);
1050 if (ids[l] > ids[l+1]) {
1051 SWAP(ids[l], ids[l+1]);
1057 do i++; while(ids[i] < a);
1058 do j--; while(ids[j] > a);
1060 SWAP(ids[i],ids[j]);
1065 if (ir-i+1 >= j-1) {
1066 istack[jstack] = ir;
1067 istack[jstack-1] = i;
1070 istack[jstack] = j-1;
1071 istack[jstack-1] = l;
1080 /* 8 bit Radix sort + insertion sort
1082 * based on code from http://www.cubic.org/docs/radix.htm
1083 * with improvements by mbackes@symas.com and hyc@symas.com
1085 * This code is O(n) but has a relatively high constant factor. For lists
1086 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1087 * Much faster than quicksort for lists longer than ~100. Insertion
1088 * sort is actually superior for lists <50.
1091 #define BUCKETS (1<<8)
1095 mdb_idl_sort( ID *ids, ID *tmp )
1097 int count, soft_limit, phase = 0, size = ids[0];
1099 unsigned char *maxv = (unsigned char *)&ids[size];
1101 if ( MDB_IDL_IS_RANGE( ids ))
1104 /* Use insertion sort for small lists */
1105 if ( size <= SMALL ) {
1109 for (j=1;j<=size;j++) {
1111 for (i=j-1;i>=1;i--) {
1112 if (ids[i] <= a) break;
1124 #if BYTE_ORDER == BIG_ENDIAN
1125 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1127 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1131 #if BYTE_ORDER == BIG_ENDIAN
1132 count = sizeof(ID)-1; count >= soft_limit; --count
1134 count = 0; count <= soft_limit; ++count
1137 unsigned int num[BUCKETS], * np, n, sum;
1139 ID *sp, *source, *dest;
1140 unsigned char *bp, *source_start;
1142 source = idls[phase]+1;
1143 dest = idls[phase^1]+1;
1144 source_start = ((unsigned char *) source) + count;
1147 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1149 /* count occurences of every byte value */
1151 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1154 /* transform count into index by summing elements and storing
1159 for ( i = BUCKETS; i > 0; --i ) {
1165 /* fill dest with the right values in the right place */
1168 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1176 /* copy back from temp if needed */
1179 for ( count = 0; count < size; ++count )
1183 #endif /* Quick vs Radix */
1185 unsigned mdb_id2l_search( ID2L ids, ID id )
1188 * binary search of id in ids
1189 * if found, returns position of id
1190 * if not found, returns first position greater than id
1193 unsigned cursor = 1;
1195 unsigned n = ids[0].mid;
1198 unsigned pivot = n >> 1;
1199 cursor = base + pivot + 1;
1200 val = IDL_CMP( id, ids[cursor].mid );
1205 } else if ( val > 0 ) {
1220 int mdb_id2l_insert( ID2L ids, ID2 *id )
1224 x = mdb_id2l_search( ids, id->mid );
1228 /* internal error */
1232 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
1237 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
1244 for (i=ids[0].mid; i>x; i--)