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 );
406 Debug( LDAP_DEBUG_ARGS,
407 "mdb_idl_insert_keys: %lx %s\n",
408 (long) id, mdb_show_key( key, buf ), 0 );
411 assert( id != NOID );
413 for ( k=0; key[k].mv_data; k++ ) {
414 /* Fetch the first data item for this key, to see if it
415 * exists and if it's a range.
417 rc = mdb_cursor_get( cursor, &key[k], &data, MDB_SET );
421 memcpy(&lo, data.mv_data, sizeof(ID));
423 /* not a range, count the number of items */
425 rc = mdb_cursor_count( cursor, &count );
430 if ( count >= MDB_IDL_DB_MAX ) {
431 /* No room, convert to a range */
433 rc = mdb_cursor_get( cursor, &key[k], &data, MDB_LAST_DUP );
434 if ( rc != 0 && rc != MDB_NOTFOUND ) {
435 err = "c_get last_dup";
440 /* Update hi/lo if needed */
443 } else if ( id > hi ) {
446 /* delete the old key */
447 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
452 /* Store the range */
453 data.mv_size = sizeof(ID);
456 rc = mdb_cursor_put( cursor, &key[k], &data, 0 );
462 rc = mdb_cursor_put( cursor, &key[k], &data, 0 );
468 rc = mdb_cursor_put( cursor, &key[k], &data, 0 );
474 /* There's room, just store it */
478 /* It's a range, see if we need to rewrite
483 if ( id < lo || id > hi ) {
485 rc = mdb_cursor_get( cursor, &key[k], &data, MDB_NEXT_DUP );
488 rc = mdb_cursor_get( cursor, &key[k], &data, MDB_NEXT_DUP );
490 data.mv_size = sizeof(ID);
492 /* Replace the current lo/hi */
493 rc = mdb_cursor_put( cursor, &key[k], &data, MDB_CURRENT );
500 } else if ( rc == MDB_NOTFOUND ) {
501 put1: data.mv_data = &id;
502 data.mv_size = sizeof(ID);
503 rc = mdb_cursor_put( cursor, &key[k], &data, MDB_NODUPDATA );
504 /* Don't worry if it's already there */
505 if ( rc == MDB_KEYEXIST )
512 /* initial c_get failed, nothing was done */
514 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_keys: "
515 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
535 Debug( LDAP_DEBUG_ARGS,
536 "mdb_idl_delete_keys: %lx %s\n",
537 (long) id, mdb_show_key( key, buf ), 0 );
539 assert( id != NOID );
541 for ( k=0; key[k].mv_data; k++) {
542 /* Fetch the first data item for this key, to see if it
543 * exists and if it's a range.
545 rc = mdb_cursor_get( cursor, &key[k], &data, MDB_SET );
548 memcpy( &tmp, data.mv_data, sizeof(ID) );
551 /* Not a range, just delete it */
553 rc = mdb_cursor_get( cursor, &key[k], &data, MDB_GET_BOTH );
558 rc = mdb_cursor_del( cursor, 0 );
564 /* It's a range, see if we need to rewrite
569 if ( id == lo || id == hi ) {
570 ID lo2 = lo, hi2 = hi;
573 } else if ( id == hi ) {
577 /* The range has collapsed... */
578 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
585 rc = mdb_cursor_get( cursor, &key[k], &data, MDB_NEXT_DUP );
590 rc = mdb_cursor_get( cursor, &key[k], &data, MDB_NEXT_DUP );
593 /* Replace the current lo/hi */
594 data.mv_size = sizeof(ID);
595 rc = mdb_cursor_put( cursor, &key[k], &data, MDB_CURRENT );
604 /* initial c_get failed, nothing was done */
606 if ( rc == MDB_NOTFOUND )
609 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
610 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
620 * idl_intersection - return a = a intersection b
623 mdb_idl_intersection(
629 ID cursora = 0, cursorb = 0, cursorc;
632 if ( MDB_IDL_IS_ZERO( a ) || MDB_IDL_IS_ZERO( b ) ) {
637 idmin = IDL_MAX( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
638 idmax = IDL_MIN( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
639 if ( idmin > idmax ) {
642 } else if ( idmin == idmax ) {
648 if ( MDB_IDL_IS_RANGE( a ) ) {
649 if ( MDB_IDL_IS_RANGE(b) ) {
650 /* If both are ranges, just shrink the boundaries */
655 /* Else swap so that b is the range, a is a list */
663 /* If a range completely covers the list, the result is
664 * just the list. If idmin to idmax is contiguous, just
665 * turn it into a range.
667 if ( MDB_IDL_IS_RANGE( b )
668 && MDB_IDL_FIRST( b ) <= MDB_IDL_FIRST( a )
669 && MDB_IDL_LAST( b ) >= MDB_IDL_LAST( a ) ) {
670 if (idmax - idmin + 1 == a[0])
679 /* Fine, do the intersection one element at a time.
680 * First advance to idmin in both IDLs.
682 cursora = cursorb = idmin;
683 ida = mdb_idl_first( a, &cursora );
684 idb = mdb_idl_first( b, &cursorb );
687 while( ida <= idmax || idb <= idmax ) {
690 ida = mdb_idl_next( a, &cursora );
691 idb = mdb_idl_next( b, &cursorb );
692 } else if ( ida < idb ) {
693 ida = mdb_idl_next( a, &cursora );
695 idb = mdb_idl_next( b, &cursorb );
708 * idl_union - return a = a union b
716 ID cursora = 0, cursorb = 0, cursorc;
718 if ( MDB_IDL_IS_ZERO( b ) ) {
722 if ( MDB_IDL_IS_ZERO( a ) ) {
727 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ) {
728 over: ida = IDL_MIN( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
729 idb = IDL_MAX( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
736 ida = mdb_idl_first( a, &cursora );
737 idb = mdb_idl_first( b, &cursorb );
741 /* The distinct elements of a are cat'd to b */
742 while( ida != NOID || idb != NOID ) {
744 if( ++cursorc > MDB_IDL_UM_MAX ) {
748 ida = mdb_idl_next( a, &cursora );
752 ida = mdb_idl_next( a, &cursora );
753 idb = mdb_idl_next( b, &cursorb );
757 /* b is copied back to a in sorted order */
762 while (cursorb <= b[0] || cursorc <= a[0]) {
767 if (cursorb <= b[0] && b[cursorb] < idb)
768 a[cursora++] = b[cursorb++];
781 * mdb_idl_notin - return a intersection ~b (or a minus b)
790 ID cursora = 0, cursorb = 0;
792 if( MDB_IDL_IS_ZERO( a ) ||
793 MDB_IDL_IS_ZERO( b ) ||
794 MDB_IDL_IS_RANGE( b ) )
796 MDB_IDL_CPY( ids, a );
800 if( MDB_IDL_IS_RANGE( a ) ) {
801 MDB_IDL_CPY( ids, a );
805 ida = mdb_idl_first( a, &cursora ),
806 idb = mdb_idl_first( b, &cursorb );
810 while( ida != NOID ) {
812 /* we could shortcut this */
814 ida = mdb_idl_next( a, &cursora );
816 } else if ( ida < idb ) {
818 ida = mdb_idl_next( a, &cursora );
820 } else if ( ida > idb ) {
821 idb = mdb_idl_next( b, &cursorb );
824 ida = mdb_idl_next( a, &cursora );
825 idb = mdb_idl_next( b, &cursorb );
833 ID mdb_idl_first( ID *ids, ID *cursor )
842 if ( MDB_IDL_IS_RANGE( ids ) ) {
843 if( *cursor < ids[1] ) {
852 pos = mdb_idl_search( ids, *cursor );
862 ID mdb_idl_next( ID *ids, ID *cursor )
864 if ( MDB_IDL_IS_RANGE( ids ) ) {
865 if( ids[2] < ++(*cursor) ) {
871 if ( ++(*cursor) <= ids[0] ) {
878 /* Add one ID to an unsorted list. We ensure that the first element is the
879 * minimum and the last element is the maximum, for fast range compaction.
880 * this means IDLs up to length 3 are always sorted...
882 int mdb_idl_append_one( ID *ids, ID id )
884 if (MDB_IDL_IS_RANGE( ids )) {
885 /* if already in range, treat as a dup */
886 if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
888 if (id < MDB_IDL_FIRST(ids))
890 else if (id > MDB_IDL_LAST(ids))
902 if ( ids[0] > 1 && id < ids[ids[0]] ) {
909 if ( ids[0] >= MDB_IDL_UM_MAX ) {
918 /* Append sorted list b to sorted list a. The result is unsorted but
919 * a[1] is the min of the result and a[a[0]] is the max.
921 int mdb_idl_append( ID *a, ID *b )
923 ID ida, idb, tmp, swap = 0;
925 if ( MDB_IDL_IS_ZERO( b ) ) {
929 if ( MDB_IDL_IS_ZERO( a ) ) {
934 ida = MDB_IDL_LAST( a );
935 idb = MDB_IDL_LAST( b );
936 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ||
937 a[0] + b[0] >= MDB_IDL_UM_MAX ) {
938 a[2] = IDL_MAX( ida, idb );
939 a[1] = IDL_MIN( a[1], b[1] );
944 if ( b[0] > 1 && ida > idb ) {
961 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
972 /* Quicksort + Insertion sort for small arrays */
975 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
978 mdb_idl_sort( ID *ids, ID *tmp )
980 int *istack = (int *)tmp;
981 int i,j,k,l,ir,jstack;
984 if ( MDB_IDL_IS_RANGE( ids ))
991 if (ir - l < SMALL) { /* Insertion sort */
992 for (j=l+1;j<=ir;j++) {
994 for (i=j-1;i>=1;i--) {
995 if (ids[i] <= a) break;
1000 if (jstack == 0) break;
1001 ir = istack[jstack--];
1002 l = istack[jstack--];
1004 k = (l + ir) >> 1; /* Choose median of left, center, right */
1005 SWAP(ids[k], ids[l+1]);
1006 if (ids[l] > ids[ir]) {
1007 SWAP(ids[l], ids[ir]);
1009 if (ids[l+1] > ids[ir]) {
1010 SWAP(ids[l+1], ids[ir]);
1012 if (ids[l] > ids[l+1]) {
1013 SWAP(ids[l], ids[l+1]);
1019 do i++; while(ids[i] < a);
1020 do j--; while(ids[j] > a);
1022 SWAP(ids[i],ids[j]);
1027 if (ir-i+1 >= j-1) {
1028 istack[jstack] = ir;
1029 istack[jstack-1] = i;
1032 istack[jstack] = j-1;
1033 istack[jstack-1] = l;
1042 /* 8 bit Radix sort + insertion sort
1044 * based on code from http://www.cubic.org/docs/radix.htm
1045 * with improvements by mbackes@symas.com and hyc@symas.com
1047 * This code is O(n) but has a relatively high constant factor. For lists
1048 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1049 * Much faster than quicksort for lists longer than ~100. Insertion
1050 * sort is actually superior for lists <50.
1053 #define BUCKETS (1<<8)
1057 mdb_idl_sort( ID *ids, ID *tmp )
1059 int count, soft_limit, phase = 0, size = ids[0];
1061 unsigned char *maxv = (unsigned char *)&ids[size];
1063 if ( MDB_IDL_IS_RANGE( ids ))
1066 /* Use insertion sort for small lists */
1067 if ( size <= SMALL ) {
1071 for (j=1;j<=size;j++) {
1073 for (i=j-1;i>=1;i--) {
1074 if (ids[i] <= a) break;
1086 #if BYTE_ORDER == BIG_ENDIAN
1087 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1089 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1093 #if BYTE_ORDER == BIG_ENDIAN
1094 count = sizeof(ID)-1; count >= soft_limit; --count
1096 count = 0; count <= soft_limit; ++count
1099 unsigned int num[BUCKETS], * np, n, sum;
1101 ID *sp, *source, *dest;
1102 unsigned char *bp, *source_start;
1104 source = idls[phase]+1;
1105 dest = idls[phase^1]+1;
1106 source_start = ((unsigned char *) source) + count;
1109 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1111 /* count occurences of every byte value */
1113 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1116 /* transform count into index by summing elements and storing
1121 for ( i = BUCKETS; i > 0; --i ) {
1127 /* fill dest with the right values in the right place */
1130 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1138 /* copy back from temp if needed */
1141 for ( count = 0; count < size; ++count )
1145 #endif /* Quick vs Radix */