1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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 );
405 Debug( LDAP_DEBUG_ARGS,
406 "mdb_idl_insert_keys: %lx %s\n",
407 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ), 0 );
410 assert( id != NOID );
412 for ( k=0; keys[k].bv_val; k++ ) {
413 /* Fetch the first data item for this key, to see if it
414 * exists and if it's a range.
416 key.mv_size = keys[k].bv_len;
417 key.mv_data = keys[k].bv_val;
418 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
422 memcpy(&lo, data.mv_data, sizeof(ID));
424 /* not a range, count the number of items */
426 rc = mdb_cursor_count( cursor, &count );
431 if ( count >= MDB_IDL_DB_MAX ) {
432 /* No room, convert to a range */
434 rc = mdb_cursor_get( cursor, &key, &data, MDB_LAST_DUP );
435 if ( rc != 0 && rc != MDB_NOTFOUND ) {
436 err = "c_get last_dup";
441 /* Update hi/lo if needed */
444 } else if ( id > hi ) {
447 /* delete the old key */
448 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
453 /* Store the range */
454 data.mv_size = sizeof(ID);
457 rc = mdb_cursor_put( cursor, &key, &data, 0 );
463 rc = mdb_cursor_put( cursor, &key, &data, 0 );
469 rc = mdb_cursor_put( cursor, &key, &data, 0 );
475 /* There's room, just store it */
479 /* It's a range, see if we need to rewrite
484 if ( id < lo || id > hi ) {
486 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
489 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
491 data.mv_size = sizeof(ID);
493 /* Replace the current lo/hi */
494 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
501 } else if ( rc == MDB_NOTFOUND ) {
502 put1: data.mv_data = &id;
503 data.mv_size = sizeof(ID);
504 rc = mdb_cursor_put( cursor, &key, &data, MDB_NODUPDATA );
505 /* Don't worry if it's already there */
506 if ( rc == MDB_KEYEXIST )
513 /* initial c_get failed, nothing was done */
515 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_keys: "
516 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
536 Debug( LDAP_DEBUG_ARGS,
537 "mdb_idl_delete_keys: %lx %s\n",
538 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ), 0 );
540 assert( id != NOID );
542 for ( k=0; keys[k].bv_val; k++) {
543 /* Fetch the first data item for this key, to see if it
544 * exists and if it's a range.
546 key.mv_size = keys[k].bv_len;
547 key.mv_data = keys[k].bv_val;
548 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
551 memcpy( &tmp, data.mv_data, sizeof(ID) );
554 /* Not a range, just delete it */
556 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
561 rc = mdb_cursor_del( cursor, 0 );
567 /* It's a range, see if we need to rewrite
572 if ( id == lo || id == hi ) {
573 ID lo2 = lo, hi2 = hi;
576 } else if ( id == hi ) {
580 /* The range has collapsed... */
581 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
588 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
593 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
596 /* Replace the current lo/hi */
597 data.mv_size = sizeof(ID);
598 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
607 /* initial c_get failed, nothing was done */
609 if ( rc == MDB_NOTFOUND )
612 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
613 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
623 * idl_intersection - return a = a intersection b
626 mdb_idl_intersection(
632 ID cursora = 0, cursorb = 0, cursorc;
635 if ( MDB_IDL_IS_ZERO( a ) || MDB_IDL_IS_ZERO( b ) ) {
640 idmin = IDL_MAX( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
641 idmax = IDL_MIN( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
642 if ( idmin > idmax ) {
645 } else if ( idmin == idmax ) {
651 if ( MDB_IDL_IS_RANGE( a ) ) {
652 if ( MDB_IDL_IS_RANGE(b) ) {
653 /* If both are ranges, just shrink the boundaries */
658 /* Else swap so that b is the range, a is a list */
666 /* If a range completely covers the list, the result is
667 * just the list. If idmin to idmax is contiguous, just
668 * turn it into a range.
670 if ( MDB_IDL_IS_RANGE( b )
671 && MDB_IDL_RANGE_FIRST( b ) <= MDB_IDL_RANGE_FIRST( a )
672 && MDB_IDL_RANGE_LAST( b ) >= MDB_IDL_RANGE_LAST( a ) ) {
673 if (idmax - idmin + 1 == a[0])
682 /* Fine, do the intersection one element at a time.
683 * First advance to idmin in both IDLs.
685 cursora = cursorb = idmin;
686 ida = mdb_idl_first( a, &cursora );
687 idb = mdb_idl_first( b, &cursorb );
690 while( ida <= idmax || idb <= idmax ) {
693 ida = mdb_idl_next( a, &cursora );
694 idb = mdb_idl_next( b, &cursorb );
695 } else if ( ida < idb ) {
696 ida = mdb_idl_next( a, &cursora );
698 idb = mdb_idl_next( b, &cursorb );
711 * idl_union - return a = a union b
719 ID cursora = 0, cursorb = 0, cursorc;
721 if ( MDB_IDL_IS_ZERO( b ) ) {
725 if ( MDB_IDL_IS_ZERO( a ) ) {
730 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ) {
731 over: ida = IDL_MIN( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
732 idb = IDL_MAX( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
739 ida = mdb_idl_first( a, &cursora );
740 idb = mdb_idl_first( b, &cursorb );
744 /* The distinct elements of a are cat'd to b */
745 while( ida != NOID || idb != NOID ) {
747 if( ++cursorc > MDB_IDL_UM_MAX ) {
751 ida = mdb_idl_next( a, &cursora );
755 ida = mdb_idl_next( a, &cursora );
756 idb = mdb_idl_next( b, &cursorb );
760 /* b is copied back to a in sorted order */
765 while (cursorb <= b[0] || cursorc <= a[0]) {
770 if (cursorb <= b[0] && b[cursorb] < idb)
771 a[cursora++] = b[cursorb++];
784 * mdb_idl_notin - return a intersection ~b (or a minus b)
793 ID cursora = 0, cursorb = 0;
795 if( MDB_IDL_IS_ZERO( a ) ||
796 MDB_IDL_IS_ZERO( b ) ||
797 MDB_IDL_IS_RANGE( b ) )
799 MDB_IDL_CPY( ids, a );
803 if( MDB_IDL_IS_RANGE( a ) ) {
804 MDB_IDL_CPY( ids, a );
808 ida = mdb_idl_first( a, &cursora ),
809 idb = mdb_idl_first( b, &cursorb );
813 while( ida != NOID ) {
815 /* we could shortcut this */
817 ida = mdb_idl_next( a, &cursora );
819 } else if ( ida < idb ) {
821 ida = mdb_idl_next( a, &cursora );
823 } else if ( ida > idb ) {
824 idb = mdb_idl_next( b, &cursorb );
827 ida = mdb_idl_next( a, &cursora );
828 idb = mdb_idl_next( b, &cursorb );
836 ID mdb_idl_first( ID *ids, ID *cursor )
845 if ( MDB_IDL_IS_RANGE( ids ) ) {
846 if( *cursor < ids[1] ) {
855 pos = mdb_idl_search( ids, *cursor );
865 ID mdb_idl_next( ID *ids, ID *cursor )
867 if ( MDB_IDL_IS_RANGE( ids ) ) {
868 if( ids[2] < ++(*cursor) ) {
874 if ( ++(*cursor) <= ids[0] ) {
881 /* Add one ID to an unsorted list. We ensure that the first element is the
882 * minimum and the last element is the maximum, for fast range compaction.
883 * this means IDLs up to length 3 are always sorted...
885 int mdb_idl_append_one( ID *ids, ID id )
887 if (MDB_IDL_IS_RANGE( ids )) {
888 /* if already in range, treat as a dup */
889 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
891 if (id < MDB_IDL_RANGE_FIRST(ids))
893 else if (id > MDB_IDL_RANGE_LAST(ids))
905 if ( ids[0] > 1 && id < ids[ids[0]] ) {
912 if ( ids[0] >= MDB_IDL_UM_MAX ) {
921 /* Append sorted list b to sorted list a. The result is unsorted but
922 * a[1] is the min of the result and a[a[0]] is the max.
924 int mdb_idl_append( ID *a, ID *b )
926 ID ida, idb, tmp, swap = 0;
928 if ( MDB_IDL_IS_ZERO( b ) ) {
932 if ( MDB_IDL_IS_ZERO( a ) ) {
937 ida = MDB_IDL_LAST( a );
938 idb = MDB_IDL_LAST( b );
939 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ||
940 a[0] + b[0] >= MDB_IDL_UM_MAX ) {
941 a[2] = IDL_MAX( ida, idb );
942 a[1] = IDL_MIN( a[1], b[1] );
947 if ( b[0] > 1 && ida > idb ) {
964 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
975 /* Quicksort + Insertion sort for small arrays */
978 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
981 mdb_idl_sort( ID *ids, ID *tmp )
983 int *istack = (int *)tmp; /* Private stack, not used by caller */
984 int i,j,k,l,ir,jstack;
987 if ( MDB_IDL_IS_RANGE( ids ))
994 if (ir - l < SMALL) { /* Insertion sort */
995 for (j=l+1;j<=ir;j++) {
997 for (i=j-1;i>=1;i--) {
998 if (ids[i] <= a) break;
1003 if (jstack == 0) break;
1004 ir = istack[jstack--];
1005 l = istack[jstack--];
1007 k = (l + ir) >> 1; /* Choose median of left, center, right */
1008 SWAP(ids[k], ids[l+1]);
1009 if (ids[l] > ids[ir]) {
1010 SWAP(ids[l], ids[ir]);
1012 if (ids[l+1] > ids[ir]) {
1013 SWAP(ids[l+1], ids[ir]);
1015 if (ids[l] > ids[l+1]) {
1016 SWAP(ids[l], ids[l+1]);
1022 do i++; while(ids[i] < a);
1023 do j--; while(ids[j] > a);
1025 SWAP(ids[i],ids[j]);
1030 if (ir-i+1 >= j-1) {
1031 istack[jstack] = ir;
1032 istack[jstack-1] = i;
1035 istack[jstack] = j-1;
1036 istack[jstack-1] = l;
1045 /* 8 bit Radix sort + insertion sort
1047 * based on code from http://www.cubic.org/docs/radix.htm
1048 * with improvements by mbackes@symas.com and hyc@symas.com
1050 * This code is O(n) but has a relatively high constant factor. For lists
1051 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1052 * Much faster than quicksort for lists longer than ~100. Insertion
1053 * sort is actually superior for lists <50.
1056 #define BUCKETS (1<<8)
1060 mdb_idl_sort( ID *ids, ID *tmp )
1062 int count, soft_limit, phase = 0, size = ids[0];
1064 unsigned char *maxv = (unsigned char *)&ids[size];
1066 if ( MDB_IDL_IS_RANGE( ids ))
1069 /* Use insertion sort for small lists */
1070 if ( size <= SMALL ) {
1074 for (j=1;j<=size;j++) {
1076 for (i=j-1;i>=1;i--) {
1077 if (ids[i] <= a) break;
1089 #if BYTE_ORDER == BIG_ENDIAN
1090 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1092 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1096 #if BYTE_ORDER == BIG_ENDIAN
1097 count = sizeof(ID)-1; count >= soft_limit; --count
1099 count = 0; count <= soft_limit; ++count
1102 unsigned int num[BUCKETS], * np, n, sum;
1104 ID *sp, *source, *dest;
1105 unsigned char *bp, *source_start;
1107 source = idls[phase]+1;
1108 dest = idls[phase^1]+1;
1109 source_start = ((unsigned char *) source) + count;
1112 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1114 /* count occurences of every byte value */
1116 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1119 /* transform count into index by summing elements and storing
1124 for ( i = BUCKETS; i > 0; --i ) {
1130 /* fill dest with the right values in the right place */
1133 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1141 /* copy back from temp if needed */
1144 for ( count = 0; count < size; ++count )
1148 #endif /* Quick vs Radix */
1150 unsigned mdb_id2l_search( ID2L ids, ID id )
1153 * binary search of id in ids
1154 * if found, returns position of id
1155 * if not found, returns first position greater than id
1158 unsigned cursor = 1;
1160 unsigned n = ids[0].mid;
1163 unsigned pivot = n >> 1;
1164 cursor = base + pivot + 1;
1165 val = IDL_CMP( id, ids[cursor].mid );
1170 } else if ( val > 0 ) {
1185 int mdb_id2l_insert( ID2L ids, ID2 *id )
1189 x = mdb_id2l_search( ids, id->mid );
1193 /* internal error */
1197 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
1202 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
1209 for (i=ids[0].mid; i>x; i--)