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";
449 if ( rc == MDB_NOTFOUND ) {
450 rc = mdb_cursor_get( cursor, &key2, &data, MDB_LAST );
456 rc = mdb_cursor_get( cursor, &key2, &data, MDB_PREV );
464 /* Update hi/lo if needed */
467 } else if ( id > hi ) {
470 /* delete the old key */
471 rc = mdb_del( txn, dbi, &key2, NULL );
476 /* Store the range */
477 data.mv_size = sizeof(ID);
480 rc = mdb_put( txn, dbi, key, &data, 0 );
482 err = "mdb_put range";
486 rc = mdb_put( txn, dbi, key, &data, 0 );
492 rc = mdb_put( txn, dbi, key, &data, 0 );
498 /* There's room, just store it */
502 /* It's a range, see if we need to rewrite
507 if ( id < lo || id > hi ) {
512 data.mv_size = sizeof(ID);
513 /* Delete the current lo/hi */
514 rc = mdb_del( txn, dbi, key, &data );
516 err = "mdb_del lo/hi";
522 } else if ( rc == MDB_NOTFOUND ) {
523 put1: data.mv_data = &id;
524 data.mv_size = sizeof(ID);
525 rc = mdb_put( txn, dbi, key, &data, MDB_NODUPDATA );
526 /* Don't worry if it's already there */
527 if ( rc != 0 && rc != MDB_KEYEXIST ) {
532 /* initial c_get failed, nothing was done */
534 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_key: "
535 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
537 mdb_cursor_close( cursor );
557 Debug( LDAP_DEBUG_ARGS,
558 "mdb_idl_delete_key: %lx %s\n",
559 (long) id, mdb_show_key( key, buf ), 0 );
561 assert( id != NOID );
563 rc = mdb_cursor_open( txn, dbi, &cursor );
565 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
566 "cursor failed: %s (%d)\n", mdb_strerror(rc), rc, 0 );
569 /* Fetch the first data item for this key, to see if it
570 * exists and if it's a range.
572 rc = mdb_cursor_get( cursor, key, &data, MDB_SET );
575 memcpy( &tmp, data.mv_data, sizeof(ID) );
578 /* Not a range, just delete it */
580 rc = mdb_del( txn, dbi, key, &data );
586 /* It's a range, see if we need to rewrite
591 if ( id == lo || id == hi ) {
592 ID lo2 = lo, hi2 = hi;
595 } else if ( id == hi ) {
599 /* The range has collapsed... */
600 rc = mdb_del( txn, dbi, key, NULL );
606 data.mv_size = sizeof(ID);
611 rc = mdb_del( txn, dbi, key, &data );
620 rc = mdb_put( txn, dbi, key, &data, 0 );
622 err = "mdb_put lo/hi";
629 /* initial c_get failed, nothing was done */
631 if ( rc != MDB_NOTFOUND ) {
632 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
633 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
636 mdb_cursor_close( cursor );
642 * idl_intersection - return a = a intersection b
645 mdb_idl_intersection(
651 ID cursora = 0, cursorb = 0, cursorc;
654 if ( MDB_IDL_IS_ZERO( a ) || MDB_IDL_IS_ZERO( b ) ) {
659 idmin = IDL_MAX( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
660 idmax = IDL_MIN( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
661 if ( idmin > idmax ) {
664 } else if ( idmin == idmax ) {
670 if ( MDB_IDL_IS_RANGE( a ) ) {
671 if ( MDB_IDL_IS_RANGE(b) ) {
672 /* If both are ranges, just shrink the boundaries */
677 /* Else swap so that b is the range, a is a list */
685 /* If a range completely covers the list, the result is
686 * just the list. If idmin to idmax is contiguous, just
687 * turn it into a range.
689 if ( MDB_IDL_IS_RANGE( b )
690 && MDB_IDL_FIRST( b ) <= MDB_IDL_FIRST( a )
691 && MDB_IDL_LAST( b ) >= MDB_IDL_LAST( a ) ) {
692 if (idmax - idmin + 1 == a[0])
701 /* Fine, do the intersection one element at a time.
702 * First advance to idmin in both IDLs.
704 cursora = cursorb = idmin;
705 ida = mdb_idl_first( a, &cursora );
706 idb = mdb_idl_first( b, &cursorb );
709 while( ida <= idmax || idb <= idmax ) {
712 ida = mdb_idl_next( a, &cursora );
713 idb = mdb_idl_next( b, &cursorb );
714 } else if ( ida < idb ) {
715 ida = mdb_idl_next( a, &cursora );
717 idb = mdb_idl_next( b, &cursorb );
730 * idl_union - return a = a union b
738 ID cursora = 0, cursorb = 0, cursorc;
740 if ( MDB_IDL_IS_ZERO( b ) ) {
744 if ( MDB_IDL_IS_ZERO( a ) ) {
749 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ) {
750 over: ida = IDL_MIN( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
751 idb = IDL_MAX( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
758 ida = mdb_idl_first( a, &cursora );
759 idb = mdb_idl_first( b, &cursorb );
763 /* The distinct elements of a are cat'd to b */
764 while( ida != NOID || idb != NOID ) {
766 if( ++cursorc > MDB_IDL_UM_MAX ) {
770 ida = mdb_idl_next( a, &cursora );
774 ida = mdb_idl_next( a, &cursora );
775 idb = mdb_idl_next( b, &cursorb );
779 /* b is copied back to a in sorted order */
784 while (cursorb <= b[0] || cursorc <= a[0]) {
789 if (cursorb <= b[0] && b[cursorb] < idb)
790 a[cursora++] = b[cursorb++];
803 * mdb_idl_notin - return a intersection ~b (or a minus b)
812 ID cursora = 0, cursorb = 0;
814 if( MDB_IDL_IS_ZERO( a ) ||
815 MDB_IDL_IS_ZERO( b ) ||
816 MDB_IDL_IS_RANGE( b ) )
818 MDB_IDL_CPY( ids, a );
822 if( MDB_IDL_IS_RANGE( a ) ) {
823 MDB_IDL_CPY( ids, a );
827 ida = mdb_idl_first( a, &cursora ),
828 idb = mdb_idl_first( b, &cursorb );
832 while( ida != NOID ) {
834 /* we could shortcut this */
836 ida = mdb_idl_next( a, &cursora );
838 } else if ( ida < idb ) {
840 ida = mdb_idl_next( a, &cursora );
842 } else if ( ida > idb ) {
843 idb = mdb_idl_next( b, &cursorb );
846 ida = mdb_idl_next( a, &cursora );
847 idb = mdb_idl_next( b, &cursorb );
855 ID mdb_idl_first( ID *ids, ID *cursor )
864 if ( MDB_IDL_IS_RANGE( ids ) ) {
865 if( *cursor < ids[1] ) {
874 pos = mdb_idl_search( ids, *cursor );
884 ID mdb_idl_next( ID *ids, ID *cursor )
886 if ( MDB_IDL_IS_RANGE( ids ) ) {
887 if( ids[2] < ++(*cursor) ) {
893 if ( ++(*cursor) <= ids[0] ) {
900 /* Add one ID to an unsorted list. We ensure that the first element is the
901 * minimum and the last element is the maximum, for fast range compaction.
902 * this means IDLs up to length 3 are always sorted...
904 int mdb_idl_append_one( ID *ids, ID id )
906 if (MDB_IDL_IS_RANGE( ids )) {
907 /* if already in range, treat as a dup */
908 if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
910 if (id < MDB_IDL_FIRST(ids))
912 else if (id > MDB_IDL_LAST(ids))
924 if ( ids[0] > 1 && id < ids[ids[0]] ) {
931 if ( ids[0] >= MDB_IDL_UM_MAX ) {
940 /* Append sorted list b to sorted list a. The result is unsorted but
941 * a[1] is the min of the result and a[a[0]] is the max.
943 int mdb_idl_append( ID *a, ID *b )
945 ID ida, idb, tmp, swap = 0;
947 if ( MDB_IDL_IS_ZERO( b ) ) {
951 if ( MDB_IDL_IS_ZERO( a ) ) {
956 ida = MDB_IDL_LAST( a );
957 idb = MDB_IDL_LAST( b );
958 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ||
959 a[0] + b[0] >= MDB_IDL_UM_MAX ) {
960 a[2] = IDL_MAX( ida, idb );
961 a[1] = IDL_MIN( a[1], b[1] );
966 if ( b[0] > 1 && ida > idb ) {
983 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
994 /* Quicksort + Insertion sort for small arrays */
997 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1000 mdb_idl_sort( ID *ids, ID *tmp )
1002 int *istack = (int *)tmp;
1003 int i,j,k,l,ir,jstack;
1006 if ( MDB_IDL_IS_RANGE( ids ))
1013 if (ir - l < SMALL) { /* Insertion sort */
1014 for (j=l+1;j<=ir;j++) {
1016 for (i=j-1;i>=1;i--) {
1017 if (ids[i] <= a) break;
1022 if (jstack == 0) break;
1023 ir = istack[jstack--];
1024 l = istack[jstack--];
1026 k = (l + ir) >> 1; /* Choose median of left, center, right */
1027 SWAP(ids[k], ids[l+1]);
1028 if (ids[l] > ids[ir]) {
1029 SWAP(ids[l], ids[ir]);
1031 if (ids[l+1] > ids[ir]) {
1032 SWAP(ids[l+1], ids[ir]);
1034 if (ids[l] > ids[l+1]) {
1035 SWAP(ids[l], ids[l+1]);
1041 do i++; while(ids[i] < a);
1042 do j--; while(ids[j] > a);
1044 SWAP(ids[i],ids[j]);
1049 if (ir-i+1 >= j-1) {
1050 istack[jstack] = ir;
1051 istack[jstack-1] = i;
1054 istack[jstack] = j-1;
1055 istack[jstack-1] = l;
1064 /* 8 bit Radix sort + insertion sort
1066 * based on code from http://www.cubic.org/docs/radix.htm
1067 * with improvements by mbackes@symas.com and hyc@symas.com
1069 * This code is O(n) but has a relatively high constant factor. For lists
1070 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1071 * Much faster than quicksort for lists longer than ~100. Insertion
1072 * sort is actually superior for lists <50.
1075 #define BUCKETS (1<<8)
1079 mdb_idl_sort( ID *ids, ID *tmp )
1081 int count, soft_limit, phase = 0, size = ids[0];
1083 unsigned char *maxv = (unsigned char *)&ids[size];
1085 if ( MDB_IDL_IS_RANGE( ids ))
1088 /* Use insertion sort for small lists */
1089 if ( size <= SMALL ) {
1093 for (j=1;j<=size;j++) {
1095 for (i=j-1;i>=1;i--) {
1096 if (ids[i] <= a) break;
1108 #if BYTE_ORDER == BIG_ENDIAN
1109 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1111 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1115 #if BYTE_ORDER == BIG_ENDIAN
1116 count = sizeof(ID)-1; count >= soft_limit; --count
1118 count = 0; count <= soft_limit; ++count
1121 unsigned int num[BUCKETS], * np, n, sum;
1123 ID *sp, *source, *dest;
1124 unsigned char *bp, *source_start;
1126 source = idls[phase]+1;
1127 dest = idls[phase^1]+1;
1128 source_start = ((unsigned char *) source) + count;
1131 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1133 /* count occurences of every byte value */
1135 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1138 /* transform count into index by summing elements and storing
1143 for ( i = BUCKETS; i > 0; --i ) {
1149 /* fill dest with the right values in the right place */
1152 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1160 /* copy back from temp if needed */
1163 for ( count = 0; count < size; ++count )
1167 #endif /* Quick vs Radix */