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 ? 1 : 0 ) )
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) );
253 if ( key->mv_size == 4 /* LUTIL_HASH_BYTES */ ) {
254 unsigned char *c = key->mv_data;
255 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
269 MDB_cursor **saved_cursor,
272 MDB_val data, key2, *kptr;
277 MDB_cursor_op opflag;
281 Debug( LDAP_DEBUG_ARGS,
282 "mdb_idl_fetch_key: %s\n",
283 mdb_show_key( key, keybuf ), 0, 0 );
285 assert( ids != NULL );
287 if ( saved_cursor && *saved_cursor ) {
289 } else if ( get_flag == LDAP_FILTER_GE ) {
290 opflag = MDB_SET_RANGE;
291 } else if ( get_flag == LDAP_FILTER_LE ) {
297 /* If we're not reusing an existing cursor, get a new one */
298 if( opflag != MDB_NEXT ) {
299 rc = mdb_cursor_open( txn, dbi, &cursor );
301 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
302 "cursor failed: %s (%d)\n", mdb_strerror(rc), rc, 0 );
306 cursor = *saved_cursor;
309 /* If this is a LE lookup, save original key so we can determine
310 * when to stop. If this is a GE lookup, save the key since it
311 * will be overwritten.
313 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
314 key2.mv_data = keybuf;
315 key2.mv_size = key->mv_size;
316 AC_MEMCPY( keybuf, key->mv_data, key->mv_size );
322 rc = mdb_cursor_get( cursor, kptr, &data, opflag );
324 /* skip presence key on range inequality lookups */
325 while (rc == 0 && kptr->mv_size != len) {
326 rc = mdb_cursor_get( cursor, kptr, &data, MDB_NEXT_NODUP );
328 /* If we're doing a LE compare and the new key is greater than
329 * our search key, we're done
331 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->mv_data,
332 key->mv_data, key->mv_size ) > 0 ) {
337 rc = mdb_cursor_get( cursor, key, &data, MDB_GET_MULTIPLE );
339 memcpy( i, data.mv_data, data.mv_size );
340 i += data.mv_size / sizeof(ID);
341 rc = mdb_cursor_get( cursor, key, &data, MDB_NEXT_MULTIPLE );
343 if ( rc == MDB_NOTFOUND ) rc = 0;
344 ids[0] = i - &ids[1];
345 /* On disk, a range is denoted by 0 in the first element */
347 if (ids[0] != MDB_IDL_RANGE_SIZE) {
348 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
349 "range size mismatch: expected %d, got %ld\n",
350 MDB_IDL_RANGE_SIZE, ids[0], 0 );
351 mdb_cursor_close( cursor );
354 MDB_IDL_RANGE( ids, ids[2], ids[3] );
356 data.mv_size = MDB_IDL_SIZEOF(ids);
359 if ( saved_cursor && rc == 0 ) {
360 if ( !*saved_cursor )
361 *saved_cursor = cursor;
364 mdb_cursor_close( cursor );
366 if( rc == MDB_NOTFOUND ) {
369 } else if( rc != 0 ) {
370 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
371 "get failed: %s (%d)\n",
372 mdb_strerror(rc), rc, 0 );
375 } else if ( data.mv_size == 0 || data.mv_size % sizeof( ID ) ) {
376 /* size not multiple of ID size */
377 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
378 "odd size: expected %ld multiple, got %ld\n",
379 (long) sizeof( ID ), (long) data.mv_size, 0 );
382 } else if ( data.mv_size != MDB_IDL_SIZEOF(ids) ) {
384 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
385 "get size mismatch: expected %ld, got %ld\n",
386 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.mv_size, 0 );
409 Debug( LDAP_DEBUG_ARGS,
410 "mdb_idl_insert_key: %lx %s\n",
411 (long) id, mdb_show_key( key, buf ), 0 );
414 assert( id != NOID );
416 rc = mdb_cursor_open( txn, dbi, &cursor );
418 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_key: "
419 "cursor failed: %s (%d)\n", mdb_strerror(rc), rc, 0 );
422 /* Fetch the first data item for this key, to see if it
423 * exists and if it's a range.
425 rc = mdb_cursor_get( cursor, key, &data, MDB_SET );
429 memcpy(&lo, data.mv_data, sizeof(ID));
431 /* not a range, count the number of items */
433 rc = mdb_cursor_count( cursor, &count );
438 if ( count >= MDB_IDL_DB_MAX ) {
439 /* No room, convert to a range */
443 rc = mdb_cursor_get( cursor, &key2, &data, MDB_NEXT_NODUP );
444 if ( rc != 0 && rc != MDB_NOTFOUND ) {
445 err = "c_get next_nodup";
448 if ( rc == MDB_NOTFOUND ) {
449 rc = mdb_cursor_get( cursor, key, &data, MDB_LAST );
455 rc = mdb_cursor_get( cursor, key, &data, MDB_PREV );
463 /* Update hi/lo if needed */
466 } else if ( id > hi ) {
469 /* delete the old key */
470 rc = mdb_del( txn, dbi, key, NULL );
475 /* Store the range */
476 data.mv_size = sizeof(ID);
479 rc = mdb_put( txn, dbi, key, &data, 0 );
481 err = "mdb_put range";
485 rc = mdb_put( txn, dbi, key, &data, 0 );
491 rc = mdb_put( txn, dbi, key, &data, 0 );
497 /* There's room, just store it */
501 /* It's a range, see if we need to rewrite
506 if ( id < lo || id > hi ) {
511 data.mv_size = sizeof(ID);
512 /* Delete the current lo/hi */
513 rc = mdb_del( txn, dbi, key, &data );
515 err = "mdb_del lo/hi";
521 } else if ( rc == MDB_NOTFOUND ) {
522 put1: data.mv_data = &id;
523 data.mv_size = sizeof(ID);
524 rc = mdb_put( txn, dbi, key, &data, MDB_NODUPDATA );
525 /* Don't worry if it's already there */
526 if ( rc != 0 && rc != MDB_KEYEXIST ) {
531 /* initial c_get failed, nothing was done */
533 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_key: "
534 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
536 mdb_cursor_close( cursor );
556 Debug( LDAP_DEBUG_ARGS,
557 "mdb_idl_delete_key: %lx %s\n",
558 (long) id, mdb_show_key( key, buf ), 0 );
560 assert( id != NOID );
562 rc = mdb_cursor_open( txn, dbi, &cursor );
564 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
565 "cursor failed: %s (%d)\n", mdb_strerror(rc), rc, 0 );
568 /* Fetch the first data item for this key, to see if it
569 * exists and if it's a range.
571 rc = mdb_cursor_get( cursor, key, &data, MDB_SET );
574 memcpy( &tmp, data.mv_data, sizeof(ID) );
577 /* Not a range, just delete it */
579 rc = mdb_del( txn, dbi, key, &data );
585 /* It's a range, see if we need to rewrite
590 if ( id == lo || id == hi ) {
591 ID lo2 = lo, hi2 = hi;
594 } else if ( id == hi ) {
598 /* The range has collapsed... */
599 rc = mdb_del( txn, dbi, key, NULL );
605 data.mv_size = sizeof(ID);
610 rc = mdb_del( txn, dbi, key, &data );
619 rc = mdb_put( txn, dbi, key, &data, 0 );
621 err = "mdb_put lo/hi";
628 /* initial c_get failed, nothing was done */
630 if ( rc != MDB_NOTFOUND ) {
631 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
632 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
635 mdb_cursor_close( cursor );
641 * idl_intersection - return a = a intersection b
644 mdb_idl_intersection(
650 ID cursora = 0, cursorb = 0, cursorc;
653 if ( MDB_IDL_IS_ZERO( a ) || MDB_IDL_IS_ZERO( b ) ) {
658 idmin = IDL_MAX( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
659 idmax = IDL_MIN( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
660 if ( idmin > idmax ) {
663 } else if ( idmin == idmax ) {
669 if ( MDB_IDL_IS_RANGE( a ) ) {
670 if ( MDB_IDL_IS_RANGE(b) ) {
671 /* If both are ranges, just shrink the boundaries */
676 /* Else swap so that b is the range, a is a list */
684 /* If a range completely covers the list, the result is
685 * just the list. If idmin to idmax is contiguous, just
686 * turn it into a range.
688 if ( MDB_IDL_IS_RANGE( b )
689 && MDB_IDL_FIRST( b ) <= MDB_IDL_FIRST( a )
690 && MDB_IDL_LAST( b ) >= MDB_IDL_LAST( a ) ) {
691 if (idmax - idmin + 1 == a[0])
700 /* Fine, do the intersection one element at a time.
701 * First advance to idmin in both IDLs.
703 cursora = cursorb = idmin;
704 ida = mdb_idl_first( a, &cursora );
705 idb = mdb_idl_first( b, &cursorb );
708 while( ida <= idmax || idb <= idmax ) {
711 ida = mdb_idl_next( a, &cursora );
712 idb = mdb_idl_next( b, &cursorb );
713 } else if ( ida < idb ) {
714 ida = mdb_idl_next( a, &cursora );
716 idb = mdb_idl_next( b, &cursorb );
729 * idl_union - return a = a union b
737 ID cursora = 0, cursorb = 0, cursorc;
739 if ( MDB_IDL_IS_ZERO( b ) ) {
743 if ( MDB_IDL_IS_ZERO( a ) ) {
748 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ) {
749 over: ida = IDL_MIN( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
750 idb = IDL_MAX( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
757 ida = mdb_idl_first( a, &cursora );
758 idb = mdb_idl_first( b, &cursorb );
762 /* The distinct elements of a are cat'd to b */
763 while( ida != NOID || idb != NOID ) {
765 if( ++cursorc > MDB_IDL_UM_MAX ) {
769 ida = mdb_idl_next( a, &cursora );
773 ida = mdb_idl_next( a, &cursora );
774 idb = mdb_idl_next( b, &cursorb );
778 /* b is copied back to a in sorted order */
783 while (cursorb <= b[0] || cursorc <= a[0]) {
788 if (cursorb <= b[0] && b[cursorb] < idb)
789 a[cursora++] = b[cursorb++];
802 * mdb_idl_notin - return a intersection ~b (or a minus b)
811 ID cursora = 0, cursorb = 0;
813 if( MDB_IDL_IS_ZERO( a ) ||
814 MDB_IDL_IS_ZERO( b ) ||
815 MDB_IDL_IS_RANGE( b ) )
817 MDB_IDL_CPY( ids, a );
821 if( MDB_IDL_IS_RANGE( a ) ) {
822 MDB_IDL_CPY( ids, a );
826 ida = mdb_idl_first( a, &cursora ),
827 idb = mdb_idl_first( b, &cursorb );
831 while( ida != NOID ) {
833 /* we could shortcut this */
835 ida = mdb_idl_next( a, &cursora );
837 } else if ( ida < idb ) {
839 ida = mdb_idl_next( a, &cursora );
841 } else if ( ida > idb ) {
842 idb = mdb_idl_next( b, &cursorb );
845 ida = mdb_idl_next( a, &cursora );
846 idb = mdb_idl_next( b, &cursorb );
854 ID mdb_idl_first( ID *ids, ID *cursor )
863 if ( MDB_IDL_IS_RANGE( ids ) ) {
864 if( *cursor < ids[1] ) {
873 pos = mdb_idl_search( ids, *cursor );
883 ID mdb_idl_next( ID *ids, ID *cursor )
885 if ( MDB_IDL_IS_RANGE( ids ) ) {
886 if( ids[2] < ++(*cursor) ) {
892 if ( ++(*cursor) <= ids[0] ) {
899 /* Add one ID to an unsorted list. We ensure that the first element is the
900 * minimum and the last element is the maximum, for fast range compaction.
901 * this means IDLs up to length 3 are always sorted...
903 int mdb_idl_append_one( ID *ids, ID id )
905 if (MDB_IDL_IS_RANGE( ids )) {
906 /* if already in range, treat as a dup */
907 if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
909 if (id < MDB_IDL_FIRST(ids))
911 else if (id > MDB_IDL_LAST(ids))
923 if ( ids[0] > 1 && id < ids[ids[0]] ) {
930 if ( ids[0] >= MDB_IDL_UM_MAX ) {
939 /* Append sorted list b to sorted list a. The result is unsorted but
940 * a[1] is the min of the result and a[a[0]] is the max.
942 int mdb_idl_append( ID *a, ID *b )
944 ID ida, idb, tmp, swap = 0;
946 if ( MDB_IDL_IS_ZERO( b ) ) {
950 if ( MDB_IDL_IS_ZERO( a ) ) {
955 ida = MDB_IDL_LAST( a );
956 idb = MDB_IDL_LAST( b );
957 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ||
958 a[0] + b[0] >= MDB_IDL_UM_MAX ) {
959 a[2] = IDL_MAX( ida, idb );
960 a[1] = IDL_MIN( a[1], b[1] );
965 if ( b[0] > 1 && ida > idb ) {
982 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
993 /* Quicksort + Insertion sort for small arrays */
996 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
999 mdb_idl_sort( ID *ids, ID *tmp )
1001 int *istack = (int *)tmp;
1002 int i,j,k,l,ir,jstack;
1005 if ( MDB_IDL_IS_RANGE( ids ))
1012 if (ir - l < SMALL) { /* Insertion sort */
1013 for (j=l+1;j<=ir;j++) {
1015 for (i=j-1;i>=1;i--) {
1016 if (ids[i] <= a) break;
1021 if (jstack == 0) break;
1022 ir = istack[jstack--];
1023 l = istack[jstack--];
1025 k = (l + ir) >> 1; /* Choose median of left, center, right */
1026 SWAP(ids[k], ids[l+1]);
1027 if (ids[l] > ids[ir]) {
1028 SWAP(ids[l], ids[ir]);
1030 if (ids[l+1] > ids[ir]) {
1031 SWAP(ids[l+1], ids[ir]);
1033 if (ids[l] > ids[l+1]) {
1034 SWAP(ids[l], ids[l+1]);
1040 do i++; while(ids[i] < a);
1041 do j--; while(ids[j] > a);
1043 SWAP(ids[i],ids[j]);
1048 if (ir-i+1 >= j-1) {
1049 istack[jstack] = ir;
1050 istack[jstack-1] = i;
1053 istack[jstack] = j-1;
1054 istack[jstack-1] = l;
1063 /* 8 bit Radix sort + insertion sort
1065 * based on code from http://www.cubic.org/docs/radix.htm
1066 * with improvements by mbackes@symas.com and hyc@symas.com
1068 * This code is O(n) but has a relatively high constant factor. For lists
1069 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1070 * Much faster than quicksort for lists longer than ~100. Insertion
1071 * sort is actually superior for lists <50.
1074 #define BUCKETS (1<<8)
1078 mdb_idl_sort( ID *ids, ID *tmp )
1080 int count, soft_limit, phase = 0, size = ids[0];
1082 unsigned char *maxv = (unsigned char *)&ids[size];
1084 if ( MDB_IDL_IS_RANGE( ids ))
1087 /* Use insertion sort for small lists */
1088 if ( size <= SMALL ) {
1092 for (j=1;j<=size;j++) {
1094 for (i=j-1;i>=1;i--) {
1095 if (ids[i] <= a) break;
1107 #if BYTE_ORDER == BIG_ENDIAN
1108 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1110 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1114 #if BYTE_ORDER == BIG_ENDIAN
1115 count = sizeof(ID)-1; count >= soft_limit; --count
1117 count = 0; count <= soft_limit; ++count
1120 unsigned int num[BUCKETS], * np, n, sum;
1122 ID *sp, *source, *dest;
1123 unsigned char *bp, *source_start;
1125 source = idls[phase]+1;
1126 dest = idls[phase^1]+1;
1127 source_start = ((unsigned char *) source) + count;
1130 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1132 /* count occurences of every byte value */
1134 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1137 /* transform count into index by summing elements and storing
1142 for ( i = BUCKETS; i > 0; --i ) {
1148 /* fill dest with the right values in the right place */
1151 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1159 /* copy back from temp if needed */
1162 for ( count = 0; count < size; ++count )
1166 #endif /* Quick vs Radix */