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 )
28 #define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ) )
31 static void idl_check( ID *ids )
33 if( MDB_IDL_IS_RANGE( ids ) ) {
34 assert( MDB_IDL_RANGE_FIRST(ids) <= MDB_IDL_RANGE_LAST(ids) );
37 for( i=1; i < ids[0]; i++ ) {
38 assert( ids[i+1] > ids[i] );
44 static void idl_dump( ID *ids )
46 if( MDB_IDL_IS_RANGE( ids ) ) {
47 Debug( LDAP_DEBUG_ANY,
48 "IDL: range ( %ld - %ld )\n",
49 (long) MDB_IDL_RANGE_FIRST( ids ),
50 (long) MDB_IDL_RANGE_LAST( ids ) );
54 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
56 for( i=1; i<=ids[0]; i++ ) {
58 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
60 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
63 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
68 #endif /* IDL_DEBUG > 1 */
69 #endif /* IDL_DEBUG > 0 */
71 unsigned mdb_idl_search( ID *ids, ID id )
73 #define IDL_BINARY_SEARCH 1
74 #ifdef IDL_BINARY_SEARCH
76 * binary search of id in ids
77 * if found, returns position of id
78 * if not found, returns first postion greater than id
91 cursor = base + pivot;
92 val = IDL_CMP( id, ids[cursor + 1] );
97 } else if ( val > 0 ) {
113 /* (reverse) linear search */
120 for( i=ids[0]; i; i-- ) {
130 int mdb_idl_insert( ID *ids, ID id )
135 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
141 if (MDB_IDL_IS_RANGE( ids )) {
142 /* if already in range, treat as a dup */
143 if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
145 if (id < MDB_IDL_FIRST(ids))
147 else if (id > MDB_IDL_LAST(ids))
152 x = mdb_idl_search( ids, id );
160 if ( x <= ids[0] && ids[x] == id ) {
165 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
168 ids[2] = ids[ids[0]-1];
169 } else if ( ids[ids[0]-1] < id ) {
172 ids[2] = ids[ids[0]-1];
178 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
191 static int mdb_idl_delete( ID *ids, ID id )
196 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
202 if (MDB_IDL_IS_RANGE( ids )) {
203 /* If deleting a range boundary, adjust */
206 else if ( ids[2] == id )
208 /* deleting from inside a range is a no-op */
210 /* If the range has collapsed, re-adjust */
211 if ( ids[1] > ids[2] )
213 else if ( ids[1] == ids[2] )
218 x = mdb_idl_search( ids, id );
226 if( x > ids[0] || ids[x] != id ) {
230 } else if ( --ids[0] == 0 ) {
236 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
254 if ( len == 4 /* LUTIL_HASH_BYTES */ ) {
255 unsigned char *c = val;
256 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
270 MDB_cursor **saved_cursor,
273 MDB_val data, key2, *kptr;
278 MDB_cursor_op opflag;
282 Debug( LDAP_DEBUG_ARGS,
283 "mdb_idl_fetch_key: %s\n",
284 mdb_show_key( keybuf, key->mv_data, key->mv_size ), 0, 0 );
286 assert( ids != NULL );
288 if ( saved_cursor && *saved_cursor ) {
290 } else if ( get_flag == LDAP_FILTER_GE ) {
291 opflag = MDB_SET_RANGE;
292 } else if ( get_flag == LDAP_FILTER_LE ) {
298 /* If we're not reusing an existing cursor, get a new one */
299 if( opflag != MDB_NEXT ) {
300 rc = mdb_cursor_open( txn, dbi, &cursor );
302 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
303 "cursor failed: %s (%d)\n", mdb_strerror(rc), rc, 0 );
307 cursor = *saved_cursor;
310 /* If this is a LE lookup, save original key so we can determine
311 * when to stop. If this is a GE lookup, save the key since it
312 * will be overwritten.
314 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
315 key2.mv_data = keybuf;
316 key2.mv_size = key->mv_size;
317 AC_MEMCPY( keybuf, key->mv_data, key->mv_size );
323 rc = mdb_cursor_get( cursor, kptr, &data, opflag );
325 /* skip presence key on range inequality lookups */
326 while (rc == 0 && kptr->mv_size != len) {
327 rc = mdb_cursor_get( cursor, kptr, &data, MDB_NEXT_NODUP );
329 /* If we're doing a LE compare and the new key is greater than
330 * our search key, we're done
332 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->mv_data,
333 key->mv_data, key->mv_size ) > 0 ) {
338 rc = mdb_cursor_get( cursor, key, &data, MDB_GET_MULTIPLE );
340 memcpy( i, data.mv_data, data.mv_size );
341 i += data.mv_size / sizeof(ID);
342 rc = mdb_cursor_get( cursor, key, &data, MDB_NEXT_MULTIPLE );
344 if ( rc == MDB_NOTFOUND ) rc = 0;
345 ids[0] = i - &ids[1];
346 /* On disk, a range is denoted by 0 in the first element */
348 if (ids[0] != MDB_IDL_RANGE_SIZE) {
349 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
350 "range size mismatch: expected %d, got %ld\n",
351 MDB_IDL_RANGE_SIZE, ids[0], 0 );
352 mdb_cursor_close( cursor );
355 MDB_IDL_RANGE( ids, ids[2], ids[3] );
357 data.mv_size = MDB_IDL_SIZEOF(ids);
360 if ( saved_cursor && rc == 0 ) {
361 if ( !*saved_cursor )
362 *saved_cursor = cursor;
365 mdb_cursor_close( cursor );
367 if( rc == MDB_NOTFOUND ) {
370 } else if( rc != 0 ) {
371 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
372 "get failed: %s (%d)\n",
373 mdb_strerror(rc), rc, 0 );
376 } else if ( data.mv_size == 0 || data.mv_size % sizeof( ID ) ) {
377 /* size not multiple of ID size */
378 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
379 "odd size: expected %ld multiple, got %ld\n",
380 (long) sizeof( ID ), (long) data.mv_size, 0 );
383 } else if ( data.mv_size != MDB_IDL_SIZEOF(ids) ) {
385 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
386 "get size mismatch: expected %ld, got %ld\n",
387 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.mv_size, 0 );
407 Debug( LDAP_DEBUG_ARGS,
408 "mdb_idl_insert_keys: %lx %s\n",
409 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ), 0 );
412 assert( id != NOID );
414 for ( k=0; keys[k].bv_val; k++ ) {
415 /* Fetch the first data item for this key, to see if it
416 * exists and if it's a range.
418 key.mv_size = keys[k].bv_len;
419 key.mv_data = keys[k].bv_val;
420 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
424 memcpy(&lo, data.mv_data, sizeof(ID));
426 /* not a range, count the number of items */
428 rc = mdb_cursor_count( cursor, &count );
433 if ( count >= MDB_IDL_DB_MAX ) {
434 /* No room, convert to a range */
436 rc = mdb_cursor_get( cursor, &key, &data, MDB_LAST_DUP );
437 if ( rc != 0 && rc != MDB_NOTFOUND ) {
438 err = "c_get last_dup";
443 /* Update hi/lo if needed */
446 } else if ( id > hi ) {
449 /* delete the old key */
450 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
455 /* Store the range */
456 data.mv_size = sizeof(ID);
459 rc = mdb_cursor_put( cursor, &key, &data, 0 );
465 rc = mdb_cursor_put( cursor, &key, &data, 0 );
471 rc = mdb_cursor_put( cursor, &key, &data, 0 );
477 /* There's room, just store it */
481 /* It's a range, see if we need to rewrite
486 if ( id < lo || id > hi ) {
488 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
491 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
493 data.mv_size = sizeof(ID);
495 /* Replace the current lo/hi */
496 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
503 } else if ( rc == MDB_NOTFOUND ) {
504 put1: data.mv_data = &id;
505 data.mv_size = sizeof(ID);
506 rc = mdb_cursor_put( cursor, &key, &data, MDB_NODUPDATA );
507 /* Don't worry if it's already there */
508 if ( rc == MDB_KEYEXIST )
515 /* initial c_get failed, nothing was done */
517 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_keys: "
518 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
538 Debug( LDAP_DEBUG_ARGS,
539 "mdb_idl_delete_keys: %lx %s\n",
540 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ), 0 );
542 assert( id != NOID );
544 for ( k=0; keys[k].bv_val; k++) {
545 /* Fetch the first data item for this key, to see if it
546 * exists and if it's a range.
548 key.mv_size = keys[k].bv_len;
549 key.mv_data = keys[k].bv_val;
550 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
553 memcpy( &tmp, data.mv_data, sizeof(ID) );
556 /* Not a range, just delete it */
558 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
563 rc = mdb_cursor_del( cursor, 0 );
569 /* It's a range, see if we need to rewrite
574 if ( id == lo || id == hi ) {
575 ID lo2 = lo, hi2 = hi;
578 } else if ( id == hi ) {
582 /* The range has collapsed... */
583 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
590 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
595 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
598 /* Replace the current lo/hi */
599 data.mv_size = sizeof(ID);
600 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
609 /* initial c_get failed, nothing was done */
611 if ( rc == MDB_NOTFOUND )
614 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
615 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
625 * idl_intersection - return a = a intersection b
628 mdb_idl_intersection(
634 ID cursora = 0, cursorb = 0, cursorc;
637 if ( MDB_IDL_IS_ZERO( a ) || MDB_IDL_IS_ZERO( b ) ) {
642 idmin = IDL_MAX( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
643 idmax = IDL_MIN( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
644 if ( idmin > idmax ) {
647 } else if ( idmin == idmax ) {
653 if ( MDB_IDL_IS_RANGE( a ) ) {
654 if ( MDB_IDL_IS_RANGE(b) ) {
655 /* If both are ranges, just shrink the boundaries */
660 /* Else swap so that b is the range, a is a list */
668 /* If a range completely covers the list, the result is
669 * just the list. If idmin to idmax is contiguous, just
670 * turn it into a range.
672 if ( MDB_IDL_IS_RANGE( b )
673 && MDB_IDL_FIRST( b ) <= MDB_IDL_FIRST( a )
674 && MDB_IDL_LAST( b ) >= MDB_IDL_LAST( a ) ) {
675 if (idmax - idmin + 1 == a[0])
684 /* Fine, do the intersection one element at a time.
685 * First advance to idmin in both IDLs.
687 cursora = cursorb = idmin;
688 ida = mdb_idl_first( a, &cursora );
689 idb = mdb_idl_first( b, &cursorb );
692 while( ida <= idmax || idb <= idmax ) {
695 ida = mdb_idl_next( a, &cursora );
696 idb = mdb_idl_next( b, &cursorb );
697 } else if ( ida < idb ) {
698 ida = mdb_idl_next( a, &cursora );
700 idb = mdb_idl_next( b, &cursorb );
713 * idl_union - return a = a union b
721 ID cursora = 0, cursorb = 0, cursorc;
723 if ( MDB_IDL_IS_ZERO( b ) ) {
727 if ( MDB_IDL_IS_ZERO( a ) ) {
732 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ) {
733 over: ida = IDL_MIN( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
734 idb = IDL_MAX( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
741 ida = mdb_idl_first( a, &cursora );
742 idb = mdb_idl_first( b, &cursorb );
746 /* The distinct elements of a are cat'd to b */
747 while( ida != NOID || idb != NOID ) {
749 if( ++cursorc > MDB_IDL_UM_MAX ) {
753 ida = mdb_idl_next( a, &cursora );
757 ida = mdb_idl_next( a, &cursora );
758 idb = mdb_idl_next( b, &cursorb );
762 /* b is copied back to a in sorted order */
767 while (cursorb <= b[0] || cursorc <= a[0]) {
772 if (cursorb <= b[0] && b[cursorb] < idb)
773 a[cursora++] = b[cursorb++];
786 * mdb_idl_notin - return a intersection ~b (or a minus b)
795 ID cursora = 0, cursorb = 0;
797 if( MDB_IDL_IS_ZERO( a ) ||
798 MDB_IDL_IS_ZERO( b ) ||
799 MDB_IDL_IS_RANGE( b ) )
801 MDB_IDL_CPY( ids, a );
805 if( MDB_IDL_IS_RANGE( a ) ) {
806 MDB_IDL_CPY( ids, a );
810 ida = mdb_idl_first( a, &cursora ),
811 idb = mdb_idl_first( b, &cursorb );
815 while( ida != NOID ) {
817 /* we could shortcut this */
819 ida = mdb_idl_next( a, &cursora );
821 } else if ( ida < idb ) {
823 ida = mdb_idl_next( a, &cursora );
825 } else if ( ida > idb ) {
826 idb = mdb_idl_next( b, &cursorb );
829 ida = mdb_idl_next( a, &cursora );
830 idb = mdb_idl_next( b, &cursorb );
838 ID mdb_idl_first( ID *ids, ID *cursor )
847 if ( MDB_IDL_IS_RANGE( ids ) ) {
848 if( *cursor < ids[1] ) {
857 pos = mdb_idl_search( ids, *cursor );
867 ID mdb_idl_next( ID *ids, ID *cursor )
869 if ( MDB_IDL_IS_RANGE( ids ) ) {
870 if( ids[2] < ++(*cursor) ) {
876 if ( ++(*cursor) <= ids[0] ) {
883 /* Add one ID to an unsorted list. We ensure that the first element is the
884 * minimum and the last element is the maximum, for fast range compaction.
885 * this means IDLs up to length 3 are always sorted...
887 int mdb_idl_append_one( ID *ids, ID id )
889 if (MDB_IDL_IS_RANGE( ids )) {
890 /* if already in range, treat as a dup */
891 if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
893 if (id < MDB_IDL_FIRST(ids))
895 else if (id > MDB_IDL_LAST(ids))
907 if ( ids[0] > 1 && id < ids[ids[0]] ) {
914 if ( ids[0] >= MDB_IDL_UM_MAX ) {
923 /* Append sorted list b to sorted list a. The result is unsorted but
924 * a[1] is the min of the result and a[a[0]] is the max.
926 int mdb_idl_append( ID *a, ID *b )
928 ID ida, idb, tmp, swap = 0;
930 if ( MDB_IDL_IS_ZERO( b ) ) {
934 if ( MDB_IDL_IS_ZERO( a ) ) {
939 ida = MDB_IDL_LAST( a );
940 idb = MDB_IDL_LAST( b );
941 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ||
942 a[0] + b[0] >= MDB_IDL_UM_MAX ) {
943 a[2] = IDL_MAX( ida, idb );
944 a[1] = IDL_MIN( a[1], b[1] );
949 if ( b[0] > 1 && ida > idb ) {
966 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
977 /* Quicksort + Insertion sort for small arrays */
980 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
983 mdb_idl_sort( ID *ids, ID *tmp )
985 int *istack = (int *)tmp;
986 int i,j,k,l,ir,jstack;
989 if ( MDB_IDL_IS_RANGE( ids ))
996 if (ir - l < SMALL) { /* Insertion sort */
997 for (j=l+1;j<=ir;j++) {
999 for (i=j-1;i>=1;i--) {
1000 if (ids[i] <= a) break;
1005 if (jstack == 0) break;
1006 ir = istack[jstack--];
1007 l = istack[jstack--];
1009 k = (l + ir) >> 1; /* Choose median of left, center, right */
1010 SWAP(ids[k], ids[l+1]);
1011 if (ids[l] > ids[ir]) {
1012 SWAP(ids[l], ids[ir]);
1014 if (ids[l+1] > ids[ir]) {
1015 SWAP(ids[l+1], ids[ir]);
1017 if (ids[l] > ids[l+1]) {
1018 SWAP(ids[l], ids[l+1]);
1024 do i++; while(ids[i] < a);
1025 do j--; while(ids[j] > a);
1027 SWAP(ids[i],ids[j]);
1032 if (ir-i+1 >= j-1) {
1033 istack[jstack] = ir;
1034 istack[jstack-1] = i;
1037 istack[jstack] = j-1;
1038 istack[jstack-1] = l;
1047 /* 8 bit Radix sort + insertion sort
1049 * based on code from http://www.cubic.org/docs/radix.htm
1050 * with improvements by mbackes@symas.com and hyc@symas.com
1052 * This code is O(n) but has a relatively high constant factor. For lists
1053 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1054 * Much faster than quicksort for lists longer than ~100. Insertion
1055 * sort is actually superior for lists <50.
1058 #define BUCKETS (1<<8)
1062 mdb_idl_sort( ID *ids, ID *tmp )
1064 int count, soft_limit, phase = 0, size = ids[0];
1066 unsigned char *maxv = (unsigned char *)&ids[size];
1068 if ( MDB_IDL_IS_RANGE( ids ))
1071 /* Use insertion sort for small lists */
1072 if ( size <= SMALL ) {
1076 for (j=1;j<=size;j++) {
1078 for (i=j-1;i>=1;i--) {
1079 if (ids[i] <= a) break;
1091 #if BYTE_ORDER == BIG_ENDIAN
1092 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1094 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1098 #if BYTE_ORDER == BIG_ENDIAN
1099 count = sizeof(ID)-1; count >= soft_limit; --count
1101 count = 0; count <= soft_limit; ++count
1104 unsigned int num[BUCKETS], * np, n, sum;
1106 ID *sp, *source, *dest;
1107 unsigned char *bp, *source_start;
1109 source = idls[phase]+1;
1110 dest = idls[phase^1]+1;
1111 source_start = ((unsigned char *) source) + count;
1114 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1116 /* count occurences of every byte value */
1118 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1121 /* transform count into index by summing elements and storing
1126 for ( i = BUCKETS; i > 0; --i ) {
1132 /* fill dest with the right values in the right place */
1135 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1143 /* copy back from temp if needed */
1146 for ( count = 0; count < size; ++count )
1150 #endif /* Quick vs Radix */
1152 unsigned mdb_id2l_search( ID2L ids, ID id )
1155 * binary search of id in ids
1156 * if found, returns position of id
1157 * if not found, returns first position greater than id
1160 unsigned cursor = 0;
1162 unsigned n = ids[0].mid;
1166 cursor = base + pivot;
1167 val = IDL_CMP( id, ids[cursor + 1].mid );
1172 } else if ( val > 0 ) {
1188 int mdb_id2l_insert( ID2L ids, ID2 *id )
1192 x = mdb_id2l_search( ids, id->mid );
1196 /* internal error */
1200 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
1205 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
1212 for (i=ids[0].mid; i>x; i--)